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`

Comments:

Patch:
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