File: validator.rst

package info (click to toggle)
python-rapidjson 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,368 kB
  • sloc: cpp: 3,332; python: 1,990; makefile: 106
file content (77 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
.. -*- coding: utf-8 -*-
.. :Project:   python-rapidjson -- Validator class documentation
.. :Author:    Lele Gaifax <lele@metapensiero.it>
.. :License:   MIT License
.. :Copyright: © 2017, 2018, 2019, 2020 Lele Gaifax
..

=================
 Validator class
=================

.. currentmodule:: rapidjson

.. testsetup::

   from rapidjson import ValidationError, Validator

.. class:: Validator(json_schema)

   :param json_schema: the `JSON schema`__, specified as a ``str`` instance or an *UTF-8*
                       ``bytes`` instance
   :raises JSONDecodeError: if `json_schema` is not a valid ``JSON`` value

   __ http://json-schema.org/documentation.html

   .. method:: __call__(json)

      :param json: the ``JSON`` value, specified as a ``str`` instance or an *UTF-8*
                   ``bytes`` instance, that will be validated
      :raises JSONDecodeError: if `json` is not a valid ``JSON`` value

      The given `json` value will be validated accordingly to the *schema*: a
      :exc:`ValidationError` will be raised if the validation fails, and the exception
      will contain three arguments, respectively the type of the error, the position in
      the schema and the position in the ``JSON`` document where the error occurred:

      .. doctest::

         >>> validate = Validator('{"required": ["a", "b"]}')
         >>> validate('{"a": null, "b": 1}')
         >>> try:
         ...   validate('{"a": null, "c": false}')
         ... except ValidationError as error:
         ...   print(error.args)
         ...
         ('required', '#', '#')

      .. doctest::

         >>> validate = Validator('{"type": "array",'
         ...                      ' "items": {"type": "string"},'
         ...                      ' "minItems": 1}')
         >>> validate('["foo", "bar"]')
         >>> try:
         ...   validate('[]')
         ... except ValidationError as error:
         ...   print(error.args)
         ...
         ('minItems', '#', '#')

      .. doctest::

         >>> try:
         ...   validate('[1]')
         ... except ValidationError as error:
         ...   print(error.args)
         ...
         ('type', '#/items', '#/0')

      When `json` is not a valid JSON document, a :exc:`JSONDecodeError` is raised instead:

      .. doctest::

         >>> validate('x')
         Traceback (most recent call last):
           File "<stdin>", line 1, in <module>
         rapidjson.JSONDecodeError: Invalid JSON