|
13 | 13 | from openapi_schema_validator._regex import has_ecma_regex |
14 | 14 | from openapi_schema_validator.settings import reset_settings_cache |
15 | 15 | from openapi_schema_validator.shortcuts import clear_validate_cache |
| 16 | +from openapi_schema_validator.validators import OAS30ReadValidator |
| 17 | +from openapi_schema_validator.validators import OAS30Validator |
| 18 | +from openapi_schema_validator.validators import OAS30WriteValidator |
| 19 | +from openapi_schema_validator.validators import OAS31Validator |
| 20 | +from openapi_schema_validator.validators import OAS32Validator |
16 | 21 |
|
17 | 22 |
|
18 | 23 | @pytest.fixture(scope="function") |
@@ -195,3 +200,186 @@ def test_validate_cache_max_size_from_env(monkeypatch): |
195 | 200 | validate("foo", schema_a, cls=OAS32Validator) |
196 | 201 |
|
197 | 202 | assert check_schema_mock.call_count == 3 |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.parametrize( |
| 206 | + "schema, cls, instance, enforce, expected_error", |
| 207 | + [ |
| 208 | + ( |
| 209 | + { |
| 210 | + "type": "object", |
| 211 | + "properties": { |
| 212 | + "id": {"type": "string"}, |
| 213 | + "nickname": {"type": "string"}, |
| 214 | + }, |
| 215 | + "required": ["id"], |
| 216 | + }, |
| 217 | + OAS30Validator, |
| 218 | + {"id": "42"}, |
| 219 | + False, |
| 220 | + None, |
| 221 | + ), |
| 222 | + ( |
| 223 | + { |
| 224 | + "type": "object", |
| 225 | + "properties": { |
| 226 | + "id": {"type": "string"}, |
| 227 | + "nickname": {"type": "string"}, |
| 228 | + }, |
| 229 | + "required": ["id"], |
| 230 | + }, |
| 231 | + OAS30Validator, |
| 232 | + {"id": "42"}, |
| 233 | + True, |
| 234 | + "'nickname' is a required property", |
| 235 | + ), |
| 236 | + ( |
| 237 | + { |
| 238 | + "type": "object", |
| 239 | + "properties": { |
| 240 | + "id": {"type": "string", "readOnly": True}, |
| 241 | + "password": {"type": "string", "writeOnly": True}, |
| 242 | + "normal": {"type": "string"}, |
| 243 | + }, |
| 244 | + }, |
| 245 | + OAS30ReadValidator, |
| 246 | + {"id": "123"}, |
| 247 | + True, |
| 248 | + "'normal' is a required property", |
| 249 | + ), |
| 250 | + ( |
| 251 | + { |
| 252 | + "type": "object", |
| 253 | + "properties": { |
| 254 | + "id": {"type": "string", "readOnly": True}, |
| 255 | + "password": {"type": "string", "writeOnly": True}, |
| 256 | + "normal": {"type": "string"}, |
| 257 | + }, |
| 258 | + }, |
| 259 | + OAS30ReadValidator, |
| 260 | + {"normal": "abc"}, |
| 261 | + True, |
| 262 | + "'id' is a required property", |
| 263 | + ), |
| 264 | + ( |
| 265 | + { |
| 266 | + "type": "object", |
| 267 | + "properties": { |
| 268 | + "id": {"type": "string", "readOnly": True}, |
| 269 | + "password": {"type": "string", "writeOnly": True}, |
| 270 | + "normal": {"type": "string"}, |
| 271 | + }, |
| 272 | + }, |
| 273 | + OAS30ReadValidator, |
| 274 | + {"id": "123", "normal": "abc"}, |
| 275 | + True, |
| 276 | + None, |
| 277 | + ), |
| 278 | + ( |
| 279 | + { |
| 280 | + "type": "object", |
| 281 | + "properties": { |
| 282 | + "id": {"type": "string", "readOnly": True}, |
| 283 | + "password": {"type": "string", "writeOnly": True}, |
| 284 | + "normal": {"type": "string"}, |
| 285 | + }, |
| 286 | + }, |
| 287 | + OAS30WriteValidator, |
| 288 | + {"normal": "abc"}, |
| 289 | + True, |
| 290 | + "'password' is a required property", |
| 291 | + ), |
| 292 | + ( |
| 293 | + { |
| 294 | + "type": "object", |
| 295 | + "properties": { |
| 296 | + "id": {"type": "string", "readOnly": True}, |
| 297 | + "password": {"type": "string", "writeOnly": True}, |
| 298 | + "normal": {"type": "string"}, |
| 299 | + }, |
| 300 | + }, |
| 301 | + OAS30WriteValidator, |
| 302 | + {"password": "secret"}, |
| 303 | + True, |
| 304 | + "'normal' is a required property", |
| 305 | + ), |
| 306 | + ( |
| 307 | + { |
| 308 | + "type": "object", |
| 309 | + "properties": { |
| 310 | + "id": {"type": "string", "readOnly": True}, |
| 311 | + "password": {"type": "string", "writeOnly": True}, |
| 312 | + "normal": {"type": "string"}, |
| 313 | + }, |
| 314 | + }, |
| 315 | + OAS30WriteValidator, |
| 316 | + {"password": "secret", "normal": "abc"}, |
| 317 | + True, |
| 318 | + None, |
| 319 | + ), |
| 320 | + ( |
| 321 | + { |
| 322 | + "type": "object", |
| 323 | + "properties": { |
| 324 | + "foo": True, |
| 325 | + }, |
| 326 | + }, |
| 327 | + OAS31Validator, |
| 328 | + {}, |
| 329 | + False, |
| 330 | + None, |
| 331 | + ), |
| 332 | + ( |
| 333 | + { |
| 334 | + "type": "object", |
| 335 | + "properties": { |
| 336 | + "foo": True, |
| 337 | + }, |
| 338 | + }, |
| 339 | + OAS31Validator, |
| 340 | + {}, |
| 341 | + True, |
| 342 | + "'foo' is a required property", |
| 343 | + ), |
| 344 | + ( |
| 345 | + { |
| 346 | + "type": "object", |
| 347 | + "properties": { |
| 348 | + "foo": {"type": "string"}, |
| 349 | + }, |
| 350 | + }, |
| 351 | + OAS32Validator, |
| 352 | + {}, |
| 353 | + False, |
| 354 | + None, |
| 355 | + ), |
| 356 | + ( |
| 357 | + { |
| 358 | + "type": "object", |
| 359 | + "properties": { |
| 360 | + "foo": {"type": "string"}, |
| 361 | + }, |
| 362 | + }, |
| 363 | + OAS32Validator, |
| 364 | + {}, |
| 365 | + True, |
| 366 | + "'foo' is a required property", |
| 367 | + ), |
| 368 | + ], |
| 369 | +) |
| 370 | +def test_enforce_properties_required( |
| 371 | + schema, cls, instance, enforce, expected_error |
| 372 | +): |
| 373 | + if expected_error: |
| 374 | + with pytest.raises(ValidationError) as exc: |
| 375 | + validate( |
| 376 | + instance, |
| 377 | + schema, |
| 378 | + cls=cls, |
| 379 | + enforce_properties_required=enforce, |
| 380 | + ) |
| 381 | + assert expected_error in str(exc.value) |
| 382 | + else: |
| 383 | + validate( |
| 384 | + instance, schema, cls=cls, enforce_properties_required=enforce |
| 385 | + ) |
0 commit comments