A lib to automaticaly create joi validation schemas from sequelize models.
npm install joi-sequelizeA lib to automaticaly create joi validation schemas from sequelize models.

A lots of Hapi projects uses Sequelize to handle database conection, data modeling and manipulation, and Joi to validate its requests and responses. In this case is common to use quite the same schema for both libraries. Hence that it is a fertile scenario to create inconsistency between both schemas, that will be likely rearranged every time the database models change. So why not use the database to auto generate joi schemas, and remove the unnecessary validation in especifica routes. This is the main idea behind joi-sequelize.
``javascript
'use strict'; // jshint ignore:line
var config = require('../../config/config'),
bcrypt = require('bcrypt'),
salt = config.saltGen;
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
id: {
allowNull: false, / will generate a .required() on joi schema /
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
description: 'Users identifier' / will generate a .description() on joi schema tha can be used by swagger /
},
firstname: {
type: DataTypes.STRING(64), / will generate .string().max(64) /
allowNull: false,
description: 'Users first name's last name'
},
lastname: {
type: DataTypes.STRING(64), / will generate .string().max(64) /
allowNull: false,
description: 'User
},
email: {
type: DataTypes.STRING(64), / will generate .string().max(64) /
allowNull: false,
description: 'Users email's password'
},
password: {
type: DataTypes.STRING, / will generate .string() /
allowNull: false,
description: 'User
},
role: {
type: DataTypes.ENUM('admin', 'common user'), / will generate .valid('admin', 'common user') /
allowNull: false,
description: 'Users role'`
},
active: {
type: DataTypes.BOOLEAN, / will generate .boolean() /
allowNull: false
}
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return User;
};
`javascript
'use strict';
var fs = require('fs'),
path = require('path'),
Sequelize = require('sequelize'),
JoiSequelize = require('joi-sequelize'),
basename = path.basename(module.filename),
env = process.env.NODE_ENV || 'development',
log = (!process.env.LOG || process.env.LOG === 'false') ? false : true,
config = require(__dirname + '/../../config/database.json')[env],
db,
sequelize;
function init() {
db = {};
config.logging = (env === 'development' && log) ? console.log : false;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.JS = {};
fs
.readdirSync(__dirname)
.filter(file => (
(file.indexOf('.') !== 0) &&
(file !== basename) &&
(file.slice(-3) === '.js')
)
)
.forEach(function (file) {
var model = sequelize'import');
db[model.name] = model;
db.JS[model.name] = new JoiSequelize(require(path.join(__dirname, file)));
});
Object.keys(db).forEach(function (modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
Object.keys(db).forEach(function (modelName) {
if (db[modelName].addScopes) {
db[modelName].addScopes(db);
}
if (db[modelName].addHooks) {
db[modelName].addHooks(db);
}
});
return db;
}
module.exports = db || init();
`
`javascript
'use strict';
const Hapi = require('hapi');
const db = require('./model');
const JS = db.JS;
const server = new Hapi.Server();
server.connection({ port: 3000 });
server.route({
method: 'POST',
path: '/hello',
handler: (request, reply) => reply(request.payload),
config: {
validate: {
payload: JS.User.joi()
}
}
});
server.start((err) => {
if (err) throw err;
console.log('Server running at:', server.info.uri);
});
`
Return a joi object with all items except the ones passed as arguments
`javascript`
JS.User.omit('role', ...);
Return a joi object with all fields passed as arguments
`javascript`
JS.User.pick('active', ...);
Return a joi object with like JS.User.joi() but with field picture of joi type .any()
`javascript`
JS.User.include({picture: joi.any()});
as required
`javascript
JS.User.withRequired();
`$3
Same as withRequired but omiting the fields passed as arguments
`javascript
JS.User.withRequiredOmit('id', 'created_at', 'updated_at', 'deleted_at'); // useful on create routes payloads
`$3
Same as withRequired but omiting the fields passed as arguments
`javascript
JS.User.withRequiredPick('id'); // useful on get, update, delete routes with id param
``