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

enum 模式如果value 为 0,但枚举规则不包含 0,则不会触发校验 #52

Open
flyfloor opened this issue Jun 22, 2017 · 1 comment

Comments

@flyfloor
Copy link

例子:
image

猜测是不是 https://github.com/yiminghe/async-validator/blob/master/src/validator/enum.js#L23 这里的问题

@jruif
Copy link

jruif commented Jun 27, 2017

if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value) {
      rules[ENUM](rule, value, source, errors, options);
    }
}

@yiminghe 我觉得没有必要第二次去判断value,因为上面已经对value做了一次验证,这里直接调用即可:

if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    rules[ENUM](rule, value, source, errors, options);
}

而且在rules[ENUM]中再次使用value的时候,也只是作为参数:

function enumerable(rule, value, source, errors, options) {
  rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
  if (rule[ENUM].indexOf(value) === -1) {
    errors.push(util.format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
  }
}

jruif added a commit to jruif/async-validator that referenced this issue Jun 27, 2017
enum 模式中value值可以为0、false等
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants