Last active
September 27, 2019 01:02
-
-
Save circa10a/e4f1ad479aa5a3adcc37721e3a9c7b4e to your computer and use it in GitHub Desktop.
example usage of joi object schema validation
This file contains hidden or 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({ | |
birthyear: Joi.number().integer().min(1900).max(2013), | |
test: Joi.string(), | |
}).without('birthyear', 'test'); | |
let result = Joi.validate({ birthyear: 2013 }, schema); | |
// No error | |
result.error ? console.log(result.error) : console.log('schema valid\n'); | |
// Intended error | |
result = Joi.validate({ birthyear: 2013, test: 'test' }, schema); | |
result.error ? console.log(JSON.stringify({ error:result.error.details[0].message })) : console.log('schema valid'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment