Repository: horejsek/python-fastjsonschema

Issue:
ipv6 format leads to cut off string in generated code
Thanks for the quick fix of #21 !

I just tried it, and indeed, the regex pattern is now there. But the code is still broken due to a different issue.

When I generate the code for this schema:

```
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://example.com/example.schema.json",
    "title": "Example",
    "description": "An example schema",
    "type": "object",
    "properties": {
        "ip": {
            "type": "string",
            "description": "The IP of the future",
            "format": "ipv6"
        }   
    },
    "required": [ "ip" ]
}
```

The resulting code contains the following dict:

```
REGEX_PATTERNS = {
    "ipv6_re_pattern": re.compile('^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,4)
}
```

It looks like the line is just cut off at that point, so the code won't work.

`SyntaxError: EOL while scanning string literal`

Discussion:

Patch:
diff --git a/fastjsonschema/draft04.py b/fastjsonschema/draft04.py
index 6d79a1e..3ee257b 100644
--- a/fastjsonschema/draft04.py
+++ b/fastjsonschema/draft04.py
@@ -18,7 +18,7 @@ JSON_TYPE_TO_PYTHON_TYPE = {
 class CodeGeneratorDraft04(CodeGenerator):
     # pylint: disable=line-too-long
     FORMAT_REGEXS = {
-        'date-time': r'^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|Z)?$',
+        'date-time': r'^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+(?:[+-][0-2]\d:[0-5]\d|Z)?$',
         'email': r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$',
         'hostname': (
             r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*'
diff --git a/fastjsonschema/generator.py b/fastjsonschema/generator.py
index 11358d2..28b9480 100644
--- a/fastjsonschema/generator.py
+++ b/fastjsonschema/generator.py
@@ -93,7 +93,7 @@ class CodeGenerator:
                     '',
                 ]
             )
-        regexs = ['"{}": {}'.format(key, value) for key, value in self._compile_regexps.items()]
+        regexs = ['"{}": re.compile(r"{}")'.format(key, value.pattern) for key, value in self._compile_regexps.items()]
         return '\n'.join(
             [
                 'import re',


Test patch:
diff --git a/tests/test_compile_to_code.py b/tests/test_compile_to_code.py
index 394e648..7312f38 100644
--- a/tests/test_compile_to_code.py
+++ b/tests/test_compile_to_code.py
@@ -1,8 +1,19 @@
 import os
 import pytest
+import shutil
 
 from fastjsonschema import JsonSchemaException, compile_to_code
 
+@pytest.yield_fixture(autouse=True)
+def run_around_tests():
+    temp_dir = 'temp'
+    # Code that will run before your test, for example:
+    if not os.path.isdir(temp_dir):
+        os.makedirs(temp_dir)
+    # A test function will be run at this point
+    yield
+    # Code that will run after your test, for example:
+    shutil.rmtree(temp_dir)
 
 def test_compile_to_code():
     code = compile_to_code({
@@ -12,11 +23,9 @@ def test_compile_to_code():
             'c': {'format': 'hostname'},
         }
     })
-    if not os.path.isdir('temp'):
-        os.makedirs('temp')
-    with open('temp/schema.py', 'w') as f:
+    with open('temp/schema_1.py', 'w') as f:
         f.write(code)
-    from temp.schema import validate
+    from temp.schema_1 import validate
     assert validate({
         'a': 'a',
         'b': 1, 
@@ -26,3 +35,18 @@ def test_compile_to_code():
         'b': 1,
         'c': 'example.com',
     }
+
+def test_compile_to_code_ipv6_regex():
+    code = compile_to_code({
+        'properties': {
+            'ip': {'format': 'ipv6'},
+        }
+    })
+    with open('temp/schema_2.py', 'w') as f:
+        f.write(code)
+    from temp.schema_2 import validate
+    assert validate({
+        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
+    }) == {
+        'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
+    }
\ No newline at end of file
diff --git a/tests/test_datetime.py b/tests/test_datetime.py
deleted file mode 100644
index 2263e9f..0000000
--- a/tests/test_datetime.py
+++ /dev/null
@@ -1,15 +0,0 @@
-
-import pytest
-
-from fastjsonschema import JsonSchemaException
-
-
-exc = JsonSchemaException('data must be date-time')
-@pytest.mark.parametrize('value, expected', [
-    ('', exc),
-    ('bla', exc),
-    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
-    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
-])
-def test_datetime(asserter, value, expected):
-    asserter({'type': 'string', 'format': 'date-time'}, value, expected)