Skip to content

Instantly share code, notes, and snippets.

@andrewjmead
Last active April 21, 2016 00:41
Show Gist options
  • Select an option

  • Save andrewjmead/06a789641719856bc7af to your computer and use it in GitHub Desktop.

Select an option

Save andrewjmead/06a789641719856bc7af to your computer and use it in GitHub Desktop.
Rajesh - Validate description is string
// ...
// POST /todos
app.post('/todos', middleware.requireAuthentication, function(req, res) {
var body = _.pick(req.body, 'description', 'completed');
db.todo.create(body).then(function (todo) {
todo.setUser(req.user).then(function () {
res.json(todo);
});
}).catch(function (e) {
res.status(400).json(e);
});
});
// ...
var _ = require('underscore');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('todo', {
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1, 250]
}
},
completed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
validate: {
descriptionIsString: function() {
if (!_.isString(this.description)) {
throw new Error('Description must be string.')
}
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment