New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
schema validation with array parameter throws odd error when a single element is passed #704
Comments
Try This,
|
There's a bit of legacy stuff here... standard validators (e.g. Whereas if you use a wildcard in the selector, but pass a string value, it will validate each of the string characters. I'll take a look if it's possible to fix this without causing a breaking change. In the mean time, suggestions are appreciated! |
@gustavohenke so, I think I have found a simple workaround/fix. app.get(
"/b",
checkSchema({
'foo' : {
toArray: true,
},
'foo.*': {
in: "query",
isAlphanumeric: true,
optional: true,
},
}),
handler
); And it seems to do the trick. And it actually works pretty well as it guarantees me an array on the other end (before, I had to check for a single value and put it in an array). |
@ericsnap your solution almost works for me! Sadly const querySchema = {
placement: { toArray: true },
'placement.*': {
in: 'query',
isIn: {
options: ['checkbox', 'flyout', 'main'],
},
optional: true,
},
};
router.post('/', [
checkSchema(querySchema),
], (req, res) => { ... } And when I make a call to {
"errors": [
{
"value": "flyout",
"msg": "Invalid value",
"param": "placement[1]",
"location": "query"
}
]
} EDIT: isIn: {
options: [['checkbox', 'flyout', 'main']],
}, |
Hi hi, https://github.com/express-validator/express-validator/releases/tag/v7.0.0 is out with a fix for this 🙂 |
As mentioned in this SO question, I was wondering how to handle optional, multi-value query parameters.
I want to check that the
foo
parameter is alphanumeric.The query
?foo=bar&foo=baz
yieldsreq.query.foo = ['bar', 'baz']
.If I want to use
express-validator
to ensure that all values passed are alphanumeric, I see some odd error messages.Here is my very simple code:
if I call it with:
curl 'localhost:8888/a?foo=barone&foo=bar%20two'
I get
{"foo":["barone","bar two"]}
because
/a
only validates 1 value offoo
.if I call it with:
curl 'localhost:8888/b?foo=barone&foo=bar%20two'
I get
{"errors":[{"location":"query","param":"foo[1]","value":"bar two","msg":"Invalid value"}]}
which is to be expected.
Unexpected behavior
However, things get wonky when I only pass a single value to
foo
.if call
curl 'localhost:8888/a?foo=bar%20two'
I get
{"errors":[{"location":"query","param":"foo","value":"bar two","msg":"Invalid value"}]}
which is correct
but if I call
curl 'localhost:8888/b?foo=bar%20two'
I get
{"errors":[{"location":"query","param":"foo[3]","value":" ","msg":"Invalid value"}]}
That last message is odd. I would expect:
param: 'foo'
param: 'foo[0]'
in the case of a single elementI put together a runkit notebook if you and to check it out live or hit the live endpoint.
The text was updated successfully, but these errors were encountered: