model-validator-ts
Version:
[](https://www.npmjs.com/package/model-validator-ts)
104 lines • 5.58 kB
TypeScript
import type { StandardSchemaV1 } from "./standard-schema.ts";
type TValidationDeps = Record<string, unknown>;
export type ValidationResult<TSchema extends StandardSchemaV1> = {
success: true;
value: StandardSchemaV1.InferOutput<TSchema>;
} | {
success: false;
errors: ErrorBag<TSchema>;
};
type RuleFunction<TSchema extends StandardSchemaV1, TDeps extends TValidationDeps> = (args: {
property: ErrorKeys<TSchema>;
data: StandardSchemaV1.InferOutput<TSchema>;
builder: ValidatorModel<TSchema, TDeps>;
deps: TDeps;
}) => unknown | Promise<unknown>;
type RuleDefinition<TSchema extends StandardSchemaV1, TDeps extends TValidationDeps> = {
attribute: ErrorKeys<TSchema>;
fn: RuleFunction<TSchema, TDeps>;
};
type ErrorKeys<TSchema extends StandardSchemaV1> = StandardSchemaV1.InferInput<TSchema> extends Record<string | number | symbol, unknown> ? keyof StandardSchemaV1.InferInput<TSchema> | "global" : "global";
export declare class ErrorBag<TSchema extends StandardSchemaV1> {
#private;
addError(key: ErrorKeys<TSchema>, message: string): void;
firstError(key: ErrorKeys<TSchema>): string | undefined;
hasErrors(): boolean;
toFlattenObject(): Record<ErrorKeys<TSchema>, string>;
get toObject(): Partial<Record<ErrorKeys<TSchema>, string[]>>;
toString(): string;
toHtml(): string;
toText(): string;
}
export declare class ValidatorDefinition<TSchema extends StandardSchemaV1, TBuildDeps extends TValidationDeps, TPreBuildDeps extends TValidationDeps = {}> {
schema: TSchema;
rules: RuleDefinition<TSchema, TBuildDeps & TPreBuildDeps>[];
['~deps']: TBuildDeps;
['~preBuildDeps']: TPreBuildDeps;
constructor(args: {
schema: TSchema;
rules: RuleDefinition<TSchema, TBuildDeps & TPreBuildDeps>[];
deps?: TBuildDeps;
preBuildDeps?: TPreBuildDeps;
});
addRule(ruledef: RuleDefinition<TSchema, TBuildDeps>): this;
addRules(ruledefs: RuleDefinition<TSchema, TBuildDeps & TPreBuildDeps>[]): this;
build(...dependencies: TBuildDeps extends Record<string, never> ? [] : [TBuildDeps]): ValidatorModel<TSchema, TPreBuildDeps & TBuildDeps>;
}
type ValidationOpts<TSchema extends StandardSchemaV1> = {
validationType?: "plain";
override?: Partial<TSchema>;
};
export declare class ValidatorModel<TSchema extends StandardSchemaV1, TDeps extends TValidationDeps> {
#private;
schema: TSchema;
errors: ErrorBag<TSchema>;
constructor(schema: TSchema, rules: RuleDefinition<TSchema, TDeps>[], deps: TDeps);
addError(key: ErrorKeys<TSchema>, message: string): ErrorBag<TSchema>;
mergeErrors(errors: Partial<Record<ErrorKeys<TSchema>, string>>): ErrorBag<TSchema>;
mapErrors<TSrc extends Record<string, string>>(args: [keyof TSrc] extends [ErrorKeys<TSchema>] ? {
src: TSrc;
} : {
src: TSrc;
mappings: {
[K in keyof TSrc as K extends ErrorKeys<TSchema> ? never : K]: ErrorKeys<TSchema>;
};
}): ErrorBag<TSchema>;
validateShape(input: StandardSchemaV1.InferInput<TSchema>, opts?: ValidationOpts<TSchema>): Promise<ValidationResult<TSchema>>;
validate(input: StandardSchemaV1.InferInput<TSchema> | unknown, opts?: ValidationOpts<TSchema>): Promise<ValidationResult<TSchema>>;
toClientFields(): void;
}
export type CommandResult<TValidator extends ValidatorModel<any, any>, TOutput> = {
validated: true;
result: TOutput;
} | {
validated: false;
errors: TValidator["errors"];
};
type InferValidatorDefinition<TValidatorDef extends ValidatorDefinition<any, any>> = TValidatorDef extends ValidatorDefinition<infer U, any> ? U : never;
export declare function createCommand<TOutput, TValidatorDef extends ValidatorDefinition<any, any, any>>(args: {
validator: TValidatorDef;
deps: TValidatorDef["~deps"];
execute: ({ data, deps, builder, }: {
data: StandardSchemaV1.InferOutput<TValidatorDef["schema"]>;
deps: TValidatorDef["~deps"] & TValidatorDef["~preBuildDeps"];
builder: ValidatorModel<TValidatorDef["schema"], TValidatorDef["~deps"] & TValidatorDef["~preBuildDeps"]>;
}) => Promise<TOutput | ErrorBag<InferValidatorDefinition<TValidatorDef>>>;
}): {
runShape(input: TValidatorDef["schema"], opts?: ValidationOpts<TValidatorDef["schema"]>): Promise<CommandResult<ValidatorModel<TValidatorDef["schema"], TValidatorDef["~deps"]>, Exclude<TOutput, ErrorBag<TValidatorDef["schema"]>>>>;
run(input: unknown, opts?: ValidationOpts<TValidatorDef["schema"]>): Promise<CommandResult<ValidatorModel<TValidatorDef["schema"], TValidatorDef["~deps"]>, Exclude<TOutput, ErrorBag<TValidatorDef["schema"]>>>>;
execute: ({ data, deps, builder, }: {
data: StandardSchemaV1.InferOutput<TValidatorDef["schema"]>;
deps: TValidatorDef["~deps"] & TValidatorDef["~preBuildDeps"];
builder: ValidatorModel<TValidatorDef["schema"], TValidatorDef["~deps"] & TValidatorDef["~preBuildDeps"]>;
}) => Promise<TOutput | ErrorBag<InferValidatorDefinition<TValidatorDef>>>;
validator: ValidatorModel<TValidatorDef["schema"], TValidatorDef["~deps"]>;
};
export declare function createValidatorBuilder<TPreBuildDeps extends TValidationDeps>(config: {
deps?: TPreBuildDeps | (() => TPreBuildDeps);
}): <TSchema extends StandardSchemaV1, TBuildDeps extends TValidationDeps>(args: {
schema: TSchema;
deps?: TBuildDeps;
rules?: RuleDefinition<TSchema, TPreBuildDeps & TBuildDeps>[];
}) => ValidatorDefinition<TSchema, TBuildDeps, TPreBuildDeps>;
export {};
//# sourceMappingURL=index.d.ts.map