gib-validate
Version:
gib-validate is a lightweight validation helper for Vue 3 (Composition API). It provides a set of common validation rules and a `useValidation` hook to validate reactive form state.
87 lines (51 loc) • 3.96 kB
TypeScript
import { ComputedRef } from 'vue';
import { MaybeRefOrGetter } from 'vue';
import { Reactive } from 'vue';
import { Ref } from 'vue';
export declare const between: (min: number, max: number, error?: string) => (value: number) => boolean | string;
export declare const contains: (valueToFind: string, error?: string) => (value: string) => boolean | string;
export declare const equalTo: <T>(target: T, error?: string) => (value: T) => boolean | string;
export declare type Errors<T extends object> = Partial<Record<keyof T, string[]>>;
export declare const fileSizeLessThan: (maxMB: number, error?: string) => (value: File) => boolean | string;
export declare const fileType: (allowedTypes: string[], error?: string) => (value: File) => boolean | string;
export declare function getValidation(name: string): ValidationState | undefined;
export declare const isDate: (error?: string) => (value: string) => boolean | string;
export declare const isEmail: (error?: string) => (value: string) => boolean | string;
export declare const isFutureDate: (error?: string) => (value: string) => boolean | string;
export declare const isInteger: (error?: string) => (value: string) => boolean | string;
export declare const isNumeric: (error?: string) => (value: string) => boolean | string;
export declare const isPassword: (minLen?: number, hasSpecialChar?: boolean, error?: string) => (value: string) => boolean | string;
export declare const isPastDate: (error?: string) => (value: string) => boolean | string;
export declare const isPhone: (error?: string) => (value: string) => boolean | string;
export declare const isUrl: (error?: string) => (value: string) => boolean | string;
export declare const maxLength: (max: number, error?: string) => (value: string) => boolean | string;
export declare type Message<T extends object> = Partial<Record<keyof T, string>>;
export declare const minLength: (min: number, error?: string) => (value: string) => boolean | string;
export declare const oneOf: <T>(list: T[], error?: string) => (value: T) => boolean | string;
export declare function registerValidation(name: string, state: ValidationState): void;
export declare const required: (error?: string) => (value: Validatable) => string | boolean;
export declare const requiredIf: (condition: () => boolean, error?: string) => (value: Validatable) => boolean | string;
export declare const sameAs: <T extends Validatable>(_equalTo: T, error?: string) => (value: T) => string | boolean;
export declare function unregisterValidation(name: string): boolean;
export declare function useNamedValidation(name: string): Ref<ValidationState | undefined>;
export declare function useValidation<T extends Record<string, Validatable>>(state: Reactive<T>, rules: MaybeRefOrGetter<ValidationRules<T>>, name?: string): ValidationResult<T>;
export declare type Validatable = string | number | boolean | Array<Validatable> | object | null | undefined;
export declare type ValidationResult<T extends object> = ComputedRef<ValidationState<T>> & ValidationState<T>;
export declare type ValidationRule<T> = (value: T) => string | boolean | Promise<string | boolean>;
export declare type ValidationRules<T> = {
[K in keyof T]?: (ValidationRule<T[K]> | ComputedRef<ValidationRule<T[K]>>)[];
};
export declare type ValidationRulesResult<T extends Record<string, Validatable>> = ComputedRef<ValidationRules<T>>;
export declare type ValidationState<T extends object = object> = {
$touch: () => void;
$reset: () => void;
$touchField: (field: keyof T | (keyof T)[]) => void;
$resetField: (field: keyof T | (keyof T)[]) => void;
$dirty: boolean;
$errors: Errors<T>;
$silentErrors: Errors<T>;
$message: Message<T>;
$children: Ref<Record<string, ValidationState<object>>>;
};
export declare type Vuelidate = Ref<ValidationState>;
export { }