lykmapipo / sails-hook-validation

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

Unique validation does not work #2

Closed isery closed 9 years ago

isery commented 9 years ago

Hey there,

I really like what you made! Also the code is really easy to read and well documented. Thanks for this!

The only thing which I couldn't get to work was unique validation.

Could it be that unique validation ist not checked in model.validate()???

see #1 for more information

lykmapipo commented 9 years ago

@isery Its true. I will appreciate a work around.

lykmapipo commented 9 years ago

@isery I have been playing with it.

Currently you may use sails-model-new which has abilty to check for a unique constraint at instance level. Check it spec

I have tried to implement at static/class level but somehow lead me on patching many of sails model core methods thats why i stoped.

lykmapipo commented 9 years ago

@isery If sails-model-new, solve you problem you may close the issue.

If not, I will accept your unique spec pull request and I will share a branch that we can work on to fix that and contribute back to waterline as it seems to be requested.

isery commented 9 years ago

@lykmapipo I like sails-model-new but it is not an option for me because I mostly work with the blueprint api.

Do you have any idea, where in the code the unique check happens? I guess its an error from the database right?

lykmapipo commented 9 years ago

@isery Yes its a database generated error.

With my current understand, it seems the unique validation is called at the moment create or createEach, or findOrCreate and findOrCreateEach are executed

You may check the following waterline validations algorithm:

Now if you check waterline create at line 174 there is a callback runner which invokes validate(), which means there is no unique constraint check up to this far as per above validations algorithms.

But if you check closely at line 213 the error returrned from adapter.create() is passed direct to callback, which i think is where the raised unique database constraint also get passed back.

I can conclude that, the unique constraint checkup are not performed at validate() cycle which implies sails-hook-validation which patch only validate() method is not able to catch database errors including unique constraint checkup.

How to fix this

I have been working with the first option.

Hope it helps

isery commented 9 years ago

Thanks! I will check it out, i just don't know when ;)

lykmapipo commented 9 years ago

@isery Am working on it...next release will have it.

lykmapipo commented 9 years ago

@isery check v0.2.0. It fixes the problem. Its already released and published to npm :+1: