How to validate an object dynamically using Joi in nodejs

28 views Asked by At
import Joi from "joi";


export const getFileLinkSchema = Joi.object({
    userId : Joi.string().required(), //TODO: Need to update
    fileId : Joi.string().required(),
    shareType : Joi.string().required(),
    shareAttributes: Joi.object({
        time: Joi.object({
            expiration:Joi.string()
        }).when('shareType',{is:"time",then:Joi.required()}),
        geoFence: Joi.object({
            latitude: Joi.number().required(),
            longitude: Joi.number().required()
        }).when('shareType',{is:"geoFence",then:Joi.required()})
    }).required()
});

The above is the schema i need to validate. Here if the shareType is time then i need the details in the shareAttribute if the shareType is "geoFence" then i need the latitude and longitude details. How to validate this using joi in nodejs

I tried the when() from joi but couldn't get the expected outcome

0

There are 0 answers