I am using express-validator to validate input to my API, but I have some problems understanding the matches function. I basically need to be able to figure out if a string matches any of the values in an array of accepted values, like shown below, but it doesn't seem to work. Any suggestions?
var schema = {
"role": {
in: 'body',
matches: {
options: ["administrator", "editor", "contributor", "user"],
errorMessage: "Invalid role"
}
}
}
req.check(schema)
The matches.options
constructs a regex. You can pass in your regex as the first element of the array. Try this:
var schema = {
"role": {
in: 'body',
matches: {
options: [/\b(?:administrator|editor|contributor|user)\b/],
errorMessage: "Invalid role"
}
}
}
As an alternative you can use this schema:
var schema = {
"role": {
in: 'body',
isIn: {
options: [["administrator", "editor", "contributor", "user"]],
errorMessage: "Invalid role"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With