UNPKG

onsubmit

Version:

Simple validation utilities in typescript.

45 lines (41 loc) 1.13 kB
interface CustomFunction { (value: string): boolean; } interface KeyValuePair { [key: string]: string; } type Criterion = string | number | CustomFunction | boolean | RegExp; interface Rule<TCriterion = Criterion> { criterion: TCriterion; message: string; } interface RequiredRule extends Rule<boolean> { } interface MinLengthRule extends Rule<number> { } interface MaxLengthRule extends Rule<number> { } interface PatternRule extends Rule<RegExp> { } interface CustomRule extends Rule<CustomFunction> { } interface RulesObject { required?: RequiredRule; minLength?: MinLengthRule; maxLength?: MaxLengthRule; pattern?: PatternRule; custom?: CustomRule; } type FormDataShape = KeyValuePair | { [k: string]: FormDataEntryValue; }; interface FieldError { name: string; message: string; } interface NameRuleMap { [key: string]: RulesObject; } declare function validateField(value: string, name: string, rulesObject: RulesObject): Array<FieldError>; declare function validateForm(data: FormDataShape, NameRuleMap: NameRuleMap): FieldError[]; export { validateField, validateForm };