Repository: horejsek/python-fastjsonschema

Issue:
JsonSchemaException when using date-time with RFC 3339 compliant string
Validation fails with this example:

```
import json

import fastjsonschema

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": {
        "date": {
            "type": "string",
            "description": "Some date",
            "format": "date-time"
        }   
    }
}

validate = fastjsonschema.compile(schema)

validate({"date": "2018-02-05T14:17:10Z"})
```

The output is:

```
Traceback (most recent call last):
  File "validate_simple_example.py", line 22, in <module>
    validate({"date": "2018-02-05T14:17:10Z"})
  File "<string>", line 16, in validate_https___example_com_example_schema_json
fastjsonschema.exceptions.JsonSchemaException
```

According to the [json schema docs](http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.3.1) all RFC 3339 timestamps should be valid.

I think the problem is the milliseconds part. It should be optional if I'm not wrong.

The above example runs fine with: `validate({"date": "2018-02-05T14:17:10.00Z"})`

The current regex is:

```
^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+(?:[+-][0-2]\d:[0-5]\d|Z)?$
```

I suggest changing it to:

```
^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:[+-][0-2]\d:[0-5]\d|Z)?$
```

Also maybe it's worth thinking about not using regexes for format validation for some of the stuff (like ips, dates, etc.)

Discussion:

Patch:
diff --git a/fastjsonschema/draft04.py b/fastjsonschema/draft04.py
index 3ee257b..6d79a1e 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])\.)*'


Test patch:
diff --git a/tests/test_datetime.py b/tests/test_datetime.py
new file mode 100644
index 0000000..2263e9f
--- /dev/null
+++ b/tests/test_datetime.py
@@ -0,0 +1,15 @@
+
+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)