- 21 Nov, 2011 9 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
- 18 Nov, 2011 7 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
- 17 Nov, 2011 4 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
- 16 Nov, 2011 6 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
- 15 Nov, 2011 2 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
- 14 Nov, 2011 4 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
… recieves the field value to validate as its sole argument. It should throw an error to indicate a validation failure. For example: <pre> User = sequelize.define('User', { name: { type: Sequelize.STRING, validate: { len: { args: 9, msg: "Need atleast 9 chars" }, customFn1: function(val) { # if name doesn't start with 'joe' then fail if (val.substr(0,3) !== "joe") throw new Error("name should start with 'joe'") }, customFn2: function(val) { # if name doesn't end in 'bloggs' then fail if (val.substr(-6) !== "bloggs") throw new Error("name should end with 'bloggs'") } } } }); </pre> You still invoke validation as you normally would, i.e.: <pre> # build user var user = User.build({ name : "test" }); # validate errors = user.validate(); # At this point errors will contain: # # { name : [ # "Need atleast 9 chars" # "name should start with 'joe'" # "name should end with 'bloggs'" # ] } # </pre>
Ram committed -
Sascha Depold committed
-
- 13 Nov, 2011 1 commit
-
-
Ram committed
-
- 12 Nov, 2011 6 commits
-
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Sascha Depold committed
-
Ram committed
-
https://github.com/chriso/node-validator
To validate your models first define the validation for each field. For example: <pre> var User = sequelize.define 'User', { id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, }, name: { type: Sequelize.STRING, allowNull: false, unique: true, validate: { len: { args: 3, msg: "Name must be atleast 3 characters in length" } } }, email: { type: Sequelize.STRING, allowNull: false, unique: true, validate: { len: { args: [6, 128], msg: "Email address must be between 6 and 128 characters in length" }, isEmail: { msg: "Email address must be valid" } } }, password: { type: Sequelize.STRING, allowNull: false, validate: { len: { args: 3 } } } } </pre> Any of the basic validation methods provided by the node-validation can be specified along with arguments, as shown above. The 'msg' key specifies the error message to return for a given validation if it fails. If this isn't provided then a default generic error message will be returned by the validation library. To actually perform the validation, call the validate() method on your model instance once your model has been built. For example: <pre> # build user var user = User.build({ name : "test" email : "test" password : "test" }); # validate errors = user.validate(); if (errors) # errors is an object with the following structure: # { field name : [list of validation failure messages] for (var prop in errors) { if (errors.hasOwnProperty(prop)) console.log("Errors for field " + prop + ": "); for (var i=0; i<errors[prop].length; ++i) { console.log("\t" + errors[prop][i]); } } else # errors is null, which means validation succeeded </pre>
Ram committed
-
- 11 Nov, 2011 1 commit
-
-
Sascha Depold committed
-