Add json-schemas package to mono repo
This commit is contained in:
7
packages/json-schemas/src/globals.d.ts
vendored
Normal file
7
packages/json-schemas/src/globals.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
declare module 'dirty-chai';
|
||||
|
||||
// es6-promisify declarations
|
||||
declare function promisify(original: any, settings?: any): ((...arg: any[]) => Promise<any>);
|
||||
declare module 'es6-promisify' {
|
||||
export = promisify;
|
||||
}
|
4
packages/json-schemas/src/index.ts
Normal file
4
packages/json-schemas/src/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export {ValidatorResult, Schema} from 'jsonschema';
|
||||
|
||||
export {SchemaValidator} from './schema_validator';
|
||||
export {schemas} from './schemas';
|
28
packages/json-schemas/src/schema_validator.ts
Normal file
28
packages/json-schemas/src/schema_validator.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import values = require('lodash.values');
|
||||
import {Validator, ValidatorResult, Schema} from 'jsonschema';
|
||||
import {schemas} from './schemas';
|
||||
|
||||
export class SchemaValidator {
|
||||
private validator: Validator;
|
||||
constructor() {
|
||||
this.validator = new Validator();
|
||||
for (const schema of values(schemas)) {
|
||||
this.validator.addSchema(schema, schema.id);
|
||||
}
|
||||
}
|
||||
public addSchema(schema: Schema) {
|
||||
this.validator.addSchema(schema, schema.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);
|
||||
}
|
||||
public isValid(instance: any, schema: Schema): boolean {
|
||||
const isValid = this.validate(instance, schema).errors.length === 0;
|
||||
return isValid;
|
||||
}
|
||||
}
|
99
packages/json-schemas/src/schemas.ts
Normal file
99
packages/json-schemas/src/schemas.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
numberSchema,
|
||||
addressSchema,
|
||||
} from '../schemas/basic_type_schemas';
|
||||
import {
|
||||
ecSignatureSchema,
|
||||
ecSignatureParameterSchema,
|
||||
} from '../schemas/ec_signature_schema';
|
||||
import {
|
||||
indexFilterValuesSchema,
|
||||
} from '../schemas/index_filter_values_schema';
|
||||
import {
|
||||
orderCancellationRequestsSchema,
|
||||
} from '../schemas/order_cancel_schema';
|
||||
import {
|
||||
orderFillOrKillRequestsSchema,
|
||||
} from '../schemas/order_fill_or_kill_requests_schema';
|
||||
import {
|
||||
orderFillRequestsSchema,
|
||||
} from '../schemas/order_fill_requests_schema';
|
||||
import {
|
||||
orderHashSchema,
|
||||
} from '../schemas/order_hash_schema';
|
||||
import {
|
||||
orderSchema,
|
||||
signedOrderSchema,
|
||||
} from '../schemas/order_schemas';
|
||||
import {
|
||||
blockParamSchema,
|
||||
subscriptionOptsSchema,
|
||||
} from '../schemas/subscription_opts_schema';
|
||||
import {
|
||||
tokenSchema,
|
||||
} from '../schemas/token_schema';
|
||||
import {
|
||||
signedOrdersSchema,
|
||||
} from '../schemas/signed_orders_schema';
|
||||
import {
|
||||
relayerApiErrorResponseSchema,
|
||||
} from '../schemas/relayer_api_error_response_schema';
|
||||
import {
|
||||
relayerApiFeesResponseSchema,
|
||||
} from '../schemas/relayer_api_fees_response_schema';
|
||||
import {
|
||||
relayerApiFeesPayloadSchema,
|
||||
} from '../schemas/relayer_api_fees_payload_schema';
|
||||
import {
|
||||
relayerApiOrderBookResponseSchema,
|
||||
} from '../schemas/relayer_api_orderbook_response_schema';
|
||||
import {
|
||||
relayerApiTokenPairsResponseSchema,
|
||||
relayerApiTokenTradeInfoSchema,
|
||||
} from '../schemas/relayer_api_token_pairs_response_schema';
|
||||
import {
|
||||
jsNumber,
|
||||
txDataSchema,
|
||||
} from '../schemas/tx_data_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelSubscribeSchema,
|
||||
relayerApiOrderbookChannelSubscribePayload,
|
||||
} from '../schemas/relayer_api_orberbook_channel_subscribe_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelUpdateSchema,
|
||||
} from '../schemas/relayer_api_orderbook_channel_update_response_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelSnapshotSchema,
|
||||
relayerApiOrderbookChannelSnapshotPayload,
|
||||
} from '../schemas/relayer_api_orderbook_channel_snapshot_schema';
|
||||
|
||||
export const schemas = {
|
||||
numberSchema,
|
||||
addressSchema,
|
||||
ecSignatureSchema,
|
||||
ecSignatureParameterSchema,
|
||||
indexFilterValuesSchema,
|
||||
orderCancellationRequestsSchema,
|
||||
orderFillOrKillRequestsSchema,
|
||||
orderFillRequestsSchema,
|
||||
orderHashSchema,
|
||||
orderSchema,
|
||||
signedOrderSchema,
|
||||
signedOrdersSchema,
|
||||
blockParamSchema,
|
||||
subscriptionOptsSchema,
|
||||
tokenSchema,
|
||||
jsNumber,
|
||||
txDataSchema,
|
||||
relayerApiErrorResponseSchema,
|
||||
relayerApiFeesPayloadSchema,
|
||||
relayerApiFeesResponseSchema,
|
||||
relayerApiOrderBookResponseSchema,
|
||||
relayerApiTokenPairsResponseSchema,
|
||||
relayerApiTokenTradeInfoSchema,
|
||||
relayerApiOrderbookChannelSubscribeSchema,
|
||||
relayerApiOrderbookChannelSubscribePayload,
|
||||
relayerApiOrderbookChannelUpdateSchema,
|
||||
relayerApiOrderbookChannelSnapshotSchema,
|
||||
relayerApiOrderbookChannelSnapshotPayload,
|
||||
};
|
Reference in New Issue
Block a user