diff options
author | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-11-15 01:56:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-15 01:56:31 +0800 |
commit | b961cb195299ce6a091ae692ec815b52f6b89300 (patch) | |
tree | a6b97b981b04b22a1296fa55e8b7f77e59eb5ba0 /python-packages/order_utils/src/zero_ex/json_schemas/__init__.py | |
parent | e1d64def2017ced0aba599b989ad42a51fdd46fe (diff) | |
download | dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar.gz dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar.bz2 dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar.lz dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar.xz dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.tar.zst dexon-sol-tools-b961cb195299ce6a091ae692ec815b52f6b89300.zip |
fix(order_utils.py): validate order w/json schema (#1260)
Diffstat (limited to 'python-packages/order_utils/src/zero_ex/json_schemas/__init__.py')
-rw-r--r-- | python-packages/order_utils/src/zero_ex/json_schemas/__init__.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/python-packages/order_utils/src/zero_ex/json_schemas/__init__.py b/python-packages/order_utils/src/zero_ex/json_schemas/__init__.py new file mode 100644 index 000000000..2a1728b8a --- /dev/null +++ b/python-packages/order_utils/src/zero_ex/json_schemas/__init__.py @@ -0,0 +1,61 @@ +"""JSON schemas and associated utilities.""" + +from os import path +import json +from typing import Mapping + +from pkg_resources import resource_string +import jsonschema + + +def assert_valid(data: Mapping, schema_id: str) -> None: + """Validate the given `data` against the specified `schema`. + + :param data: Python dictionary to be validated as a JSON object. + :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( + ... {'v': 27, 'r': '0x'+'f'*64, 's': '0x'+'f'*64}, + ... '/ECSignature', + ... ) + """ + # noqa + class LocalRefResolver(jsonschema.RefResolver): + """Resolve package-local JSON schema id's.""" + + def __init__(self): + self.ref_to_file = { + "/addressSchema": "address_schema.json", + "/hexSchema": "hex_schema.json", + "/orderSchema": "order_schema.json", + "/wholeNumberSchema": "whole_number_schema.json", + "/ECSignature": "ec_signature_schema.json", + "/ecSignatureParameterSchema": ( + "ec_signature_parameter_schema.json" + "" + ), + } + jsonschema.RefResolver.__init__(self, "", "") + + def resolve_from_url(self, url): + """Resolve the given URL.""" + ref = url.replace("file://", "") + if ref in self.ref_to_file: + return json.loads( + resource_string( + "zero_ex.json_schemas", + f"schemas/{self.ref_to_file[ref]}", + ) + ) + raise jsonschema.ValidationError( + f"Unknown ref '{ref}'. " + + f"Known refs: {list(self.ref_to_file.keys())}." + ) + + resolver = LocalRefResolver() + jsonschema.validate( + data, resolver.resolve_from_url(schema_id), resolver=resolver + ) |