Retrieve the validator class appropriate for validating the given schema. Uses the :kw:`$schema` keyword that should be present in the given schema to look up the appropriate validator class. Arguments: schema (collections.abc.Mapping or bool): the schema to
(
schema,
default: type[Validator] | _utils.Unset = _UNSET,
)
| 1333 | |
| 1334 | |
| 1335 | def validator_for( |
| 1336 | schema, |
| 1337 | default: type[Validator] | _utils.Unset = _UNSET, |
| 1338 | ) -> type[Validator]: |
| 1339 | """ |
| 1340 | Retrieve the validator class appropriate for validating the given schema. |
| 1341 | |
| 1342 | Uses the :kw:`$schema` keyword that should be present in the given |
| 1343 | schema to look up the appropriate validator class. |
| 1344 | |
| 1345 | Arguments: |
| 1346 | |
| 1347 | schema (collections.abc.Mapping or bool): |
| 1348 | |
| 1349 | the schema to look at |
| 1350 | |
| 1351 | default: |
| 1352 | |
| 1353 | the default to return if the appropriate validator class |
| 1354 | cannot be determined. |
| 1355 | |
| 1356 | If unprovided, the default is to return the latest supported |
| 1357 | draft. |
| 1358 | |
| 1359 | Examples: |
| 1360 | |
| 1361 | The :kw:`$schema` JSON Schema keyword will control which validator |
| 1362 | class is returned: |
| 1363 | |
| 1364 | >>> schema = { |
| 1365 | ... "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 1366 | ... "type": "integer", |
| 1367 | ... } |
| 1368 | >>> jsonschema.validators.validator_for(schema) |
| 1369 | <class 'jsonschema.validators.Draft202012Validator'> |
| 1370 | |
| 1371 | |
| 1372 | Here, a draft 7 schema instead will return the draft 7 validator: |
| 1373 | |
| 1374 | >>> schema = { |
| 1375 | ... "$schema": "http://json-schema.org/draft-07/schema#", |
| 1376 | ... "type": "integer", |
| 1377 | ... } |
| 1378 | >>> jsonschema.validators.validator_for(schema) |
| 1379 | <class 'jsonschema.validators.Draft7Validator'> |
| 1380 | |
| 1381 | |
| 1382 | Schemas with no ``$schema`` keyword will fallback to the default |
| 1383 | argument: |
| 1384 | |
| 1385 | >>> schema = {"type": "integer"} |
| 1386 | >>> jsonschema.validators.validator_for( |
| 1387 | ... schema, default=Draft7Validator, |
| 1388 | ... ) |
| 1389 | <class 'jsonschema.validators.Draft7Validator'> |
| 1390 | |
| 1391 | or if none is provided, to the latest version supported. |
| 1392 | Always including the keyword when authoring schemas is highly |
no test coverage detected