Last active
April 21, 2016 00:41
-
-
Save andrewjmead/06a789641719856bc7af to your computer and use it in GitHub Desktop.
Rajesh - Validate description is string
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
| // ... | |
| // 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); | |
| }); | |
| }); | |
| // ... |
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
| 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