Skip to content

Commit

Permalink
feat: I18n translate possible by override the validate.translate me…
Browse files Browse the repository at this point in the history
…thod.
  • Loading branch information
huacnlee committed Sep 14, 2015
1 parent 85779cb commit 16b9e5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ $ npm install parameter --save
- `validate.addRule(type, check)` - add custom rules.
- `type` - rule type, required and must be string type.
- `check` - check handler. can be a `function` or a `RegExp`.
- `validate.translate` - You can override the `translate` method to implement I18n.

### Example

```js
var validate = require('parameter');

validate.translate = function() {
var args = Array.prototype.slice.call(arguments);
// Assume there have I18n.t method for convert language.
return I18n.t.apply(this, args);
}

var data = {
name: 'foo',
age: 24,
Expand Down
66 changes: 39 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

'use strict';

var util = require('util');

/**
* Module exports
* @type {Function}
*/

module.exports = validate;
validate.addRule = addRule;
validate.translate = null;

/**
* Regexps
Expand Down Expand Up @@ -81,9 +84,9 @@ function validate(rules, obj) {
if (!has) {
if (rule.required !== false) {
errors.push({
message: key + ' required',
field: key,
code: 'missing_field'
message: t('%s required', key),
field: t(key),
code: t('missing_field')
});
}
continue;
Expand All @@ -98,17 +101,17 @@ function validate(rules, obj) {
var msg = checker(rule, obj[key], obj);
if (typeof msg === 'string') {
errors.push({
message: key + ' ' + msg,
code: 'invalid',
field: key
message: t('%s ' + msg, key),
code: t('invalid'),
field: t(key)
});
}

if (Array.isArray(msg)) {
msg.forEach(function (e) {
var dot = rule.type === 'object' ? '.' : '';
e.message = key + dot + e.message;
e.field = key + dot + e.field;
e.message = t('%s%s' + e.message, key, dot);
e.field = t('%s%s%s', key, dot, e.field);
errors.push(e);
});
}
Expand Down Expand Up @@ -183,15 +186,15 @@ function formatRule(rule) {

function checkInt(rule, value) {
if (typeof value !== 'number' || value % 1 !== 0) {
return 'should be an integer';
return t('should be an integer');
}

if (rule.hasOwnProperty('max') && value > rule.max) {
return 'should smaller than ' + rule.max;
return t('should smaller than %s', rule.max);
}

if (rule.hasOwnProperty('min') && value < rule.min) {
return 'should bigger than ' + rule.min;
return t('should bigger than %s', rule.min);
}
}

Expand All @@ -210,13 +213,13 @@ function checkInt(rule, value) {

function checkNumber(rule, value) {
if (typeof value !== 'number') {
return 'should be a number';
return t('should be a number');
}
if (rule.hasOwnProperty('max') && value > rule.max) {
return 'should smaller than ' + rule.max;
return t('should smaller than %s', rule.max);
}
if (rule.hasOwnProperty('min') && value < rule.min) {
return 'should bigger than ' + rule.min;
return t('should bigger than %s', rule.min);
}
}

Expand All @@ -237,24 +240,24 @@ function checkNumber(rule, value) {

function checkString(rule, value) {
if (typeof value !== 'string') {
return 'should be a string';
return t('should be a string');
}
var allowEmpty = rule.hasOwnProperty('allowEmpty')
? rule.allowEmpty
: rule.empty;

if (rule.hasOwnProperty('max') && value.length > rule.max) {
return 'length should smaller than ' + rule.max;
return t('length should smaller than %s', rule.max);
}
if (rule.hasOwnProperty('min') && value.length < rule.min) {
return 'length should bigger than ' + rule.min;
return t('length should bigger than %s', rule.min);
}

if (!allowEmpty && value === '') {
return 'should not be empty';
return t('should not be empty');
}
if (rule.format && !rule.format.test(value)) {
return rule.message || 'should match ' + rule.format;
return rule.message || t('should match %s', rule.format);
}
}

Expand Down Expand Up @@ -311,7 +314,7 @@ function checkDateTime(rule, value) {

function checkBoolean(rule, value) {
if (typeof value !== 'boolean') {
return 'should be a boolean';
return t('should be a boolean');
}
}

Expand All @@ -332,7 +335,7 @@ function checkEnum(rule, value) {
throw new TypeError('check enum need array type values');
}
if (rule.values.indexOf(value) === -1) {
return 'should be one of ' + rule.values.join(', ');
return t('should be one of %s', rule.values.join(', '));
}
}

Expand All @@ -348,7 +351,7 @@ function checkEnum(rule, value) {
function checkEmail(rule, value) {
return checkString({
format: EMAIL_RE,
message: rule.message || 'should be an email'
message: rule.message || t('should be an email')
}, value);
}

Expand All @@ -372,7 +375,7 @@ function checkPassword(rule, value, obj) {
return error;
}
if (rule.compare && obj[rule.compare] !== value) {
return 'should equal to ' + rule.compare;
return t('should equal to %s', rule.compare);
}
}

Expand All @@ -388,7 +391,7 @@ function checkPassword(rule, value, obj) {
function checkUrl(rule, value) {
return checkString({
format: URL_RE,
message: rule.message || 'should be a url'
message: rule.message || t('should be a url')
}, value);
}

Expand All @@ -406,7 +409,7 @@ function checkUrl(rule, value) {

function checkObject(rule, value) {
if (typeof value !== 'object') {
return 'should be an object';
return t('should be an object');
}

if (rule.rule) {
Expand Down Expand Up @@ -438,7 +441,7 @@ function checkObject(rule, value) {

function checkArray(rule, value) {
if (!Array.isArray(value)) {
return 'should be an array';
return t('should be an array');
}

if (!rule.itemType) {
Expand All @@ -464,7 +467,7 @@ function checkArray(rule, value) {
errors.push({
field: index,
message: index + ' ' + errs,
code: 'invalid'
code: t('invalid')
});
}
if (Array.isArray(errs)) {
Expand All @@ -478,3 +481,12 @@ function checkArray(rule, value) {

return errors;
}

function t() {
var args = Array.prototype.slice.call(arguments);
if (typeof validate.translate === 'function') {
return validate.translate.apply(this, args);
} else {
return util.format.apply(this, args);
}
}
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

var should = require('should');
var util = require('util');
var validate = require('..');

describe('parameter', function () {
Expand Down Expand Up @@ -574,4 +575,19 @@ describe('parameter', function () {
validate(rule, value)[0].message.should.equal('key should match /^prefix/');
});
});

describe('custom translate function', function(){
it('should work', function(){
var newValidate = require('..');
newValidate.translate = function() {
var args = Array.prototype.slice.call(arguments);
args[0] = args[0] + '-add.';
return util.format.apply(this, args);
};
var rule = { name: 'string' };
var error = newValidate(rule, {})[0];
error.message.should.equal('name required-add.');
error.code.should.equal('missing_field-add.');
});
});
});

0 comments on commit 16b9e5a

Please sign in to comment.