{"action":"created","issue":{"url":"https://api.github.com/repos/ctavan/express-validator/issues/489","repository_url":"https://api.github.com/repos/ctavan/express-validator","labels_url":"https://api.github.com/repos/ctavan/express-validator/issues/489/labels{/name}","comments_url":"https://api.github.com/repos/ctavan/express-validator/issues/489/comments","events_url":"https://api.github.com/repos/ctavan/express-validator/issues/489/events","html_url":"https://github.com/ctavan/express-validator/issues/489","id":286591147,"number":489,"title":"[Feature Request] Let withMessage accept a callback","user":{"login":"hrastnik","id":3407234,"avatar_url":"https://avatars2.githubusercontent.com/u/3407234?v=4","gravatar_id":"","url":"https://api.github.com/users/hrastnik","html_url":"https://github.com/hrastnik","followers_url":"https://api.github.com/users/hrastnik/followers","following_url":"https://api.github.com/users/hrastnik/following{/other_user}","gists_url":"https://api.github.com/users/hrastnik/gists{/gist_id}","starred_url":"https://api.github.com/users/hrastnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hrastnik/subscriptions","organizations_url":"https://api.github.com/users/hrastnik/orgs","repos_url":"https://api.github.com/users/hrastnik/repos","events_url":"https://api.github.com/users/hrastnik/events{/privacy}","received_events_url":"https://api.github.com/users/hrastnik/received_events","type":"User","site_admin":false},"labels":[{"id":203759350,"url":"https://api.github.com/repos/ctavan/express-validator/labels/enhancement","name":"enhancement","color":"0e8a16","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-01-07T19:07:28Z","updated_at":"2018-02-13T10:26:41Z","closed_at":"2018-01-20T03:03:39Z","author_association":"NONE","body":"It would be great if `withMessage` could accept a callback function that would receive the field name as the argument.\r\n\r\nFor example, I have to check if some fields exist in the body of the incoming request. The field values are expected to be numbers and each field has different allowed ranges.\r\n\r\n    body('field1')\r\n      .exists().withMessage('Field required')\r\n      .custom(val => 10 < val && val < 20).withMessage('Invalid range')\r\n\r\n    body('field2')\r\n      .exists().withMessage('Field required')\r\n      .custom(val => 100 < val && val < 200).withMessage('Invalid range')\r\n\r\nThis works, however, it would be nice if I could define the messages in a function that receives some parameters like the name and value of the field as well as some other data if possible.\r\n\r\nThen we could use it like this\r\n\r\n    function messageCallback({field, value}) {\r\n      return \"Field\" + field + \" received invalid value: \" + value;\r\n    }\r\n  \r\n    body('field1')\r\n      .exists().withMessage('Field required')\r\n      .custom(val => 10 < val && val < 20).withMessage(messageCallback)\r\n\r\n    body('field2')\r\n      .exists().withMessage('Field required')\r\n      .custom(val => 100 < val && val < 200).withMessage(messageCallback)\r\n\r\nThis would give extra flexibility to the users in terms of handling errors. "},"comment":{"url":"https://api.github.com/repos/ctavan/express-validator/issues/comments/365220425","html_url":"https://github.com/ctavan/express-validator/issues/489#issuecomment-365220425","issue_url":"https://api.github.com/repos/ctavan/express-validator/issues/489","id":365220425,"user":{"login":"gustavohenke","id":826553,"avatar_url":"https://avatars2.githubusercontent.com/u/826553?v=4","gravatar_id":"","url":"https://api.github.com/users/gustavohenke","html_url":"https://github.com/gustavohenke","followers_url":"https://api.github.com/users/gustavohenke/followers","following_url":"https://api.github.com/users/gustavohenke/following{/other_user}","gists_url":"https://api.github.com/users/gustavohenke/gists{/gist_id}","starred_url":"https://api.github.com/users/gustavohenke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gustavohenke/subscriptions","organizations_url":"https://api.github.com/users/gustavohenke/orgs","repos_url":"https://api.github.com/users/gustavohenke/repos","events_url":"https://api.github.com/users/gustavohenke/events{/privacy}","received_events_url":"https://api.github.com/users/gustavohenke/received_events","type":"User","site_admin":false},"created_at":"2018-02-13T10:26:41Z","updated_at":"2018-02-13T10:26:41Z","author_association":"COLLABORATOR","body":"Hello @hrastnik this is now possible [express-validator v5.0.0](https://github.com/ctavan/express-validator/releases/tag/v5.0.0).\r\nCheck [this section of the docs](https://github.com/ctavan/express-validator#dynamic-messages) for more information on how to do it."}}
{"actor":{"display_login":"gustavohenke"}}