@@ -63,6 +63,9 @@ def to_regex(resolver: Resolver, instance: dict):
63
63
- Handle types defined as a list
64
64
- Handle constraints on numbers
65
65
- Handle special patterns: `date`, `uri`, etc.
66
+ - Handle optional fields (not in `required`)
67
+
68
+ This does not support recursive definitions.
66
69
67
70
Parameters
68
71
----------
@@ -116,12 +119,14 @@ def to_regex(resolver: Resolver, instance: dict):
116
119
# The enum keyword is used to restrict a value to a fixed set of values. It
117
120
# must be an array with at least one element, where each element is unique.
118
121
elif "enum" in instance :
119
- if instance ["type" ] == "string" :
120
- choices = [f'"{ re .escape (choice )} "' for choice in instance ["enum" ]]
121
- return f"({ '|' .join (choices )} )"
122
- else :
123
- choices = [re .escape (str (choice )) for choice in instance ["enum" ]]
124
- return f"({ '|' .join (choices )} )"
122
+ choices = []
123
+ for choice in instance ["enum" ]:
124
+ if type (choice ) in [int , float , bool , None ]:
125
+ choices .append (re .escape (str (choice )))
126
+ elif type (choice ) == str :
127
+ choices .append (f'"{ re .escape (choice )} "' )
128
+
129
+ return f"({ '|' .join (choices )} )"
125
130
126
131
elif "$ref" in instance :
127
132
path = f"{ instance ['$ref' ]} "
@@ -134,8 +139,8 @@ def to_regex(resolver: Resolver, instance: dict):
134
139
# the name of one of the basic types, and each element is unique. In this
135
140
# case, the JSON snippet is valid if it matches any of the given types.
136
141
elif "type" in instance :
137
- type = instance ["type" ]
138
- if type == "string" :
142
+ instance_type = instance ["type" ]
143
+ if instance_type == "string" :
139
144
if "maxLength" in instance or "minLength" in instance :
140
145
max_length = instance .get ("maxLength" , "" )
141
146
min_length = instance .get ("minLength" , "" )
@@ -156,13 +161,13 @@ def to_regex(resolver: Resolver, instance: dict):
156
161
else :
157
162
return type_to_regex ["string" ]
158
163
159
- elif type == "number" :
164
+ elif instance_type == "number" :
160
165
return type_to_regex ["number" ]
161
166
162
- elif type == "integer" :
167
+ elif instance_type == "integer" :
163
168
return type_to_regex ["integer" ]
164
169
165
- elif type == "array" :
170
+ elif instance_type == "array" :
166
171
if "items" in instance :
167
172
items_regex = to_regex (resolver , instance ["items" ])
168
173
return rf"\[({ items_regex } )(,({ items_regex } ))*\]"
@@ -180,17 +185,19 @@ def to_regex(resolver: Resolver, instance: dict):
180
185
regexes = [to_regex (resolver , t ) for t in types ]
181
186
return rf"\[({ '|' .join (regexes )} )(,({ '|' .join (regexes )} ))*\]"
182
187
183
- elif type == "boolean" :
188
+ elif instance_type == "boolean" :
184
189
return type_to_regex ["boolean" ]
185
190
186
- elif type == "null" :
191
+ elif instance_type == "null" :
187
192
return type_to_regex ["null" ]
188
193
189
- elif isinstance (type , list ):
194
+ elif isinstance (instance_type , list ):
190
195
# Here we need to make the choice to exclude generating an object
191
196
# if the specification of the object is not give, even though a JSON
192
197
# object that contains an object here would be valid under the specification.
193
- regexes = [to_regex (resolver , {"type" : t }) for t in type if t != "object" ]
198
+ regexes = [
199
+ to_regex (resolver , {"type" : t }) for t in instance_type if t != "object"
200
+ ]
194
201
return rf"({ '|' .join (regexes )} )"
195
202
196
203
raise NotImplementedError (
0 commit comments