@@ -462,6 +462,46 @@ def validator_for(schema, default=_unset):
462
462
463
463
464
464
def validate (instance , schema , cls = None , * args , ** kwargs ):
465
+ """
466
+ Validate an instance under the given schema.
467
+
468
+ >>> validate([2, 3, 4], {"maxItems" : 2})
469
+ Traceback (most recent call last):
470
+ ...
471
+ ValidationError: [2, 3, 4] is too long
472
+
473
+ :func:`validate` will first verify that the provided schema is itself
474
+ valid, since not doing so can lead to less obvious error messages and fail
475
+ in less obvious or consistent ways. If you know you have a valid schema
476
+ already or don't care, you might prefer using the
477
+ :meth:`~IValidator.validate` method directly on a specific validator
478
+ (e.g. :meth:`Draft4Validator.validate`).
479
+
480
+
481
+ :argument instance: the instance to validate
482
+ :argument schema: the schema to validate with
483
+ :argument cls: an :class:`IValidator` class that will be used to validate
484
+ the instance.
485
+
486
+ If the ``cls`` argument is not provided, two things will happen in
487
+ accordance with the specification. First, if the schema has a
488
+ :validator:`$schema` property containing a known meta-schema [#]_ then the
489
+ proper validator will be used. The specification recommends that all
490
+ schemas contain :validator:`$schema` properties for this reason. If no
491
+ :validator:`$schema` property is found, the default validator class is
492
+ :class:`Draft4Validator`.
493
+
494
+ Any other provided positional and keyword arguments will be passed on when
495
+ instantiating the ``cls``.
496
+
497
+ :raises:
498
+ :exc:`ValidationError` if the instance is invalid
499
+
500
+ :exc:`SchemaError` if the schema itself is invalid
501
+
502
+ .. rubric:: Footnotes
503
+ .. [#] known by a validator registered with :func:`validates`
504
+ """
465
505
if cls is None :
466
506
cls = validator_for (schema )
467
507
cls .check_schema (schema )
0 commit comments