Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

条件验证 #3

Closed
seekcx opened this issue Aug 25, 2018 · 0 comments
Closed

条件验证 #3

seekcx opened this issue Aug 25, 2018 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@seekcx
Copy link
Owner

seekcx commented Aug 25, 2018

有时候,我们需要基于其它参数的值才能确定当前参数应该用什么规则进行验证。这个功能在初期版本的设想中被搁置了,一部分原因是因为时间关系,另一部分原因是因为没有特别好的实现方案。

参考了一下 yup ,有了大概的实现方案:

基本实现

在 acr 主类上实现 when 方法,第一个参数为相关参数名,第二个参数为预期值,第三个参数的闭包返回值作为符合预期的条件验证。

const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.string().required().min(18);
    }),
};

// 抛出错误,age 必填
await acr.validate({
    type: 'personal',
}, rules);

// 抛出错误,age 不能小于 18
await acr.validate({
    type: 'personal',
    age: 16,
}, rules);

// 通过
await acr.validate({
    type: 'common',
    age: 16,
}, rules);

逆向条件的验证规则

使用 other 方法来实现逆向条件的验证

const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.number().required().min(18);
    }).other(() => {
        return acr.string().equal('');
    }),
};

// 抛出错误,age 不等于 '空字符串'
await acr.validate({
    type: 'common',
    age: 16,
}, rules);

多条件规则

在 when 方法后面,可以连接多个 when 方法,来实现更加复杂的条件验证。最后,也可以使用 other 来实现其它可能的验证。

从逻辑上来说,other 包含 when 没有包含的所有可能,因此,无论声明顺序是怎样,other 总是在所有条件之后执行,并且多次调用 other 只有最后一次有效。

const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.number().required().min(18);
    }).when('type', 'common', () => {
        return acr.string().equal('16');
    }).other(() => {
        return acr.string().equal('');
    }),
};

多个参数联合验证

一个参数值的匹配,可以满足大多数情况的需求,针对一些极端条件,第一个参数可以提供闭包,返回一个 boolean 值来确定是否满足条件。第 3 位参数位置顺延至第 2 位。

const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    gender: acr.string().required().in([ 'male', 'female' ]),
    age: acr.when((data, context) => {
        return data['type'] === 'personal' && data['gender'] === 'male';
    }, () => {
        return acr.number().required().min(18);
    }),
}

异步支持

当第一个参数为闭包时,支持使用异步方法来判断是否符合条件。

const rules = {
    gender: acr.string().required().in([ 'male', 'female' ]),
    age: acr.when(async (data, context) => {
        return await Promise.resolve(data['gender'] === 'male');
    }, () => {
        return acr.number().required().min(18);
    }),
}
@seekcx seekcx added this to the 1.2 milestone Aug 25, 2018
@seekcx seekcx added the enhancement New feature or request label Aug 25, 2018
seekcx added a commit that referenced this issue Aug 26, 2018
@seekcx seekcx mentioned this issue Aug 26, 2018
2 tasks
@seekcx seekcx closed this as completed in #5 Aug 26, 2018
seekcx added a commit that referenced this issue Aug 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant