lykmapipo/sails-hook-validation

bug in sails 0.12

mortezakarimi opened this issue · 6 comments

I think you need change your readme code to this in sails 0.12

User
    .create({}, function(error, user) {
        var expect = require('expect');
        //you will expect the following
        //error to exist on error.Errors based on 
        //your custom validation messages

        expect(error.invalidAttributes.email).toExist;

        expect(error.invalidAttributes.email[0].message)
          .toEqual(User.validationMessages.email.email);

        expect(error.invalidAttributes.email[1].message)
          .toEqual(User.validationMessages.email.required);


        expect(error.invalidAttributes.username).toExist;
        expect(error.invalidAttributes.username[0].message)
          .toEqual(User.validationMessages.username.required);

        done();
      });

But it still does not work properly for me I think the problem of "expect", because the message "Object.assert [as default]" is displayed on the console

@mortezakarimi

expect are just there to show you direction of what we real expect. They have nothing to do with your codes.

@mortezakarimi

In your codes you will end up having something like

User.create({}, function(error, user) {
  //your codes
});

I use your example files but still not working

@mortezakarimi

Can you send me a stacktrace of the error thrown or a scenario that cause a problem.

@lykmapipo
I hope that is right for you noticed
when i use your examples noting to see in console and in browser
If i use my code application stop and see this in console

/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:77
        throw err; // Rethrow non-MySQL errors
        ^

Error: Expected 'A record with that `email` already exists (`mortezak1373@gmail.com`).' to equal 'Provide valid email address'
    at Object.assert [as default] (/var/www/nodejs/khayyam_computer/node_modules/expect/lib/assert.js:20:9)
    at Expectation.toEqual (/var/www/nodejs/khayyam_computer/node_modules/expect/lib/Expectation.js:69:26)
    at /var/www/nodejs/khayyam_computer/api/controllers/SiteController.js:80:58
    at wrapper (/var/www/nodejs/khayyam_computer/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
    at module.exports.callback.wrappedCallback (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:331:16)
    at module.exports.callback.callback.error (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/normalize.js:42:31)
    at module.exports._switch (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/factory.js:56:28)
    at /var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:223:14
    at wrapper (/var/www/nodejs/khayyam_computer/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
    at module.exports.callback.wrappedCallback (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:331:16)
    at module.exports.callback.callback.error (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/normalize.js:42:31)
    at module.exports._switch (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/factory.js:56:28)
    at afterwards (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:87:16)
    at wrapper (/var/www/nodejs/khayyam_computer/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:417:80)
    at module.exports.callback.wrappedCallback (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:331:16)
    at module.exports.callback.callback.error (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/normalize.js:42:31)
    at module.exports._switch (/var/www/nodejs/khayyam_computer/node_modules/sails/node_modules/switchback/lib/factory.js:56:28)
    at sendBackError (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/lib/connections/spawn.js:97:11)
    at Object.module.exports.poolfully [as releaseConnection] (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/lib/connections/release.js:28:12)
    at /var/www/nodejs/khayyam_computer/node_modules/sails-mysql/lib/connections/spawn.js:90:37
    at Query._callback (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/lib/adapter.js:395:27)
    at Query.Sequence.end (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Query.ErrorPacket (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:94:8)
    at Protocol._parsePacket (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/var/www/nodejs/khayyam_computer/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:73:12)

Process finished with exit code 1

here is my code

subscribe: function (req, res) {

    Subscription.create({email: 'mortezak1373@gmail.com'}).exec(function (error, email) {
      var expect = require('expect');
      if (error) {
        //you will expect the following
        //error to exist on error.Errors based on
        //your custom validation messages
        //return res.json(error);
        expect(error.invalidAttributes.email).toExist;
      expect(error.invalidAttributes.email[0].message).toEqual(Subscription.validationMessages.email.email);

        return res.json(error);
      }
      return res.json(email);
    });
  }

Sorry for my bad English

error.invalidAttributes is set by the database adapter and not by this plugin.
Using Sails v0.12, err.Errors are not set at all (on validation error it is still undefined).
Running npm test on Sails v0.12.1 fails since error.Errors is undefined.

EDIT: This seems to be fixed with v0.4.5, great!