Skip to content

Commit ab27d46

Browse files
committed
Added suport for format attribute in jsonschema
1 parent d22417b commit ab27d46

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
jsonschema
1+
jsonschema[format]
22
scrapy
33
six

scrapy_jsonschema/item.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
Draft4Validator,
77
Draft6Validator,
88
Draft7Validator,
9+
FormatChecker
910
)
10-
1111
from scrapy_jsonschema.draft import (
1212
JSON_SCHEMA_DRAFT_3,
1313
JSON_SCHEMA_DRAFT_4,
@@ -34,6 +34,8 @@ def _merge_schema(base, new):
3434

3535
class JsonSchemaMeta(ABCMeta):
3636

37+
format_checker = FormatChecker()
38+
3739
draft_to_validator = {
3840
JSON_SCHEMA_DRAFT_3: Draft3Validator,
3941
JSON_SCHEMA_DRAFT_4: Draft4Validator,
@@ -71,7 +73,7 @@ def _get_validator(cls, schema):
7173
validator_class = cls.draft_to_validator.get(
7274
draft_version, Draft4Validator
7375
)
74-
return validator_class(schema)
76+
return validator_class(schema, format_checker=cls.format_checker)
7577

7678

7779
@six.add_metaclass(JsonSchemaMeta)

tests/test_item.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from scrapy_jsonschema.item import JsonSchemaItem, _merge_schema
88
from scrapy_jsonschema.draft import JSON_SCHEMA_DRAFT_7
9+
from jsonschema.exceptions import ValidationError
910

1011
from . import (
1112
valid_schema,
@@ -85,3 +86,21 @@ def test_get_validator(self):
8586
no_draft_chema = {"title": "Item without schema Draft"}
8687
default_validator = JsonSchemaItem._get_validator(no_draft_chema)
8788
self.assertTrue(isinstance(default_validator, Draft4Validator))
89+
90+
def test_format_validation(self):
91+
schema = {
92+
"$schema": JSON_SCHEMA_DRAFT_7,
93+
"title": "Item with Schema Draft",
94+
"properties": {
95+
"url": {
96+
"type": "string",
97+
"format": "uri"
98+
}
99+
}
100+
}
101+
102+
with pytest.raises(ValidationError):
103+
draft7_validator = JsonSchemaItem._get_validator(schema)
104+
draft7_validator.validate({'url': 'this is not an uri'})
105+
106+
draft7_validator.validate({'url': 'http://localhost'})

0 commit comments

Comments
 (0)