Juan
Juan

Reputation: 139

Joi validation length number

I'm trying to do a length validation on a number, but it doesn't seem to work correctly.
This is my schema:

const schema = createJoiSchema({
  email: Joi.string().required(),
  password: Joi.string().required(),
  phone: Joi.number().integer().min(10).max(10).required(),    
})

This is my test object:

{
    "email":"[email protected]",
    "password":"miguel123",
    "phone": 1234456789
}

And I get this error:

"ValidationError: \"phone\" must be less than or equal to 10.

Upvotes: 1

Views: 4745

Answers (1)

PotatoParser
PotatoParser

Reputation: 1058

Since you use min and max on an integer, it should actually be between 10^9 (min has 10 digits) and 10^10 - 1 (max has 10 digits):

const schema = createJoiSchema({
  email: Joi.string().required(),
  password: Joi.string().required(),
  phone: Joi.number().integer().min(1000000000).max(9999999999).required(),
})

AKA

const schema = createJoiSchema({
  email: Joi.string().required(),
  password: Joi.string().required(),
  phone: Joi.number().integer().min(10**9).max(10**10 - 1).required(),
})

Upvotes: 2

Related Questions