mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 01:22:54 +00:00
feat: add feature to validate schemas directly
This commit is contained in:
@@ -82,14 +82,20 @@ export class Validator {
|
|||||||
* Validates anything against a given schema name
|
* Validates anything against a given schema name
|
||||||
*
|
*
|
||||||
* @param instance Instance to validate
|
* @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 {
|
public validate(instance: any, schema: string | Schema): ValidatorResult {
|
||||||
if (typeof this.schemas[schemaName] !== 'object') {
|
if (typeof schema === 'string') {
|
||||||
throw new Error(`No schema available for ${schemaName}.`);
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user