Skip to content

Instantly share code, notes, and snippets.

@brunoluiz
Last active August 24, 2017 03:58
Show Gist options
  • Save brunoluiz/51e42719be0b4c6d91759dc53169c79b to your computer and use it in GitHub Desktop.
Save brunoluiz/51e42719be0b4c6d91759dc53169c79b to your computer and use it in GitHub Desktop.
joi-from-doc.js
const Joi = require('joi')
const schema = Joi.object().keys({
a: Joi.any()
.valid('x')
.when('b', {
is: 5,
then: Joi.valid('y'),
otherwise: Joi.valid('z')
}),
b: Joi.any()
})
console.log(schema.validate({ a: 'x' }).error === null) // valid
console.log(schema.validate({ a: 'z' }).error === null) // valid
console.log(schema.validate({ a: 'z', b: 0 }).error === null) // valid
console.log(schema.validate({ a: 'y', b: 0 }).error !== null) // invalid
console.log(schema.validate({ a: 'y', b: 5 }).error === null) // valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment