Skip to content

Commit 818cd23

Browse files
sarikefengmk2
andauthored
feat: custom error message (#68)
closes #31 closes #15 所有的 rule 中添加字段 “message",可以自定义错误信息,完全向前兼容。 rule 示例: ``` { type: 'int', max: 200, message: { max: '年龄不能大于 200 岁', required: '请填写你的年龄' } } ``` ``` { type: 'email', message: { email: '邮箱格式不正确', required: '前输入您的邮箱' } } ``` Co-authored-by: fengmk2 <fengmk2@gmail.com>
1 parent 12c51b3 commit 818cd23

3 files changed

Lines changed: 135 additions & 30 deletions

File tree

example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Parameter = require('./');
22

33
var rule = {
44
name: 'string',
5-
age: {type: 'int', max: 200},
5+
age: {type: 'int', max: 200, message: { max: '年龄不能大于 200 岁' }},
66
gender: ['male', 'female'],
77
working: 'boolean',
88
salary: {type: 'number', min: 0},
@@ -15,7 +15,7 @@ var rule = {
1515
required: false,
1616
rule: {
1717
name: 'string',
18-
age: 'int',
18+
age: { type: 'int', message: { required: '子女年龄不能为空' }},
1919
gender: ['male', 'female'],
2020
birthday: {type: 'date', required: false}
2121
}

index.js

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Parameter {
4444
}
4545
}
4646

47+
message(rule, key, defaultMessage) {
48+
return rule.message && rule.message[key] || defaultMessage;
49+
}
50+
4751
/**
4852
* validate
4953
*
@@ -89,7 +93,7 @@ class Parameter {
8993
if (!has) {
9094
if (rule.required !== false) {
9195
errors.push({
92-
message: this.t('required'),
96+
message: this.message(rule, 'required', this.t('required')),
9397
field: key,
9498
code: this.t('missing_field')
9599
});
@@ -254,6 +258,13 @@ function formatRule(rule) {
254258
rule.required = false;
255259
}
256260

261+
rule.message = rule.message || {}
262+
if (typeof rule.message === 'string') {
263+
rule.message = {
264+
[rule.type]: rule.message,
265+
}
266+
}
267+
257268
return rule;
258269
}
259270

@@ -317,15 +328,15 @@ function convert(rule, obj, key, defaultConvert) {
317328

318329
function checkInt(rule, value) {
319330
if (typeof value !== 'number' || value % 1 !== 0) {
320-
return this.t('should be an integer');
331+
return this.message(rule, 'int', this.t('should be an integer'));
321332
}
322333

323334
if (rule.hasOwnProperty('max') && value > rule.max) {
324-
return this.t('should smaller than %s', rule.max);
335+
return this.message(rule, 'max', this.t('should smaller than %s', rule.max));
325336
}
326337

327338
if (rule.hasOwnProperty('min') && value < rule.min) {
328-
return this.t('should bigger than %s', rule.min);
339+
return this.message(rule, 'min', this.t('should bigger than %s', rule.min));
329340
}
330341
}
331342

@@ -344,13 +355,13 @@ function checkInt(rule, value) {
344355

345356
function checkNumber(rule, value) {
346357
if (typeof value !== 'number' || isNaN(value)) {
347-
return this.t('should be a number');
358+
return this.message(rule, 'number', this.t('should be a number'));
348359
}
349360
if (rule.hasOwnProperty('max') && value > rule.max) {
350-
return this.t('should smaller than %s', rule.max);
361+
return this.message(rule, 'max', this.t('should smaller than %s', rule.max));
351362
}
352363
if (rule.hasOwnProperty('min') && value < rule.min) {
353-
return this.t('should bigger than %s', rule.min);
364+
return this.message(rule, 'min', this.t('should bigger than %s', rule.min));
354365
}
355366
}
356367

@@ -372,7 +383,7 @@ function checkNumber(rule, value) {
372383

373384
function checkString(rule, value) {
374385
if (typeof value !== 'string') {
375-
return this.t('should be a string');
386+
return this.message(rule, rule.type || 'string', this.t('should be a string'));
376387
}
377388

378389
// if required === false, set allowEmpty to true by default
@@ -386,18 +397,18 @@ function checkString(rule, value) {
386397

387398
if (!value) {
388399
if (allowEmpty) return;
389-
return this.t('should not be empty');
400+
return this.message(rule, 'allowEmpty', '') || this.message(rule, 'empty', '') || this.t('should not be empty');
390401
}
391402

392403
if (rule.hasOwnProperty('max') && value.length > rule.max) {
393-
return this.t('length should smaller than %s', rule.max);
404+
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max));
394405
}
395406
if (rule.hasOwnProperty('min') && value.length < rule.min) {
396-
return this.t('length should bigger than %s', rule.min);
407+
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min));
397408
}
398409

399410
if (rule.format && !rule.format.test(value)) {
400-
return rule.message || this.t('should match %s', rule.format);
411+
return this.message(rule, 'format', this.t('should match %s', rule.format));
401412
}
402413
}
403414

@@ -412,7 +423,10 @@ function checkString(rule, value) {
412423
*/
413424

414425
function checkId(rule, value) {
415-
return checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value);
426+
const errorMessage = checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value);
427+
if (errorMessage) {
428+
return this.message(rule, 'id', errorMessage);
429+
}
416430
}
417431

418432
/**
@@ -426,7 +440,10 @@ function checkId(rule, value) {
426440
*/
427441

428442
function checkDate(rule, value) {
429-
return checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
443+
const errorMessage = checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
444+
if (errorMessage) {
445+
return this.message(rule, 'date', errorMessage);
446+
}
430447
}
431448

432449
/**
@@ -440,7 +457,10 @@ function checkDate(rule, value) {
440457
*/
441458

442459
function checkDateTime(rule, value) {
443-
return checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
460+
const errorMessage = checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
461+
if (errorMessage) {
462+
return this.message(rule, 'dateTime', errorMessage);
463+
}
444464
}
445465

446466
/**
@@ -454,7 +474,7 @@ function checkDateTime(rule, value) {
454474

455475
function checkBoolean(rule, value) {
456476
if (typeof value !== 'boolean') {
457-
return this.t('should be a boolean');
477+
return this.message(rule, 'bool', '') || this.message(rule, 'boolean', '') || this.t('should be a boolean');
458478
}
459479
}
460480

@@ -475,7 +495,7 @@ function checkEnum(rule, value) {
475495
throw new TypeError('check enum need array type values');
476496
}
477497
if (rule.values.indexOf(value) === -1) {
478-
return this.t('should be one of %s', rule.values.join(', '));
498+
return this.message(rule, 'enum', this.t('should be one of %s', rule.values.join(', ')));
479499
}
480500
}
481501

@@ -489,11 +509,13 @@ function checkEnum(rule, value) {
489509
*/
490510

491511
function checkEmail(rule, value) {
492-
return checkString.call(this, {
512+
const errorMessage = checkString.call(this, {
493513
format: EMAIL_RE,
494-
message: rule.message || this.t('should be an email'),
495514
allowEmpty: rule.allowEmpty,
496515
}, value);
516+
if (errorMessage) {
517+
return this.message(rule, 'email', this.t('should be an email'));
518+
}
497519
}
498520

499521
/**
@@ -513,10 +535,10 @@ function checkPassword(rule, value, obj) {
513535
rule.format = PASSWORD_RE;
514536
var error = checkString.call(this, rule, value);
515537
if (error) {
516-
return error;
538+
return this.message(rule, 'password', error);
517539
}
518540
if (rule.compare && obj[rule.compare] !== value) {
519-
return this.t('should equal to %s', rule.compare);
541+
return this.message(rule, 'compare', this.t('should equal to %s', rule.compare));
520542
}
521543
}
522544

@@ -530,11 +552,13 @@ function checkPassword(rule, value, obj) {
530552
*/
531553

532554
function checkUrl(rule, value) {
533-
return checkString.call(this, {
555+
const error = checkString.call(this, {
534556
format: URL_RE,
535-
message: rule.message || this.t('should be a url'),
536557
allowEmpty: rule.allowEmpty
537558
}, value);
559+
if (error) {
560+
return this.message(rule, 'url', this.t('should be a url'));
561+
}
538562
}
539563

540564
/**
@@ -551,7 +575,7 @@ function checkUrl(rule, value) {
551575

552576
function checkObject(rule, value) {
553577
if (typeof value !== 'object') {
554-
return this.t('should be an object');
578+
return this.message(rule, 'object', this.t('should be an object'));
555579
}
556580

557581
if (rule.rule) {
@@ -583,14 +607,14 @@ function checkObject(rule, value) {
583607

584608
function checkArray(rule, value) {
585609
if (!Array.isArray(value)) {
586-
return this.t('should be an array');
610+
return this.message(rule, 'array', this.t('should be an array'));
587611
}
588612

589613
if (rule.hasOwnProperty('max') && value.length > rule.max) {
590-
return this.t('length should smaller than %s', rule.max);
614+
return this.message(rule, 'max', this.t('length should smaller than %s', rule.max));
591615
}
592616
if (rule.hasOwnProperty('min') && value.length < rule.min) {
593-
return this.t('length should bigger than %s', rule.min);
617+
return this.message(rule, 'min', this.t('length should bigger than %s', rule.min));
594618
}
595619

596620
if (!rule.itemType) {

0 commit comments

Comments
 (0)