feat: add feature to validate schemas directly

This commit is contained in:
Wieland Schöbl
2019-04-03 12:33:53 +02:00
parent 3e5fa7fc2f
commit 1022150ca3

View File

@@ -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);
}
}
}