@stackoverfloweth/vue-compositions
Version:
A collection of reusable vue compositions.
47 lines (46 loc) • 1.77 kB
TypeScript
import { ToRefs, MaybeRef, MaybeRefOrGetter } from 'vue';
import { NoInfer } from '../types/generics';
import { MaybeArray, MaybePromise } from '../types/maybe';
export type UseValidationState = {
valid: boolean;
invalid: boolean;
error: string;
pending: boolean;
validated: boolean;
};
export type ValidateMethodOptions = {
source?: string;
};
export type ValidateMethod = (options?: ValidateMethodOptions) => Promise<boolean>;
export type UseValidation = ToRefs<UseValidationState> & {
validate: ValidateMethod;
/**
* Reset the validation state.
*
* @param resetCallback An optional callback that will be called within a pause/resume block.
* This allows you to reset validation state and then reset the value without triggering
* another validation on change.
*
* @example
* ```ts
* const value = ref(0)
* const { reset } = useValidation(value, isGreaterThanZero)
* reset(() => value.value = 0)
* ```
*/
reset(resetCallback?: () => void): void;
pause: () => void;
resume: () => void;
state: UseValidationState;
};
export type ResetMethod = UseValidation['reset'];
export type ValidationRuleContext<T> = {
signal: AbortSignal;
source: string | undefined;
previousValue: T | undefined;
};
export type ValidationRule<T> = (value: T, name: string, meta: ValidationRuleContext<T>) => MaybePromise<boolean | string | undefined>;
type RulesArg<T> = MaybeRef<MaybeArray<ValidationRule<T>>>;
export declare function useValidation<T>(value: MaybeRefOrGetter<T>, rules: RulesArg<NoInfer<T>>): UseValidation;
export declare function useValidation<T>(value: MaybeRefOrGetter<T>, name: MaybeRef<string>, rules: RulesArg<NoInfer<T>>): UseValidation;
export {};