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

Equality with another attribute #29

Closed
dmhts opened this Issue Feb 10, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@dmhts
Copy link

dmhts commented Feb 10, 2015

I'm wondering how to check the equality with another attribute? I've looked through the documentation and haven't found any possibility to define a custom validator only the dynamic one. Did I miss something?

@ansman

This comment has been minimized.

Copy link
Owner

ansman commented Feb 12, 2015

validate.validators.equality = function(value, options, key, attributes) {
  var other = attributes[options.attribute];
  if (value !== other) {
    var attr = validate.prettify(options.attribute);
    return options.message ||
      validate.format("is not equal to %{a}", {a: attr});
  }
};

var constraints = {
  a1: {
    equality: {
      attribute: "a2"
    }
  }
};

console.log(validate({}, constraints));
// => undefined

console.log(validate({a1: "foo", a2: "foo"}, constraints));
// => undefined

console.log(validate({a1: "foo", a2: "bar"}, constraints));
// => {a1: ["A1 is not equal to a2"]}

Hope this helps. It doesn't work with nested attributes (foo.bar) and doesn't support more complex values such as arrays but it's a start.

@dmhts

This comment has been minimized.

Copy link
Author

dmhts commented Feb 13, 2015

@ansman Thank you for code and I'm sorry for my inattention. Also I think it makes sense to add this type of a validator to the predefined list in the documentation. It's really a common task to check equality for email, password fields, etc.

@ansman

This comment has been minimized.

Copy link
Owner

ansman commented Feb 18, 2015

Yeah, that seems like a good idea. I'll implement it for a future release.

@ansman ansman closed this in 52fd2a4 Mar 7, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment