From e2df4b355ba97e4212f5e42251defe8d9e432763 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 8 Jan 2019 17:07:10 +0100 Subject: Add assertions to catch trying to add undefined schemas --- packages/json-schemas/src/schema_validator.ts | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'packages/json-schemas') diff --git a/packages/json-schemas/src/schema_validator.ts b/packages/json-schemas/src/schema_validator.ts index 3f303137b..6ccd981be 100644 --- a/packages/json-schemas/src/schema_validator.ts +++ b/packages/json-schemas/src/schema_validator.ts @@ -8,12 +8,18 @@ import { schemas } from './schemas'; */ export class SchemaValidator { private readonly _validator: Validator; + private static _assertSchemaNotUndefined(schema: Schema): void { + if (schema === undefined) { + throw new Error(`Cannot add undefined schema`); + } + } /** * Instantiates a SchemaValidator instance */ constructor() { this._validator = new Validator(); for (const schema of values(schemas)) { + SchemaValidator._assertSchemaNotUndefined(schema); this._validator.addSchema(schema, schema.id); } } @@ -24,6 +30,7 @@ export class SchemaValidator { * @param schema The schema to add */ public addSchema(schema: Schema): void { + SchemaValidator._assertSchemaNotUndefined(schema); this._validator.addSchema(schema, schema.id); } // In order to validate a complex JS object using jsonschema, we must replace any complex @@ -37,6 +44,8 @@ export class SchemaValidator { * @returns The results of the validation */ public validate(instance: any, schema: Schema): ValidatorResult { + console.log('validating....'); + SchemaValidator._assertSchemaNotUndefined(schema); const jsonSchemaCompatibleObject = JSON.parse(JSON.stringify(instance)); return this._validator.validate(jsonSchemaCompatibleObject, schema); } -- cgit v1.2.3 From 0cfeea7c8c9b4c07cc53b3a47bfd01e6b1c81281 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 8 Jan 2019 17:15:05 +0100 Subject: Remove stray console --- packages/json-schemas/src/schema_validator.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'packages/json-schemas') diff --git a/packages/json-schemas/src/schema_validator.ts b/packages/json-schemas/src/schema_validator.ts index 6ccd981be..8fadae97a 100644 --- a/packages/json-schemas/src/schema_validator.ts +++ b/packages/json-schemas/src/schema_validator.ts @@ -44,7 +44,6 @@ export class SchemaValidator { * @returns The results of the validation */ public validate(instance: any, schema: Schema): ValidatorResult { - console.log('validating....'); SchemaValidator._assertSchemaNotUndefined(schema); const jsonSchemaCompatibleObject = JSON.parse(JSON.stringify(instance)); return this._validator.validate(jsonSchemaCompatibleObject, schema); -- cgit v1.2.3 From 28aa12691e00344fe80bd022188a1e7657397aa5 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 9 Jan 2019 11:06:15 +0100 Subject: rename method --- packages/json-schemas/src/schema_validator.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/json-schemas') diff --git a/packages/json-schemas/src/schema_validator.ts b/packages/json-schemas/src/schema_validator.ts index 8fadae97a..43647b594 100644 --- a/packages/json-schemas/src/schema_validator.ts +++ b/packages/json-schemas/src/schema_validator.ts @@ -8,7 +8,7 @@ import { schemas } from './schemas'; */ export class SchemaValidator { private readonly _validator: Validator; - private static _assertSchemaNotUndefined(schema: Schema): void { + private static _assertSchemaDefined(schema: Schema): void { if (schema === undefined) { throw new Error(`Cannot add undefined schema`); } @@ -19,7 +19,7 @@ export class SchemaValidator { constructor() { this._validator = new Validator(); for (const schema of values(schemas)) { - SchemaValidator._assertSchemaNotUndefined(schema); + SchemaValidator._assertSchemaDefined(schema); this._validator.addSchema(schema, schema.id); } } @@ -30,7 +30,7 @@ export class SchemaValidator { * @param schema The schema to add */ public addSchema(schema: Schema): void { - SchemaValidator._assertSchemaNotUndefined(schema); + SchemaValidator._assertSchemaDefined(schema); this._validator.addSchema(schema, schema.id); } // In order to validate a complex JS object using jsonschema, we must replace any complex @@ -44,7 +44,7 @@ export class SchemaValidator { * @returns The results of the validation */ public validate(instance: any, schema: Schema): ValidatorResult { - SchemaValidator._assertSchemaNotUndefined(schema); + SchemaValidator._assertSchemaDefined(schema); const jsonSchemaCompatibleObject = JSON.parse(JSON.stringify(instance)); return this._validator.validate(jsonSchemaCompatibleObject, schema); } -- cgit v1.2.3