{"action":"created","issue":{"url":"https://api.github.com/repos/ctavan/express-validator/issues/227","repository_url":"https://api.github.com/repos/ctavan/express-validator","labels_url":"https://api.github.com/repos/ctavan/express-validator/issues/227/labels{/name}","comments_url":"https://api.github.com/repos/ctavan/express-validator/issues/227/comments","events_url":"https://api.github.com/repos/ctavan/express-validator/issues/227/events","html_url":"https://github.com/ctavan/express-validator/issues/227","id":147929187,"number":227,"title":"No method for validating existence of property in req.params, req.body, or req.query","user":{"login":"fvgs","id":9427813,"avatar_url":"https://avatars.githubusercontent.com/u/9427813?v=3","gravatar_id":"","url":"https://api.github.com/users/fvgs","html_url":"https://github.com/fvgs","followers_url":"https://api.github.com/users/fvgs/followers","following_url":"https://api.github.com/users/fvgs/following{/other_user}","gists_url":"https://api.github.com/users/fvgs/gists{/gist_id}","starred_url":"https://api.github.com/users/fvgs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fvgs/subscriptions","organizations_url":"https://api.github.com/users/fvgs/orgs","repos_url":"https://api.github.com/users/fvgs/repos","events_url":"https://api.github.com/users/fvgs/events{/privacy}","received_events_url":"https://api.github.com/users/fvgs/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-04-13T02:29:17Z","updated_at":"2016-06-25T23:03:17Z","closed_at":null,"body":"Suppose I have a route expecting to receive the parameter `req.body.heading`. If this parameter exists and was sent by the client, then my validation will run as expected. I will check `req.validationErrors()` and proceed accordingly. However, if `req.body.heading` was not sent by the client, its value will be `undefined`. Based on my limited testing, when this happens, the `undefined` value will be coerced to an empty string and validation will proceed on that empty string. A `validator deprecated` message also gets printed to the console, and I understand this is going to be addressed in the future. However, the problem here is that there is currently no way to check if a value is `undefined` and produce an error when it is `undefined`. Currently, the `undefined` value gets coerced to an empty string and validated as an empty string.\r\n\r\nThis is problematic because there is a big difference between not receiving a value from the client and receiving an empty string. These constitute independent cases that one may wish to handle differently. In my case, I consider receiving no value (i.e. `undefined`) as an error, whereas receiving an empty string may or may not be valid depending on the context.\r\n\r\nMy solution has been to create a custom validator to check if the value exists:\r\n\r\n```javascript\r\ncustomValidators: {\r\n  exists: value => value !== undefined,\r\n}\r\n```\r\n\r\nThis achieves the desired behavior whereby if the value is `undefined`, an error will be produced in `req.validationErrors()`. In practice, I also use the `optional()` method immediately after `exists()` to stop any further validation when the value is `undefined`.\r\n\r\nTypical usage would look something like this:\r\n```javascript\r\nreq.checkBody('heading').exists().optional()...;\r\n```\r\n\r\nWith that in mind, I'd appreciate some feedback on whether or not this is the best way to address this issue. Likewise, if this makes sense and doesn't conflict with any upgrade plans, I'd like to make a PR to add an `exists()` method. In this PR I would go ahead and bundle the `optional()` behavior into the `exists()` method since using it without that behavior doesn't really make sense. Though I would add an option to allow `exists()` to be used without the `optional()` behavior."},"comment":{"url":"https://api.github.com/repos/ctavan/express-validator/issues/comments/228574894","html_url":"https://github.com/ctavan/express-validator/issues/227#issuecomment-228574894","issue_url":"https://api.github.com/repos/ctavan/express-validator/issues/227","id":228574894,"user":{"login":"fvgs","id":9427813,"avatar_url":"https://avatars.githubusercontent.com/u/9427813?v=3","gravatar_id":"","url":"https://api.github.com/users/fvgs","html_url":"https://github.com/fvgs","followers_url":"https://api.github.com/users/fvgs/followers","following_url":"https://api.github.com/users/fvgs/following{/other_user}","gists_url":"https://api.github.com/users/fvgs/gists{/gist_id}","starred_url":"https://api.github.com/users/fvgs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fvgs/subscriptions","organizations_url":"https://api.github.com/users/fvgs/orgs","repos_url":"https://api.github.com/users/fvgs/repos","events_url":"https://api.github.com/users/fvgs/events{/privacy}","received_events_url":"https://api.github.com/users/fvgs/received_events","type":"User","site_admin":false},"created_at":"2016-06-25T23:03:17Z","updated_at":"2016-06-25T23:03:17Z","body":"@srkimir Based on the commits that have been made since opening this issue, I believe the issue remains as described originally.\r\n\r\nThe behavior you describe for the `isEmail()` method is consistent with the issue. An `undefined` value will be coerced to an empty string. Thus, `isEmail()` will validate an empty string. Given the nature of its validation, an empty string as its input will produce an error since an empty string is not a valid email address.\r\n\r\nThe issue I describe is that of `undefined` values (i.e. values not submitted by the user) being coerced to empty strings and validated as such. This canonicalizes an `undefined` value to an empty string even though a user of this package may wish for `undefined` values and empty strings to have different validation behavior.\r\n\r\nFor instance, one may have a field that, if not submitted, should produce an error, but, if submitted as an empty string, should validate."}}
{"actor":{"display_login":"fvgs"}}