"optional" with parameters "checkFalsy", "nullable" seems not work again.
See original GitHub issueHello!
In data below, fields external_id
and external_system_id
may be null
or not present or contain proper value. So, i use checkFalsy: true
and nullable: true
but that not work and validation returns error:
{
: "errors":
: [
: : {
: : : "location":"body",
: : : "param":"external_id",
: : : "value":null,
: : : "msg":"Invalid value"
: : },
: : {
: : : "location":"body",
: : : "param":"external_system_id",
: : : "value":null,
: : : "msg":"Invalid value"
: : }
: ]
}
Data:
{
"id":"23efb1b1-c195-472c-b43e-f2c4ed26fa6e",
"external_id":null,
"external_system_id":null,
"name":"test"
}
{
"id":"23efb1b1-c195-472c-b43e-f2c4ed26fa6e",
"name":"test"
}
Rule (typescript):
export const checkSchema: ValidationSchema = {
id: {
in: ['body'],
isUUID: true
},
external_id: {
optional: {
checkFalsy: true,
nullable: true
},
in: ['body'],
isString: true
},
external_system_id: {
optional: {
checkFalsy: true,
},
in: ['body'],
isUUID: true
},
name: {
in: ['body'],
isString: true,
isLength: {
options: {
min: 1,
max: 100
},
errorMessage: 'Accepted length 1-100'
}
}
};
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Why is Express Validator not allowing null values in optional ...
On a hunch I tried reversing the method calls. This validator worked for me: body("date_field").isDate().optional({ nullable: true }),.
Read more >Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >Validation Chain API - express-validator
When the middleware runs, it will run each validator or sanitizer in the order ... checkNull : if true , fields with null...
Read more >Why is Express Validator not allowing null values in optional ...
I tried researching but couldn't find a good, simplistic solution, so I tried the following approach to allow null values in case of...
Read more >PHP 8.0: Deprecate required parameters after optional ...
PHP 8.0: Deprecate required parameters after optional parameters in function/method signatures. Version8.0. TypeDeprecation. When declaring a function or a ...
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
https://github.com/express-validator/express-validator/pull/693
Thanks matiasnl23, i did the same fix on my side last week and it works for my needs. I don’t known if it cover all possible situations.