Python zero_ex.json_schemas

0x JSON schemas and associated utilities.

Setup

Install the package with pip:

pip install 0x-json-schemas
zero_ex.json_schemas.assert_valid(data, schema_id)[source]

Validate the given data against the specified schema.

Parameters
  • data (Mapping[~KT, +VT_co]) – Python dictionary to be validated as a JSON object.

  • schema_id (str) – id property of the JSON schema to validate against. Must be one of those listed in the 0x JSON schema files.

Raises an exception if validation fails.

>>> from zero_ex.json_schemas import assert_valid
>>> from zero_ex.contract_addresses import chain_to_addresses, ChainId
>>> from eth_utils import remove_0x_prefix
>>> import random
>>> from datetime import datetime, timedelta
>>> assert_valid(
...     {'makerAddress': '0x5409ed021d9299bf6814279a6a1411a7e866a631',
...      'takerAddress': '0x0000000000000000000000000000000000000000',
...      'senderAddress': '0x0000000000000000000000000000000000000000',
...      'exchangeAddress': '0x4f833a24e1f95d70f028921e27040ca56e09ab0b',
...      'feeRecipientAddress': (
...          '0x0000000000000000000000000000000000000000'
...      ),
...      'makerAssetData': (
...          chain_to_addresses(ChainId.MAINNET).zrx_token
...      ),
...      'takerAssetData': (
...          chain_to_addresses(ChainId.MAINNET).ether_token
...      ),
...      'salt': random.randint(1, 100000000000000000),
...      'makerFee': 0,
...      'makerFeeAssetData': '0x' + '00'*20,
...      'takerFee': 0,
...      'takerFeeAssetData': '0x' + '00'*20,
...      'makerAssetAmount': 1000000000000000000,
...      'takerAssetAmount': 500000000000000000000,
...      'expirationTimeSeconds': round(
...          (datetime.utcnow() + timedelta(days=1)).timestamp()
...      ),
...      'chainId': 50
...     },
...     "/orderSchema"
... )
Return type

None

zero_ex.json_schemas.assert_valid_json(data, schema_id)[source]

Validate the given data against the specified schema.

Parameters
  • data (str) – JSON string to be validated.

  • schema_id (str) –

    id property of the JSON schema to validate against. Must be one of those listed in the 0x JSON schema files.

Raises an exception if validation fails.

>>> assert_valid_json(
...     '''{
...         "v": 27,
...         "r": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
...         "s": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
...     }''',
...     '/ecSignatureSchema',
... )
Return type

None

Indices and tables