Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test language. #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 --"
},
Expand Down
121 changes: 121 additions & 0 deletions tests/strategySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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."]
});
});

});