Have three parameters: latitude, longitude, zipcode
I need a joi validation that
Something like this?
Joi.object().keys({
latitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
longitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
zipcode: Joi.number().when(['latitude', 'longitude'], { is: undefined, then: Joi.required() })
});
I'm thinking there is a more elegant solution maybe using object.and()
You can validate multiple conditions in the following schema.
const schema = Joi.object().keys({ searchby: Joi.string().valid('phone', '_id', 'cno').required(), // field name searchvalue: Joi .when('searchby', { is: "phone", then: Joi.string().regex(/^(923)\d{9}$/, 'numbers').max(12).min(12).required() }) .when('searchby', { is: "_id", then: Joi.objectId().required() }) .when('searchby', { is: "nic", then: Joi.number().required() }) });
This solution may be useful:
schema = Joi.object().keys({
location: Joi.object().keys({
lat: Joi.number(),
long: Joi.number()
}).and('lat', 'long'),
timezone: Joi.alternatives()
.when('location', {
is: null,
then: Joi.number().required(),
otherwise: Joi.number()
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With