I'm trying to validate obj, look at the code below.
This doesn't work
const Joi = require('joi');
const schema = Joi.object({
cron: Joi.string(),
hourly: Joi.object({
minute: Joi.alternatives.try(Joi.number(), Joi.array())
})
});
const obj = {
hourly: {
minute: 1
}
};
const res = Joi.validate(obj, schema);
console.log(res);
This doesn't work too
const schema = Joi.object().keys({
cron: Joi.string(),
hourly: Joi.object({
minute: Joi.alternatives.try(Joi.number(), Joi.array())
})
});
The error
MBP:schema user$ node test.js
/path/server/schema/test.js:6
minute: Joi.alternatives.try(Joi.number(), Joi.array())
^
TypeError: Joi.alternatives.try is not a function
at Object.<anonymous> (/path/server/schema/test.js:6:33)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Joi validates obj successfully
{ error: null,
value: { hourly: { minute: 1 } },
then: [Function: then],
catch: [Function: catch] }
The short syntax works
const schema = Joi.object({
cron: Joi.string(),
hourly: Joi.object({
minute: [Joi.number(), Joi.array()]
})
});
Result
{ error: null,
value: { hourly: { minute: 1 } },
then: [Function: then],
catch: [Function: catch] }
You're missing the alternatives() method parens: Joi.alternatives.try() vs Joi.alternatives().try()
@WesTyler yeah, it is a typo. Thanks!
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
@WesTyler yeah, it is a typo. Thanks!