From 0de2a0de1c3ed884a93236048141c555ec188b68 Mon Sep 17 00:00:00 2001 From: "bondarenko@itsirius.su" Date: Mon, 28 Nov 2016 12:54:01 +0300 Subject: [PATCH 1/4] set language --- lib/strategy.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/strategy.js b/lib/strategy.js index 24c5b6c..630339f 100644 --- a/lib/strategy.js +++ b/lib/strategy.js @@ -76,7 +76,20 @@ module.exports = { } else { rules = schema.rules; } - + + //set language + if (typeof schema.callback === 'function') { + try { + var validatorForLang = new Validator(); + schema.callback(validatorForLang); + } + catch (e) {} + if ( validatorForLang.hasOwnProperty('lang') ){ + var lang = validatorForLang['lang']; + Validator.useLang(lang); + } + } + var validator = new Validator(data, rules, schema.messages); // If a callback has been specified on the schema, call it to allow customisation of the validator From d80977e147622c2f05a5cf72a7ac19a596a833d9 Mon Sep 17 00:00:00 2001 From: "bondarenko@itsirius.su" Date: Mon, 28 Nov 2016 16:34:17 +0300 Subject: [PATCH 2/4] Update package.json. Set validatorjs 3.9.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2466349..14b7ba9 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Strategy for using validatorjs with react-validation-mixin", "main": "./lib/strategy.js", "dependencies": { - "validatorjs": "2.0.5" + "validatorjs": "3.9.0" }, "devDependencies": { "babel-cli": "^6.5.1", From ee5e93b424c9456d29ed4d92272d6136138cd702 Mon Sep 17 00:00:00 2001 From: "bondarenko@itsirius.su" Date: Tue, 20 Dec 2016 15:54:13 +0300 Subject: [PATCH 3/4] add test language --- package.json | 2 +- tests/strategySpec.js | 123 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 14b7ba9..2755f93 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "scripts": { "start": "npm install && npm run build && npm run watch", "watch": "onchange './lib/strategy.js' -- npm run build", - "test": "jasmine-node tests", + "test": "jasmine-node tests --verbose", "build": "npm run build:umd", "build:umd": "babel ./lib/strategy.js --plugins transform-es2015-modules-umd | uglifyjs -o ./dist/strategy.min.js --" }, diff --git a/tests/strategySpec.js b/tests/strategySpec.js index d839a64..f2fa7d1 100644 --- a/tests/strategySpec.js +++ b/tests/strategySpec.js @@ -241,3 +241,126 @@ describe('strategy', function() { }); }); }); + +describe('strategy validation client-side with an inactive schema and language', function () { + beforeEach(function () { + this.strategy = require('../lib/strategy'); + + this.rules = { + name: 'required', + email: 'required|email', + confirm_email: 'required|email' + }; + + this.messages = null; + + this.data = { + name: '', + email: 'not-an-email-address', + confirm_email: 'also-invalid' + }; + + this.validateCallback = jasmine.createSpy('validateCallback'); + }); + + it('ru', function () { + this.schemaCallback = function (validator) { + validator.lang = 'ru'; + } + this.schema = this.strategy.createInactiveSchema(this.rules, null, this.schemaCallback); + + this.strategy.validate( + this.data, + this.schema, + {}, + this.validateCallback + ); + + expect(this.validateCallback).toHaveBeenCalledWith({ + name: ["Поле name обязательно для заполнения."], + email: ["Поле email должно быть действительным электронным адресом."], + confirm_email: ["Поле confirm email должно быть действительным электронным адресом."] + }); + }); + + it('de', function () { + this.schemaCallback = function (validator) { + validator.lang = 'de'; + } + this.schema = this.strategy.createInactiveSchema(this.rules, null, this.schemaCallback); + + this.strategy.validate( + this.data, + this.schema, + {}, + this.validateCallback + ); + + expect(this.validateCallback).toHaveBeenCalledWith({ + name: ["Das name Feld muss ausgefüllt sein."], + email: ["Das email Format ist ungültig."], + confirm_email: ["Das confirm email Format ist ungültig."] + }); + }); + + it('es', function () { + this.schemaCallback = function (validator) { + validator.lang = 'es'; + } + this.schema = this.strategy.createInactiveSchema(this.rules, null, this.schemaCallback); + + this.strategy.validate( + this.data, + this.schema, + {}, + this.validateCallback + ); + + expect(this.validateCallback).toHaveBeenCalledWith({ + name: ["El campo name es obligatorio."], + email: ["El campo email no es un correo válido"], + confirm_email: ["El campo confirm email no es un correo válido"] + }); + }); + + it('fr', function () { + this.schemaCallback = function (validator) { + validator.lang = 'fr'; + } + this.schema = this.strategy.createInactiveSchema(this.rules, null, this.schemaCallback); + + this.strategy.validate( + this.data, + this.schema, + {}, + this.validateCallback + ); + + expect(this.validateCallback).toHaveBeenCalledWith({ + name: ["Le champs name est obligatoire."], + email: ["Le champs email contient un format invalide."], + confirm_email: ["Le champs confirm email contient un format invalide."] + }); + }); + + it('it', function () { + this.schemaCallback = function (validator) { + validator.lang = 'it'; + } + this.schema = this.strategy.createInactiveSchema(this.rules, null, this.schemaCallback); + + this.strategy.validate( + this.data, + this.schema, + {}, + this.validateCallback + ); + + expect(this.validateCallback).toHaveBeenCalledWith({ + name: ["Il campo name è richiesto."], + email: ["Il formato dell\'attributo email non è valido."], + confirm_email: ["Il formato dell\'attributo confirm email non è valido."] + }); + }); + +}); From 601198d7be8db262af170bde26357310f25f6ae5 Mon Sep 17 00:00:00 2001 From: "bondarenko@itsirius.su" Date: Tue, 20 Dec 2016 15:57:06 +0300 Subject: [PATCH 4/4] add test language --- tests/strategySpec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/strategySpec.js b/tests/strategySpec.js index f2fa7d1..3f9b4b0 100644 --- a/tests/strategySpec.js +++ b/tests/strategySpec.js @@ -252,8 +252,6 @@ describe('strategy validation client-side with an inactive schema and language', confirm_email: 'required|email' }; - this.messages = null; - this.data = { name: '', email: 'not-an-email-address',