From 71be75e0ed5a7e930bcb2fe8aa06ab1ec033fdee Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 10:40:06 +0200 Subject: Convert to JSON schema compatible object by default --- src/contract_wrappers/exchange_wrapper.ts | 26 +++++++------------------- src/utils/schema_validator.ts | 7 ++++--- 2 files changed, 11 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 73ec0886c..aaabd585f 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -121,9 +121,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, takerAddress: string): Promise { - assert.doesConformToSchema('signedOrder', - SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), - signedOrderSchema); + assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); @@ -172,9 +170,8 @@ export class ExchangeWrapper extends ContractWrapper { await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); _.forEach(orderFillRequests, async (orderFillRequest: OrderFillRequest, i: number) => { - assert.doesConformToSchema(`orderFillRequests[${i}].signedOrder`, - SchemaValidator.convertToJSONSchemaCompatibleObject(orderFillRequest.signedOrder as object), - signedOrderSchema); + assert.doesConformToSchema(`orderFillRequests[${i}].signedOrder`, orderFillRequest.signedOrder, + signedOrderSchema); assert.isBigNumber(`orderFillRequests[${i}].takerTokenFillAmount`, orderFillRequest.takerTokenFillAmount); await this.validateFillOrderAndThrowIfInvalidAsync( orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress); @@ -231,9 +228,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, takerAddress: string) { - assert.doesConformToSchema('signedOrder', - SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), - signedOrderSchema); + assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('fillTakerAmount', fillTakerAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); @@ -277,10 +272,7 @@ export class ExchangeWrapper extends ContractWrapper { public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[], takerAddress: string) { await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); - assert.doesConformToSchema('orderFillOrKillRequests', - SchemaValidator.convertToJSONSchemaCompatibleObject(orderFillOrKillRequests), - orderFillOrKillRequestsSchema, - ); + assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests, orderFillOrKillRequestsSchema); const exchangeInstance = await this.getExchangeContractAsync(); _.each(orderFillOrKillRequests, request => { this.validateFillOrKillOrderAndThrowIfInvalidAsync(request.signedOrder, @@ -332,9 +324,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async cancelOrderAsync( order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise { - assert.doesConformToSchema('order', - SchemaValidator.convertToJSONSchemaCompatibleObject(order as object), - orderSchema); + assert.doesConformToSchema('order', order, orderSchema); assert.isBigNumber('takerTokenCancelAmount', takerTokenCancelAmount); await assert.isSenderAddressAsync('order.maker', order.maker, this.web3Wrapper); @@ -375,9 +365,7 @@ export class ExchangeWrapper extends ContractWrapper { await assert.isSenderAddressAsync('maker', maker, this.web3Wrapper); _.forEach(orderCancellationRequests, async (cancellationRequest: OrderCancellationRequest, i: number) => { - assert.doesConformToSchema(`orderCancellationRequests[${i}].order`, - SchemaValidator.convertToJSONSchemaCompatibleObject(cancellationRequest.order as object), orderSchema, - ); + assert.doesConformToSchema(`orderCancellationRequests[${i}].order`, cancellationRequest.order, orderSchema); assert.isBigNumber(`orderCancellationRequests[${i}].takerTokenCancelAmount`, cancellationRequest.takerTokenCancelAmount, ); diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts index 72f6afffa..41c4696d6 100644 --- a/src/utils/schema_validator.ts +++ b/src/utils/schema_validator.ts @@ -11,7 +11,7 @@ export class SchemaValidator { // sub-types (e.g BigNumber) with a simpler string representation. 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: any): any { + private static convertToJSONSchemaCompatibleObject(obj: any): any { return JSON.parse(JSON.stringify(obj)); } constructor() { @@ -25,7 +25,8 @@ export class SchemaValidator { this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id); this.validator.addSchema(orderFillOrKillRequestsSchema, orderFillOrKillRequestsSchema.id); } - public validate(instance: object, schema: Schema): ValidatorResult { - return this.validator.validate(instance, schema); + public validate(instance: any, schema: Schema): ValidatorResult { + const jsonSchemaCompatibleObject = SchemaValidator.convertToJSONSchemaCompatibleObject(instance); + return this.validator.validate(jsonSchemaCompatibleObject, schema); } } -- cgit From 77174341d2498f772b60554fdc29100e001415c6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 11:15:26 +0200 Subject: Make static function public --- src/contract_wrappers/exchange_wrapper.ts | 1 - src/utils/schema_validator.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index aaabd585f..be6234ff9 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -30,7 +30,6 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; -import {SchemaValidator} from '../utils/schema_validator'; import {constants} from '../utils/constants'; import {TokenWrapper} from './token_wrapper'; diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts index 41c4696d6..786dca624 100644 --- a/src/utils/schema_validator.ts +++ b/src/utils/schema_validator.ts @@ -11,7 +11,7 @@ export class SchemaValidator { // sub-types (e.g BigNumber) with a simpler string representation. 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. - private static convertToJSONSchemaCompatibleObject(obj: any): any { + static convertToJSONSchemaCompatibleObject(obj: any): any { return JSON.parse(JSON.stringify(obj)); } constructor() { -- cgit From f05d1df4058cc5dbb64bcdbe46b73fd553f55038 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 16:29:27 +0200 Subject: Fix tests --- src/contract_wrappers/exchange_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 522be4588..e504674e4 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -170,7 +170,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema); - for (const orderFillrequest of orderFillRequests) { + for (const orderFillRequest of orderFillRequests) { await this.validateFillOrderAndThrowIfInvalidAsync( orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress); } -- cgit From b4f3a889b360a515dc0672e60c831a6b89c23fdf Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 16:43:12 +0200 Subject: Fix tests one more --- src/contract_wrappers/exchange_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index e504674e4..438255260 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -366,7 +366,7 @@ export class ExchangeWrapper extends ContractWrapper { await this.validateCancelOrderAndThrowIfInvalidAsync( cancellationRequest.order, cancellationRequest.takerTokenCancelAmount, ); - }); + } const exchangeInstance = await this.getExchangeContractAsync(); const orderAddressesValuesAndTakerTokenCancelAmounts = _.map(orderCancellationRequests, cancellationRequest => { return [ -- cgit From 33741c5e1e258523cb49d8171de0b5218e70ca3d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 18:28:49 +0200 Subject: Fix test --- src/contract_wrappers/exchange_wrapper.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 96e0ac38d..c76de79e9 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -172,15 +172,12 @@ export class ExchangeWrapper extends ContractWrapper { ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); - assert.doesConformToSchema( - 'signedOrders', SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrders), signedOrdersSchema - ); + assert.doesConformToSchema('signedOrders', signedOrders, signedOrdersSchema); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); - _.forEach(signedOrders, - async (signedOrder: SignedOrder, i: number) => { - await this.validateFillOrderAndThrowIfInvalidAsync( - signedOrder, takerTokenFillAmount, takerAddress); - }); + for (const signedOrder of signedOrders) { + await this.validateFillOrderAndThrowIfInvalidAsync( + signedOrder, takerTokenFillAmount, takerAddress); + } if (_.isEmpty(signedOrders)) { return; // no-op } -- cgit From 4e9a83132cdec4a6278052919310118c2abbc0ca Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 19:26:21 +0200 Subject: Address feedback --- src/contract_wrappers/exchange_wrapper.ts | 2 +- src/utils/schema_validator.ts | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index c76de79e9..65a873a9f 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -426,7 +426,7 @@ export class ExchangeWrapper extends ContractWrapper { await this.validateCancelOrderAndThrowIfInvalidAsync( cancellationRequest.order, cancellationRequest.takerTokenCancelAmount, ); - }; + } if (_.isEmpty(orderCancellationRequests)) { return; // no-op } diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts index 786dca624..6e0e6ab9c 100644 --- a/src/utils/schema_validator.ts +++ b/src/utils/schema_validator.ts @@ -7,13 +7,6 @@ import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_reque 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 representation. 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. - static convertToJSONSchemaCompatibleObject(obj: any): any { - return JSON.parse(JSON.stringify(obj)); - } constructor() { this.validator = new Validator(); this.validator.addSchema(tokenSchema, tokenSchema.id); @@ -25,8 +18,12 @@ export class SchemaValidator { this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id); this.validator.addSchema(orderFillOrKillRequestsSchema, orderFillOrKillRequestsSchema.id); } + // In order to validate a complex JS object using jsonschema, we must replace any complex + // sub-types (e.g BigNumber) with a simpler string representation. 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 validate(instance: any, schema: Schema): ValidatorResult { - const jsonSchemaCompatibleObject = SchemaValidator.convertToJSONSchemaCompatibleObject(instance); + const jsonSchemaCompatibleObject = JSON.parse(JSON.stringify(instance)); return this.validator.validate(jsonSchemaCompatibleObject, schema); } } -- cgit From 1b828b37aa8e3cc762926c19d9d4e3940c4e6618 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 19:38:34 +0200 Subject: Fix tests --- src/0x.js.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 2bf8cad5e..7a5d14aa8 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -130,8 +130,7 @@ export class ZeroEx { * Computes the orderHash for a given order and returns it as a hex encoded string. */ public async getOrderHashHexAsync(order: Order|SignedOrder): Promise { - assert.doesConformToSchema('order', SchemaValidator.convertToJSONSchemaCompatibleObject(order as object), - orderSchema); + assert.doesConformToSchema('order', order, orderSchema); const exchangeContractAddr = await this.getExchangeAddressAsync(); const orderHashHex = utils.getOrderHashHex(order, exchangeContractAddr); -- cgit