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

feat: custom error message #68

Merged
merged 5 commits into from
Mar 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Parameter = require('./');

var rule = {
name: 'string',
age: {type: 'int', max: 200},
age: {type: 'int', max: 200, message: { max: '年龄不能大于 200 岁' }},
gender: ['male', 'female'],
working: 'boolean',
salary: {type: 'number', min: 0},
Expand All @@ -15,7 +15,7 @@ var rule = {
required: false,
rule: {
name: 'string',
age: 'int',
age: { type: 'int', message: { required: '子女年龄不能为空' }},
gender: ['male', 'female'],
birthday: {type: 'date', required: false}
}
Expand Down
78 changes: 51 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Parameter {
}
}

message(rule, key, defaultMessage) {
return rule.message && rule.message[key] || defaultMessage;
}

/**
* validate
*
Expand Down Expand Up @@ -89,7 +93,7 @@ class Parameter {
if (!has) {
if (rule.required !== false) {
errors.push({
message: this.t('required'),
message: this.message(rule, 'required', this.t('required')),
field: key,
code: this.t('missing_field')
});
Expand Down Expand Up @@ -254,6 +258,13 @@ function formatRule(rule) {
rule.required = false;
}

rule.message = rule.message || {}
if (typeof rule.message === 'string') {
rule.message = {
[rule.type]: rule.message,
}
}

return rule;
}

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

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

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

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

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

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

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

function checkString(rule, value) {
if (typeof value !== 'string') {
return this.t('should be a string');
return this.message(rule, rule.type || 'string', this.t('should be a string'));
}

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

if (!value) {
if (allowEmpty) return;
return this.t('should not be empty');
return this.message(rule, 'allowEmpty', '') || this.message(rule, 'empty', '') || this.t('should not be empty');
}

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

if (rule.format && !rule.format.test(value)) {
return rule.message || this.t('should match %s', rule.format);
return this.message(rule, 'format', this.t('should match %s', rule.format));
}
}

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

function checkId(rule, value) {
return checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value);
const errorMessage = checkString.call(this, { format: ID_RE, allowEmpty: rule.allowEmpty }, value);
if (errorMessage) {
return this.message(rule, 'id', errorMessage);
}
}

/**
Expand All @@ -426,7 +440,10 @@ function checkId(rule, value) {
*/

function checkDate(rule, value) {
return checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
const errorMessage = checkString.call(this, { format: DATE_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
if (errorMessage) {
return this.message(rule, 'date', errorMessage);
}
}

/**
Expand All @@ -440,7 +457,10 @@ function checkDate(rule, value) {
*/

function checkDateTime(rule, value) {
return checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
const errorMessage = checkString.call(this, { format: DATETIME_TYPE_RE, allowEmpty: rule.allowEmpty }, value);
if (errorMessage) {
return this.message(rule, 'dateTime', errorMessage);
}
}

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

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

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

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

function checkEmail(rule, value) {
return checkString.call(this, {
const errorMessage = checkString.call(this, {
format: EMAIL_RE,
message: rule.message || this.t('should be an email'),
allowEmpty: rule.allowEmpty,
}, value);
if (errorMessage) {
return this.message(rule, 'email', this.t('should be an email'));
}
}

/**
Expand All @@ -513,10 +535,10 @@ function checkPassword(rule, value, obj) {
rule.format = PASSWORD_RE;
var error = checkString.call(this, rule, value);
if (error) {
return error;
return this.message(rule, 'password', error);
}
if (rule.compare && obj[rule.compare] !== value) {
return this.t('should equal to %s', rule.compare);
return this.message(rule, 'compare', this.t('should equal to %s', rule.compare));
}
}

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

function checkUrl(rule, value) {
return checkString.call(this, {
const error = checkString.call(this, {
format: URL_RE,
message: rule.message || this.t('should be a url'),
allowEmpty: rule.allowEmpty
}, value);
if (error) {
return this.message(rule, 'url', this.t('should be a url'));
}
}

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

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

if (rule.rule) {
Expand Down Expand Up @@ -583,14 +607,14 @@ function checkObject(rule, value) {

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

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

if (!rule.itemType) {
Expand Down
Loading