question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`custom()` validation not working

See original GitHub issue

Here’s an abstract of the code I’m working with:

import { check } from 'express-validator/check';

router.post(
  '/myendpoint',
  [
    check('myparameter', 'This should be hardcoded to fail')
      .custom((value, { req }) => {
        console.log(value);
        return false;
      })
  ],
  myEndpointHandler
);

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 the console.log(value) and it’s printing the correct value that gets passed in. Is there something I’m missing, or is custom() not fully working, or is the documentation out of date?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
gustavohenkecommented, Jan 27, 2018

Hey guys, so I checked both of your codes and they both work.

@valeco I added a console.log(req.flash()) to your error handler and it prints all messages, including Wrong! 😉

@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 validationResult.

Will close the issue for now. Feel free to continue the discussion.

1reaction
gustavohenkecommented, Jan 27, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Validator not firing - Stack Overflow
CustomValidators don't fire if other Validators in your ASPx are not validating. You may need to force a Page.Validate("something") ...
Read more >
Server Side CustomValidator not working - MSDN - Microsoft
The key here is that you should always setup server side validation. Its already been mentioned. Make sure Page.Validate() runs (which happens ...
Read more >
Custom validation not firing :( | WordPress.org
Hello, I'm trying to add a custom validation to a text field to both Login and Registration forms, but I can't get it...
Read more >
Form validation is not working in my custom form in Magento 2
this is my phtml code. Here when i am submitting the form with empty fields, form validation is not triggering, it is submitting...
Read more >
custom validation not working - Our Umbraco
$scope.validationTest = null; $scope.validateMandatory = function () { return { isValid: false, errorMsg: "Value cannot be empty", errorKey: " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found