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) }) }
validate(params, schema) { const res = this.validator.validate(params, schema); if (res.error) throw new ValidationError(res.error.message, null, res.error.details); return true; }
async function insert(params) { const user = joi.attempt(params, insertSchema) return db(tableName) .insert(user) .returning('*') .then(fp.first) }
function validationMiddlware(req) { return Joi.validate( req.payload.pattern.message, Joi.object() .keys({ from: Joi.string().required(), to: Joi.string().required(), subject: Joi.string().required(), text: Joi.string().optional(), html: Joi.string().optional(), bcc: Joi.string().optional(), cc: Joi.string().optional() }) .required() ) }
async function insertOrReplace(params) { const contribution = joi.attempt(params, insertSchema) const query = ` INSERT INTO :tableName: ("user", repository, line_count) VALUES (:user, :repository, :line_count) ON CONFLICT ("user", repository) DO UPDATE SET line_count = :line_count RETURNING *; ` return db.raw(query, Object.assign({ tableName }, contribution)) .then(fp.first) }
async function read(params) { const selection = joi.attempt(params, readSchema) return db(tableName) .where(selection) .select() .first() }
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) }) }
async function insert(params) { const repository = joi.attempt(params, insertSchema) return db(tableName) .insert(repository) .returning('*') .then(fp.first) }
async function insert(params) { const contribution = joi.attempt(params, insertSchema) return db(tableName) .insert(contribution) .returning('*') .then(fp.first) }