Last active
August 24, 2017 03:58
-
-
Save brunoluiz/51e42719be0b4c6d91759dc53169c79b to your computer and use it in GitHub Desktop.
joi-from-doc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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