Unique validation does not work
isery opened this issue · 8 comments
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
@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.
@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.
@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?
@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
- We can patch the remain methods in
sails-hook-validation
and make sure it can make database error to use friendly messages - or update
waterline
validate cycle to includedatabase level validations
so that we can get error object prior anyCRUD method
calls.
I have been working with the first option.
Hope it helps
Thanks! I will check it out, i just don't know when ;)