Skip to content

Instantly share code, notes, and snippets.

@crizstian
Last active February 3, 2017 04:16
Show Gist options
  • Save crizstian/18fbe1edd818377c544b5e6bd2654968 to your computer and use it in GitHub Desktop.
Save crizstian/18fbe1edd818377c544b5e6bd2654968 to your computer and use it in GitHub Desktop.
const joi = require('joi')
const user = require('./user.model')(joi)
const booking = require('./booking.model')(joi)
const ticket = require('./ticket.model')(joi)
const schemas = Object.create({user, booking, ticket})
const schemaValidator = (object, type) => {
return new Promise((resolve, reject) => {
if (!object) {
reject(new Error('object to validate not provided'))
}
if (!type) {
reject(new Error('schema type to validate not provided'))
}
const {error, value} = joi.validate(object, schemas[type])
if (error) {
reject(new Error(`invalid ${type} data, err: ${error}`))
}
resolve(value)
})
}
module.exports = Object.create({validate: schemaValidator})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment