-
-
Notifications
You must be signed in to change notification settings - Fork 615
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
custom()
validation not working
#505
Comments
How are your requests being made? |
HTTP POST request via Firefox RESTClient using |
This isn't sounding good. |
I'm struggling with the exact same problem! I just asked a question on StackOverflow about it. 🤦♂️ I'm using a custom validation just like @KrashLeviathan does: router.post('/person/add',
personController.validateData,
asyncHandler(personController.createPerson)
) exports.validateData = [
// ...Other built-in validations that work...
body('field')
.custom((value) => {
console.log('This message shows up!')
return false
})
.withMessage('Wrong!'),
// ...Other built-in validations that work...
] I never get the Any idea why this is happening? I don't actually know how to debug it. 😞 Update: @gustavohenke This is my project repo and this is the line of my above example. It's not that minimal project you're asking for but it's not that much large (it has just one controller with CRUD operations). Worth say that requests are made successfully and the rest of validations are working fine. |
Sure. The project is open source, so you can take a look at the project setup here: https://github.com/hammer-io/yggdrasil. (It's still very much under development, so take things with a grain of salt!) The router.post(
'/projects/:projectId/invites/:user',
[
check('daysFromCreationUntilExpiration', 'This should be hardcoded to fail')
.custom((value, { req }) => {
console.log(value);
return false;
})
],
invitesController.addInviteToProject
); I hope that's enough information to try to track down the issue (or to point out what I was doing wrong). :-) |
Hey guys, so I checked both of your codes and they both work. @valeco I added a @KrashLeviathan I replaced that route with the following: router.post(
'/foo',
// authController.isAuthenticated,
check('daysFromCreationUntilExpiration', 'This should be hardcoded to fail')
.custom((value, { req }) => {
console.log(value);
return false;
}),
(req, res, next) => {
const errors = validationResult(req).mapped();
res.json({ errors });
}
); You were probably missing the call to Will close the issue for now. Feel free to continue the discussion. |
Hey @gustavohenke, thank you so much for spend your time on this and for your quick response! You're right, the In my case, if you fill the fields |
Sorry @valeco but I still can't reproduce. I commented out the other validators and sanitisers and it still works. I must also comment that I had to remove the mongoose connection. Can you please create a minimal example that reproduces your issue? |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Here's an abstract of the code I'm working with:
I'm using a custom check to validate a query parameter, and after trying everything to get it to work, I distilled it down to something that looks just like the code in the documentation, and it still won't work. It's not asynchronous, it's just a boolean check. I took out my boolean check and hardcoded in
false
, but it still doesn't work. I'm running it with v4.3.0. I added theconsole.log(value)
and it's printing the correct value that gets passed in. Is there something I'm missing, or iscustom()
not fully working, or is the documentation out of date?Thanks!
The text was updated successfully, but these errors were encountered: