1

I was working on my application and all was ok, but after reinstalling system to Windows 10 i always have an email validation fail. I don't really think this happens because of Windows 10, but it's only thing that was changed.

For testing i have created new shiny model:

module.exports = {
  attributes: {
    email: {
      type: 'string',
      required: true,
      unique: true,
      email: true
    }
  }
}

And if i start application with sails console and type TestModel.create({ email: 'alex@yahoo.com' }).exec(function(err, created) { console.log(err); console.log(created); }) i'll get something like this:

Error (E_VALIDATION) :: 1 attribute is invalid at WLValidationError.WLError (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLError.js:26:15) at new WLValidationError (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLValidationError.js:20:28) at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\query\validate.js:46:43 at allValidationsChecked (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:210:5) at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:49:16 at done (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:239:19) at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:40:16 at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:201:14 at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:49:16 at done (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:239:19) at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:40:16 at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:164:64 at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:162:20 at C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:230:13 at _arrayEach (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:81:9) at _each (C:\Users\My name is Alex\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:72:13)

Invalid attributes sent to TestModel: • email • "email" validation rule failed for input: 'alex@yahoo.com'

If i disable email validation, all works nice.

{ email: 'alex@yahoo.com',
  createdAt: '2015-08-08T21:09:25.118Z',
  updatedAt: '2015-08-08T21:09:25.118Z',
  id: 1 }

Own email validation method - isn't good solution.

I tried trivial stupid things like database reinstall, but it doesn't helps. Sorry for my bad english, hope i'll find answer here.

  • If you would like an additional resource in the future, here is a chat room for sails.js, node.js, and waterline questions. https://gitter.im/balderdashy/sails – Travis Webb Aug 12 '15 at 02:54

2 Answers2

7

I have one weird solution now. I changed attribute type to 'email' and deleted email: true type.

module.exports = {
  attributes: {
    email: {
      type: 'email',
      required: true,
      unique: true
    }
  }
};

And now it works. It's ok for me, but it is doesn't documented and i still want to know why default way doesn't works.

1

According to waterline docs:

Validations are defined directly in you Collection attributes. In addition you may set the attribute type to any supported Anchor type and Waterline will build a validation and set the schema type as a string for that attribute.

So when you set a type that is not one of the core types waterline will use it to validate the type in one of the validation rules supported by Anchor and keep the data as string

Therefore defining the schema like this will work:

module.exports = {
  attributes: {
    email: {
      type: 'email',
      required: true,
      unique: true
    }
  }
};
Ofer Herman
  • 3,018
  • 1
  • 20
  • 21