mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 19:22:51 +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
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user