-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply.js
328 lines (278 loc) · 9.46 KB
/
apply.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use strict';
const safe = require('safe-regex')
const joiSchema = require('./schema.js');
const {isUndefined , isObject ,getMsgTip , isEmpty} = require('./util.js');
/**
* stack 默认是false 默认不输入error里面的stack 这里改为输出
*/
function validate(schema , param, options ){
const { error, value } = schema.validate(param , {errors : { stack : true} , convert : false});
if(error){
let message = `${error.message}, ${getMsgTip(param)}` ;
error.details[0].message = message;
error.message = message;
if(options && options['error']){
if(options.hasOwnProperty('clear') && !options['clear']){
}else{
console.error( error );
}
throw options['error'];
}
throw error;
}else{
return true;
}
}
/**
* @method requireAndNotEmptyForStr
* @param {string} param -- 要检验的参数值
* @description The string is mandatory and not empty 字符串必填且非空
*/
exports.requireAndNotEmptyForStr = function requireAndNotEmptyForStr(...args) {
let [param, options] = args;
checkCommon(options)
return validate(joiSchema.requireAndNotEmptyForStrSchema(param) , param, options);
}
/**
* @method requireAndIsEmptyForStr
* @param {string} param -- 要检验的参数值
* @description The string must be empty 字符串必填可以为空
*/
exports.requireAndIsEmptyForStr = function requireAndIsEmptyForStr(...args) {
let [param, options] = args;
checkCommon(options)
return validate(joiSchema.requireAndIsEmptyForStrSchema() , param, options);
}
/**
* @param {object} options
*/
function checkCommon(options){
if(isEmpty(options)) return;
if(!isObject(options)) throw new TypeError("options must be a object!");
if(Object.keys(options).length == 0 ) return;
if(options.hasOwnProperty('error') ){
// if( isEmpty(options.error) ) throw new Error("options.error cannot be empty!");
if( !(options.error instanceof Error) ) throw new TypeError("options.error must be a Error!");
}
}
/**
* @method requirePhone
* @param {string} param -- 要检验的参数值
* @description 必填且合法电话号码
*/
exports.requirePhone = function requirePhone(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requirePhoneSchema(options) , param, options );
}
/**
* @method requireForNum
* @param {string} param -- 要检验的参数值
* @description 必填数字
*/
exports.requireForNum = function requireForNum(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireForNumSchema(param , options ) , param, options );
}
/**
* @method requireEmail
* @param {string} param -- 要检验的参数值
* @description 必填且合法邮箱
*/
exports.requireEmail = function requireEmail(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireEmailSchema(options) , param, options);
}
/**
* @method requireID
* @param {string} param -- 要检验的参数值
* @description 必填且合法身份证
*/
exports.requireID = function requireID(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireIDSchema(options) , param, options);
}
/**
* @method requireIP
* @param {string} param -- 要检验的参数值
* @description 必填且合法IP
*/
exports.requireIP = function requireIP(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireIPSchema(options) , param, options);
}
/**
* @method requireUrl
* @param {string} param -- 要检验的参数值
* @description 必填且合法URL
*/
exports.requireUrl = function requireUrl(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireUrlSchema(options) , param, options);
}
/**
* @method requireAndEnumForStr
* @param {string} param -- 要检验的参数值
* @param {string[]} enumArr -- 枚举值
* @description 必填枚举字符串
*/
exports.requireAndEnumForStr = function requireAndEnumForStr(...args) {
let [param , enumArr , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndEnumForStrSchema(enumArr , options) , param, options);
}
/**
* @method length
* @param {string} param -- 要检验的参数值
* @param {integer} limit -- 长度
* @description 必填字符串特定长度
*/
exports.length = function length(...args) {
let [param , limit , options] = args;
checkCommon(options)
return validate(joiSchema.lengthSchema(param , limit , options) , param, options);
}
/**
* @method max
* @param {string} param -- 要检验的参数值
* @param {integer[]} limit -- 长度
* @description 必填字符串最大长度
*/
exports.max = function max(...args) {
let [param , limit , options] = args;
checkCommon(options)
return validate(joiSchema.maxSchema(param , limit , options) , param, options);
}
/**
* @method min
* @time 2021-11-03
* @param {string} param -- 要检验的参数值
* @param {integer[]} limit -- 长度
* @description 必填字符串最小长度
*/
exports.min = function min(...args) {
let [param , limit , options] = args;
checkCommon(options)
return validate(joiSchema.minSchema(param , limit , options) , param, options);
}
/**
* @method name
* @param {string} param -- 要检验的参数值
* @description 必填且合法中文姓名
*/
exports.name = function name(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.nameSchema(param , options) , param, options);
}
/**
* @method requireAndNotEmptyForArr
* @param {array} param -- 要检验的参数值
* @description 必填数组且非空
*/
exports.requireAndNotEmptyForArr = function requireAndNotEmptyForArr(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndNotEmptyForArrSchema(param , options) , param, options);
}
/**
* @method requireAndIsEmptyForArr
* @param {array} param -- 要检验的参数值
* @description 必填数组可以为空
*/
exports.requireAndIsEmptyForArr = function requireAndIsEmptyForArr(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndIsEmptyForArrSchema(param , options) , param, options);
}
/**
* @method requireAndNotEmptyForObj
* @param {object} param -- 要检验的参数值
* @description 必填对象且非空
*/
exports.requireAndNotEmptyForObj = function requireAndNotEmptyForObj(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndNotEmptyForObjSchema(param , options) , param, options);
}
/**
* @method requireAndIsEmptyForObj
* @param {object} param -- 要检验的参数值
* @description 必填对象可以为空
*/
exports.requireAndIsEmptyForObj = function requireAndIsEmptyForObj(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndIsEmptyForObjSchema(param , options) , param, options);
}
/**
* @method requireAndEnumForNum
* @param {number} param -- 要检验的参数值
* @param {number[]} enumArr -- 枚举值
* @description 必填枚举数字
*/
exports.requireAndEnumForNum = function requireAndEnumForNum(...args) {
let [param , enumArr , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndEnumForNumSchema(enumArr , options) , param, options);
}
/**
* @method requireForRangeNum
* @param {number} param -- 要检验的参数值
* @param {string} op
* @param {number || number[]} otherValue -- 限定的大小
* @description 必填且 Y>P>X 任意数
*/
exports.requireForRangeNum = function requireForRangeNum(...args) {
let [param , op , otherValue , options] = args;
checkCommon(options)
return validate(joiSchema.requireForRangeSchema(param , op , otherValue , options) , param, options );
}
/**
* @method requireForRangeInt
* @param {integer} param -- 要检验的参数值
* @param {string} op
* @param {integer || integer[]} otherValue -- 限定的大小
* @description 必填且 Y>P>X 整数
*/
exports.requireForRangeInt = function requireForRangeInt(...args) {
let [param , op , otherValue , options] = args;
checkCommon(options)
return validate(joiSchema.requireForRangeSchema(param , op , otherValue , options, 'integer') , param, options);
}
/**
* @method requireAndPrecision
* @param {number} param -- 要检验的参数值
* @param {integer} limit -- 小数位位数
* @description 必填最多N位小数
*/
exports.requireAndPrecision = function requireAndPrecision(...args) {
let [param , limit , options] = args;
checkCommon(options)
return validate(joiSchema.requireAndPrecisionSchema(param , limit , options) , param, options);
}
/**
* @method requireForInt
* @param {integer} param -- 要检验的参数值
* @description 必填整数
*/
exports.requireForInt = function requireForInt(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireForIntSchema(options) , param, options);
}
/**
* @method requireForBool
* @param {boolean} param -- 要检验的参数值
* @description 必填布尔
*/
exports.requireForBool = function requireForBool(...args) {
let [param , options] = args;
checkCommon(options)
return validate(joiSchema.requireForBoolSchema(options) , param, options);
}