不要怂,就是干,撸起袖子干!

You need to sign in or sign up before continuing.
  1. 18 Nov, 2011 1 commit
  2. 17 Nov, 2011 4 commits
  3. 16 Nov, 2011 6 commits
  4. 15 Nov, 2011 2 commits
  5. 14 Nov, 2011 4 commits
    • private method scoping + validations · b27e5512
      Sascha Depold committed
    • moved association specs to own file · 957893c6
      Sascha Depold committed
    • Added ability to supply custom validation methods. Each custom validation method… · 56237112
      … 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
    • scoped validation tests · 6ae3b957
      Sascha Depold committed
  6. 13 Nov, 2011 1 commit
  7. 12 Nov, 2011 6 commits
    • lol@english · 98bd1f44
      Sascha Depold committed
    • fixed travis notification · 75bffb1e
      Sascha Depold committed
    • Added validation to models using node-validator (https://github.com/chriso/node-validator). · 841ab0ce
      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
  8. 11 Nov, 2011 1 commit
  9. 10 Nov, 2011 9 commits
  10. 09 Nov, 2011 6 commits