lykmapipo / sails-hook-validation

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

Made change to support the meta._req object for precise Locale per request #57

Open Maelstrom96 opened 6 years ago

Maelstrom96 commented 6 years ago

This is to actually fix https://github.com/lykmapipo/sails-hook-validation/issues/34

I've made a test where I had a policy that had a setTimeout for 3 seconds before actually going forward to the controller. I tried calling the route with a header "accept-language: en" and before the timeout actually finished, I tried doing the same request with a different accept-language value and the result from the first request would the return in the language of the last request accept-language header value.

This fix is kind of a last resort but I was not sure how to implement it while keeping the same transparent implementation. Instead, I used the "metaContainer" value that is usually found has the 3rd parameter for the different methods and if you populate it with { _req: req }, it will use the request locale, else if meta._req is not populated, it will fallback to the old method.

I've also changed the "handle deferred where" piece of code because it wouldn't work with the current implementation and just ported the code block from the sails 0.12 codebase.

I might also create some additional tests to prove my point and also check that it's not breaking the default implementation of all the model methods.