lykmapipo / sails-hook-validation

Custom validation error messages for sails model with i18n support
104 stars 29 forks source link

Can't see custom errors #11

Closed NoRKin closed 9 years ago

NoRKin commented 9 years ago

Hi again,

I understand that error.Errors is added to the response, but custom error messages are never being sent.

This is because sails will serialize the error object to JSON. It removes at the same time the custom message you added.

Please test the spec below to understand what I mean. I serialize to JSON and parse again and you will see error.Errors will disappear.

describe('Hook#create JSON serialization errors', function() {

        it('should throw unique error message after being serialized to JSON using stringify', function(done) {

            User
            .create({
                email: email,
                username: username,
                birthday: faker.date.past()
            }, function(error, user) {
                var errorJSON = JSON.stringify(error);
                error = JSON.parse(errorJSON);
                expect(error.Errors.email).to.exist;

                expect(error.Errors.email[0].message)
                .to.equal(User.validationMessages.email.unique);

                done();
            });
        });

        it('should throw unique error message after being serialized to JSON using toJSON', function(done) {

            User
            .create({
                email: email,
                username: username,
                birthday: faker.date.past()
            }, function(error, user) {
                var errorJSON = error.toJSON();
                error = JSON.parse(errorJSON);
                expect(error.Errors.email).to.exist;

                expect(error.Errors.email[0].message)
                .to.equal(User.validationMessages.email.unique);

                done();
            });
        });

    });
lykmapipo commented 9 years ago

@NoRKin I get you. But I can tell that, you are serializing the whole error object. I think you were supposed to give JSON.stringify a custom serializer. See JSON.stringify

Am reviewing sails-hook-validation codes too. May you please send a pull request with the above codes in test/serialization.spec.js.

Thanks.

lanhaoxiang commented 9 years ago

Hi @lykmapipo, I just got a problem while dealing with the validation errors in front end "sails": "~0.11.0", error.Errors field will never appear because the definition of WLValidationError (walterline validation error) are:

WLValidationError.prototype.toJSON =
WLValidationError.prototype.toPOJO =
function () {
  return {
    error: this.code,
    status: this.status,
    summary: this.reason,
    model: this.model,
    invalidAttributes: this.invalidAttributes
  };
}; 

Any ideas?

lykmapipo commented 9 years ago

@lanhaoxiang

Current custom error message have not been integrated to sails blueprint, thats why you are not able to receive error messages on your frontend.

You will have to override your blueprints for it to work.

lykmapipo commented 9 years ago

@lanhaoxiang

Am looking for a way to fix this by patching WLValidationError toJSON and toPOJO to allow for Errors or errors to be available when error is serialized

lykmapipo commented 9 years ago

@NoRKin and @lanhaoxiang

Custom Errors serialization is now available. Check for v0.4.0