Skip to content

Commit

Permalink
add the error message to the code field.
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Nov 4, 2020
1 parent 823e8ed commit cfc17b6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 62 deletions.
111 changes: 63 additions & 48 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
"use strict";
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
Expand All @@ -26,50 +15,68 @@ var __read = (this && this.__read) || function (o, n) {
}
return ar;
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = exports.checkLength = exports.validSign = exports.filterData = void 0;
exports.toValue = exports.formatData = exports.parseBody = exports.parseData = exports.checkLength = exports.errorInfo = exports.validSign = exports.filterData = void 0;
var lodash_1 = require("lodash");
var rule_judgment_1 = require("rule-judgment");
var MD5 = require("md5.js");
function filterData(options, customize) {
return function (data) {
return function (data, errorCode) {
var e_1, _a;
var values = {};
try {
for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
var item = options_1_1.value;
var key = item.key, type = item.type, rules = item.rules, format = item.format, defaultValue = item.defaultValue, md5 = item.md5, separator = item.separator;
var value = data[key];
if (/\[\]$/.test(type) && !lodash_1.isArray(value)) {
value = toValue('string')(value || '').split(separator || /\,/);
var _loop_1 = function (item) {
var key = item.key, type = item.type, rules = item.rules, format = item.format, defaultValue = item.defaultValue, md5 = item.md5, separator = item.separator;
var value = data[key];
if (/\[\]$/.test(type) && !lodash_1.isArray(value)) {
value = toValue('string')(value || '').split(separator || /\,/);
}
if (/\[\]$/.test(type) && lodash_1.isArray(value)) {
var _a = __read(type.match(/(\S+)(\[\])$/), 2), itype = _a[1];
value = lodash_1.compact(value).map(toValue(itype));
if (rules) {
value.forEach(function (v) { return validateRule(rules || [], customize)(v, errorCode); });
}
if (/\[\]$/.test(type) && lodash_1.isArray(value)) {
var _b = __read(type.match(/(\S+)(\[\])$/), 2), itype = _b[1];
value = lodash_1.compact(value).map(toValue(itype));
rules && value.forEach(validateRule(rules, customize));
if (defaultValue && value.length === 0) {
value = defaultValue;
}
if (format) {
value = value.map(formatData(format, customize));
}
if (defaultValue && value.length === 0) {
value = defaultValue;
}
else {
value = toValue(type)(value);
rules && validateRule(rules, customize)(value);
value = value || defaultValue;
if (format) {
value = formatData(format, customize)(value);
}
if (md5) {
value = new MD5().update(lodash_1.template(md5)(values)).digest('hex');
}
if (format) {
value = value.map(formatData(format, customize));
}
lodash_1.set(values, key, value);
}
else {
value = toValue(type)(value);
if (rules) {
validateRule(rules, customize)(value, errorCode);
}
value = value || defaultValue;
if (format) {
value = formatData(format, customize)(value);
}
if (md5) {
value = new MD5().update(lodash_1.template(md5)(values)).digest('hex');
}
}
lodash_1.set(values, key, value);
};
try {
for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
var item = options_1_1.value;
_loop_1(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand All @@ -92,26 +99,26 @@ function validSign(options, sign) {
}
exports.validSign = validSign;
function validateRule(rules, customize) {
return function (value) {
return function (value, errorCode) {
var e_2, _a;
try {
for (var rules_1 = __values(rules), rules_1_1 = rules_1.next(); !rules_1_1.done; rules_1_1 = rules_1.next()) {
var rule = rules_1_1.value;
var required = rule.required, message = rule.message, min = rule.min, max = rule.max, pattern = rule.pattern, validator = rule.validator;
var required = rule.required, message = rule.message, min = rule.min, max = rule.max, pattern = rule.pattern, validator = rule.validator, code = rule.code;
if (required && (lodash_1.isUndefined(value) || value === '')) {
throw new Error(message);
throw errorInfo(message, code || errorCode);
}
if (lodash_1.isString(value)) {
if (min && checkLength(value) < min) {
throw new Error(message);
throw errorInfo(message, code || errorCode);
}
if (max && checkLength(value) > max) {
throw new Error(message);
throw errorInfo(message, code || errorCode);
}
if (pattern) {
var reg = getRegexp(pattern);
if (!reg.test(value)) {
throw new Error(message);
throw errorInfo(message, code || errorCode);
}
}
}
Expand All @@ -122,7 +129,7 @@ function validateRule(rules, customize) {
}
if (validator && lodash_1.isFunction(validator)) {
if (!validator(value) || String(value) === 'Invalid Date') {
throw new Error(message);
throw errorInfo(message, code || errorCode);
}
}
}
Expand All @@ -136,6 +143,14 @@ function validateRule(rules, customize) {
}
};
}
function errorInfo(message, code) {
var error = new Error(message);
if (code) {
error.code = code;
}
return error;
}
exports.errorInfo = errorInfo;
function checkLength(str) {
var e_3, _a;
var size = 0;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-string",
"version": "1.2.1",
"version": "1.2.2",
"description": "Parse the string into a Map.",
"main": "dist/index.js",
"typings": "types/index.d.ts",
Expand Down
41 changes: 29 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as MD5 from 'md5.js'
* @param options
* @param customize
*/
export function filterData (options: FilterData.options[], customize?: Record<string, Function>): (data: Record<string, any>) => Record<string, any> {
return (data: Record<string, any>) => {
export function filterData (options: FilterData.options[], customize?: Record<string, Function>): (data: Record<string, any>, errorCode?: number) => Record<string, any> {
return (data: Record<string, any>, errorCode?: number) => {
let values: Record<string, any> = {}
for (let item of options) {
let { key, type, rules, format, defaultValue, md5, separator } = item
Expand All @@ -20,7 +20,9 @@ export function filterData (options: FilterData.options[], customize?: Record<st
if (/\[\]$/.test(type) && isArray(value)) {
let [, itype] = type.match(/(\S+)(\[\])$/)
value = compact(value).map( toValue(itype as ParseData.parseType) )
rules && value.forEach( validateRule(rules, customize) )
if (rules) {
value.forEach( v => validateRule(rules || [], customize)(v, errorCode) )
}
if (defaultValue && value.length === 0) {
value = defaultValue
}
Expand All @@ -30,7 +32,9 @@ export function filterData (options: FilterData.options[], customize?: Record<st
}
else {
value = toValue(type as ParseData.parseType)(value)
rules && validateRule(rules, customize)(value)
if (rules) {
validateRule(rules, customize)(value, errorCode)
}
value = value || defaultValue
if (format) {
value = formatData(format, customize)(value)
Expand Down Expand Up @@ -62,24 +66,24 @@ export function validSign (options: string, sign: string = 'sign'): (data: Recor
* @param rules
* @param customize
*/
function validateRule (rules: FilterData.rule[], customize?: Record<string, Function>): (value: any) => void {
return (value: any) => {
function validateRule (rules: FilterData.rule[], customize?: Record<string, Function>): (value: any, errorCode?: number) => void {
return (value: any, errorCode?: number) => {
for (let rule of rules ) {
let { required, message, min, max, pattern, validator } = rule
let { required, message, min, max, pattern, validator, code } = rule
if (required && (isUndefined(value) || value === '')) {
throw new Error(message)
throw errorInfo(message, code || errorCode)
}
if (isString(value)) {
if (min && checkLength(value) < min) {
throw new Error(message)
throw errorInfo(message, code || errorCode)
}
if (max && checkLength(value) > max) {
throw new Error(message)
throw errorInfo(message, code || errorCode)
}
if (pattern) {
let reg = getRegexp(pattern)
if (!reg.test(value)) {
throw new Error(message)
throw errorInfo(message, code || errorCode)
}
}
}
Expand All @@ -90,13 +94,26 @@ function validateRule (rules: FilterData.rule[], customize?: Record<string, Func
}
if (validator && isFunction(validator)) {
if (!validator(value) || String(value) === 'Invalid Date') {
throw new Error(message)
throw errorInfo(message, code || errorCode)
}
}
}
}
}

/**
* 生成错误信息
* @param message
* @param code
*/
export function errorInfo (message: string, code?: number): FilterData.error {
let error: FilterData.error = new Error(message)
if (code) {
error.code = code
}
return error
}

/**
* 检测字符串长度,中文算2个字符
* @param str string
Expand Down
12 changes: 11 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export declare namespace FilterData {
message : string
min ?: number
max ?: number
code ?: number
}

interface error extends Error {
code ?: number
}
}

Expand All @@ -89,6 +94,11 @@ export function checkLength (str: string): number

export function filterData (options: FilterData.options[]): (data: Record<string, any>) => Record<string, any>
export function filterData (options: FilterData.options[], customize: Record<string, Function>): (data: Record<string, any>) => Record<string, any>
export function filterData (options: FilterData.options[]): (data: Record<string, any>, errorCode: number) => Record<string, any>
export function filterData (options: FilterData.options[], customize: Record<string, Function>): (data: Record<string, any>, errorCode: number) => Record<string, any>

export function validSign (options: string): (data: Record<string, any>) => boolean
export function validSign (options: string, sign: string): (data: Record<string, any>) => boolean
export function validSign (options: string, sign: string): (data: Record<string, any>) => boolean

export function errorInfo (message: string): FilterData.error
export function errorInfo (message: string, code: number): FilterData.error

0 comments on commit cfc17b6

Please sign in to comment.