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

Added support for user defined custom validation callback #10

Merged
merged 3 commits into from
Jun 17, 2015

Conversation

davidstevens37
Copy link
Contributor

This change adds the ability for a user to define a callback to test the property against whatever they want.

the callback is passed 3 arguments: key, value, and the model's scope. This is awesome because you're able to compare against another property (or properties) on the model. return a truthy value back and it will pass the validation or return a falsy value back to fail the validation. the second example works the exact same way except will use the custom message instead of default.

if the object syntax (2) is used, and the user does not define a validate property or the validate property is not a function, the validator will not invoke the callback and the validation will pass.

  validations: {
    lotteryNumber: {
      custom: funciton(key, value, model){
        return model.get('accountBalance') > 1 ? true : false;
      }
    }
  }
  validations: {
    lotteryNumber: {
      custom: {
        validation: funciton(key, value, model){
          return model.get('accountBalance') > 1 ? true : false;
        },
        message: 'You can't win off of good looks and charm.'
      }
    }
  }

if (Ember.typeOf(validation.custom) === 'object' && validation.custom.hasOwnProperty('validation')) {
customValidator = validation.custom.validation;
}
return typeof customValidator === 'function' ? customValidator : false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidstevens37 Shouldn't this return the return value from the function?

return typeof customValidator === 'function' ? customValidator() : false;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is determining whether the customValidation function is located at validations.{property}.custom or validations.{property}.custom.validation (in the case that a custom message is defined), ensuring that the callback is in fact a function and returns the function to be invoked by the _validateCustom method on line 52 where the customValidator will be passed the actual key, value, and scope.

if the customValidator is a function, return it to be executed on line 52. if it is not a function, return false and then line 51 will evaluate to false at which case line 52 will not be executed.

@esbanarango
Copy link
Owner

Merging! Thank you @davidstevens37! Great work.

esbanarango added a commit that referenced this pull request Jun 17, 2015
Added support for user defined custom validation callback
@esbanarango esbanarango merged commit a4d6e5b into esbanarango:master Jun 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants