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.

Customize validator returns library internal error

See original GitHub issue

Reproduction:

package.json

{
  "name": "w",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "express-validator": "^5.2.0"
  }
}

app.json

const {body, validationResult} = require('express-validator/check')
require('express')()
  .use(require('body-parser').json())
  .use(
    body('pwd').isLength({min: 8}, (pwd, {req}) => 'length must >= 8'),
    body('pwdCfrm').custom((pwdCfrm, {req}) => {
      if (pwdCfrm !== req.body.pwd)
        throw new Error('pwds do not match')
    }),
    (req, res) =>  res.end(JSON.stringify(validationResult(req).array()))
  ).listen(8000, () => console.log('app is listening'))

result when opening ‘localhost:8000’ in browser

[{"location":"body","param":"pwd","msg":"Invalid value"},{"location":"body","param":"pwdCfrm","msg":"Cannot read property 'then' of undefined"}]

while expected result is only 'length must >= 8' error

I also tried some workaround but thing does not work at all

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tranvansangcommented, Jun 20, 2018

I think always wrapping returned result by Promise.resolve() should work.

const func = x => {
if (x) throw new Error('x is true')
}
Promise.resolve(func(false)).then(console.log)
Promise.resolve(func(true)).catch(console.log)
0reactions
lock[bot]commented, May 31, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Display custom validator error with mat-error - Stack Overflow
The single line of code in this method is quite simple; it returns true (error exists) if the parent control (our FormGroup) is...
Read more >
Error Handler does not return an expected response ... - GitHub
Issue Description The built-in error handler should handle validation error by the library validator as mentioned in the docs Checklist ...
Read more >
Angular Custom Form Validators: Complete Guide
As we can see, the ValidationErrors object that we returned as the output of our custom validator was added to the pasword field...
Read more >
Angular custom validators + trick for flexible custom validator
Here we are validating if the input can be converted to a date and if not, we return an error object with “invalidDate”...
Read more >
Custom Validators - Vuelidate
A simple, but powerful, lightweight model-based validation for Vue.js 3 and 2.
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