mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
feat: more fixes
This commit is contained in:
@@ -24,6 +24,10 @@ type NameOf<I extends SchemaMap[keyof SchemaMap]> = keyof IncludeProperty<Schema
|
||||
export class Validator {
|
||||
private readonly ajv: Ajv.default;
|
||||
|
||||
get errors() {
|
||||
return this.ajv.errors;
|
||||
}
|
||||
|
||||
constructor(additionalSchemas: AnySchema[] = []) {
|
||||
this.ajv = new Ajv.default({
|
||||
schemas: [schema, ...additionalSchemas],
|
||||
@@ -36,12 +40,21 @@ export class Validator {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add additional schemas to the validator
|
||||
*/
|
||||
public addSchema(...schema: AnySchema[]) {
|
||||
this.ajv.addSchema(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates anything against a given schema name or infers schema name from object
|
||||
* @param instance Instance to validate
|
||||
* @param schema Name of schema to validate instance against or the schema itself
|
||||
*/
|
||||
public validate<T>(instance: unknown, schema: NameOf<T>): instance is T {
|
||||
return this.ajv.validate(schema as string, instance);
|
||||
public validate<T extends SchemaMap[keyof SchemaMap]>(instance: unknown, schema: NameOf<T>): instance is T;
|
||||
public validate(instance: unknown, schema: Ajv.Schema): boolean;
|
||||
public validate(instance: unknown, schema: Ajv.Schema | string): boolean {
|
||||
return this.ajv.validate(schema, instance);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user