Check schema optional options should include optional and checkFalsy
See original GitHub issueAfter 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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 useisEmpty: { negated: true }
.And to make it optional, know that
optional: true
is telling express-validator to skip validation if the value isundefined
. SettingcheckFalsy: 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.
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
andisEmpty
, but I think that with what you said I’m able to go forward now.Thanks!