diff options
author | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-12-29 04:28:30 +0800 |
---|---|---|
committer | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-12-29 04:28:30 +0800 |
commit | e62e61bf7145c0a8011e098aa22168416d01a781 (patch) | |
tree | 76ab61f1258abd9c73d104e42d08a377326d954c /python-packages | |
parent | a3eab71908cd2e188438b4d411b7c426c874f0b2 (diff) | |
download | dexon-0x-contracts-e62e61bf7145c0a8011e098aa22168416d01a781.tar.gz dexon-0x-contracts-e62e61bf7145c0a8011e098aa22168416d01a781.tar.zst dexon-0x-contracts-e62e61bf7145c0a8011e098aa22168416d01a781.zip |
Add entry point for validation of JSON strings
Diffstat (limited to 'python-packages')
-rw-r--r-- | python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py b/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py index 792e6041f..10c564b99 100644 --- a/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py +++ b/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py @@ -59,3 +59,25 @@ def assert_valid(data: Mapping, schema_id: str) -> None: _, schema = _LOCAL_RESOLVER.resolve(schema_id) jsonschema.validate(data, schema, resolver=_LOCAL_RESOLVER) + + +def assert_valid_json(data: str, schema_id: str) -> None: + """Validate the given `data` against the specified `schema`. + + :param data: JSON string to be validated. + :param schema_id: id property of the JSON schema to validate against. Must + be one of those listed in `the 0x JSON schema files + <https://github.com/0xProject/0x-monorepo/tree/development/packages/json-schemas/schemas>`_. + + Raises an exception if validation fails. + + >>> assert_valid_json( + ... r'''{ + ... "v": 27, + ... "r": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + ... "s": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + ... }''', + ... '/ecSignatureSchema', + ... ) + """ # noqa: E501 (line too long) + assert_valid(json.loads(data), schema_id) |