From 362b7aa05954f0a5ca343324cd2daa208902eca1 Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 16:58:56 -0500 Subject: [PATCH 1/6] Made change to support the meta._req object for precise Locale per request --- lib/create.js | 15 ++++++++------- lib/createEach.js | 13 +++++++------ lib/findOrCreate.js | 6 ++++-- lib/findOrCreateEach.js | 6 ++++-- lib/update.js | 5 +++-- lib/validate.js | 6 ++++-- lib/validateCustom.js | 8 ++++---- 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lib/create.js b/lib/create.js index 31b9677..8d23ddd 100644 --- a/lib/create.js +++ b/lib/create.js @@ -4,6 +4,7 @@ //import sails waterline Deferred var Deferred = require('waterline/lib/waterline/query/deferred'); var WLValidationError = require('./WLValidationError'); +var _ = require('lodash'); /** * @description path sails `create()` method to allow @@ -21,15 +22,15 @@ module.exports = function(model, validateCustom) { //prepare new create method //which wrap sailsCreate //with custom error message checking - function create(values, callback) { - + function create(values, callback, meta) { + if (!meta) { meta = {}; } + // handle Deferred where // it passes criteria first // see https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/dql/create.js#L26 - if (arguments.length === 3) { - var args = Array.prototype.slice.call(arguments); - callback = args.pop(); - values = args.pop(); + if(_.isPlainObject(arguments[0]) && (_.isPlainObject(arguments[1]) || _.isArray(arguments[1]))) { + values = arguments[1]; + callback = arguments[2]; } // return Deferred @@ -54,7 +55,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object // when using associations diff --git a/lib/createEach.js b/lib/createEach.js index 9c16231..9a26849 100644 --- a/lib/createEach.js +++ b/lib/createEach.js @@ -19,14 +19,15 @@ module.exports = function(model, validateCustom) { var sailsCreate = model.createEach; //prepare new createEach method - function createEach(values, callback) { + function createEach(values, callback, meta) { + if (!meta) { meta = {}; } + // handle Deferred where // it passes criteria first // See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/aggregate.js#L27 - if (arguments.length === 3) { - var args = Array.prototype.slice.call(arguments); - callback = args.pop(); - values = args.pop(); + if(_.isPlainObject(arguments[0]) && (_.isPlainObject(arguments[1]) || _.isArray(arguments[1]))) { + values = arguments[1]; + callback = arguments[2]; } // return Deferred @@ -51,7 +52,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object // when using associations diff --git a/lib/findOrCreate.js b/lib/findOrCreate.js index a448199..0c67e44 100644 --- a/lib/findOrCreate.js +++ b/lib/findOrCreate.js @@ -19,7 +19,9 @@ module.exports = function(model, validateCustom) { var sailsFindOrCreate = model.findOrCreate; //prepare new findOrCreate method - function findOrCreate(criteria, values, callback) { + function findOrCreate(criteria, values, callback, meta) { + if (!meta) { meta = {}; } + // return Deferred // if no callback passed // See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/composite.js#L43 @@ -42,7 +44,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object when using associations if (Object.keys(customError).length !== 0) { diff --git a/lib/findOrCreateEach.js b/lib/findOrCreateEach.js index 963fd36..afd82a7 100644 --- a/lib/findOrCreateEach.js +++ b/lib/findOrCreateEach.js @@ -20,7 +20,9 @@ module.exports = function(model, validateCustom) { var sailsFindOrCreateEach = model.findOrCreateEach; //prepare new findOrCreateEach method - function findOrCreateEach(criterias, values, callback) { + function findOrCreateEach(criterias, values, callback, meta) { + if (!meta) { meta = {}; } + // return Deferred // if no callback passed // See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/aggregate.js#L96 @@ -43,7 +45,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object // when using associations diff --git a/lib/update.js b/lib/update.js index ce8a432..3aa4808 100644 --- a/lib/update.js +++ b/lib/update.js @@ -21,7 +21,8 @@ module.exports = function(model, validateCustom) { //prepare new update method //which wrap sailsUpdate //with custom error message checking - function update(criterias, values, callback) { + function update(criterias, values, callback, meta) { + if (!meta) { meta = {}; } // return Deferred // if no callback passed @@ -45,7 +46,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object // when using associations diff --git a/lib/validate.js b/lib/validate.js index 0543183..920abb5 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -16,7 +16,9 @@ module.exports = function(model, validateCustom) { var sailsValidate = model.validate; //prepare new validation method - function validate(values, presentOnly, callback) { + function validate(values, presentOnly, callback, meta) { + if (!meta) { meta = {}; } + if(typeof presentOnly === 'function'){ callback = presentOnly; presentOnly = null; @@ -33,7 +35,7 @@ module.exports = function(model, validateCustom) { //custom errors messages if (error.invalidAttributes) { var customError = - validateCustom(model, error.invalidAttributes); + validateCustom(model, error.invalidAttributes, meta._req); // will return and override with empty object // when using associations diff --git a/lib/validateCustom.js b/lib/validateCustom.js index 6998346..c22c5ee 100644 --- a/lib/validateCustom.js +++ b/lib/validateCustom.js @@ -13,11 +13,11 @@ * * @param {Object} model valid sails model definition * @param {Object} invalidAttributes a valid sails validation error object. + * @param {Object} [req] a request object - It's necessary to get the exact locale * * @returns {Object} an object with friendly validation error conversions. */ -module.exports = function(model, invalidAttributes) { - +module.exports = function(model, invalidAttributes, req) { //grab model validations definitions var validations = model._validator.validations || {}; @@ -70,8 +70,8 @@ module.exports = function(model, invalidAttributes) { if(sails.config.i18n){ //deduce locale from request else - //use default locale - locale = + //use default locale from config + locale = (typeof req !== 'undefined' && req.getLocale !== 'undefined') ? req.getLocale() : sails.config.i18n.requestLocale || sails.config.i18n.defaultLocale; From 27fb4278f403ffbdda76af2555cdc52e3d2df689 Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 17:15:18 -0500 Subject: [PATCH 2/6] Bump version to 0.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e1b62b3..9587bcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sails-hook-validation", - "version": "0.4.7", + "version": "0.5.0", "description": "Custom validation error messages for sails model with i18n support", "main": "index.js", "sails": { From 76f6a061c45daa7cf847653f1ec33c1422616fb9 Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 17:15:58 -0500 Subject: [PATCH 3/6] Bump version to 0.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e1b62b3..9587bcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sails-hook-validation", - "version": "0.4.7", + "version": "0.5.0", "description": "Custom validation error messages for sails model with i18n support", "main": "index.js", "sails": { From 3df0238c14b1cf0fe195311cce65dcf286743339 Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 17:24:54 -0500 Subject: [PATCH 4/6] Use globaly defined lodash or sails lodash --- lib/create.js | 3 +-- lib/createEach.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/create.js b/lib/create.js index 8d23ddd..f322325 100644 --- a/lib/create.js +++ b/lib/create.js @@ -4,7 +4,6 @@ //import sails waterline Deferred var Deferred = require('waterline/lib/waterline/query/deferred'); var WLValidationError = require('./WLValidationError'); -var _ = require('lodash'); /** * @description path sails `create()` method to allow @@ -28,7 +27,7 @@ module.exports = function(model, validateCustom) { // handle Deferred where // it passes criteria first // see https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/dql/create.js#L26 - if(_.isPlainObject(arguments[0]) && (_.isPlainObject(arguments[1]) || _.isArray(arguments[1]))) { + if((_ || sails.util._).isPlainObject(arguments[0]) && ((_ || sails.util._).isPlainObject(arguments[1]) || (_ || sails.util._).isArray(arguments[1]))) { values = arguments[1]; callback = arguments[2]; } diff --git a/lib/createEach.js b/lib/createEach.js index 9a26849..a035da6 100644 --- a/lib/createEach.js +++ b/lib/createEach.js @@ -25,7 +25,7 @@ module.exports = function(model, validateCustom) { // handle Deferred where // it passes criteria first // See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/aggregate.js#L27 - if(_.isPlainObject(arguments[0]) && (_.isPlainObject(arguments[1]) || _.isArray(arguments[1]))) { + if((_ || sails.util._).isPlainObject(arguments[0]) && ((_ || sails.util._).isPlainObject(arguments[1]) || (_ || sails.util._).isArray(arguments[1]))) { values = arguments[1]; callback = arguments[2]; } From 4834e53079105e497f6ef9189ac97e596b9ef55e Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 17:33:50 -0500 Subject: [PATCH 5/6] Republishing package --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9587bcf..267bfce 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "sails-hook-validation", + "name": "@floatplane/sails-hook-validation", "version": "0.5.0", "description": "Custom validation error messages for sails model with i18n support", "main": "index.js", @@ -12,7 +12,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/lykmapipo/sails-hook-validation.git" + "url": "https://github.com/fpm-git/sails-hook-validation.git" }, "keywords": [ "sails", @@ -41,9 +41,9 @@ }, "license": "MIT", "bugs": { - "url": "https://github.com/lykmapipo/sails-hook-validation/issues" + "url": "https://github.com/fpm-git/sails-hook-validation/issues" }, - "homepage": "https://github.com/lykmapipo/sails-hook-validation", + "homepage": "https://github.com/fpm-git/sails-hook-validation", "contributors": [{ "name": "lykmapipo", "github": "https://github.com/lykmapipo" From 24d3451bd08fc7450474a3a1fffbc19b3556d6a5 Mon Sep 17 00:00:00 2001 From: Alexandre-Jacques St-Jacques Date: Thu, 22 Feb 2018 17:38:21 -0500 Subject: [PATCH 6/6] Changed some part of the readme --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2ba7076..8f25dcd 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,17 @@ sails-hook-validation ===================== -[![Build Status](https://travis-ci.org/lykmapipo/sails-hook-validation.svg?branch=master)](https://travis-ci.org/lykmapipo/sails-hook-validation) +[![Build Status](https://travis-ci.org/fpm-git/sails-hook-validation.svg?branch=master)](https://travis-ci.org/fpm-git/sails-hook-validation) Custom validation error messages for sails model with i18n support. Its works with `callback`, `deferred` and `promise` style `model API` provided with sails. *Note:* -- *This requires Sails v0.11.0+. If v0.11.0+ isn't published to NPM yet, you'll need to install it via Github.* - *`sails-hook-validation` work by patch model static `validate()`, `create()`, `createEach()`, `findOrCreate()`, `findOrCreateEach()` and `update()`.* -- *To have custom error messages at model instance level consider using [sails-model-new](https://github.com/lykmapipo/sails-model-new).* - *`sails-hook-validation` opt to use `error.Errors` and not to re-create or remove any properties of error object so as to remain with sails legacy options* ## Installation ```sh -$ npm install --save sails-hook-validation +$ npm install --save '@floatplane/sails-hook-validation' ``` ## Usage