@@ -119,55 +119,31 @@ def contains(validator, contains, instance, schema):
119
119
if not validator .is_type (instance , "array" ):
120
120
return
121
121
122
- min_contains = max_contains = None
123
-
124
- if "minContains" in schema :
125
- min_contains = schema ["minContains" ]
126
-
127
- if "maxContains" in schema :
128
- max_contains = schema ["maxContains" ]
129
-
130
- # minContains set to 0 will ignore contains
131
- if min_contains == 0 :
132
- return
133
-
134
- matches = sum (1 for each in instance if validator .is_valid (each , contains ))
135
-
136
- if not matches :
137
- yield ValidationError (
138
- f"{ instance !r} does not contain items matching the given schema" ,
139
- )
140
- return
141
-
142
- if min_contains and max_contains is None :
143
- if matches < min_contains :
144
- yield ValidationError (
145
- "Too few items match the given schema "
146
- f"(expected { min_contains } but only { matches } matched)" ,
147
- )
148
- return
149
-
150
- if min_contains is None and max_contains :
151
- if matches > max_contains :
122
+ matches = 0
123
+ min_contains = schema .get ("minContains" , 1 )
124
+ max_contains = schema .get ("maxContains" , len (instance ))
125
+
126
+ for each in instance :
127
+ if validator .is_valid (each , contains ):
128
+ matches += 1
129
+ if matches > max_contains :
130
+ yield ValidationError (
131
+ "Too many items match the given schema "
132
+ f"(expected at most { max_contains } )" ,
133
+ )
134
+ return
135
+
136
+ if matches < min_contains :
137
+ if not matches :
152
138
yield ValidationError (
153
- "Too many items match the given schema "
154
- f"(expected at most { max_contains } but { matches } matched) " ,
139
+ f" { instance !r } does not contain items "
140
+ "matching the given schema " ,
155
141
)
156
- return
157
-
158
- if min_contains and max_contains :
159
- if matches < min_contains :
160
- only = "only "
161
- elif matches > max_contains :
162
- only = ""
163
142
else :
164
- only = None
165
- if only is not None :
166
143
yield ValidationError (
167
- f"Expected between { min_contains } and { max_contains } items "
168
- f"to match the given schema but { only } { matches } matched" ,
144
+ "Too few items match the given schema (expected at least "
145
+ f"{ min_contains } but only { matches } matched) " ,
169
146
)
170
- return
171
147
172
148
173
149
def exclusiveMinimum (validator , minimum , instance , schema ):
0 commit comments