I’m puzzled with a validator while trying express.
An optional parameter I receive to my route, flags, should either be an integer or an array of integers.
The array of integers part is checked with the help of a custom validator chained after isArray()
.
Below is my validating code, and examples of data it lets in.
oneOf([
body("flags").isInt(),
body("flags")
.optional()
.isArray()
.custom((value) => {
//return value.every(Number.isInteger);
if (!value.every(Number.isInteger)) {
throw new Error('permissionFlags should contain Integers only');
}
return true;
}),
], {
message: 'it should be an integer or an array of integers',
}),
All the following sorts of values are being let in, however the first one isn’t an array of integers.
flags: [ 8, 1, 'WRONG_PERMISSION' ] // this is not an int, neither an array of integers.
flags: [ 8, 1 ]
flags: 8
A big thank in advance for a good advice.