diff options
author | Fabio Berger <me@fabioberger.com> | 2017-06-01 20:46:09 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-06-01 20:46:09 +0800 |
commit | c74b16eda9465104e1969b1635ac826835d203eb (patch) | |
tree | ecf750865f425d65a830fb786359ad07451a805c /src | |
parent | 0991191a9b6c00fac80cd9f5ffa8e76c62f82eb5 (diff) | |
download | dexon-sol-tools-c74b16eda9465104e1969b1635ac826835d203eb.tar.gz dexon-sol-tools-c74b16eda9465104e1969b1635ac826835d203eb.tar.zst dexon-sol-tools-c74b16eda9465104e1969b1635ac826835d203eb.zip |
Add convertToJSONSchemaCompatibleObject method
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.js.ts | 5 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 5 | ||||
-rw-r--r-- | src/utils/schema_validator.ts | 7 |
3 files changed, 15 insertions, 2 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index e5eb6b520..92a5d2c54 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -11,6 +11,7 @@ import {Web3Wrapper} from './web3_wrapper'; import {constants} from './utils/constants'; import {utils} from './utils/utils'; import {assert} from './utils/assert'; +import {SchemaValidator} from './utils/schema_validator'; import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; @@ -33,7 +34,9 @@ export class ZeroEx { * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ public static getOrderHashHex(exchangeContractAddr: string, order: Order): string { - assert.doesConformToSchema('order', JSON.parse(JSON.stringify(order)), orderSchema); + assert.doesConformToSchema('order', + SchemaValidator.convertToJSONSchemaCompatibleObject(order as object), + orderSchema); const taker = _.isEmpty(order.taker) ? constants.NULL_ADDRESS : order.taker ; const orderParts = [ diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 754210778..59e6c755e 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -14,6 +14,7 @@ import {ContractWrapper} from './contract_wrapper'; import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; import {signedOrderSchema} from '../schemas/signed_order_schema'; +import {SchemaValidator} from '../utils/schema_validator'; import {ContractResponse} from '../types'; import {constants} from '../utils/constants'; @@ -56,7 +57,9 @@ export class ExchangeWrapper extends ContractWrapper { } public async fillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean = true): Promise<void> { - assert.doesConformToSchema('signedOrder', JSON.parse(JSON.stringify(signedOrder)), signedOrderSchema); + assert.doesConformToSchema('signedOrder', + SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), + signedOrderSchema); assert.isBigNumber('fillAmount', fillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts index cf45d0343..9ff7259d9 100644 --- a/src/utils/schema_validator.ts +++ b/src/utils/schema_validator.ts @@ -5,6 +5,13 @@ import {tokenSchema} from '../schemas/token_schema'; export class SchemaValidator { private validator: Validator; + // In order to validate a complex JS object using jsonschema, we must replace any complex + // sub-types (e.g BigNumber) with a simpler string represenation. Since BigNumber and other + // complex types implement the `toString` method, we can stringify the object and + // then parse it. The resultant object can then be checked using jsonschema. + public static convertToJSONSchemaCompatibleObject(obj: object): object { + return JSON.parse(JSON.stringify(obj)); + } constructor() { this.validator = new Validator(); this.validator.addSchema(tokenSchema, tokenSchema.id); |