Hello guys and thank you for your time. I am trying to create a validation middleware. I am using Joi to validate the information i receive from the request body.
i would like to allow only the following strings for example: ['a','b','c'] but also allow both upper case and lowercase instances. (for example 'A' is also valid);
what i tried to do:
const schema = Joi.object().keys({
letter: Joi.string().valid('a','b','c').insensitive()
})
schema.validate({letter: 'B'}) //return error but i want it to return 'B'
I have also tried every bination of lowercase and uppercase functions with convert but i could not make it work. i would like that for example {letter: 'A'} would turn out valid.
is there a way to bine valid function with insensitive so that uppercase and lowercase will all get accepted?
Thank you for your time.
Hello guys and thank you for your time. I am trying to create a validation middleware. I am using Joi to validate the information i receive from the request body.
i would like to allow only the following strings for example: ['a','b','c'] but also allow both upper case and lowercase instances. (for example 'A' is also valid);
what i tried to do:
const schema = Joi.object().keys({
letter: Joi.string().valid('a','b','c').insensitive()
})
schema.validate({letter: 'B'}) //return error but i want it to return 'B'
I have also tried every bination of lowercase and uppercase functions with convert but i could not make it work. i would like that for example {letter: 'A'} would turn out valid.
is there a way to bine valid function with insensitive so that uppercase and lowercase will all get accepted?
Thank you for your time.
Share Improve this question asked Mar 5, 2020 at 15:18 Reuven Regev FaragReuven Regev Farag 511 gold badge1 silver badge2 bronze badges1 Answer
Reset to default 5I guess it is working fine. If you run the following code you will get {"letter": "b"}
const Joi = require("@hapi/joi");
const schema = Joi.object().keys({
"letter": Joi.string().valid("a", "b", "c").insensitive()
});
console.log(schema.validate({"letter": "B"}, {"convert": true}));
With convert false, you will get B in caps.
You could see running code here - https://repl.it/repls/HarmfulEvenPhases
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744198452a4562766.html
评论列表(0条)