7

I tested the following Employee.js model in Sails.js and I found something that seems hard for me to understand.

When the form for creating a new Employee is posted, I got the following errors with the 1st definition of Employee.js model, but the 2nd definition of Employee.js works. So what's the difference between the two definitions of Employee.js?

Errors:

Error (E_VALIDATION) :: 3 attributes are invalid
    at WLValidationError.WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:33:18)
    at new WLValidationError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:45:43
    at allValidationsChecked (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:195:5)
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:186:14
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:157:64

Invalid attributes sent to Employee:
 • name
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null
 • email
   • `undefined` should be a email (instead of "null", which is a object)

   • "required" validation rule failed for input: null
 • password
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null

Definition 1: Employee.js

module.exports = {
  attributes: {

    name: {
      type: 'STRING',
    },

    email: {
      type: 'STRING',
      email: true,
    },

    password: {
      type: 'STRING',
    },

    toJSON: function() {
      var obj = this.toObject();

      return {
        name: obj.name,
        email: obj.email,
        password: obj.password
      }
    }

  }
};

Definition2: Employee.js

module.exports = {
  attributes: {

    name: 'STRING',
    email: 'STRING',
    password: 'STRING',

    toJSON: function() {
      var obj = this.toObject();

      return {
        name: obj.name,
        email: obj.email,
        password: obj.password
      }
    }

  }
};

The form for creating a new Employee is as following:

<form action="/signupemployee" method="POST">
  <table>
    <tr><td>Name</td><td><input type=”text” name=”name”></td></tr>
    <tr><td>Password</td><td><input type=”password” name=”password”></td></tr>
    <tr><td>Email</td><td><input type=”email” name=”email”></td></tr>
    <tr><td></td><td><input type="submit"></td>
  </table>
</form>
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
TonyW
  • 18,375
  • 42
  • 110
  • 183

2 Answers2

0

There is only one difference that should exist. In your second model the email attribute will not require a string that contains a valid email format. To my knowledge, definitions such as;

name: {
    type: "STRING"
}

and

name: "STRING"

Are intended to be equivalent, so this is potentially a bug in Waterline.

Although this question was asked a while ago it might still be worth pulling out an issue to ensure it gets addressed.

Ryan Loader
  • 185
  • 1
  • 8
0

This should work:

email: {
    type: 'email',
    required: true,
    unique: true
}
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
aug70co
  • 3,965
  • 5
  • 30
  • 44