-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements a very simple wrapper to the RapidJSON SchemaValidator() facility, addressing issue #71.
- Loading branch information
Showing
4 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
loads | ||
encoder | ||
decoder | ||
validator | ||
|
||
.. data:: __author__ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
================= | ||
Validator class | ||
================= | ||
|
||
.. module:: rapidjson | ||
|
||
.. testsetup:: | ||
|
||
from rapidjson import Validator | ||
|
||
.. class:: Validator(json_schema) | ||
|
||
:param json_schema: the `JSON schema`__, specified as a ``str`` instance or an *UTF-8* | ||
``bytes`` instance | ||
|
||
__ 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 | ||
|
||
The given `json` value will be validated accordingly to the *schema*: a ``ValueError`` | ||
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 ValueError as error: | ||
... print(error.args) | ||
... | ||
('required', '#', '#') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
|
||
import rapidjson as rj | ||
|
||
|
||
@pytest.mark.parametrize('schema,json', ( | ||
('{ "type": ["number", "string"] }', '42'), | ||
('{ "type": ["number", "string"] }', '"Life, the universe, and everything"'), | ||
)) | ||
@pytest.mark.unit | ||
def test_valid(schema, json): | ||
validate = rj.Validator(schema) | ||
validate(json) | ||
|
||
|
||
@pytest.mark.parametrize('schema,json,details', ( | ||
('{ "type": ["number", "string"] }', | ||
'["Life", "the universe", "and everything"]', | ||
('type', '#', '#'), | ||
), | ||
)) | ||
@pytest.mark.unit | ||
def test_invalid(schema, json, details): | ||
validate = rj.Validator(schema) | ||
with pytest.raises(ValueError) as error: | ||
validate(json) | ||
assert error.value.args == details |