paulalexandru
paulalexandru

Reputation: 9530

JS Joi Validation fails on string length

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

Answers (2)

Ayemun Hossain Ashik
Ayemun Hossain Ashik

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

xMayank
xMayank

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

Related Questions