feat: more fixes

This commit is contained in:
2023-11-01 16:10:12 +01:00
parent 4bdd4b20d0
commit 62d5ea4275
23 changed files with 80 additions and 289 deletions

View File

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