Skip to content

Instantly share code, notes, and snippets.

@circa10a
Last active September 27, 2019 01:02
Show Gist options
  • Save circa10a/e4f1ad479aa5a3adcc37721e3a9c7b4e to your computer and use it in GitHub Desktop.
Save circa10a/e4f1ad479aa5a3adcc37721e3a9c7b4e to your computer and use it in GitHub Desktop.
example usage of joi object schema validation
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