Use 0x-json-schemas

This commit is contained in:
Leonid Logvinov 2017-08-28 16:47:49 +02:00
parent 0afc95982b
commit 96da2c26dc
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
21 changed files with 40 additions and 644 deletions

View File

@ -90,13 +90,13 @@
"webpack": "^3.1.0"
},
"dependencies": {
"0x-json-schemas": "^0.1.5",
"bignumber.js": "^4.0.2",
"compare-versions": "^3.0.1",
"es6-promisify": "^5.0.0",
"ethereumjs-abi": "^0.6.4",
"ethereumjs-util": "^5.1.1",
"find-versions": "^2.0.0",
"jsonschema": "^1.1.1",
"lodash": "^4.17.4",
"publish-release": "^1.3.3",
"truffle-contract": "^2.0.1",

View File

@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
import {SchemaValidator, schemas} from '0x-json-schemas';
import {bigNumberConfigs} from './bignumber_config';
import * as ethUtil from 'ethereumjs-util';
import contract = require('truffle-contract');
@ -13,13 +14,9 @@ import {assert} from './utils/assert';
import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
import {EtherTokenWrapper} from './contract_wrappers/ether_token_wrapper';
import {ecSignatureSchema} from './schemas/ec_signature_schema';
import {TokenWrapper} from './contract_wrappers/token_wrapper';
import {TokenTransferProxyWrapper} from './contract_wrappers/token_transfer_proxy_wrapper';
import {ECSignature, ZeroExError, Order, SignedOrder, Web3Provider} from './types';
import {orderHashSchema} from './schemas/order_hash_schema';
import {orderSchema} from './schemas/order_schemas';
import {SchemaValidator} from './utils/schema_validator';
// Customize our BigNumber instances
bigNumberConfigs.configure();
@ -70,7 +67,7 @@ export class ZeroEx {
*/
public static isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean {
assert.isHexString('data', data);
assert.doesConformToSchema('signature', signature, ecSignatureSchema);
assert.doesConformToSchema('signature', signature, schemas.ecSignatureSchema);
assert.isETHAddressHex('signerAddress', signerAddress);
const dataBuff = ethUtil.toBuffer(data);
@ -113,7 +110,7 @@ export class ZeroEx {
// format, we only assert that we were indeed passed a string.
assert.isString('orderHash', orderHash);
const schemaValidator = new SchemaValidator();
const isValidOrderHash = schemaValidator.validate(orderHash, orderHashSchema).valid;
const isValidOrderHash = schemaValidator.validate(orderHash, schemas.orderHashSchema).valid;
return isValidOrderHash;
}
/**
@ -154,7 +151,7 @@ export class ZeroEx {
* @return The resulting orderHash from hashing the supplied order.
*/
public static getOrderHashHex(order: Order|SignedOrder): string {
assert.doesConformToSchema('order', order, orderSchema);
assert.doesConformToSchema('order', order, schemas.orderSchema);
const orderHashHex = utils.getOrderHashHex(order);
return orderHashHex;
}

View File

@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
import {SchemaValidator, schemas} from '0x-json-schemas';
import promisify = require('es6-promisify');
import {Web3Wrapper} from '../web3_wrapper';
import {
@ -32,15 +33,6 @@ import {utils} from '../utils/utils';
import {eventUtils} from '../utils/event_utils';
import {OrderValidationUtils} from '../utils/order_validation_utils';
import {ContractWrapper} from './contract_wrapper';
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
import {signedOrdersSchema} from '../schemas/signed_orders_schema';
import {subscriptionOptsSchema} from '../schemas/subscription_opts_schema';
import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema';
import {orderFillRequestsSchema} from '../schemas/order_fill_requests_schema';
import {orderCancellationRequestsSchema} from '../schemas/order_cancel_schema';
import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema';
import {orderHashSchema} from '../schemas/order_hash_schema';
import {signedOrderSchema, orderSchema} from '../schemas/order_schemas';
import {constants} from '../utils/constants';
import {TokenWrapper} from './token_wrapper';
import {decorators} from '../utils/decorators';
@ -96,7 +88,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @return The amount of the order (in taker tokens) that has either been filled or canceled.
*/
public async getUnavailableTakerAmountAsync(orderHash: string): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('orderHash', orderHash, orderHashSchema);
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
const exchangeContract = await this._getExchangeContractAsync();
let unavailableTakerTokenAmount = await exchangeContract.getUnavailableTakerTokenAmount.call(orderHash);
@ -110,7 +102,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @return The amount of the order (in taker tokens) that has already been filled.
*/
public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('orderHash', orderHash, orderHashSchema);
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
const exchangeContract = await this._getExchangeContractAsync();
let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHash);
@ -125,7 +117,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @return The amount of the order (in taker tokens) that has been cancelled.
*/
public async getCanceledTakerAmountAsync(orderHash: string): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('orderHash', orderHash, orderHashSchema);
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
const exchangeContract = await this._getExchangeContractAsync();
let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHash);
@ -155,7 +147,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async fillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber.BigNumber,
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
takerAddress: string): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount);
assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
@ -215,7 +207,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async fillOrdersUpToAsync(signedOrders: SignedOrder[], fillTakerTokenAmount: BigNumber.BigNumber,
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
takerAddress: string): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('signedOrders', signedOrders, signedOrdersSchema);
assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema);
const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress);
assert.hasAtMostOneUniqueValue(takerTokenAddresses,
ExchangeContractErrs.MultipleTakerTokensInFillUpToDisallowed);
@ -301,7 +293,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async batchFillOrdersAsync(orderFillRequests: OrderFillRequest[],
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
takerAddress: string): Promise<void> {
assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema);
assert.doesConformToSchema('orderFillRequests', orderFillRequests, schemas.orderFillRequestsSchema);
const exchangeContractAddresses = _.map(
orderFillRequests,
orderFillRequest => orderFillRequest.signedOrder.exchangeContractAddress,
@ -372,7 +364,7 @@ export class ExchangeWrapper extends ContractWrapper {
@decorators.contractCallErrorHandler
public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber.BigNumber,
takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
@ -417,7 +409,8 @@ export class ExchangeWrapper extends ContractWrapper {
@decorators.contractCallErrorHandler
public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[],
takerAddress: string): Promise<void> {
assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests, orderFillOrKillRequestsSchema);
assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests,
schemas.orderFillOrKillRequestsSchema);
const exchangeContractAddresses = _.map(
orderFillOrKillRequests,
orderFillOrKillRequest => orderFillOrKillRequest.signedOrder.exchangeContractAddress,
@ -483,7 +476,7 @@ export class ExchangeWrapper extends ContractWrapper {
@decorators.contractCallErrorHandler
public async cancelOrderAsync(
order: Order|SignedOrder, cancelTakerTokenAmount: BigNumber.BigNumber): Promise<BigNumber.BigNumber> {
assert.doesConformToSchema('order', order, orderSchema);
assert.doesConformToSchema('order', order, schemas.orderSchema);
assert.isBigNumber('takerTokenCancelAmount', cancelTakerTokenAmount);
await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper);
@ -522,7 +515,7 @@ export class ExchangeWrapper extends ContractWrapper {
@decorators.contractCallErrorHandler
public async batchCancelOrdersAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> {
assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests,
orderCancellationRequestsSchema);
schemas.orderCancellationRequestsSchema);
const exchangeContractAddresses = _.map(
orderCancellationRequests,
orderCancellationRequest => orderCancellationRequest.order.exchangeContractAddress,
@ -584,8 +577,8 @@ export class ExchangeWrapper extends ContractWrapper {
Promise<ContractEventEmitter> {
assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress);
assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents);
assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, subscriptionOptsSchema);
assert.doesConformToSchema('indexFilterValues', indexFilterValues, indexFilterValuesSchema);
assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, schemas.subscriptionOptsSchema);
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
const exchangeContract = await this._getExchangeContractAsync();
let createLogEvent: CreateContractEvent;
switch (eventName) {
@ -637,7 +630,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async validateFillOrderThrowIfInvalidAsync(signedOrder: SignedOrder,
fillTakerTokenAmount: BigNumber.BigNumber,
takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
const zrxTokenAddress = await this._getZRXTokenAddressAsync();
@ -652,7 +645,7 @@ export class ExchangeWrapper extends ContractWrapper {
*/
public async validateCancelOrderThrowIfInvalidAsync(
order: Order, cancelTakerTokenAmount: BigNumber.BigNumber): Promise<void> {
assert.doesConformToSchema('order', order, orderSchema);
assert.doesConformToSchema('order', order, schemas.orderSchema);
assert.isBigNumber('cancelTakerTokenAmount', cancelTakerTokenAmount);
const orderHash = utils.getOrderHashHex(order);
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
@ -670,7 +663,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async validateFillOrKillOrderThrowIfInvalidAsync(signedOrder: SignedOrder,
fillTakerTokenAmount: BigNumber.BigNumber,
takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
const zrxTokenAddress = await this._getZRXTokenAddressAsync();
@ -705,7 +698,7 @@ export class ExchangeWrapper extends ContractWrapper {
private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature,
signerAddressHex: string): Promise<boolean> {
assert.isHexString('dataHex', dataHex);
assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema);
assert.doesConformToSchema('ecSignature', ecSignature, schemas.ecSignatureSchema);
assert.isETHAddressHex('signerAddressHex', signerAddressHex);
const exchangeInstance = await this._getExchangeContractAsync();

View File

@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
import {SchemaValidator, schemas} from '0x-json-schemas';
import {Web3Wrapper} from '../web3_wrapper';
import {assert} from '../utils/assert';
import {utils} from '../utils/utils';
@ -8,8 +9,6 @@ import {constants} from '../utils/constants';
import {ContractWrapper} from './contract_wrapper';
import * as TokenArtifacts from '../artifacts/Token.json';
import * as TokenTransferProxyArtifacts from '../artifacts/TokenTransferProxy.json';
import {subscriptionOptsSchema} from '../schemas/subscription_opts_schema';
import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema';
import {
TokenContract,
ZeroExError,
@ -230,8 +229,8 @@ export class TokenWrapper extends ContractWrapper {
indexFilterValues: IndexedFilterValues): Promise<ContractEventEmitter> {
assert.isETHAddressHex('tokenAddress', tokenAddress);
assert.doesBelongToStringEnum('eventName', eventName, TokenEvents);
assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, subscriptionOptsSchema);
assert.doesConformToSchema('indexFilterValues', indexFilterValues, indexFilterValuesSchema);
assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, schemas.subscriptionOptsSchema);
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
const tokenContract = await this._getTokenContractAsync(tokenAddress);
let createLogEvent: CreateContractEvent;
switch (eventName) {

49
src/globals.d.ts vendored
View File

@ -25,55 +25,6 @@ declare namespace Chai {
}
/* tslint:enable */
// jsonschema declarations
// Source: https://github.com/tdegrunt/jsonschema/blob/master/lib/index.d.ts
declare interface Schema {
id?: string;
$schema?: string;
title?: string;
description?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
additionalItems?: boolean | Schema;
items?: Schema | Schema[];
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
required?: string[];
additionalProperties?: boolean | Schema;
definitions?: {
[name: string]: Schema;
};
properties?: {
[name: string]: Schema;
};
patternProperties?: {
[name: string]: Schema;
};
dependencies?: {
[name: string]: Schema | string[];
};
'enum'?: any[];
type?: string | string[];
allOf?: Schema[];
anyOf?: Schema[];
oneOf?: Schema[];
not?: Schema;
// This is the only property that's not defined in https://github.com/tdegrunt/jsonschema/blob/master/lib/index.d.ts
// There is an open issue for that: https://github.com/tdegrunt/jsonschema/issues/194
// There is also an opened PR: https://github.com/tdegrunt/jsonschema/pull/218/files
// As soon as it gets merged we should be good to use types from 'jsonschema' package
$ref?: string;
}
declare module '*.json' {
const json: any;
/* tslint:disable */

View File

@ -1,11 +0,0 @@
export const addressSchema = {
id: '/addressSchema',
type: 'string',
pattern: '^0x[0-9a-f]{40}$',
};
export const numberSchema = {
id: '/numberSchema',
type: 'string',
pattern: '^\\d+(\\.\\d+)?$',
};

View File

@ -1,20 +0,0 @@
export const ecSignatureParameterSchema = {
id: '/ecSignatureParameter',
type: 'string',
pattern: '^0[xX][0-9A-Fa-f]{64}$',
};
export const ecSignatureSchema = {
id: '/ECSignature',
properties: {
v: {
type: 'number',
minimum: 27,
maximum: 28,
},
r: {$ref: '/ecSignatureParameter'},
s: {$ref: '/ecSignatureParameter'},
},
required: ['v', 'r', 's'],
type: 'object',
};

View File

@ -1,11 +0,0 @@
export const indexFilterValuesSchema = {
id: '/indexFilterValues',
additionalProperties: {
oneOf: [
{$ref: '/numberSchema'},
{$ref: '/addressSchema'},
{$ref: '/orderHashSchema'},
],
},
type: 'object',
};

View File

@ -1,12 +0,0 @@
export const orderCancellationRequestsSchema = {
id: '/OrderCancellationRequests',
type: 'array',
items: {
properties: {
order: {$ref: '/orderSchema'},
takerTokenCancelAmount: {$ref: '/numberSchema'},
},
required: ['order', 'takerTokenCancelAmount'],
type: 'object',
},
};

View File

@ -1,12 +0,0 @@
export const orderFillOrKillRequestsSchema = {
id: '/OrderFillOrKillRequests',
type: 'array',
items: {
properties: {
signedOrder: {$ref: '/signedOrderSchema'},
fillTakerAmount: {$ref: '/numberSchema'},
},
required: ['signedOrder', 'fillTakerAmount'],
type: 'object',
},
};

View File

@ -1,12 +0,0 @@
export const orderFillRequestsSchema = {
id: '/OrderFillRequests',
type: 'array',
items: {
properties: {
signedOrder: {$ref: '/signedOrderSchema'},
takerTokenFillAmount: {$ref: '/numberSchema'},
},
required: ['signedOrder', 'takerTokenFillAmount'],
type: 'object',
},
};

View File

@ -1,5 +0,0 @@
export const orderHashSchema = {
id: '/orderHashSchema',
type: 'string',
pattern: '^0x[0-9a-fA-F]{64}$',
};

View File

@ -1,39 +0,0 @@
export const orderSchema = {
id: '/orderSchema',
properties: {
maker: {$ref: '/addressSchema'},
taker: {$ref: '/addressSchema'},
makerFee: {$ref: '/numberSchema'},
takerFee: {$ref: '/numberSchema'},
makerTokenAmount: {$ref: '/numberSchema'},
takerTokenAmount: {$ref: '/numberSchema'},
makerTokenAddress: {$ref: '/addressSchema'},
takerTokenAddress: {$ref: '/addressSchema'},
salt: {$ref: '/numberSchema'},
feeRecipient: {$ref: '/addressSchema'},
expirationUnixTimestampSec: {$ref: '/numberSchema'},
exchangeContractAddress: {$ref: '/addressSchema'},
},
required: [
'maker', 'taker', 'makerFee', 'takerFee', 'makerTokenAmount', 'takerTokenAmount',
'salt', 'feeRecipient', 'expirationUnixTimestampSec', 'exchangeContractAddress',
],
type: 'object',
};
export const signedOrderSchema = {
id: '/signedOrderSchema',
allOf: [
{ $ref: '/orderSchema' },
{
properties: {
ecSignature: {$ref: '/ECSignature'},
},
required: ['ecSignature'],
},
],
};

View File

@ -1,5 +0,0 @@
export const signedOrdersSchema = {
id: '/signedOrdersSchema',
type: 'array',
items: {$ref: '/signedOrderSchema'},
};

View File

@ -1,20 +0,0 @@
export const blockParamSchema = {
id: '/blockParam',
oneOf: [
{
type: 'number',
},
{
enum: ['latest', 'earliest', 'pending'],
},
],
};
export const subscriptionOptsSchema = {
id: '/subscriptionOpts',
properties: {
fromBlock: {$ref: '/blockParam'},
toBlock: {$ref: '/blockParam'},
},
type: 'object',
};

View File

@ -1,11 +0,0 @@
export const tokenSchema = {
id: '/token',
properties: {
name: {type: 'string'},
symbol: {type: 'string'},
decimals: {type: 'number'},
address: {$ref: '/addressSchema'},
},
required: ['name', 'symbol', 'decimals', 'address'],
type: 'object',
};

View File

@ -2,7 +2,7 @@ import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
import * as Web3 from 'web3';
import {Web3Wrapper} from '../web3_wrapper';
import {SchemaValidator} from './schema_validator';
import {SchemaValidator, Schema} from '0x-json-schemas';
const HEX_REGEX = /^0x[0-9A-F]*$/i;

View File

@ -1,36 +0,0 @@
import {Validator, ValidatorResult, Schema as JSONSchema} from 'jsonschema';
import {ecSignatureSchema, ecSignatureParameterSchema} from '../schemas/ec_signature_schema';
import {orderHashSchema} from '../schemas/order_hash_schema';
import {orderSchema, signedOrderSchema} from '../schemas/order_schemas';
import {addressSchema, numberSchema} from '../schemas/basic_type_schemas';
import {tokenSchema} from '../schemas/token_schema';
import {subscriptionOptsSchema, blockParamSchema} from '../schemas/subscription_opts_schema';
import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema';
import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema';
export class SchemaValidator {
private validator: Validator;
constructor() {
this.validator = new Validator();
this.validator.addSchema(tokenSchema as JSONSchema, tokenSchema.id);
this.validator.addSchema(orderSchema as JSONSchema, orderSchema.id);
this.validator.addSchema(numberSchema as JSONSchema, numberSchema.id);
this.validator.addSchema(addressSchema as JSONSchema, addressSchema.id);
this.validator.addSchema(orderHashSchema as JSONSchema, orderHashSchema.id);
this.validator.addSchema(blockParamSchema as JSONSchema, blockParamSchema.id);
this.validator.addSchema(ecSignatureSchema as JSONSchema, ecSignatureSchema.id);
this.validator.addSchema(signedOrderSchema as JSONSchema, signedOrderSchema.id);
this.validator.addSchema(subscriptionOptsSchema as JSONSchema, subscriptionOptsSchema.id);
this.validator.addSchema(indexFilterValuesSchema as JSONSchema, indexFilterValuesSchema.id);
this.validator.addSchema(ecSignatureParameterSchema as JSONSchema, ecSignatureParameterSchema.id);
this.validator.addSchema(orderFillOrKillRequestsSchema as JSONSchema, 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 = JSON.parse(JSON.stringify(instance));
return this.validator.validate(jsonSchemaCompatibleObject, schema);
}
}

View File

@ -1,354 +0,0 @@
import 'mocha';
import * as _ from 'lodash';
import * as chai from 'chai';
import * as BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');
import {constants} from './utils/constants';
import {SchemaValidator} from '../src/utils/schema_validator';
import {tokenSchema} from '../src/schemas/token_schema';
import {orderHashSchema} from '../src/schemas/order_hash_schema';
import {orderSchema, signedOrderSchema} from '../src/schemas/order_schemas';
import {addressSchema, numberSchema} from '../src/schemas/basic_type_schemas';
import {orderFillOrKillRequestsSchema} from '../src/schemas/order_fill_or_kill_requests_schema';
import {ecSignatureParameterSchema, ecSignatureSchema} from '../src/schemas/ec_signature_schema';
import {orderCancellationRequestsSchema} from '../src/schemas/order_cancel_schema';
import {orderFillRequestsSchema} from '../src/schemas/order_fill_requests_schema';
import {blockParamSchema, subscriptionOptsSchema} from '../src/schemas/subscription_opts_schema';
chai.config.includeStack = true;
const expect = chai.expect;
describe('Schema', () => {
const validator = new SchemaValidator();
const validateAgainstSchema = (testCases: any[], schema: any, shouldFail = false) => {
_.forEach(testCases, (testCase: any) => {
if (shouldFail) {
expect(validator.validate(testCase, schema).errors).to.be.lengthOf.at.least(1);
} else {
expect(validator.validate(testCase, schema).errors).to.be.lengthOf(0);
}
});
};
describe('#numberSchema', () => {
it('should validate valid numbers', () => {
const testCases = ['42', '0', '1.3', '0.2', '00.00'];
validateAgainstSchema(testCases, numberSchema);
});
it('should fail for invalid numbers', () => {
const testCases = ['.3', '1.', 'abacaba', 'и', '1..0'];
const shouldFail = true;
validateAgainstSchema(testCases, numberSchema, shouldFail);
});
});
describe('#addressSchema', () => {
it('should validate valid addresses', () => {
const testCases = ['0x8b0292b11a196601ed2ce54b665cafeca0347d42', constants.NULL_ADDRESS];
validateAgainstSchema(testCases, addressSchema);
});
it('should fail for invalid addresses', () => {
const testCases = [
'0x',
'0',
'0x00',
'0xzzzzzzB11a196601eD2ce54B665CaFEca0347D42',
'0x8b0292B11a196601eD2ce54B665CaFEca0347D42',
];
const shouldFail = true;
validateAgainstSchema(testCases, addressSchema, shouldFail);
});
});
describe('#ecSignatureParameterSchema', () => {
it('should validate valid parameters', () => {
const testCases = [
'0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
'0X40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
];
validateAgainstSchema(testCases, ecSignatureParameterSchema);
});
it('should fail for invalid parameters', () => {
const testCases = [
'0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc3', // shorter
'0xzzzz9190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', // invalid characters
'40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', // no 0x
];
const shouldFail = true;
validateAgainstSchema(testCases, ecSignatureParameterSchema, shouldFail);
});
});
describe('#ecSignatureSchema', () => {
it('should validate valid signature', () => {
const signature = {
v: 27,
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
};
const testCases = [
signature,
{
...signature,
v: 28,
},
];
validateAgainstSchema(testCases, ecSignatureSchema);
});
it('should fail for invalid signature', () => {
const v = 27;
const r = '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33';
const s = '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254';
const testCases = [
{},
{v},
{r, s, v: 31},
];
const shouldFail = true;
validateAgainstSchema(testCases, ecSignatureSchema, shouldFail);
});
});
describe('#orderHashSchema', () => {
it('should validate valid order hash', () => {
const testCases = [
'0x61a3ed31B43c8780e905a260a35faefEc527be7516aa11c0256729b5b351bc33',
'0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
];
validateAgainstSchema(testCases, orderHashSchema);
});
it('should fail for invalid order hash', () => {
const testCases = [
{},
'0x',
'0x8b0292B11a196601eD2ce54B665CaFEca0347D42',
'61a3ed31B43c8780e905a260a35faefEc527be7516aa11c0256729b5b351bc33',
];
const shouldFail = true;
validateAgainstSchema(testCases, orderHashSchema, shouldFail);
});
});
describe('#blockParamSchema', () => {
it('should validate valid block param', () => {
const testCases = [
42,
'latest',
'pending',
'earliest',
];
validateAgainstSchema(testCases, blockParamSchema);
});
it('should fail for invalid block param', () => {
const testCases = [
{},
'42',
'pemding',
];
const shouldFail = true;
validateAgainstSchema(testCases, blockParamSchema, shouldFail);
});
});
describe('#subscriptionOptsSchema', () => {
it('should validate valid subscription opts', () => {
const testCases = [
{fromBlock: 42, toBlock: 'latest'},
{fromBlock: 42},
{},
];
validateAgainstSchema(testCases, subscriptionOptsSchema);
});
it('should fail for invalid subscription opts', () => {
const testCases = [
{fromBlock: '42'},
];
const shouldFail = true;
validateAgainstSchema(testCases, subscriptionOptsSchema, shouldFail);
});
});
describe('#tokenSchema', () => {
const token = {
name: 'Zero Ex',
symbol: 'ZRX',
decimals: 100500,
address: '0x8b0292b11a196601ed2ce54b665cafeca0347d42',
url: 'https://0xproject.com',
};
it('should validate valid token', () => {
const testCases = [
token,
];
validateAgainstSchema(testCases, tokenSchema);
});
it('should fail for invalid token', () => {
const testCases = [
{
...token,
address: null,
},
{
...token,
decimals: undefined,
},
[],
4,
];
const shouldFail = true;
validateAgainstSchema(testCases, tokenSchema, shouldFail);
});
});
describe('order including schemas', () => {
const order = {
maker: constants.NULL_ADDRESS,
taker: constants.NULL_ADDRESS,
makerFee: '1',
takerFee: '2',
makerTokenAmount: '1',
takerTokenAmount: '2',
makerTokenAddress: constants.NULL_ADDRESS,
takerTokenAddress: constants.NULL_ADDRESS,
salt: '256',
feeRecipient: constants.NULL_ADDRESS,
exchangeContractAddress: constants.NULL_ADDRESS,
expirationUnixTimestampSec: '42',
};
describe('#orderSchema', () => {
it('should validate valid order', () => {
const testCases = [
order,
];
validateAgainstSchema(testCases, orderSchema);
});
it('should fail for invalid order', () => {
const testCases = [
{
...order,
salt: undefined,
},
{
...order,
salt: 'salt',
},
'order',
];
const shouldFail = true;
validateAgainstSchema(testCases, orderSchema, shouldFail);
});
});
describe('signed order including schemas', () => {
const signedOrder = {
...order,
ecSignature: {
v: 27,
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
},
};
describe('#signedOrderSchema', () => {
it('should validate valid signed order', () => {
const testCases = [
signedOrder,
];
validateAgainstSchema(testCases, signedOrderSchema);
});
it('should fail for invalid signed order', () => {
const testCases = [
{
...signedOrder,
ecSignature: undefined,
},
];
const shouldFail = true;
validateAgainstSchema(testCases, signedOrderSchema, shouldFail);
});
});
describe('#orderFillOrKillRequestsSchema', () => {
const orderFillOrKillRequests = [
{
signedOrder,
fillTakerAmount: '5',
},
];
it('should validate valid order fill or kill requests', () => {
const testCases = [
orderFillOrKillRequests,
];
validateAgainstSchema(testCases, orderFillOrKillRequestsSchema);
});
it('should fail for invalid order fill or kill requests', () => {
const testCases = [
[
{
...orderFillOrKillRequests[0],
fillTakerAmount: undefined,
},
],
];
const shouldFail = true;
validateAgainstSchema(testCases, orderFillOrKillRequestsSchema, shouldFail);
});
});
describe('#orderCancellationRequestsSchema', () => {
const orderCancellationRequests = [
{
order,
takerTokenCancelAmount: '5',
},
];
it('should validate valid order cancellation requests', () => {
const testCases = [
orderCancellationRequests,
];
validateAgainstSchema(testCases, orderCancellationRequestsSchema);
});
it('should fail for invalid order cancellation requests', () => {
const testCases = [
[
{
...orderCancellationRequests[0],
takerTokenCancelAmount: undefined,
},
],
];
const shouldFail = true;
validateAgainstSchema(testCases, orderCancellationRequestsSchema, shouldFail);
});
});
describe('#orderFillRequestsSchema', () => {
const orderFillRequests = [
{
signedOrder,
takerTokenFillAmount: '5',
},
];
it('should validate valid order fill requests', () => {
const testCases = [
orderFillRequests,
];
validateAgainstSchema(testCases, orderFillRequestsSchema);
});
it('should fail for invalid order fill requests', () => {
const testCases = [
[
{
...orderFillRequests[0],
takerTokenFillAmount: undefined,
},
],
];
const shouldFail = true;
validateAgainstSchema(testCases, orderFillRequestsSchema, shouldFail);
});
});
});
});
describe('BigNumber serialization', () => {
it('should correctly serialize BigNumbers', () => {
const testCases = {
'42': '42',
'0': '0',
'1.3': '1.3',
'0.2': '0.2',
'00.00': '0',
'.3': '0.3',
};
_.forEach(testCases, (serialized: string, input: string) => {
expect(JSON.parse(JSON.stringify(new BigNumber(input)))).to.be.equal(serialized);
});
});
});
});

View File

@ -1,13 +1,11 @@
import * as _ from 'lodash';
import 'mocha';
import * as chai from 'chai';
import {SchemaValidator, schemas} from '0x-json-schemas';
import {chaiSetup} from './utils/chai_setup';
import {web3Factory} from './utils/web3_factory';
import {ZeroEx, Token} from '../src';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
import {SchemaValidator} from '../src/utils/schema_validator';
import {tokenSchema} from '../src/schemas/token_schema';
import {addressSchema} from '../src/schemas/basic_type_schemas';
chaiSetup.configure();
const expect = chai.expect;
@ -49,7 +47,7 @@ describe('TokenRegistryWrapper', () => {
const schemaValidator = new SchemaValidator();
_.each(tokens, token => {
const validationResult = schemaValidator.validate(token, tokenSchema);
const validationResult = schemaValidator.validate(token, schemas.tokenSchema);
expect(validationResult.errors).to.have.lengthOf(0);
});
});
@ -61,7 +59,7 @@ describe('TokenRegistryWrapper', () => {
const schemaValidator = new SchemaValidator();
_.each(tokenAddresses, tokenAddress => {
const validationResult = schemaValidator.validate(tokenAddress, addressSchema);
const validationResult = schemaValidator.validate(tokenAddress, schemas.addressSchema);
expect(validationResult.errors).to.have.lengthOf(0);
expect(tokenAddress).to.not.be.equal(ZeroEx.NULL_ADDRESS);
});
@ -113,7 +111,7 @@ describe('TokenRegistryWrapper', () => {
const token = await zeroEx.tokenRegistry.getTokenIfExistsAsync(aToken.address);
const schemaValidator = new SchemaValidator();
const validationResult = schemaValidator.validate(token, tokenSchema);
const validationResult = schemaValidator.validate(token, schemas.tokenSchema);
expect(validationResult.errors).to.have.lengthOf(0);
});
it('should return return undefined when passed a token address not in the tokenRegistry', async () => {

View File

@ -2,6 +2,12 @@
# yarn lockfile v1
"0x-json-schemas@^0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/0x-json-schemas/-/0x-json-schemas-0.1.5.tgz#e114128ceefe33a0bc3981ec35af6780dec971c1"
dependencies:
jsonschema "^1.2.0"
"@types/bignumber.js@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/bignumber.js/-/bignumber.js-4.0.2.tgz#22a16946c9faa9f2c9c0ad4c7c3734a3033320ae"
@ -2746,9 +2752,9 @@ jsonpointer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9"
jsonschema@*, jsonschema@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/jsonschema/-/jsonschema-1.1.1.tgz#3cede8e3e411d377872eefbc9fdf26383cbc3ed9"
jsonschema@*, jsonschema@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/jsonschema/-/jsonschema-1.2.0.tgz#d6ebaf70798db7b3a20c544f6c9ef9319b077de2"
jsprim@^1.2.2:
version "1.4.0"