aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/schemas/ec_signature_schema.ts2
-rw-r--r--src/schemas/order_cancel_schema.ts2
-rw-r--r--src/schemas/order_fill_or_kill_requests_schema.ts2
-rw-r--r--src/schemas/order_fill_requests_schema.ts2
-rw-r--r--src/schemas/token_schema.ts14
-rw-r--r--src/utils/schema_validator.ts4
-rw-r--r--test/schema_test.ts268
7 files changed, 276 insertions, 18 deletions
diff --git a/src/schemas/ec_signature_schema.ts b/src/schemas/ec_signature_schema.ts
index e39a8bd70..df1121989 100644
--- a/src/schemas/ec_signature_schema.ts
+++ b/src/schemas/ec_signature_schema.ts
@@ -1,4 +1,4 @@
-export const ecSignatureParameter = {
+export const ecSignatureParameterSchema = {
id: '/ecSignatureParameter',
type: 'string',
pattern: '^0[xX][0-9A-Fa-f]{64}$',
diff --git a/src/schemas/order_cancel_schema.ts b/src/schemas/order_cancel_schema.ts
index 5cc8f745c..04b2c179f 100644
--- a/src/schemas/order_cancel_schema.ts
+++ b/src/schemas/order_cancel_schema.ts
@@ -4,7 +4,7 @@ export const orderCancellationRequestsSchema = {
items: {
properties: {
order: {$ref: '/orderSchema'},
- takerTokenCancelAmount: {type: '/numberSchema'},
+ takerTokenCancelAmount: {$ref: '/numberSchema'},
},
required: ['order', 'takerTokenCancelAmount'],
type: 'object',
diff --git a/src/schemas/order_fill_or_kill_requests_schema.ts b/src/schemas/order_fill_or_kill_requests_schema.ts
index 4db7113de..6f7878c45 100644
--- a/src/schemas/order_fill_or_kill_requests_schema.ts
+++ b/src/schemas/order_fill_or_kill_requests_schema.ts
@@ -4,7 +4,7 @@ export const orderFillOrKillRequestsSchema = {
items: {
properties: {
signedOrder: {$ref: '/signedOrderSchema'},
- fillTakerAmount: {type: '/numberSchema'},
+ fillTakerAmount: {$ref: '/numberSchema'},
},
required: ['signedOrder', 'fillTakerAmount'],
type: 'object',
diff --git a/src/schemas/order_fill_requests_schema.ts b/src/schemas/order_fill_requests_schema.ts
index 44f4e33e2..0f3ee1e99 100644
--- a/src/schemas/order_fill_requests_schema.ts
+++ b/src/schemas/order_fill_requests_schema.ts
@@ -4,7 +4,7 @@ export const orderFillRequestsSchema = {
items: {
properties: {
signedOrder: {$ref: '/signedOrderSchema'},
- takerTokenFillAmount: {type: '/numberSchema'},
+ takerTokenFillAmount: {$ref: '/numberSchema'},
},
required: ['signedOrder', 'takerTokenFillAmount'],
type: 'object',
diff --git a/src/schemas/token_schema.ts b/src/schemas/token_schema.ts
index 01702af68..1236e07c1 100644
--- a/src/schemas/token_schema.ts
+++ b/src/schemas/token_schema.ts
@@ -4,8 +4,18 @@ export const tokenSchema = {
name: {type: 'string'},
symbol: {type: 'string'},
decimals: {type: 'number'},
- address: {type: 'string'},
- url: {type: 'string'},
+ address: {$ref: '/addressSchema'},
+ url: {
+ oneOf: [
+ {
+ type: 'string',
+ format: 'uri',
+ },
+ {
+ enum: [''],
+ },
+ ],
+ },
},
required: ['name', 'symbol', 'decimals', 'address', 'url'],
type: 'object',
diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts
index 6e0e6ab9c..9097dce88 100644
--- a/src/utils/schema_validator.ts
+++ b/src/utils/schema_validator.ts
@@ -1,5 +1,5 @@
import {Validator, ValidatorResult} from 'jsonschema';
-import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema';
+import {ecSignatureSchema, ecSignatureParameterSchema} from '../schemas/ec_signature_schema';
import {orderSchema, signedOrderSchema} from '../schemas/order_schemas';
import {addressSchema, numberSchema} from '../schemas/basic_type_schemas';
import {tokenSchema} from '../schemas/token_schema';
@@ -15,7 +15,7 @@ export class SchemaValidator {
this.validator.addSchema(addressSchema, addressSchema.id);
this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id);
this.validator.addSchema(signedOrderSchema, signedOrderSchema.id);
- this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id);
+ this.validator.addSchema(ecSignatureParameterSchema, ecSignatureParameterSchema.id);
this.validator.addSchema(orderFillOrKillRequestsSchema, orderFillOrKillRequestsSchema.id);
}
// In order to validate a complex JS object using jsonschema, we must replace any complex
diff --git a/test/schema_test.ts b/test/schema_test.ts
index 8d786fb73..b251a68f9 100644
--- a/test/schema_test.ts
+++ b/test/schema_test.ts
@@ -3,26 +3,274 @@ 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/basic_type_schemas';
+import {constants} from './utils/constants';
import {SchemaValidator} from '../src/utils/schema_validator';
+import {tokenSchema} from '../src/schemas/token_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';
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', () => {
- describe('number regex', () => {
- it('should validate valid numbers', () => {
- const testCases = ['42', '0', '1.3', '0.2', '00.00'];
- _.forEach(testCases, (testCase: string) => {
- expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(0);
+ 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'];
+ 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('#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,
+ {
+ ...token,
+ url: 'not an url',
+ },
+ ];
+ 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,
+ 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);
});
});
- it('should fail for invalid numbers', () => {
- const testCases = ['.3', '1.', 'abacaba', 'и', '1..0'];
- _.forEach(testCases, (testCase: string) => {
- expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(1);
+ 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);
});
});
});