From 1022150ca3d2af846b819e0e0e46ead71134d5f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wieland=20Sch=C3=B6bl?= Date: Wed, 3 Apr 2019 12:33:53 +0200 Subject: [PATCH] feat: add feature to validate schemas directly --- src/validate.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/validate.ts b/src/validate.ts index 7385a3e8..282c7e1a 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -82,14 +82,20 @@ export class Validator { * Validates anything against a given schema name * * @param instance Instance to validate - * @param schemaName Name of schema to validate instance against + * @param schema Name of schema to validate instance against or the schema itself */ - public validate(instance: any, schemaName: string): ValidatorResult { - if (typeof this.schemas[schemaName] !== 'object') { - throw new Error(`No schema available for ${schemaName}.`); - } + public validate(instance: any, schema: string | Schema): ValidatorResult { + if (typeof schema === 'string') { + // if you want to access a schema that is contained in the validator object + if (typeof this.schemas[schema] !== 'object') { + throw new Error(`No schema available for ${schema}.`); + } - return this.validator.validate(instance, this.schemas[schemaName]); + return this.validator.validate(instance, this.schemas[schema]); + } else { + // if you have a schema and want to validate it directly + return this.validator.validate(instance, schema); + } } }