Reputation: 9530
After spending almost 2 days to investigate why the validation is not working, I've got to a point. Basically I found out that if my string has more than 40 chars, the validation will fail. If it has 40 or bellow it will work.
So now I am using validator.joi.string()
but I also try to fix this issue with : validator.joi.string().min(0).max(500)
but it doesn't seem to work.
Any solution to this?
Upvotes: 2
Views: 14515
Reputation: 510
I think the problem is with your validator initiation. If that fine then the following will work.
Joi.string().max(10)
Upvotes: 1
Reputation: 1995
Just was testing. Sorry for the wrong formatting.
But It does work fine.
const Joi = require('@hapi/joi');
const schema = Joi.object({
username: Joi.string()
.min(3)
.max(100)
.required(),
})
schema.validate({ username: 'abc' });
// -> { value: { username: 'abc' } }
schema.validate({});
// -> { value: {}, error: '"username" is required' }
// Also -
async function run(){
const value = await schema.validateAsync({ username: 'abcedeedsdsd sfdfghgdf sgfdghsfdsfdjgsfdgs shgdfshgdbshgdf sdhghsjfgfkhgj' });
console.log(value)
}
run();
Upvotes: 6