import { validatorMap, preparationMap, transformationMap, messageMap, plugins, } from "./validator-map.js"; import { SwitchConfig, SwitchSchema } from "./schemas/switch.js"; import { InferSchemaType, SObjectProperties, InferSObjectType, } from "./types.js"; import { ValidatorConfig, Validator } from "./types.js"; import { Schema } from "./schemas/schema.js"; import { ArraySchema } from "./schemas/array.js"; import { ObjectSchema } from "./schemas/object.js"; import { SetSchema } from "./schemas/set.js"; import { UnionSchema, UnionValidatorConfig } from "./schemas/union.js"; import { lazy } from "./utils.js"; export { SwitchSchema, Schema }; export * from "./types.js"; type Builder = { [P in Exclude< (typeof plugins)[number], { dataType: | "switch" | "object" | "literal" | "array" | "record" | "map" | "set" | "union" | "instanceof"; } > as P["dataType"]]: ( config?: ValidatorConfig< P extends Validator ? TOutput : never > ) => Schema< P extends Validator ? TOutput : never, P extends Validator ? TInput : never >; } & { array< T extends Schema, TOutput = InferSchemaType[], TInput = InferSchemaType[] >( itemSchema: T, config?: ValidatorConfig ): ArraySchema; object

>( config: ValidatorConfig & { validate?: { properties?: P } } ): ObjectSchema; switch( config: SwitchConfig ): Schema; literal( value: T, config?: ValidatorConfig ): Schema; record< K extends Schema, V extends Schema, TOutput = Record, InferSchemaType>, TInput = Record, InferSchemaType> >( keySchema: K, valueSchema: V, config?: ValidatorConfig ): Schema; map< K extends Schema, V extends Schema, TOutput = Map, InferSchemaType>, TInput = Map, InferSchemaType> >( keySchema: K, valueSchema: V, config?: ValidatorConfig ): Schema; set< T extends Schema, TOutput = Set>, TInput = Set> >( config?: ValidatorConfig & { validate?: { ofType?: T } } ): SetSchema; union< T extends readonly [Schema, ...Schema[]], TOutput = InferSchemaType, TInput = InferSchemaType >( config: UnionValidatorConfig ): UnionSchema; instanceof( constructor: new (...args: any[]) => T, config?: ValidatorConfig ): Schema; lazy>(resolver: () => T): T; }; function createSchemaBuilder(): Builder { const builder: any = {}; for (const plugin of plugins) { if (plugin.dataType === "switch") continue; if (plugin.dataType === "array") { builder.array = ( itemSchema: Schema, config: Record = {} ) => { return new ArraySchema(itemSchema, config); }; continue; } if (plugin.dataType === "object") { builder.object = (config: Record = {}) => new ObjectSchema(config); continue; } if (plugin.dataType === "literal") { builder.literal = (value: any, config: Record = {}) => { return new Schema("literal", { ...config, validate: { ...(config as any).validate, equals: value }, }); }; continue; } if (plugin.dataType === "record") { builder.record = ( keySchema: Schema, valueSchema: Schema, config: Record = {} ) => { return new Schema("record", { ...config, validate: { ...(config as any).validate, keysAndValues: [keySchema, valueSchema], }, }); }; continue; } if (plugin.dataType === "map") { builder.map = ( keySchema: Schema, valueSchema: Schema, config: Record = {} ) => { return new Schema("map", { ...config, validate: { ...(config as any).validate, entries: [keySchema, valueSchema], }, }); }; continue; } if (plugin.dataType === "set") { builder.set = >( config: ValidatorConfig & { validate?: { ofType?: T } } = {} ) => { const itemSchema = (config as any)?.validate?.ofType ?? new Schema("any"); return new SetSchema(itemSchema, config); }; continue; } if (plugin.dataType === "instanceof") { builder.instanceof = ( constructor: any, config: Record = {} ) => { return new Schema("instanceof", { ...config, validate: { ...(config as any).validate, constructor: constructor }, }); }; continue; } if (plugin.dataType === "union") { builder.union = (config: Record = {}) => { return new UnionSchema(config as any); }; continue; } builder[plugin.dataType] = (config: Record = {}) => { return new Schema(plugin.dataType, config); }; } builder.switch = (config: SwitchConfig) => { return new SwitchSchema(config); }; builder.lazy = lazy; return builder as Builder; } export const s = createSchemaBuilder(); export namespace s { export type infer> = T extends Schema ? U : never; }