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.

checkSchema - custom validator and exists errorMessage

See original GitHub issue

Hi!

I’m trying to use Schema Validation with 5.0.0.

  1. custom validator throw validation error validatorCfg.validator is not a function
  2. For exists param errorMessage ignored
checkSchema({
  title: {
    in: ['body'],
    exists: {
      errorMessage: 'title is required', // <- doesn't work
    },
    custom(value) { // <- doesn't work
      console.log(value);
      return true;
    }
  },
  password: {
    isLength: {
      errorMessage: 'Password should be at least 7 chars long', // <- works
    },
  },
});

customValidators param and all in this doc section https://github.com/ctavan/express-validator#legacy-api are deprecated…

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
gustavohenkecommented, Feb 17, 2018

Hm, the exists one is strange, by “doesn’t work” do you mean the error message is still Invalid value? It shouldn’t be, so there might be something else wrong with your setup.

The custom one should be specified by passing options. I will add an example to help clear future questions about it.

This is a very minimal app with both validators working:

const { checkSchema, validationResult } = require('./check');

const express = require('express');
const app = express();
app.use(express.json());
app.post('/*', checkSchema({
  foo: {
    in: ['body'],
    exists: {
      errorMessage: 'bla'
    },
    custom: {
      options: value => (value || '').startsWith('foo')
    }
  }
}), (req, res) => {
  res.json(validationResult(req).array());
});

app.listen(3000);
1reaction
gustavohenkecommented, Jun 29, 2018

Hi @prettymuchbryce, it’s out of the plans for the check APIs to have any interactions with the legacy ones (the expressValidator middleware you used), as they are gonna be phased out at some point. The less state we store in the request, the better.

What I would like to do instead is something like validatorResult.withDefaults(options): a factory for the check API that you can inject custom defaults, like validators:

// Use these, don't import them from `express-validator/check`!
const { check, checkSchema, validationResult, etc } = createCheckAPI({
  customSanitizers: { ... },
  customValidators: { ... },
  someOtherOption: value
})

For now, what you can do is move these validators or schemas (or whatever you like) into separate files, and import them in your schemas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

express-validator checkSchema not raise errors - Stack Overflow
I took a look at documentation and I saw that: app.post( '/user', body('username').isEmail(), body('password').isLength({ min: 5 }), (req, ...
Read more >
Schema Validation - express-validator
Schemas are a special, object-based way of defining validations or sanitizations on requests. At the root-level, you specify field paths as keys, and...
Read more >
express-validator.checkSchema JavaScript and Node.js code ...
Best JavaScript code snippets using express-validator. ... errorMessage: lang.t('errors.password.exists'), custom: { options: (password, { req }) => User.
Read more >
Form Data Validation in Node.js with express-validator
Let's now switch to the registration endpoint and cover tasks like custom validation rules, error messages, schema validation and ...
Read more >
How to perform custom validation in your Express.js app (Part-2)
Custom validators return Promises to show an async validation or throw any value/reject a promise to use a custom error message.
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