Add orderFillOrKillRequestsSchema and validate using it instead of looping over the requests and validating them individually
This commit is contained in:
@@ -28,6 +28,7 @@ import {utils} from '../utils/utils';
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
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';
|
||||
@@ -276,12 +277,12 @@ 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,
|
||||
);
|
||||
const exchangeInstance = await this.getExchangeContractAsync();
|
||||
_.each(orderFillOrKillRequests, request => {
|
||||
assert.doesConformToSchema('signedOrder',
|
||||
SchemaValidator.convertToJSONSchemaCompatibleObject(request.signedOrder as object),
|
||||
signedOrderSchema);
|
||||
assert.isBigNumber('fillTakerAmount', request.fillTakerAmount);
|
||||
this.validateFillOrKillOrderAndThrowIfInvalidAsync(request.signedOrder,
|
||||
exchangeInstance.address,
|
||||
request.fillTakerAmount);
|
||||
|
||||
11
src/schemas/basic_type_schemas.ts
Normal file
11
src/schemas/basic_type_schemas.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const addressSchema = {
|
||||
id: '/addressSchema',
|
||||
type: 'string',
|
||||
pattern: '^0[xX][0-9A-Fa-f]{40}$',
|
||||
};
|
||||
|
||||
export const numberSchema = {
|
||||
id: '/numberSchema',
|
||||
type: 'string',
|
||||
pattern: '^\\d+(\\.\\d+)?$',
|
||||
};
|
||||
12
src/schemas/order_fill_or_kill_requests_schema.ts
Normal file
12
src/schemas/order_fill_or_kill_requests_schema.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export const orderFillOrKillRequestsSchema = {
|
||||
id: '/OrderFillOrKillRequests',
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
signedOrder: {$ref: '/signedOrderSchema'},
|
||||
fillTakerAmount: {type: '/numberSchema'},
|
||||
},
|
||||
required: ['signedOrder', 'fillTakerAmount'],
|
||||
type: 'object',
|
||||
},
|
||||
};
|
||||
@@ -1,15 +1,3 @@
|
||||
export const addressSchema = {
|
||||
id: '/addressSchema',
|
||||
type: 'string',
|
||||
pattern: '^0[xX][0-9A-Fa-f]{40}$',
|
||||
};
|
||||
|
||||
export const numberSchema = {
|
||||
id: '/numberSchema',
|
||||
type: 'string',
|
||||
pattern: '^\\d+(\\.\\d+)?$',
|
||||
};
|
||||
|
||||
export const orderSchema = {
|
||||
id: '/orderSchema',
|
||||
properties: {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import {Validator, ValidatorResult} from 'jsonschema';
|
||||
import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema';
|
||||
import {addressSchema, numberSchema, orderSchema, signedOrderSchema} from '../schemas/order_schemas';
|
||||
import {orderSchema, signedOrderSchema} from '../schemas/order_schemas';
|
||||
import {addressSchema, numberSchema} from '../schemas/basic_type_schemas';
|
||||
import {tokenSchema} from '../schemas/token_schema';
|
||||
import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema';
|
||||
|
||||
export class SchemaValidator {
|
||||
private validator: Validator;
|
||||
@@ -9,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: object): object {
|
||||
public static convertToJSONSchemaCompatibleObject(obj: any): any {
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
constructor() {
|
||||
@@ -21,6 +23,7 @@ export class SchemaValidator {
|
||||
this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id);
|
||||
this.validator.addSchema(signedOrderSchema, signedOrderSchema.id);
|
||||
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);
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as _ from 'lodash';
|
||||
import * as chai from 'chai';
|
||||
import * as BigNumber from 'bignumber.js';
|
||||
import promisify = require('es6-promisify');
|
||||
import {numberSchema} from '../src/schemas/order_schemas';
|
||||
import {numberSchema} from '../src/schemas/basic_type_schemas';
|
||||
import {SchemaValidator} from '../src/utils/schema_validator';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
|
||||
Reference in New Issue
Block a user