@codeparticle/formal
Version:
A <2kb library for validating data of any kind
50 lines (47 loc) • 1.76 kB
TypeScript
// Type definitions for @codeparticle/formal 2.0.0
// Project: https://github.com/codeparticle/formal
// Definitions by: Nick Krause <synapseradio@gmail.com> <https://github.com/codeparticle>
// Definitions: https://github.com/codeparticle/formal
// TypeScript Version: ^4.6.3
interface Validator {
rules: ValidationRuleset;
result: ValidationM | null;
of(rules: ValidationRuleset): Validator;
validate(value: any): Validator;
then(opts: ValidationActions): any;
}
interface ValidationRule {
message: string | ((v?: any) => string);
opts: CustomValidatorOptions;
check(v: any): ValidationM;
}
interface ValidationActions {
onSuccess: (val: any) => any;
onFail: (errs: string[]) => any;
}
interface CustomValidatorOptions {
condition: (v: any) => boolean;
message: string | ((v: any) => string);
transform?: (v: any) => any;
}
declare type ValidationM = Success | Fail;
interface Fail {
value: any;
errors: string[];
isSuccess: boolean;
map(fn: (value: any) => any): Fail;
chain(fn: (value: any, errors: any) => ValidationM): Fail;
fold(opts: ValidationActions): any;
}
interface Success {
value: any;
errors: string[];
isSuccess: boolean;
map(fn: (value: any) => any): Success;
chain(fn: (value: any) => ValidationM): ValidationM;
fold(opts: ValidationActions): any;
}
declare type ValidationCheck = (v: any, vs?: any) => ValidationM;
declare type ValidationRuleset = (ValidationRule | ((v: any) => ValidationRule))[];
declare type ValidationErrorMessage = (fn: (v: any) => any) => string;
export { CustomValidatorOptions, Fail, Success, ValidationActions, ValidationCheck, ValidationErrorMessage, ValidationM, ValidationRule, ValidationRuleset, Validator };