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

默认值支持 #2

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

默认值支持 #2

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

一些可选的参数常常拥有一个默认值,虽然实现并不复杂,但这无疑增加了代码的复杂程度,也不够优雅。

基于此,设想一下默认值方法的基本使用:

const [ foo ] = await acr.validate({}, {
  foo: acr.string().default('bar'),
});

foo; // 'bar';

嗯,实现了功能,但不够强大,如果这个默认值可能会基于其它参数的值来确定呢?

const [ foo ] = await acr.validate({
  bax: 123,
}, {
  foo: acr.string().default((data, context) => {
    return data['bax'] === 123 ? 'bar' : 456;
  }),
});

foo; // 'bar';

这样应该算实现了一个完整的默认值功能。

@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 added a commit that referenced this issue Aug 26, 2018
@seekcx seekcx closed this as completed 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