Last active
February 3, 2017 04:16
-
-
Save crizstian/18fbe1edd818377c544b5e6bd2654968 to your computer and use it in GitHub Desktop.
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 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