MCPcopy Index your code
hub / github.com/python-jsonschema/jsonschema / validate

Function validate

jsonschema/validators.py:1265–1332  ·  view source on GitHub ↗

Validate an instance under the given schema. >>> validate([2, 3, 4], {"maxItems": 2}) Traceback (most recent call last): ... ValidationError: [2, 3, 4] is too long :func:`~jsonschema.validators.validate` will first verify that the provided schema is

(instance, schema, cls=None, *args, **kwargs)

Source from the content-addressed store, hash-verified

1263
1264
1265def validate(instance, schema, cls=None, *args, **kwargs): # noqa: D417
1266 """
1267 Validate an instance under the given schema.
1268
1269 >>> validate([2, 3, 4], {"maxItems": 2})
1270 Traceback (most recent call last):
1271 ...
1272 ValidationError: [2, 3, 4] is too long
1273
1274 :func:`~jsonschema.validators.validate` will first verify that the
1275 provided schema is itself valid, since not doing so can lead to less
1276 obvious error messages and fail in less obvious or consistent ways.
1277
1278 If you know you have a valid schema already, especially
1279 if you intend to validate multiple instances with
1280 the same schema, you likely would prefer using the
1281 `jsonschema.protocols.Validator.validate` method directly on a
1282 specific validator (e.g. ``Draft202012Validator.validate``).
1283
1284
1285 Arguments:
1286
1287 instance:
1288
1289 The instance to validate
1290
1291 schema:
1292
1293 The schema to validate with
1294
1295 cls (jsonschema.protocols.Validator):
1296
1297 The class that will be used to validate the instance.
1298
1299 If the ``cls`` argument is not provided, two things will happen
1300 in accordance with the specification. First, if the schema has a
1301 :kw:`$schema` keyword containing a known meta-schema [#]_ then the
1302 proper validator will be used. The specification recommends that
1303 all schemas contain :kw:`$schema` properties for this reason. If no
1304 :kw:`$schema` property is found, the default validator class is the
1305 latest released draft.
1306
1307 Any other provided positional and keyword arguments will be passed
1308 on when instantiating the ``cls``.
1309
1310 Raises:
1311
1312 `jsonschema.exceptions.ValidationError`:
1313
1314 if the instance is invalid
1315
1316 `jsonschema.exceptions.SchemaError`:
1317
1318 if the schema itself is invalid
1319
1320 .. rubric:: Footnotes
1321 .. [#] known by a validator registered with
1322 `jsonschema.validators.validates`

Calls 3

validator_forFunction · 0.85
check_schemaMethod · 0.45
iter_errorsMethod · 0.45