@23zane/vue-validator 中文文档教程

发布于 3年前 浏览 37 项目主页 更新于 3年前

Vue Validator

Vue 3 验证器基于 @vue/composition-api, @vuelidate/validators@vuelidate/core

这个包是为了在一个通用的反应对象上使用验证而构建的,它显示与其类型相关的输入。

Usage

这个包有一个主要功能:useValidation

export const useValidation: <FormDataType, FormDataValuesType extends unknown, InputTypes extends GenericInput = GenericInput>(
    inputs: Ref<InputType<FormDataType, InputTypes>> | InputType<FormDataType, InputTypes>,
    formData: Record<keyof FormDataType, FormDataValuesType> | Ref<Record<keyof FormDataType, FormDataValuesType>>,
    checkDirty?: boolean,
    registerAs?: string,
    callbacks?: Partial<{
        onInputChange: <K extends keyof FormDataType = keyof FormDataType>(key: K, value: FormDataType[K]) => void;
        onInputInvalid: <K extends keyof FormDataType = keyof FormDataType>(key: K) => void;
        onInputValid: <K extends keyof FormDataType = keyof FormDataType>(key: K) => void;
    }>) => {
    v: Ref<Validation<ValidationArgs<FormDataType>, Record<keyof FormDataType, FormDataValuesType>>>,
    isInvalid: boolean,
    isInputInvalid: (key: keyof FormDataType, excludeDirty?: boolean) => boolean,
    isInputTouched: (key: keyof FormDataType) => boolean
};

它接收具有以下属性的通用输入

export type GenericInput = {
    mandatory: boolean;
    name: string;
    rules: (string | {key: string, func: (value: any) => boolean})[];
    exclude_dirty?: boolean; //To avoid dirty check
}

记录:FormDataType 的记录,以及其他属性:

checkDirty:是否将表单值设置为无效,直到它们被删除't modified - 查看脏状态

registerAs: vuelidate registerAs 属性 - 查看 https://vuelidate-next.netlify.app/

回调:这些回调,如果通过,则为每个属性切换,

The function returns an object containing

{
    v: Vuelidate object containing your form attributes, ready to be touched, see https://vuelidate-next.netlify.app/guide.html#the-dirty-state,
    isInvalid: Vuelidate generic isInvalid object,
    isInputInvalid(key): Function that returns if an input is invalid,
    isInputTouched(key): Function that returns if an input is touched, aka the form attribute has changed.    
}

The library is NOT touching the attributes, if you want to apply automatic touch on change, you can use onChange callback like below:

const {v, isInvalid, isInputInvalid, isInputTouched} = useValidation<FormData, string | boolean>(inputs, formData, false, undefined, {
    onInputChange(key){
        v.value[key].$touch();
    }
});

List of rules applicable:

requiredIf:name,value 设置当 $name 具有 $value 值时所需的输入;

requiredIfNot:name,value 设置当$name 的值不同于$value 时需要的输入;

sameAs:name 输入值需要与$name值相同;

notSameAs:name 输入值需要不同于$name值;

min:number 输入值至少需要 $number 个字符;

ma​​x:number 输入值需要少于$number 个字符;

minValue:number 输入值必须大于$number

ma​​xValue:number 输入值必须小于$number

日期 输入必须是有效日期(YYYY-MM-DD);

日期时间 输入必须是有效日期(YYYY-MM-DD HH:mm:ss);

email 输入必须是有效的电子邮件;

required 在对象模型中输入是必需的(同mandatory:true);

accepted 输入必须=== true 或=== 1;

full_number 输入必须是数字;

number 输入必须有数字;

char 输入必须有一个字符;

regex:regex 输入必须匹配正则表达式,es: regex:/^[az]+$/;

minDate:date 输入日期必须晚于或等于$date (YYYY-MM-DD);

ma​​xDate:date 输入日期必须早于或等于$date (YYYY-MM-DD);

minDatetime:date 输入日期必须晚于或等于$date (YYYY-MM-DD HH:mm:ss);

ma​​xDatetime:date 输入日期必须早于或等于$date (YYYY-MM-DD HH:mm:ss);

minMonth:date 输入值必须晚于或等于$date (YY-MM);

ma​​xMonth:date 输入值必须早于或等于$date (YY-MM);

iban 输入值必须是有效的欧洲 IBAN;

Example

type FormData = {
    name: string;
    surname: string;
    email: string;
    privacy: boolean;
}

const inputs: Record<keyof FormData, GenericInput> = {
    name: {
        name: 'name',
        mandatory: false,
        rules: ['requiredIf:surname,'],
    },
    surname: {
        name: 'surname',
        mandatory: false,
        rules: ['requiredIf:name,'],
    },
    email: {
        name: 'email',
        mandatory: true,
        rules: []
    },
    privacy: {
        name: 'privacy',
        mandatory: true,
        rules: ['boolean']
    }
};
const formData = reactive<FormData>({
    name: "test",
    surname: "",
    email: "",
    privacy: false
});

const {v, isInvalid, isInputInvalid, isInputTouched} = useValidation<FormData, string | boolean>(inputs, formData, false);

return {
    v,
    isInvalid,
    isInputInvalid,
    isInputTouched
}

Vue Validator

Vue 3 validator based on @vue/composition-api, @vuelidate/validators and @vuelidate/core.

This package is built to use validation on a generic reactive object,which displays inputs related to its type.

Usage

This package has one main function: useValidation

export const useValidation: <FormDataType, FormDataValuesType extends unknown, InputTypes extends GenericInput = GenericInput>(
    inputs: Ref<InputType<FormDataType, InputTypes>> | InputType<FormDataType, InputTypes>,
    formData: Record<keyof FormDataType, FormDataValuesType> | Ref<Record<keyof FormDataType, FormDataValuesType>>,
    checkDirty?: boolean,
    registerAs?: string,
    callbacks?: Partial<{
        onInputChange: <K extends keyof FormDataType = keyof FormDataType>(key: K, value: FormDataType[K]) => void;
        onInputInvalid: <K extends keyof FormDataType = keyof FormDataType>(key: K) => void;
        onInputValid: <K extends keyof FormDataType = keyof FormDataType>(key: K) => void;
    }>) => {
    v: Ref<Validation<ValidationArgs<FormDataType>, Record<keyof FormDataType, FormDataValuesType>>>,
    isInvalid: boolean,
    isInputInvalid: (key: keyof FormDataType, excludeDirty?: boolean) => boolean,
    isInputTouched: (key: keyof FormDataType) => boolean
};

It receives a record of generic inputs with these properties:

export type GenericInput = {
    mandatory: boolean;
    name: string;
    rules: (string | {key: string, func: (value: any) => boolean})[];
    exclude_dirty?: boolean; //To avoid dirty check
}

a record of FormDataType, and other attributes:

checkDirty: whether to set invalid the form values until they aren't modified - see The dirty state

registerAs: vuelidate registerAs attribute - see https://vuelidate-next.netlify.app/

callbacks: these callbacks, if passed, are toggled for every attribute,

The function returns an object containing

{
    v: Vuelidate object containing your form attributes, ready to be touched, see https://vuelidate-next.netlify.app/guide.html#the-dirty-state,
    isInvalid: Vuelidate generic isInvalid object,
    isInputInvalid(key): Function that returns if an input is invalid,
    isInputTouched(key): Function that returns if an input is touched, aka the form attribute has changed.    
}

The library is NOT touching the attributes, if you want to apply automatic touch on change, you can use onChange callback like below:

const {v, isInvalid, isInputInvalid, isInputTouched} = useValidation<FormData, string | boolean>(inputs, formData, false, undefined, {
    onInputChange(key){
        v.value[key].$touch();
    }
});

List of rules applicable:

requiredIf:name,value Sets input required when $name has $value value;

requiredIfNot:name,value Sets input required when $name has a value different from $value;

sameAs:name Input value needs to be same as $name value;

notSameAs:name Input value needs to be different from $name value;

min:number Input value needs to be at least $number chars long;

max:number Input value needs to have less than $number chars;

minValue:number Input value must be greater than $number;

maxValue:number Input value must be lower than $number;

date Input must be a valid date (YYYY-MM-DD);

datetime Input must be a valid date (YYYY-MM-DD HH:mm:ss);

email Input must be a valid email;

required Input is required (same as mandatory: true) in the object model;

accepted Input must be === true or === 1;

full_number Input must be numeric;

number Input must have a number in it;

char Input must have a char in it;

regex:regex Input must match the regex, es: regex:/^[a-z]+$/;

minDate:date Input date must be after or same as $date (YYYY-MM-DD);

maxDate:date Input date must be before or same as $date (YYYY-MM-DD);

minDatetime:date Input date must be after or same as $date (YYYY-MM-DD HH:mm:ss);

maxDatetime:date Input date must be before or same as $date (YYYY-MM-DD HH:mm:ss);

minMonth:date Input value must be after or equal to $date (YY-MM);

maxMonth:date Input value must be before or equal to $date (YY-MM);

iban Input value must be a valid european IBAN;

Example

type FormData = {
    name: string;
    surname: string;
    email: string;
    privacy: boolean;
}

const inputs: Record<keyof FormData, GenericInput> = {
    name: {
        name: 'name',
        mandatory: false,
        rules: ['requiredIf:surname,'],
    },
    surname: {
        name: 'surname',
        mandatory: false,
        rules: ['requiredIf:name,'],
    },
    email: {
        name: 'email',
        mandatory: true,
        rules: []
    },
    privacy: {
        name: 'privacy',
        mandatory: true,
        rules: ['boolean']
    }
};
const formData = reactive<FormData>({
    name: "test",
    surname: "",
    email: "",
    privacy: false
});

const {v, isInvalid, isInputInvalid, isInputTouched} = useValidation<FormData, string | boolean>(inputs, formData, false);

return {
    v,
    isInvalid,
    isInputInvalid,
    isInputTouched
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文