vue-tiny-validator
Version:
Tiny form validation tool for Vue 3
62 lines (61 loc) • 1.89 kB
TypeScript
import { ComputedRef, Ref } from 'vue';
declare const FormSymbol: unique symbol;
export declare type FormError = {
identifier?: string;
value: unknown;
message: string;
};
export declare type FormField = {
validate: () => boolean;
validateAsync: () => Promise<boolean>;
reset: () => void;
error: Ref<string>;
identifier?: string;
value: Ref<unknown>;
};
export declare type FormContext = {
registerField: (field: FormField) => void;
unregisterField: (field: FormField) => void;
};
export declare type FormRule = (value: any) => unknown;
export declare type FieldOptions = {
value: Ref<any> | ComputedRef<any> | (() => any);
rules: FormRule[] | Ref<FormRule[]> | ComputedRef<FormRule[]> | (() => FormRule[]);
identifier?: string;
form?: {
[FormSymbol]: FormContext;
};
};
/**
* Validates a field value against a list of rules.
*
* @param options
* @param options.value The model-value of the field
* @param options.rules List of rules that the value needs to fulfill
* @param options.identifier Optional identifier for this field, passed later to the form
* @param options.form Optional form context when this field is not a child of the form
*/
export declare function useField({ value, rules, identifier, form }: FieldOptions): {
validate: () => boolean;
validateAsync: () => Promise<boolean>;
error: ComputedRef<string>;
reset: () => void;
};
/**
* Validates all the nested field components.
*/
export declare function useForm(): {
validate: () => boolean;
validateAsync: () => Promise<boolean>;
errors: ComputedRef<{
message: string;
value: unknown;
identifier: string | undefined;
}[]>;
reset: () => void;
form: {
[FormSymbol]: FormContext;
};
isValid: ComputedRef<boolean>;
};
export {};