File tree 3 files changed +24
-3
lines changed
3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 1
- jsonschema
1
+ jsonschema [ format ]
2
2
scrapy
3
3
six
Original file line number Diff line number Diff line change 6
6
Draft4Validator ,
7
7
Draft6Validator ,
8
8
Draft7Validator ,
9
+ FormatChecker
9
10
)
10
-
11
11
from scrapy_jsonschema .draft import (
12
12
JSON_SCHEMA_DRAFT_3 ,
13
13
JSON_SCHEMA_DRAFT_4 ,
@@ -34,6 +34,8 @@ def _merge_schema(base, new):
34
34
35
35
class JsonSchemaMeta (ABCMeta ):
36
36
37
+ format_checker = FormatChecker ()
38
+
37
39
draft_to_validator = {
38
40
JSON_SCHEMA_DRAFT_3 : Draft3Validator ,
39
41
JSON_SCHEMA_DRAFT_4 : Draft4Validator ,
@@ -71,7 +73,7 @@ def _get_validator(cls, schema):
71
73
validator_class = cls .draft_to_validator .get (
72
74
draft_version , Draft4Validator
73
75
)
74
- return validator_class (schema )
76
+ return validator_class (schema , format_checker = cls . format_checker )
75
77
76
78
77
79
@six .add_metaclass (JsonSchemaMeta )
Original file line number Diff line number Diff line change 6
6
7
7
from scrapy_jsonschema .item import JsonSchemaItem , _merge_schema
8
8
from scrapy_jsonschema .draft import JSON_SCHEMA_DRAFT_7
9
+ from jsonschema .exceptions import ValidationError
9
10
10
11
from . import (
11
12
valid_schema ,
@@ -85,3 +86,21 @@ def test_get_validator(self):
85
86
no_draft_chema = {"title" : "Item without schema Draft" }
86
87
default_validator = JsonSchemaItem ._get_validator (no_draft_chema )
87
88
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' })
You can’t perform that action at this time.
0 commit comments