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.

Check schema optional options should include optional and checkFalsy

See original GitHub issue

After extensive searching (and checking the TypeScript types), I can’t find how to set the value of the optional validator and set checkFalsy to true at the same time using the checkSchema validation method.

The documentation says that a field’s validation can be made optional by including optional: true in the ValidationParamSchema, but so far as I can tell there isn’t a way to set optional to false and set checkFalsy to true. My request is to add a key to ValidatorOptions.OptionalOptions that allows the user to set whether or not the schema field validation should be optional, and if not optional, to check falsy values.

Here’s my setup:

// Passed as middleware into route

public static checkSearch: ValidationChain[] = [
        ...checkSchema({
            pageIndex: SearchValidator.pageIndex(),
            pageSize: SearchValidator.pageSize(),
            order: SearchValidator.order(),
            term: SearchValidator.term(true) // true is passed to indicate the field should be optional
        })
    ];
// Called in checkSchema()

public static term = (isOptional: boolean = false): ValidationParamSchema => {
        return {
            in: 'body',
            errorMessage: 'Term is invalid',
            optional: {
                KEY: isOptional // here I want to be able to set whether or not the field is optional
                checkFalsy: true,
            },
            isLength: {
                errorMessage: 'Term not provided',
                options: {
                    min: 1
                }
            }
        };
    }

Thanks for any ideas and help you can offer.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
gustavohenkecommented, Apr 25, 2018

What are you looking for exactly, @elboletaire? I see you want to make your field somehow optional. But it is not clear how.

Please note that notEmpty is not a validator on the check API yet. It is on the legacy API though. So, to check that a string has some chars in it, you should use isEmpty: { negated: true }.

And to make it optional, know that optional: true is telling express-validator to skip validation if the value is undefined. Setting checkFalsy: true will also skip validation when the value is any of the falsy ones in JS: undefined, null, false, 0 or '' (empty string).

I’m closing this issue since we don’t seem to have uncovered any bug.

1reaction
elboletairecommented, Apr 26, 2018

Sincerely? I’m not sure right now 😅

Everything started because I wanted just one validation to check two fields (I’ve ended up splitting the logic). Then the issue was just trying to find out the correct combination between optional and isEmpty, but I think that with what you said I’m able to go forward now.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Express validator - how to allow optional fields
You can use the optional method: req.check('notexist', 'This works').optional().isInt();. This won't work if the field is an empty string ...
Read more >
Validation Chain API - express-validator
options (optional): an object of options to customize the behavior of exists. Returns: the current validation chain instance. Adds a validator to check...
Read more >
express-validator - npm
message (optional): an error message to use when failed validators don't specify a message. Defaults to Invalid value . Returns: a Validation ...
Read more >
web-app/node_modules/express-validator - SFU CS Gitlab
Installation; Usage; Middleware options; Validation; Validation by schema ... The customValidators option can be used to add additional validation methods ...
Read more >
express-validator.ValidationChain.optional JavaScript and ...
isInt(), check('address').optional({ checkFalsy: true, nullable: true }). ... Function to run if user sends a PUT request router.put(['/', '/actions/fade'], ...
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