@@ -88,11 +88,37 @@ def test_compile_complex_one_of_all_of():
88
88
89
89
90
90
def test_compile_to_code_custom_format ():
91
- formats = {'identifier ' : str .isidentifier }
92
- code = compile_to_code ({'type' : 'string' , 'format' : 'identifier ' }, formats = formats )
91
+ formats = {'my-format ' : str .isidentifier }
92
+ code = compile_to_code ({'type' : 'string' , 'format' : 'my-format ' }, formats = formats )
93
93
with open ('temp/schema_3.py' , 'w' ) as f :
94
94
f .write (code )
95
95
from temp .schema_3 import validate
96
- assert validate ("identifier" , formats ) == "identifier"
97
- with pytest .raises (JsonSchemaValueException ):
98
- validate ("not-identifier" , formats )
96
+ assert validate ("valid" , formats ) == "valid"
97
+ with pytest .raises (JsonSchemaValueException ) as exc :
98
+ validate ("not-valid" , formats )
99
+ assert exc .value .message == "data must be my-format"
100
+
101
+
102
+ def test_compile_to_code_custom_format_with_refs ():
103
+ schema = {
104
+ 'type' : 'object' ,
105
+ 'properties' : {
106
+ 'a' : {'$ref' : '#/definitions/a' }
107
+ },
108
+ 'definitions' : {
109
+ 'a' : {
110
+ '$id' : '#/definitions/a' ,
111
+ 'type' : 'array' ,
112
+ 'items' : {'type' : 'string' , 'format' : 'my-format' }
113
+ }
114
+ }
115
+ }
116
+ formats = {'my-format' : str .isidentifier }
117
+ code = compile_to_code (schema , formats = formats )
118
+ with open ('temp/schema_4.py' , 'w' ) as f :
119
+ f .write (code )
120
+ from temp .schema_4 import validate
121
+ assert validate ({"a" : ["identifier" ]}, formats ) is not None
122
+ with pytest .raises (JsonSchemaValueException ) as exc :
123
+ validate ({"a" : ["identifier" , "not-valid" ]}, formats )
124
+ assert exc .value .message == "data[1] must be my-format"
0 commit comments