UNPKG

@fajarnugraha37/validator

Version:

A typed façade over [AJV](https://ajv.js.org/) that brings schema builders, caching, and loader pipelines to the `nope` workspace

90 lines (86 loc) 4.68 kB
import { ValidationResult } from '@fajarnugraha37/error'; import { LruTtlOpts } from '@fajarnugraha37/cache'; import { SchemaBuilder } from './schema-builder.cjs'; declare class ValidatorBuilder<TSchemas extends SchemaRegistry = SchemaRegistry> { private options; private schemas; private loader?; private cacheOptions?; static create<TSchemas extends SchemaRegistry = SchemaRegistry>(): ValidatorBuilder<TSchemas>; withOptions(options: ValidatorOptions): this; withSchema<K extends keyof TSchemas>(type: K, schema: TSchemas[K]): this; withSchemaBuilder<K extends keyof TSchemas>(type: K, builder: SchemaBuilder): this; withSchemas(schemas: Partial<TSchemas>): this; withSchemaBuilders(builders: Partial<Record<keyof TSchemas, SchemaBuilder>>): this; fromJSON(input: SchemaJsonInput<TSchemas>): this; withSchemaLoader(loader: SchemaLoader<TSchemas>): this; withCache(options?: SchemaCacheOptions<TSchemas>): this; build(): Validator<TSchemas>; } interface SchemaRegistry { "expression-schema": any; "form-schema"?: any; "step-schema"?: any; "component-schema"?: any; "hook-schema"?: any; "ui-schema"?: any; [key: string]: any; } interface ValidatorOptions { strict?: boolean; allErrors?: boolean; removeAdditional?: boolean | "all" | "failing"; useDefaults?: boolean; coerceTypes?: boolean | "array"; verbose?: boolean; } type SchemaLoader<TSchemas extends SchemaRegistry = SchemaRegistry> = <K extends keyof TSchemas>(type: K) => Promise<TSchemas[K] | undefined> | TSchemas[K] | undefined; type SchemaCacheOptions<TSchemas extends SchemaRegistry = SchemaRegistry> = LruTtlOpts<TSchemas[keyof TSchemas]> & { ttlMs?: number; slidingTtlMs?: number; }; type SchemaJsonInput<TSchemas extends SchemaRegistry = SchemaRegistry> = string | Partial<TSchemas>; declare class Validator<TSchemas extends SchemaRegistry = SchemaRegistry> { private ajv; private validators; private schemas; private schemaLoader?; private schemaCache?; private schemaCacheTtl?; constructor(options?: ValidatorOptions); static builder<TSchemas extends SchemaRegistry = SchemaRegistry>(): ValidatorBuilder<TSchemas>; registerSchema<K extends keyof TSchemas>(type: K, schema: TSchemas[K]): this; registerSchemas(schemas: Partial<TSchemas>): this; registerSchemaBuilder<K extends keyof TSchemas>(type: K, builder: SchemaBuilder): this; registerSchemaBuilders(builders: Partial<Record<keyof TSchemas, SchemaBuilder>>): this; enableSchemaCache(options?: SchemaCacheOptions<TSchemas>): this; setSchemaLoader(loader: SchemaLoader<TSchemas>): this; importSchemasFromJSON(input: SchemaJsonInput<TSchemas>, options?: { register?: boolean; }): Partial<TSchemas>; exportSchemas(types?: (keyof TSchemas)[]): Partial<TSchemas>; exportSchemasToJSON(types?: (keyof TSchemas)[], space?: number): string; validate<K extends keyof TSchemas>(type: K, data: unknown): ValidationResult<TSchemas[K]>; validateAsync<K extends keyof TSchemas>(type: K, data: unknown): Promise<ValidationResult<TSchemas[K]>>; validateStrict<K extends keyof TSchemas>(type: K, data: unknown): TSchemas[K]; validateStrictAsync<K extends keyof TSchemas>(type: K, data: unknown): Promise<TSchemas[K]>; hasSchema<K extends keyof TSchemas>(type: K): boolean; getSchema<K extends keyof TSchemas>(type: K): TSchemas[K] | undefined; getRegisteredTypes(): (keyof TSchemas)[]; validateMany<K extends keyof TSchemas>(type: K, items: unknown[]): ValidationResult<TSchemas[K][]>; validateManyAsync<K extends keyof TSchemas>(type: K, items: unknown[]): Promise<ValidationResult<TSchemas[K][]>>; createTypeValidator<K extends keyof TSchemas>(type: K): { validate: (data: unknown) => ValidationResult<TSchemas[K]>; validateAsync: (data: unknown) => Promise<ValidationResult<TSchemas[K]>>; validateStrict: (data: unknown) => TSchemas[K]; validateStrictAsync: (data: unknown) => Promise<TSchemas[K]>; validateMany: (items: unknown[]) => ValidationResult<TSchemas[K][]>; validateManyAsync: (items: unknown[]) => Promise<ValidationResult<TSchemas[K][]>>; }; addCustomValidation<K extends keyof TSchemas>(type: K, name: string, validationFn: (data: any) => boolean | string): this; private ensureSchemaLoaded; private cacheSchema; private normalizeSchemaInput; private formatErrors; } export { type SchemaRegistry as S, Validator as V, type ValidatorOptions as a, type SchemaCacheOptions as b, type SchemaLoader as c, type SchemaJsonInput as d, ValidatorBuilder as e };