Skip to content

Conversation

@stuartf
Copy link

@stuartf stuartf commented Dec 19, 2013

I commented in #207 about this. A project I work on is passing a custom error object like {code: 404, msg: 'Not Found'} as the message in our validators. The latest versions of validator break this by leaving this.msg as null when the message is an Object.

@chriso
Copy link
Collaborator

chriso commented Dec 19, 2013

This PR breaks the unit tests introduced in #191.

The library interprets a message object as a mapping of validator methods to error messages.

check('foo@bar.com', {
    notNull: 'An email is required'
  , isEmail: 'The email is invalid'
}).notNull().isEmail();

Consider the following case

check('foo', {
    notNull: 'Expected something'
}).notNull().isInt()

When the isInt() validation fails, the library checks to see whether an error message was defined since an object containing validators => messages was specified. In this case, no isInt message was defined so it fails with the library default of Invalid integer. Your PR causes the library to instead fail with {notNull: 'Expected something'} as the error (old behaviour).

I can't see a way for both to coexist. Without reverting #191, I'd say the only way to customise the library so that it supports error objects is to patch your validator instance's check (or possibly Validator.prototype.check if you're not concerned about collisions).

@chriso chriso closed this Dec 19, 2013
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

Successfully merging this pull request may close these issues.

2 participants