weixin_39661589
weixin_39661589
2020-12-27 18:34

Validate on runs validation on all attributes

I'm wondering why when using on during validate, validate still validates each attr and only stores the results of those whitelisted. This seems like unnecessary processing. Why not only run the validation if on is not defined or the attr is specified.

 js
function validate(options = {}, async = true) {
  var model = get(this, 'model');
  var whiteList = makeArray(options.on);
  var blackList = makeArray(options.excludes);
  var validationResult, value;

  var validationResults = get(this, '_validatableAttributes').reduce((v, name) => {

    if (!isEmpty(blackList) && blackList.indexOf(name) !== -1) {
      return v;
    }

    if (isEmpty(whiteList) || whiteList.indexOf(name) !== -1) {
      validationResult = get(this, `attrs.${name}`);

     // If an async validation is found, throw an error
     if (!async && get(validationResult, 'isAsync')) {
       throw new Error(`[ember-cp-validations] Synchronous validation failed due to ${name} being an async validation.`);
     }

      value = get(validationResult, 'value');
      v.push(validationResult);
    }
    return v;
  }, []);

  var validationResultsCollection = ValidationResultCollection.create({
    content: validationResults
  });

  var resultObject = {
    model,
    validations: validationResultsCollection
  };

  if (async) {
    if (get(validationResultsCollection, 'isAsync')) {
      resultObject.promise = get(validationResultsCollection, 'value');
    }
    return RSVP.hash(resultObject);
  }

  return resultObject;
}

该提问来源于开源项目:offirgolan/ember-cp-validations

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39716044 weixin_39716044 4月前

    Im not sure I'm understanding you question but if on is provided, we only validate the attributes that are given in that collection

     js
     if (isEmpty(whiteList) || whiteList.indexOf(name) !== -1) { ... }
    
    点赞 评论 复制链接分享
  • weixin_39661589 weixin_39661589 4月前

    That is the thing we don't only validate the attributes that are in the given collection. We validate all the attributes but only return the results of the whitelisted attributes.

    The original validate calls validationResult = get(this, 'attrs.${name}'); on all attributes then if whitelisted adds the results to validationResults.

    My updated code above only runs the validations if the attribute is whitelisted.

    点赞 评论 复制链接分享
  • weixin_39716044 weixin_39716044 4月前

    Oh you posted modified code! I thought that was the original code lol.

    I see what you mean! I'll make the change :smile_cat:

    点赞 评论 复制链接分享
  • weixin_39716044 weixin_39716044 4月前

    Thanks for creating the issue and bringing this to my attention!

    点赞 评论 复制链接分享
  • weixin_39661589 weixin_39661589 4月前

    If you add this to the CHANGELOG can you attribute it to me? thank you

    点赞 评论 复制链接分享
  • weixin_39824020 weixin_39824020 4月前

    good catch

    点赞 评论 复制链接分享
  • weixin_39716044 weixin_39716044 4月前

    Fixed with v2.4.1, attributed you in the changelog as well :smile:

    点赞 评论 复制链接分享

相关推荐