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.

express validator without express

See original GitHub issue

express-validator is awesome, why just express!

I recently used express-validator in my express app and it was the best solution and now I am working on the front-end of that app with react. but I miss express-validator functionality in front-end for form validation.

I searched a lot on the net but I can’t find a way to implement it on my front-end is there a way? if there is no way so why keep this powerful thing on just express?

a separated library may be the solution

although I know that the express-validator uses validator.js under the hood but it can be very nice to have something like validation chains in the web apps something like :

import {createValidator} from 'the-name-for-separated-library'

const validator=createValidator([
    check('some-field')
    .exists()
    .isEmail().withMessage('invalid email')
]);

const validationResult=validator.validate({
    'some-field':'an invalid email'
});

if (!validationResult.isEmpty())
      showToEndUserSomeHow(validationResult.array({onlyFirstError:true}))

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
chinesedfancommented, Oct 30, 2020

If were familiar with how express middlewares run, you can reuse those APIs without changes in express-validator. I will show the main process and you can wrap them as a function as you wish. Not tested but should able to work.

const { body, validationResult } = require('express-validator')

// prepare an object
const req = {
  body: {} // put the data to be validated here
}

// prepare validators(middlewares)
const validators = [
  body('somefield1').isEmail(),
  body('somefield2').isEmail(),
]

// execute them and check the result 
Promise.all(
  // empty `res` and `next`
  validators.map(v => v(req, {}, () => {}))
).then(() => {
  const errors = validationResult(req)
})
0reactions
fedecicommented, Dec 27, 2020

Closing this /cc @gustavohenke

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running validations imperatively - express-validator
Running validations imperatively. express-validator favors the declarative way of doing things that express middlewares bring. This means most of the APIs look ...
Read more >
A Clean Approach to Using Express Validator
I recently used express validator in a project and stumbled upon a few challenges which I'm going to share in this article.
Read more >
Reusing validation rules without request · Issue #828 - GitHub
One possible solution is to actually rewrite the code (which I don't want to do) to just use validator package. In that case,...
Read more >
How to use Express Validator | Getting Started with Express ...
Express -validator is an express middleware that provides us with validation and sanitization functions. Actually, Express validator is built on top of validator ......
Read more >
How to validate request body in Node.js, Express
Express.js doesn't have a built-in validator. But you can use express-validator or joi. Both of these libraries are good.
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