javascript - How can i use Joi valid function and allow both lowercase and uppercase letters - Stack Overflow

Hello guys and thank you for your time.I am trying to create a validation middleware.I am using Joi t

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信