I was working on a personal project and I wanted to use ‘joi’ library for validation of my request body.
import * as Joi from 'joi';
const joiSchema= Joi.object({
ans: Joi.number().required().not(0),
}).unknown(true).keys();
const data = {
ans: 0,
};
async function validateData() {
try {
const value = await joiSchema.validateAsync(data);
console.log('Validation succeeded:', value);
} catch (err) {
console.error('Validation error:', err);
}
}
// Call the async function
validateData();
I have validation succeeded for ans:0 in this case but i have clearly mentioned that ans should not be zero , and infact the function validateData() gives validation succeeded even when i am passing string to it.
But when i remove keys() method from joiSchema I am getting the desired results.
I looked at the documentation also for keys() but could not find any satisfactory answers.