lykmapipo / sails-hook-validation

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

Unable to show validation errors #4

Closed adavia closed 9 years ago

adavia commented 9 years ago

So i have been following all these steps in order to show the errors but i came up with nothing. Even when i try to access error.Errors i get undefined. So just to make sure im using sails 0.11 i have already set up the validations messages in my user model (same as the example) and then in my controller i did this:

module.exports = {
create: function(req, res, next) {
    var userObj = {
       username: req.param('username'),
       email: req.param('email')
    }

User.create(userObj).exec(function(err, user) {
 if (err) {
      expect(err.Errors.email).to.exist; // Get undefined email error here

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

    expect(err.Errors.email[1].message)
        .to.equal(User.validationMessages.email.required);

    expect(err.Errors.username).to.exist;
    expect(err.Errors.username[0].message)
        .to.equal(User.validationMessages.username.required);

    done();
    }
    if (user) {
        console.log("hi");
    }else{
    return res.json(404, {err: 'User not found.'});
    }
  });
},
lykmapipo commented 9 years ago

@adavia In order for sails-hook-validation to work you have to do the following:

module.exports = {
    attributes: {
        username: {
            type: 'string',
            required: true
        },
        email: {
            type: 'email',
            required: true,
            unique: true
        }
    },
    //model validation messages definitions
    validationMessages: { //hand for i18n & l10n
        email: {
            required: 'Email is required',
            email: 'Provide valid email address',
            unique: 'Email address is already taken'
        },
        username: {
            required: 'Username is required'
        }
    }
};

If problem persist

If problem persist, Am here to help.

adavia commented 9 years ago

It doesnt work as expected.. Do you have any sample project where you have already implemented this functionality?

lykmapipo commented 9 years ago

@adavia check repo spec. There is a sample model that is used.

If problem persist am here.

grd2345 commented 9 years ago

I tried this and it doesnt work at all. there must be something else that is missing.

lykmapipo commented 9 years ago

@adavia Please may you share with me your setup. May be I can assist you or I can know what is the issue.

grd2345 commented 9 years ago

i am on sails 11.

this is my model.

module.exports = {
  schema: true,
  attributes: {   
    locationId: {
      type: 'string',      
      required: true,
      index: true
    },
    membershipId: {
      type: 'string',      
      required: true
    },
    description: {
      type: 'string',      
      required: true
    },
    rate: {
      type: 'float',      
      required: true
    },
    unlimitedCredit: {
      type: 'boolean',
      required: true
    },
    credits: {
      type: 'float'
    },
    deduction: {
      type: 'float'
    },
    neverExpires: {
      type: 'boolean',
      required: true
    },
    quantity: {
      type: 'float'
    },
    span: {
      type: 'string'
    },
    taxable: {
      type: 'string',      
      required: true
    },
    status: {
      type: 'string',      
      required: true
    }
  },
  validationMessages: {
    description: {
        required: 'Membership Rate is required'
    }
  },
  beforeCreate: function(values,next) {   
    if (values.unlimitedCredit) {
      values.deduction = 0;
      values.credit = 0;
    }

    if (values.neverExpires)
      values.quantity = 0;

    next();  
  } 
};

when i call MembershipRate.create(), the error messages show the standard messages without the hook messages

lykmapipo commented 9 years ago

@adavia

For custom validation messages to work you will have to define custom message per model attribute per each validation rule defined on the attributes as below

module.exports = {
    attributes: {
        username: {
            type: 'string',
            required: true
        },
        email: {
            type: 'email',
            required: true,
            unique: true
        }
    },
    //validation messages definitions
    validationMessages: { //hand for i18n & l10n
        email: {
            required: 'Email is required',
            email: 'Provide valid email address',
            unique: 'Email address is already taken'
        },
        username: {
            required: 'Username is required'
        }
    }

};

Try that and tell if problem still exists

lykmapipo commented 9 years ago

@adavia

Let say i have a model as your where taxable is string and required. Validation messages for those rules on taxable will then be

...
validationMessages:{
  taxable:{
     required: 'Must be taxable',
     string: 'Must be a string'
  }
}
...

The whole idea behind sails-hook-validation is to enable custom error message definition per validation rules defined in the model attributes.

grd2345 commented 9 years ago

ah, ok, so each property I have on my model have to have a custom message? I can't just do one of them?

grd2345 commented 9 years ago

I tried another model with your suggestion and the hook still doesnt work. Here is my model.

module.exports = {
  schema: true,
  attributes: {   
    memberId: {
      type: 'string',      
      required: true,
      index: true
    },
    locationId: {
      type: 'string',
      index: true
    },
    type: {
      type: 'string',
      required: true
    },
    note: {
      type: 'string',
      required: true
    }
  },
  validationMessages: {
    memberId: {
      required: 'Member Id is required',
      string: 'Invalid member id'
    },
    locationId: {
      string: 'Invalid location Id'
    },
    type: {
      required: 'Note type is required',
      string: 'Invalid note type'
    },
    Note: {
      required: 'Note is required',
      string: 'Note is invalid'
    }
  }
};

Any suggestions

lykmapipo commented 9 years ago

@grd2345 Not all attributes must have the custom message.

The one that does not have custom message, if validation error occur there will be no custom message associate with them.

lykmapipo commented 9 years ago

@grd2345

If you look on sails-hook-validation package.json it depends on sails 0.11.0-rc10 and above

I have re-run the spec and still, I can not reproduce the problem you guys have.

But in simple, the repo has a sample model and it specification which are all passing. May you check or run them to validate you issue.

If issue persist, @grd2345 may you please initiate a repo and add your sample codes in it so that I can track what is going wrong.

grd2345 commented 9 years ago

I added a sample project to the following repo.

https://github.com/grd2345/testvalidation

lykmapipo commented 9 years ago

:+1:

Am on it.

Thanks.

grd2345 commented 9 years ago

thanks, I appreciate your help on this.

lykmapipo commented 9 years ago

@grd2345

I have update your sample and below are steps i took to show how sails-hook-validation works:

Just run

$ npm test 

after accept the pull request.

Hope it helps.

grd2345 commented 9 years ago

when running tests, states Note undefined. failing

On Wed, Mar 18, 2015 at 12:38 PM, lally elias notifications@github.com wrote:

@grd2345 https://github.com/grd2345

I have update your sample and below are steps i took to show how sails-hook-validation https://github.com/lykmapipo/sails-hook-validation works:

-

I installed sails-hook-validation https://github.com/lykmapipo/sails-hook-validation

$ npm install && npm install --save sails-hook-validation

-

I added some validations please check it https://github.com/lykmapipo/testvalidation/blob/master/api/models/note.js

And finaly i just add a simple spec to help you see how it works. See https://github.com/lykmapipo/testvalidation/blob/master/test/note.spec.js

Just run

$ npm test

after accept the pull request.

Hope it helps.

— Reply to this email directly or view it on GitHub https://github.com/lykmapipo/sails-hook-validation/issues/4#issuecomment-83077890 .

Dalum Software Solutions http://www.mygymsoftware.com Phone: 1-877-401-4567 Fax: 1-877-401-4567

lykmapipo commented 9 years ago

@grd2345 Rename you note.js model to Note.js that all

grd2345 commented 9 years ago

dang, sorry for that, i missed that for some reason. i understand it now and the issue i have been having was that if i console.log(err), it fails so I thought err.Errors were not there, but I see that they are there now. Do you have an example of using i18n with this?

On Wed, Mar 18, 2015 at 1:32 PM, lally elias notifications@github.com wrote:

@grd2345 https://github.com/grd2345 Rename you note.js model to Note.js that all

— Reply to this email directly or view it on GitHub https://github.com/lykmapipo/sails-hook-validation/issues/4#issuecomment-83113210 .

Dalum Software Solutions http://www.mygymsoftware.com Phone: 1-877-401-4567 Fax: 1-877-401-4567

lykmapipo commented 9 years ago

@grd2345 No problem.

I do not have any 18n sample but this sails internationalization will help you.

grd2345 commented 9 years ago

ok, i saw that, does the validationmessages addition grab the values at each request or on lift. reason being is with internationalization, each request can be a different locale. or should i just add the key as the message?

On Wed, Mar 18, 2015 at 2:27 PM, lally elias notifications@github.com wrote:

@grd2345 https://github.com/grd2345 No problem.

I do not have any 18n sample but this sails internationalization https://github.com/balderdashy/sails-docs/blob/master/concepts/Internationalization/Internationalization.md will help you.

— Reply to this email directly or view it on GitHub https://github.com/lykmapipo/sails-hook-validation/issues/4#issuecomment-83130304 .

Dalum Software Solutions http://www.mygymsoftware.com Phone: 1-877-401-4567 Fax: 1-877-401-4567

lykmapipo commented 9 years ago

@grd2345 May you please create another issue relate to 18n so that we can allow others to contribute

I will appreciate that

grd2345 commented 9 years ago

sure, thanks

On Wed, Mar 18, 2015 at 2:53 PM, lally elias notifications@github.com wrote:

@grd2345 https://github.com/grd2345 May you please create another issue relate to 18n so that we can allow others to contribute

I will appreciate that

— Reply to this email directly or view it on GitHub https://github.com/lykmapipo/sails-hook-validation/issues/4#issuecomment-83145000 .

Dalum Software Solutions http://www.mygymsoftware.com Phone: 1-877-401-4567 Fax: 1-877-401-4567