baja-lite 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -9,4 +9,3 @@ export * from './snowflake.js';
9
9
  export * from './sql.js';
10
10
  export * from './sqlite.js';
11
11
  export * from './string.js';
12
- export * from './validate.js';
package/index.js CHANGED
@@ -9,4 +9,3 @@ export * from './snowflake.js';
9
9
  export * from './sql.js';
10
10
  export * from './sqlite.js';
11
11
  export * from './string.js';
12
- export * from './validate.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/util-man",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "@msgpack/msgpack": "3.1.1",
39
39
  "@types/request-promise": "4.1.51",
40
40
  "axios": "1.8.4",
41
- "baja-lite-field": "1.3.2",
41
+ "baja-lite-field": "1.3.6",
42
42
  "decimal.js": "10.5.0",
43
43
  "html-parse-stringify": "3.0.1",
44
44
  "iterare": "1.2.1",
package/validate.d.ts DELETED
@@ -1,170 +0,0 @@
1
- type Patter = {
2
- 'upper-letter': patter;
3
- 'lower-letter': patter;
4
- letter: patter;
5
- 'letter-2': patter;
6
- chinese: patter;
7
- zonecode: patter;
8
- qq: patter;
9
- wechat: patter;
10
- email: patter;
11
- mobile: patter;
12
- tel: patter;
13
- allphone: patter;
14
- license: patter;
15
- organ: patter;
16
- bank: patter;
17
- float: patter;
18
- string: patter;
19
- plus: patter;
20
- 'plus-int': patter;
21
- 'plus-float': patter;
22
- minus: patter;
23
- 'minus-int': patter;
24
- 'minus-float': patter;
25
- 'un-plus': patter;
26
- 'un-plus-int': patter;
27
- 'un-plus-float': patter;
28
- 'un-minus': patter;
29
- 'un-minus-int': patter;
30
- 'un-minus-float': patter;
31
- 'id-card': patter;
32
- number: patter;
33
- number2: patter;
34
- int: patter;
35
- price: patter;
36
- 'price-2': patter;
37
- 'price-3': patter;
38
- 'price-4': patter;
39
- 'price-5': patter;
40
- 'rebate-1': patter;
41
- 'rebate-10': patter;
42
- 'rebate-100': patter;
43
- url: patter;
44
- post: patter;
45
- };
46
- export interface ValidateProps {
47
- label?: string;
48
- prop?: string;
49
- required?: boolean;
50
- mkType?: 'string' | 'number' | 'boolean';
51
- mkRealProp?: string | (() => string);
52
- mkLabel?: string;
53
- mkTrigger?: 'blur' | 'change';
54
- mkEq?: string | number | (() => string | number);
55
- mkNe?: string | number | (() => string | number);
56
- mkLt?: string | number | (() => string | number);
57
- mkLe?: string | number | (() => string | number);
58
- mkGe?: string | number | (() => string | number);
59
- mkGt?: string | number | (() => string | number);
60
- mkEqProp?: string | (() => string);
61
- mkNeProp?: string | (() => string);
62
- mkLtProp?: string | (() => string);
63
- mkLeProp?: string | (() => string);
64
- mkGeProp?: string | (() => string);
65
- mkGtProp?: string | (() => string);
66
- mkEqLength?: number | (() => number);
67
- mkNeLength?: number | (() => number);
68
- mkLtLength?: number | (() => number);
69
- mkLeLength?: number | (() => number);
70
- mkGeLength?: number | (() => number);
71
- mkGtLength?: number | (() => number);
72
- mkEqLengthProp?: string | (() => string);
73
- mkNeLengthProp?: string | (() => string);
74
- mkLtLengthProp?: string | (() => string);
75
- mkLeLengthProp?: string | (() => string);
76
- mkGeLengthProp?: string | (() => string);
77
- mkGtLengthProp?: string | (() => string);
78
- mkEnum?: string[] | (() => string[]);
79
- mkInlay?: keyof Patter;
80
- mkPattern?: RegExp;
81
- mkCustom?: <T>(value: any, prop?: string, form?: T) => Promise<Error | null>;
82
- mkTrim?: boolean;
83
- mkIdCard?: boolean;
84
- }
85
- export declare class ValidateItem<FormType> {
86
- private props;
87
- private data;
88
- private value;
89
- constructor(validItem: ValidateProps, data: FormType);
90
- get length(): number;
91
- get realLabel(): string;
92
- get emptyValue(): boolean;
93
- myValue(): any;
94
- isEmptyValue(vl: any): boolean;
95
- getLength(vl: any): number;
96
- valueConvert(value: any): any;
97
- getValue(key: any): any;
98
- getProp(prop: any): any;
99
- getValueLength(key: any): number;
100
- getPropLength(prop: any): number;
101
- patternFn(value: any, [ms, p]: patter): void;
102
- required(): Promise<void>;
103
- mkEnum(): Promise<void>;
104
- mkInlay(): Promise<void>;
105
- mkPattern(): Promise<void>;
106
- mkCustom(): Promise<Error | null>;
107
- mkEq(): Promise<void>;
108
- mkNe(): Promise<void>;
109
- mkLt(): Promise<void>;
110
- mkLe(): Promise<void>;
111
- mkGt(): Promise<void>;
112
- mkGe(): Promise<void>;
113
- mkEqProp(): Promise<void>;
114
- mkNeProp(): Promise<void>;
115
- mkLtProp(): Promise<void>;
116
- mkLeProp(): Promise<void>;
117
- mkGtProp(): Promise<void>;
118
- mkGeProp(): Promise<void>;
119
- mkEqLength(): Promise<void>;
120
- mkNeLength(): Promise<void>;
121
- mkLtLength(): Promise<void>;
122
- mkLeLength(): Promise<void>;
123
- mkGtLength(): Promise<void>;
124
- mkGeLength(): Promise<void>;
125
- mkEqLengthProp(): Promise<void>;
126
- mkNeLengthProp(): Promise<void>;
127
- mkLtLengthProp(): Promise<void>;
128
- mkLeLengthProp(): Promise<void>;
129
- mkGtLengthProp(): Promise<void>;
130
- mkGeLengthProp(): Promise<void>;
131
- mkIdCard(): Promise<void>;
132
- element(): {
133
- validator: (_rule: any, value: any, callback: (e?: Error) => void, _source: any, _op: any) => void;
134
- trigger: "blur" | "change" | undefined;
135
- }[];
136
- wot(): {
137
- required: boolean | undefined;
138
- validator: (val: any) => Promise<unknown[]>;
139
- }[];
140
- service(): Promise<void>;
141
- }
142
- export declare class ValidateForm<FormType> {
143
- private valids;
144
- constructor(valids: ValidForm<FormType>, data: FormType);
145
- element(): {
146
- [k: string]: {
147
- validator: (_rule: any, value: any, callback: (e?: Error) => void, _source: any, _op: any) => void;
148
- trigger: "blur" | "change" | undefined;
149
- }[];
150
- };
151
- wot(): {
152
- [k: string]: {
153
- required: boolean | undefined;
154
- validator: (val: any) => Promise<unknown[]>;
155
- }[];
156
- };
157
- service(): Promise<void>;
158
- }
159
- type Paths<T, ParentPath extends string = ''> = {
160
- [K in keyof T]: T[K] extends Record<string, any> | Array<any> ? ParentPath extends '' ? Paths<T[K], `${K & string}`> : `${ParentPath}.${Paths<T[K], K & string>}` : ParentPath extends '' ? K & string : `${ParentPath}.${K & string}`;
161
- }[keyof T];
162
- type ArrayPaths<T, ParentPath extends string> = T extends Array<infer U> ? U extends Record<string, any> ? `${ParentPath}.${number}` | `${ParentPath}.${number}.${Paths<U>}` : `${ParentPath}.${number}` : never;
163
- type AllPaths<T> = T extends Record<string, any> ? {
164
- [K in keyof T]: T[K] extends Array<infer U> ? `${K & string}` | ArrayPaths<T[K], K & string> : T[K] extends Record<string, any> ? `${K & string}` | `${K & string}.${Paths<T[K]>}` : `${K & string}`;
165
- }[keyof T] : never;
166
- type ValidForm<FormType> = Partial<{
167
- [K in AllPaths<FormType>]: ValidateProps;
168
- }>;
169
- type patter = [string, RegExp];
170
- export {};
package/validate.js DELETED
@@ -1,380 +0,0 @@
1
- import LGet from 'lodash.get';
2
- const patterns = {
3
- 'upper-letter': [' is not capital letters', /^[A-Z]+$/],
4
- 'lower-letter': [' is not lowercase letters', /^[a-z]+$/],
5
- letter: [' is not letters', /^[a-zA-Z]+$/],
6
- 'letter-2': [' is not two letters', /^[a-zA-Z]{2}$/],
7
- chinese: [' is not chinese', /^[\u0391-\uFFE5]+$/],
8
- zonecode: [' is wong postal code', /^\d{6}$/],
9
- qq: [' is wong qq', /^[1-9]\d{4,}$/],
10
- wechat: [' is wong wechat', /^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$/],
11
- email: [' is wong email', /^([0-9A-Za-z\-_.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/],
12
- mobile: [' is wong mobile', /^1(3|4|5|6|7|8)\d{9}$/],
13
- tel: [' is wong tel', /^0\d{2,3}-?\d{7,8}$/],
14
- allphone: [' is wong tel', /^1(3|4|5|6|7|8)\d{9}$|(^0\d{2,3}-?\d{7,8}$)/],
15
- license: [' is wong license', /^[A-Za-z0-9]{15}$|^[A-Za-z0-9]{18}$/],
16
- organ: [' is wong organ', /^[0-9A-Za-z]{8}\-[0-9A-Za-z]$/],
17
- bank: [' is wong bank', /^(\d{15}|\d{16}|\d{17}|\d{18}|\d{19})$/],
18
- float: [' need float', /^-?(\d+\.\d+)$/],
19
- string: [' enter and space not allow', /^[^\n\r\t]$/],
20
- plus: [' need positive numbers', /^\d*[1-9]\d*$|^\d+\.\d*[0-9]\d*$/],
21
- 'plus-int': [' need positive integers', /^\d*[1-9]\d*$/],
22
- 'plus-float': [' need positive decimals', /^\d+\.\d*[0-9]\d*$/],
23
- minus: [' need negative numbers', /^-\d*[1-9]\d*$|^-\d+\.\d*[0-9]\d*$/],
24
- 'minus-int': [' need negative integers', /^-\d*[1-9]\d*$/],
25
- 'minus-float': [' need negative decimals', /^-\d+\.\d*[0-9]\d*$/],
26
- 'un-plus': [' need zero or negative number', /^(-[0-9]\d*|-\d+\.\d+|0[0]*|0\.[0]+)$/],
27
- 'un-plus-int': [' need zero or negative integer', /^(-[0-9]\d*|0[0]*)$/],
28
- 'un-plus-float': [' need zero or negative decimals', /^(-\d+\.\d+|0[0]*|0\.[0]+)$/],
29
- 'un-minus': [' need zero or positive numbe', /^([0-9]\d*|\d+\.\d+|0[0]*)$/],
30
- 'un-minus-int': [' need zero or positive integer', /^([0-9]\d*)$/],
31
- 'un-minus-float': [' need zero or decimal', /^(\d+\.\d+|0[0]*|0\.[0]+)$/],
32
- 'id-card': [' is wong idCard', /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/],
33
- number: [' is not number', /^[1-9]\d*$|^0\.\d*[0-9]\d*$|^[1-9]\d*\.\d*[0-9]\d*$|^-[1-9]\d*$|^-0\.\d*[0-9]\d*$|^-[1-9]\d*\.\d*[0-9]\d*$/],
34
- number2: [' is not number', /^[1-9]\d*$|^0$|^0\.\d*[0-9]\d*$|^[1-9]\d*\.\d*[0-9]\d*$|^-[1-9]\d*$|^-0\.\d*[0-9]\d*$|^-[1-9]\d*\.\d*[0-9]\d*$/],
35
- int: [' is not int', /^[1-9]\d*$|^-[1-9]\d*$/],
36
- price: ['只能是正数且最多两位小数', /^([0-9]\d*|\d+\.\d{1,2}|0[0]*)$/],
37
- 'price-2': ['金额大于0且最多两位小数', /^([1-9]\d*(\.\d{1,2})?)$|(0\.\d{1,2})$/],
38
- 'price-3': ['只能是正数且最多3位小数', /^([0-9]\d*|\d+\.\d{1,3}|0[0]*)$/],
39
- 'price-4': ['只能是正数且最多4位小数', /^([0-9]\d*|\d+\.\d{1,4}|0[0]*)$/],
40
- 'price-5': ['只能是正数且最多5位小数', /^([0-9]\d*|\d+\.\d{1,5}|0[0]*)$/],
41
- 'rebate-1': ['只能在0到1之间且最多两位小数', /^(0|1|(0\.([0-9]){1,2}))$/],
42
- 'rebate-10': ['只能在0到10之间且最多两位小数', /^((([0-9])\.([0-9]){1,2})$)|(^([1-9]|10)$)/],
43
- 'rebate-100': ['只能在0到100之间且最多两位小数', /^((([0-9]){1,2}\.([0-9]){1,2})$)|(^([1-9]{1,2}|10|100)$)/],
44
- url: ['网址格式错误', /http(s)*:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!#]*([^<>\"\"])*$/],
45
- post: ['邮编格式错误', /^\d{6}$/],
46
- };
47
- const ms = {
48
- required: (realLabel) => `${realLabel} is required`,
49
- mkEq: (vl, realLabel) => `${realLabel} must equal to ${vl}`,
50
- mkNe: (vl, realLabel) => `${realLabel} cannot equal to ${vl}`,
51
- mkLt: (vl, realLabel) => `${realLabel} must less than ${vl}`,
52
- mkLe: (vl, realLabel) => `${realLabel} must less than or equal to ${vl}`,
53
- mkGt: (vl, realLabel) => `${realLabel} must greater than ${vl}`,
54
- mkGe: (vl, realLabel) => `${realLabel} must greater than or equal to ${vl}`,
55
- mkEqLength: (vl, realLabel) => `${realLabel} must ${vl} words`,
56
- mkNeLength: (vl, realLabel) => `${realLabel} cannot ${vl} words`,
57
- mkLtLength: (vl, realLabel) => `${realLabel} must less than ${vl} words`,
58
- mkLeLength: (vl, realLabel) => `${realLabel} at most ${vl} words`,
59
- mkGtLength: (vl, realLabel) => `${realLabel} must more than ${vl} words`,
60
- mkGeLength: (vl, realLabel) => `${realLabel} must least ${vl} words`,
61
- mkEnum: (vl, realLabel) => `${realLabel} Can only be one of ${vl.join(',')}`,
62
- mkPattern: (realLabel) => `${realLabel} error format`,
63
- };
64
- const defProps = {
65
- mkType: 'string',
66
- mkTrigger: 'change',
67
- mkTrim: true
68
- };
69
- async function runSequentially(promiseFactories) {
70
- const results = [];
71
- try {
72
- for (const createPromise of promiseFactories) {
73
- const result = await createPromise();
74
- results.push(result);
75
- }
76
- return results;
77
- }
78
- catch (error) {
79
- throw error;
80
- }
81
- }
82
- export class ValidateItem {
83
- constructor(validItem, data) {
84
- this.props = Object.assign({}, defProps, validItem);
85
- this.data = data;
86
- }
87
- get length() {
88
- return this.getLength(this.value);
89
- }
90
- get realLabel() {
91
- return this.props.mkLabel || this.props.label || '';
92
- }
93
- get emptyValue() {
94
- return this.isEmptyValue(this.value);
95
- }
96
- myValue() {
97
- return this.getValue(this.props.prop);
98
- }
99
- isEmptyValue(vl) {
100
- return this.value === '' || this.value === undefined || this.value === null;
101
- }
102
- getLength(vl) {
103
- return this.isEmptyValue(vl) ? 0 : `${vl}`.length;
104
- }
105
- valueConvert(value) {
106
- switch (this.props.mkType) {
107
- case 'string':
108
- return this.isEmptyValue(value) ? '' : this.props.mkTrim ? `${value}`.trim() : `${value}`;
109
- case 'number':
110
- return this.isEmptyValue(value) ? null : value - 0;
111
- case 'boolean':
112
- return this.isEmptyValue(value) ? null : value === 'true' || value === true;
113
- default:
114
- return value;
115
- }
116
- }
117
- getValue(key) {
118
- return this.valueConvert(typeof key === 'function' ? key() : key);
119
- }
120
- getProp(prop) {
121
- let path = typeof prop === 'function' ? prop() : this.isEmptyValue(prop) ? '' : prop;
122
- if (!path) {
123
- return '';
124
- }
125
- if (path.includes(':')) {
126
- path = path.replace(/:/g, '.');
127
- }
128
- const v = LGet(this.data, path);
129
- return this.valueConvert(v);
130
- }
131
- getValueLength(key) {
132
- return this.getLength(this.getValue(key));
133
- }
134
- getPropLength(prop) {
135
- return this.getLength(this.getProp(prop));
136
- }
137
- patternFn(value, [ms, p]) {
138
- if (this.isEmptyValue(value) !== true && p.test(value) === false) {
139
- throw new Error(ms);
140
- }
141
- }
142
- async required() {
143
- if (this.props.required === true && this.emptyValue === true) {
144
- throw new Error(ms.required(this.realLabel));
145
- }
146
- }
147
- async mkEnum() {
148
- const target = (typeof this.props.mkEnum === 'function' ? this.props.mkEnum() : this.props.mkEnum);
149
- if (target.includes(this.value) === false) {
150
- throw new Error(ms.mkEnum(target, this.realLabel));
151
- }
152
- }
153
- async mkInlay() {
154
- if (patterns.hasOwnProperty(this.props.mkInlay) === true) {
155
- this.patternFn(this.value, patterns[this.props.mkInlay]);
156
- }
157
- }
158
- async mkPattern() {
159
- if (this.props.mkPattern.test(this.value) !== true) {
160
- throw new Error(ms.mkPattern(this.realLabel));
161
- }
162
- }
163
- async mkCustom() {
164
- return await this.props.mkCustom(this.value, this.props.prop, this.data);
165
- }
166
- async mkEq() {
167
- const target = this.getValue(this.props.mkEq);
168
- if (this.value !== target) {
169
- throw new Error(ms.mkEq(target, this.realLabel));
170
- }
171
- }
172
- async mkNe() {
173
- const target = this.getValue(this.props.mkNe);
174
- if (this.value === target) {
175
- throw new Error(ms.mkNe(target, this.realLabel));
176
- }
177
- }
178
- async mkLt() {
179
- const target = this.getValue(this.props.mkLt);
180
- if (this.value >= target) {
181
- throw new Error(ms.mkLt(target, this.realLabel));
182
- }
183
- }
184
- async mkLe() {
185
- const target = this.getValue(this.props.mkLe);
186
- if (this.value > target) {
187
- throw new Error(ms.mkLe(target, this.realLabel));
188
- }
189
- }
190
- async mkGt() {
191
- const target = this.getValue(this.props.mkGt);
192
- if (this.value <= target) {
193
- throw new Error(ms.mkGt(target, this.realLabel));
194
- }
195
- }
196
- async mkGe() {
197
- const target = this.getValue(this.props.mkGe);
198
- if (this.value < target) {
199
- throw new Error(ms.mkGe(target, this.realLabel));
200
- }
201
- }
202
- async mkEqProp() {
203
- const target = this.getProp(this.props.mkEqProp);
204
- if (this.value !== target) {
205
- throw new Error(ms.mkEq(target, this.realLabel));
206
- }
207
- }
208
- async mkNeProp() {
209
- const target = this.getProp(this.props.mkNeProp);
210
- if (this.value === target) {
211
- throw new Error(ms.mkNe(target, this.realLabel));
212
- }
213
- }
214
- async mkLtProp() {
215
- const target = this.getProp(this.props.mkLtProp);
216
- if (this.value >= target) {
217
- throw new Error(ms.mkLt(target, this.realLabel));
218
- }
219
- }
220
- async mkLeProp() {
221
- const target = this.getProp(this.props.mkLeProp);
222
- if (this.value > target) {
223
- throw new Error(ms.mkLe(target, this.realLabel));
224
- }
225
- }
226
- async mkGtProp() {
227
- const target = this.getProp(this.props.mkGtProp);
228
- if (this.value <= target) {
229
- throw new Error(ms.mkGt(target, this.realLabel));
230
- }
231
- }
232
- async mkGeProp() {
233
- const target = this.getProp(this.props.mkGeProp);
234
- if (this.value < target) {
235
- throw new Error(ms.mkGe(target, this.realLabel));
236
- }
237
- }
238
- async mkEqLength() {
239
- const target = this.getValueLength(this.props.mkEqLength);
240
- if (this.value !== target) {
241
- throw new Error(ms.mkEqLength(target, this.realLabel));
242
- }
243
- }
244
- async mkNeLength() {
245
- const target = this.getValueLength(this.props.mkNeLength);
246
- if (this.value === target) {
247
- throw new Error(ms.mkNeLength(target, this.realLabel));
248
- }
249
- }
250
- async mkLtLength() {
251
- const target = this.getValueLength(this.props.mkLtLength);
252
- if (this.value >= target) {
253
- throw new Error(ms.mkLtLength(target, this.realLabel));
254
- }
255
- }
256
- async mkLeLength() {
257
- const target = this.getValueLength(this.props.mkLeLength);
258
- if (this.value > target) {
259
- throw new Error(ms.mkLeLength(target, this.realLabel));
260
- }
261
- }
262
- async mkGtLength() {
263
- const target = this.getValueLength(this.props.mkGtLength);
264
- if (this.value <= target) {
265
- throw new Error(ms.mkGtLength(target, this.realLabel));
266
- }
267
- }
268
- async mkGeLength() {
269
- const target = this.getValueLength(this.props.mkGeLength);
270
- if (this.value < target) {
271
- throw new Error(ms.mkGeLength(target, this.realLabel));
272
- }
273
- }
274
- async mkEqLengthProp() {
275
- const target = this.getPropLength(this.props.mkEqLengthProp);
276
- if (this.value !== target) {
277
- throw new Error(ms.mkEqLength(target, this.realLabel));
278
- }
279
- }
280
- async mkNeLengthProp() {
281
- const target = this.getPropLength(this.props.mkNeLengthProp);
282
- if (this.value === target) {
283
- throw new Error(ms.mkNeLength(target, this.realLabel));
284
- }
285
- }
286
- async mkLtLengthProp() {
287
- const target = this.getPropLength(this.props.mkLtLengthProp);
288
- if (this.value >= target) {
289
- throw new Error(ms.mkLtLength(target, this.realLabel));
290
- }
291
- }
292
- async mkLeLengthProp() {
293
- const target = this.getPropLength(this.props.mkLeLengthProp);
294
- if (this.value > target) {
295
- throw new Error(ms.mkLeLength(target, this.realLabel));
296
- }
297
- }
298
- async mkGtLengthProp() {
299
- const target = this.getPropLength(this.props.mkGtLengthProp);
300
- if (this.value <= target) {
301
- throw new Error(ms.mkGtLength(target, this.realLabel));
302
- }
303
- }
304
- async mkGeLengthProp() {
305
- const target = this.getPropLength(this.props.mkGeLengthProp);
306
- if (this.value < target) {
307
- throw new Error(ms.mkGeLength(target, this.realLabel));
308
- }
309
- }
310
- async mkIdCard() {
311
- if (this.props.mkIdCard === true) {
312
- // 15位身份证号码正则表达式
313
- const regex15 = /^[1-9]\d{5}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$/;
314
- // 18位身份证号码正则表达式
315
- const regex18 = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/;
316
- const value = this.value;
317
- // 首先检查是否符合15位或18位的正则表达式
318
- if (regex15.test(value)) {
319
- return;
320
- }
321
- else if (regex18.test(value)) {
322
- // 18位身份证号码需要进一步校验校验位
323
- const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
324
- const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
325
- let sum = 0;
326
- for (let i = 0; i < 17; i++) {
327
- sum += parseInt(value[i]) * factor[i];
328
- }
329
- const mod = sum % 11;
330
- const checkDigit = value[17].toUpperCase();
331
- if (checkDigit !== parity[mod]) {
332
- throw new Error('身份证号码错误');
333
- }
334
- }
335
- throw new Error('身份证号码错误');
336
- }
337
- }
338
- element() {
339
- return [{
340
- validator: (_rule, value, callback, _source, _op) => {
341
- this.value = value;
342
- runSequentially(Object.keys(this.props).filter(key => this.props[key] !== undefined && this[key] && typeof this[key] === 'function').map(key => () => this[key]())).catch(error => callback(error)).then(() => callback());
343
- }, trigger: this.props.mkTrigger
344
- }];
345
- }
346
- wot() {
347
- return [{
348
- required: this.props.required,
349
- validator: (val) => {
350
- this.value = val;
351
- return runSequentially(Object.keys(this.props).filter(key => this.props[key] !== undefined && this[key] && typeof this[key] === 'function').map(key => () => this[key]()));
352
- }
353
- }];
354
- }
355
- async service() {
356
- this.value = this.getProp(this.props.mkRealProp || this.props.prop);
357
- for (const key in this.props) {
358
- if (this.props[key] !== undefined && this[key] && typeof this[key] === 'function') {
359
- await this[key]();
360
- }
361
- }
362
- }
363
- }
364
- export class ValidateForm {
365
- constructor(valids, data) {
366
- this.valids = Object.fromEntries(Object.entries(valids).map(([k, v]) => ([k, new ValidateItem(v, data)])));
367
- }
368
- element() {
369
- return Object.fromEntries(Object.entries(this.valids).map(([k, v]) => ([k, v.element()])));
370
- }
371
- wot() {
372
- return Object.fromEntries(Object.entries(this.valids).map(([k, v]) => ([k, v.wot()])));
373
- }
374
- async service() {
375
- for (const key in this.valids) {
376
- await this.valids[key]?.service();
377
- }
378
- }
379
- }
380
- // #endregion