@@ -137,10 +137,63 @@ def contains(validator, contains, instance, schema):
137
137
if not validator .is_type (instance , "array" ):
138
138
return
139
139
140
- if not any (validator .is_valid (element , contains ) for element in instance ):
140
+ min_contains = max_contains = None
141
+
142
+ if 'minContains' in schema :
143
+ min_contains = schema ['minContains' ]
144
+ if not validator .is_type (min_contains , "integer" ):
145
+ yield ValidationError (
146
+ "minContains of %r in not valid under the given schema" % (min_contains ,)
147
+ )
148
+ return
149
+
150
+ if 'maxContains' in schema :
151
+ max_contains = schema ['maxContains' ]
152
+ if not validator .is_type (max_contains , "integer" ):
153
+ yield ValidationError (
154
+ "maxContains of %r is not valid under the given schema" % (instance ,)
155
+ )
156
+ return
157
+
158
+ # minContains set to 0 will ignore contains
159
+ if min_contains == 0 :
160
+ return
161
+
162
+ matches = len (list (filter (lambda x : x , [validator .is_valid (element , contains ) for element in instance ])))
163
+
164
+ # default contains behavior
165
+ if not matches :
141
166
yield ValidationError (
142
167
"None of %r are valid under the given schema" % (instance ,)
143
168
)
169
+ return
170
+
171
+ if min_contains and max_contains is None :
172
+ if matches < min_contains :
173
+ yield ValidationError (
174
+ "Invalid number or matches of %r under the given schema, expected min %d, got %d" % (
175
+ instance , min_contains , matches
176
+ )
177
+ )
178
+ return
179
+
180
+ if min_contains is None and max_contains :
181
+ if matches > max_contains :
182
+ yield ValidationError (
183
+ "Invalid number or matches of %r under the given schema, expected max %d, got %d" % (
184
+ instance , max_contains , matches
185
+ )
186
+ )
187
+ return
188
+
189
+ if min_contains and max_contains :
190
+ if matches < min_contains or matches > max_contains :
191
+ yield ValidationError (
192
+ "Invalid number or matches of %r under the given schema, expected min %d and max %d, got %d" % (
193
+ instance , min_contains , max_contains , matches
194
+ )
195
+ )
196
+ return
144
197
145
198
146
199
def exclusiveMinimum (validator , minimum , instance , schema ):
0 commit comments