Saturday, May 29, 2021

Data validation using joi, syntax changed for validate

 We used static validate function of joi class.


But the syntax is changed now.


Reference: https://stackoverflow.com/questions/57956609/joi-1-default-validate-is-not-a-function


const Json = require("json");

old code:

function validateUser(user){

    const schema = {

        name: Joi.string().min(5).max(50).required(),

        email: Joi.string().min(5).max(255).required().email(),

        password: Joi.string().min(5).max(255).required()

    };

    return Json.validate(user,schema);//Json class static method

}


Latest code:

function validateUser(user){

//passed as Joi.object instead of generic json object above.

    const schema = Joi.object({

        name: Joi.string().min(5).max(50).required(),

        email: Joi.string().min(5).max(255).required().email(),

        password: Joi.string().min(5).max(255).required()

    });

    return schema.validate(user);//direct calling of schema object

}

No comments:

Post a Comment