hapijs / joi

The most powerful data validation library for JS
Other
20.96k stars 1.51k forks source link

Joi validation passes even when provided with falsy values #2904

Closed ajimbong closed 1 year ago

ajimbong commented 1 year ago

Support plan

Context

Object validation passes even though I provide falsy values to the schema.validate() function.

How can we help?

I have an Object schema with two properties 'name & age' where name is a string with a minimum length of 5 characters and it is required, the age is a number and it's required too. If I validate this object {age: '2'} against this schema, it should return an error because I haven't provided the name and the age value is a string instead of a number. But after testing this, I don't receive any errors. Could there be something I am doing wrong?

const Joi = require('joi');

const schema = Joi.object({
    name: Joi.string().min(5).required(),
    age: Joi.number().required()
})

const {err, value} = schema.validate({age: '2'});

if(err) console.log(err)
console.log(value)
Marsup commented 1 year ago

Are you really using v13.1.0? It's a 5 years old version, you shouldn't do that. Also, it's not err but error.

ajimbong commented 1 year ago

Ohh that was my mistake, I should have used error instead of err. And the version I'm using is instead v17.7.0, mistakenly copied the version number of a different package in package.json.

Thank you very much for your feedback, everything is now working well.