How to use the @0x/json-schemas.SchemaValidator function in @0x/json-schemas

To help you get started, we’ve selected a few @0x/json-schemas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 0xProject / 0x-coordinator-server / ts / src / utils.ts View on Github external
import { Schema, SchemaValidator } from '@0x/json-schemas';
import { Order, SignedOrder, SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
import * as ethUtil from 'ethereumjs-util';
import { ValidationError as SchemaValidationError } from 'jsonschema';
import * as _ from 'lodash';

import { ValidationError, ValidationErrorCodes, ValidationErrorItem } from './errors';
import { Configs } from './types';

const schemaValidator = new SchemaValidator();

export const utils = {
    log: (...args: any[]) => {
        // tslint:disable-next-line:no-console
        console.log(...args);
    },
    validateSchema(instance: any, schema: Schema): void {
        const validationResult = schemaValidator.validate(instance, schema);
        if (_.isEmpty(validationResult.errors)) {
            return;
        } else {
            const validationErrorItems = _.map(
                validationResult.errors,
                (schemaValidationError: SchemaValidationError) =>
                    schemaValidationErrorToValidationErrorItem(schemaValidationError),
            );
github 0xProject / 0x-monorepo / packages / website / ts / schemas / validator.ts View on Github external
import { SchemaValidator } from '@0x/json-schemas';
import { orderMetadataSchema } from 'ts/schemas/metadata_schema';
import { portalOrderSchema } from 'ts/schemas/portal_order_schema';
import { portalTokenMetadataSchema } from 'ts/schemas/portal_token_metadata';

const validator = new SchemaValidator();
validator.addSchema(portalTokenMetadataSchema);
validator.addSchema(orderMetadataSchema);
validator.addSchema(portalOrderSchema);

export { validator };
github 0xProject / 0x-monorepo / packages / assert / src / index.ts View on Github external
doesConformToSchema(variableName: string, value: any, schema: Schema, subSchemas?: Schema[]): void {
        if (value === undefined) {
            throw new Error(`${variableName} can't be undefined`);
        }
        const schemaValidator = new SchemaValidator();
        if (subSchemas !== undefined) {
            _.map(subSchemas, schemaValidator.addSchema.bind(schemaValidator));
        }
        const validationResult = schemaValidator.validate(value, schema);
        const hasValidationErrors = validationResult.errors.length > 0;
        const msg = `Expected ${variableName} to conform to schema ${schema.id}
Encountered: ${JSON.stringify(value, null, '\t')}
Validation errors: ${validationResult.errors.join(', ')}`;
        assert.assert(!hasValidationErrors, msg);
    },
    isWebUri(variableName: string, value: any): void {