import { Ref, UnwrapRef, defineComponent, ComponentInternalInstance } from 'vue-demi'; type Component = ReturnType; /* * Structure * * This package is mainly a validation engine. This engine requires 2 * inputs: validation arguments and the state to be validated. The output * is a validation result. * * The structure of the validation object is constrained by the structure * of the validation arguments. These validation arguments also constraint * the type of the states that can be validated. And although the state does * not affect the structure of the validation result, it can narrow the * property "$model" in the validation result. * * Why do validation arguments constraint the type of the states that can * be validated? Because validation arguments make assumptions about the state. * For instance we expect a state `{ foo?: string }`, and we want to check * that `foo` is not empty, so we write `(foo?.length ?? 0) > 0`. If now we try * to validate the state `{ foo: [1] }` our validation result is meaningless. * This state would pass our test, but clearly it's not a valid object. This * situation was possible because that state violated our initial assumptions. * * * Validation Arguments * | * _____|____ * / \ * | | * | States to be validated * | | * | | * Validation Result * */ export interface ValidatorResponse { $valid: boolean [key: string]: any } export type ValidatorFn = (value: T, siblingState: K, vm: S) => boolean | ValidatorResponse | Promise; export interface ValidationRuleWithoutParams { $validator: ValidatorFn $message?: string | Ref | (() => string) } export interface ValidationRuleWithParams

{ $validator: ValidatorFn $message: (input: { $params: P }) => string $params: P } export type ValidationRule = ValidationRuleWithParams | ValidationRuleWithoutParams | ValidatorFn; export type ValidationRuleCollection = Record>; export type ValidationArgs = { [key in keyof T]: ValidationArgs | ValidationRuleCollection | ValidationRule } export interface RuleResultWithoutParams { readonly $message: string readonly $pending: boolean readonly $invalid: boolean readonly $response: any } export interface RuleResultWithParams

extends RuleResultWithoutParams { readonly $params: P } export type RuleResult = RuleResultWithoutParams | RuleResultWithParams; type ExtractRuleResult = R extends ValidationRuleWithParams ? RuleResultWithParams

: RuleResultWithoutParams; type ExtractRulesResults | undefined> = { readonly [K in keyof Vrules]: Vrules[K] extends ValidationRule ? ExtractRuleResult : undefined; }; export interface ErrorObject { readonly $propertyPath: string readonly $property: string readonly $validator: string readonly $message: string | Ref readonly $params: object readonly $pending: boolean readonly $response: any, readonly $uid: string, } export type BaseValidation < T = unknown, Vrules extends ValidationRuleCollection | undefined = undefined, > = ( Vrules extends ValidationRuleCollection ? ExtractRulesResults : unknown) & { $model: T // const validationGetters readonly $dirty: boolean readonly $error: boolean readonly $errors: ErrorObject[] readonly $silentErrors: ErrorObject[] readonly $externalResults: ({ $validator: '$externalResults', $response: null, $pending: false, $params: {} } & ErrorObject)[] readonly $invalid: boolean readonly $anyDirty: boolean readonly $pending: boolean readonly $path: string // const validationMethods readonly $touch: () => void readonly $reset: () => void readonly $commit: () => void readonly $validate: () => Promise // For accessing individual form properties on v$ readonly [key: string]: any }; export type NestedValidations = { readonly [K in keyof Vargs]: BaseValidation< T extends Record ? T[K] : unknown, Vargs[K] extends ValidationRuleCollection ? Vargs[K] : undefined > & ( Vargs[K] extends Record ? NestedValidations ? T[K] : unknown> : unknown ) }; interface ChildValidations { readonly $getResultsForChild: (key: string) => (BaseValidation & ChildValidations) | undefined readonly $clearExternalResults: () => void } export type Validation = NestedValidations & BaseValidation & ChildValidations; export type ExtractStateLeaf = Vrules extends ValidationRuleCollection ? T : unknown; export type ChildStateLeafs = { [K in keyof Vargs]?: ( Vargs[K] extends ValidationRuleCollection ? ExtractStateLeaf : unknown ) & ( Vargs[K] extends Record ? ChildStateLeafs : unknown ) }; export type ExtractState = Vargs extends ValidationRuleCollection ? ExtractStateLeaf & ChildStateLeafs : ChildStateLeafs; type ToRefs = { [K in keyof T]: Ref }; export interface ServerErrors { [key: string]: string | string[] | ServerErrors } export interface GlobalConfig { $registerAs?: string $scope?: string | number | symbol | boolean $stopPropagation?: boolean $autoDirty?: boolean $lazy?: boolean, $externalResults?: ServerErrors | Ref | UnwrapRef, $rewardEarly?: boolean, currentVueInstance?: ComponentInternalInstance | null } export function useVuelidate(globalConfig?: GlobalConfig): Ref; export function useVuelidate< T extends {[key in keyof Vargs]: any}, Vargs extends ValidationArgs = ValidationArgs, EState extends ExtractState = ExtractState >( validationsArgs: Ref | Vargs, state: T | Ref | ToRefs, globalConfig?: GlobalConfig ): Ref>; export default useVuelidate;