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 diff --git a/package.json b/package.json index 2466349..2755f93 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", @@ -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..3f9b4b0 100644 --- a/tests/strategySpec.js +++ b/tests/strategySpec.js @@ -241,3 +241,124 @@ 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.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."] + }); + }); + +});