xzwebx-httpfilter 1.3.1 → 2.0.1

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.
Files changed (2) hide show
  1. package/HttpFilter.js +111 -54
  2. package/package.json +1 -1
package/HttpFilter.js CHANGED
@@ -9,10 +9,10 @@ let ModuleInterfaceMap = {}
9
9
  let FieldMap = {}
10
10
  async function response(req, res, next) {
11
11
  let retData = {
12
- codeKey: 'SYSTEM_ERR',
12
+ codeKey: 'SVC_ERR',
13
13
  data: []
14
14
  }
15
- retData.codeKey = 'SUCCESS'
15
+ retData.codeKey = 'SUCC'
16
16
  return res.send(res.Msg(retData))
17
17
  }
18
18
  function Init(language, resultCodeMap, tipsMap, moduleMap, moduleInterfaceMap, fieldMap) {
@@ -101,13 +101,13 @@ function CheckReq(req, res, next) {
101
101
 
102
102
  const module = ModuleMap[baseUrl]
103
103
  if (!module) {
104
- return res.send(res.Msg({codeKey: 'SYSTEM_TIPS', msg: ['ERR_URL']}))
104
+ return res.send(res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']}))
105
105
  }
106
106
 
107
107
  if (!ModuleInterfaceMap[module.id] ||
108
108
  !ModuleInterfaceMap[module.id][subUri] ||
109
109
  !ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]) {
110
- return res.send(res.Msg({codeKey: 'SYSTEM_TIPS', msg: ['ERR_URL']}))
110
+ return res.send(res.Msg({codeKey: 'CLT_ERR', msg: ['WEBX_ERR_URL']}))
111
111
  }
112
112
 
113
113
  req.headers['interfaceInfo'] = ModuleInterfaceMap[module.id][subUri][req.method.toLowerCase()]
@@ -118,7 +118,7 @@ function CheckReq(req, res, next) {
118
118
 
119
119
  let retMsgData = cycleCheckParams(FieldMap[reqMsgId], req.body)
120
120
  if (retMsgData) {
121
- return res.send(res.Msg({codeKey: 'SYSTEM_TIPS', msg: retMsgData}))
121
+ return res.send(res.Msg({codeKey: 'CLT_ERR', msg: retMsgData}))
122
122
  }
123
123
 
124
124
  return next()
@@ -161,10 +161,14 @@ function cycleCheckParams(msgFieldMap, data) {
161
161
  }
162
162
  let fatherFieldType = fCfgItem.fieldType
163
163
  if (fatherFieldType == 'LIST' || (fatherFieldType == 'OBJ' && fCfgItem.keyType == 'VOBJ')) {
164
- for (let idx in data) {
165
- retMsgData = cycleCheckParams(msgFieldMap[fieldName], data[idx])
166
- if (retMsgData) {
167
- return retMsgData
164
+ if (isRoot) {
165
+ retMsgData = cycleCheckParams(msgFieldMap[fieldName], data)
166
+ } else {
167
+ for (let idx in data) {
168
+ retMsgData = cycleCheckParams(msgFieldMap[fieldName], data[idx])
169
+ if (retMsgData) {
170
+ return retMsgData
171
+ }
168
172
  }
169
173
  }
170
174
  } else {
@@ -181,6 +185,17 @@ function cycleCheckParams(msgFieldMap, data) {
181
185
  }
182
186
  return null
183
187
  }
188
+
189
+ function getCustomTips(tips) {
190
+ if (tips.length > 3 && tips.substring(0, 2) === '${' && tips.substring(tips.length -1, tips.length) === '}') {
191
+ let tipsKey = tips.substring(2, tips.length -1)
192
+ tipsKey = tipsKey.trim()
193
+ if (TipsMap[tipsKey]) {
194
+ return TipsMap[tipsKey].tips
195
+ }
196
+ }
197
+ return tips
198
+ }
184
199
  function IsStringOk(fCfgItem, paramValue) {
185
200
  if (!fCfgItem || !fCfgItem.ifMust) {
186
201
  return null
@@ -195,12 +210,11 @@ function IsStringOk(fCfgItem, paramValue) {
195
210
  return null
196
211
  }
197
212
 
198
- if (!paramValue) {
199
- return ['NULL_STR_FIELD', fCfgItem.fieldUrl]
200
- }
201
-
202
- if (typeof paramValue != 'string') {
203
- return ['NOT_STR_VALUE', fCfgItem.fieldUrl]
213
+ if (!paramValue || typeof paramValue != 'string') {
214
+ if (fCfgItem.nullTips) {
215
+ return getCustomTips(fCfgItem.nullTips)
216
+ }
217
+ return ['WEBX_NULL_FIELD', 'string', fCfgItem.fieldUrl]
204
218
  }
205
219
 
206
220
  for (let idx in fCfgItem.rules) {
@@ -222,9 +236,9 @@ function IsStringOk(fCfgItem, paramValue) {
222
236
 
223
237
  if (!isPass) {
224
238
  if (rule.ruleDesc) {
225
- return rule.ruleDesc
239
+ return getCustomTips(rule.ruleDesc)
226
240
  }
227
- return ['WRONG_STR_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
241
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
228
242
  }
229
243
  } else if (rule.checkType == 'ENU') {
230
244
  if (rule.isCaseSensitive == 1) {
@@ -232,16 +246,16 @@ function IsStringOk(fCfgItem, paramValue) {
232
246
  if (rule.isMatched === 1) {
233
247
  if (!rule.exprVal.includes(paramValue)) {
234
248
  if (rule.ruleDesc) {
235
- return rule.ruleDesc
249
+ return getCustomTips(rule.ruleDesc)
236
250
  }
237
- return ['WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
251
+ return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
238
252
  }
239
253
  } else {
240
254
  if (rule.exprVal.includes(paramValue)) {
241
255
  if (rule.ruleDesc) {
242
- return rule.ruleDesc
256
+ return getCustomTips(rule.ruleDesc)
243
257
  }
244
- return ['EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
258
+ return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
245
259
  }
246
260
  }
247
261
  }
@@ -263,12 +277,12 @@ function IsStringOk(fCfgItem, paramValue) {
263
277
  }
264
278
  if (!isPass) {
265
279
  if (rule.ruleDesc) {
266
- return rule.ruleDesc
280
+ return getCustomTips(rule.ruleDesc)
267
281
  }
268
282
  if (rule.isMatched === 1) {
269
- return ['WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
283
+ return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
270
284
  } else {
271
- return ['EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
285
+ return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
272
286
  }
273
287
  }
274
288
  }
@@ -292,9 +306,9 @@ function IsStringOk(fCfgItem, paramValue) {
292
306
  }
293
307
  if (!isPass) {
294
308
  if (rule.ruleDesc) {
295
- return rule.ruleDesc
309
+ return getCustomTips(rule.ruleDesc)
296
310
  }
297
- return ['WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
311
+ return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
298
312
  }
299
313
  }
300
314
  }
@@ -315,12 +329,11 @@ function IsIntOk(fCfgItem, paramValue) {
315
329
  return null
316
330
  }
317
331
 
318
- if (paramValue == undefined) {
319
- return ['NULL_INT_FIELD', fCfgItem.fieldUrl]
320
- }
321
-
322
- if (typeof paramValue != 'number') {
323
- return ['NOT_INT_VALUE', fCfgItem.fieldUrl]
332
+ if (paramValue == undefined || typeof paramValue != 'number') {
333
+ if (fCfgItem.nullTips) {
334
+ return getCustomTips(fCfgItem.nullTips)
335
+ }
336
+ return ['WEBX_NULL_FIELD', 'number', fCfgItem.fieldUrl]
324
337
  }
325
338
 
326
339
  for (let idx in fCfgItem.rules) {
@@ -342,25 +355,25 @@ function IsIntOk(fCfgItem, paramValue) {
342
355
 
343
356
  if (!isPass) {
344
357
  if (rule.ruleDesc) {
345
- return rule.ruleDesc
358
+ return getCustomTips(rule.ruleDesc)
346
359
  }
347
- return ['WRONG_INT_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
360
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
348
361
  }
349
362
  } else if (rule.checkType == 'ENU') {
350
363
  if (rule.exprVal && rule.exprVal.length) {
351
364
  if (rule.isMatched === 1) {
352
365
  if (!rule.exprVal.includes(paramValue)) {
353
366
  if (rule.ruleDesc) {
354
- return rule.ruleDesc
367
+ return getCustomTips(rule.ruleDesc)
355
368
  }
356
- return ['WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
369
+ return ['WEBX_WRONG_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
357
370
  }
358
371
  } else {
359
372
  if (rule.exprVal.includes(paramValue)) {
360
373
  if (rule.ruleDesc) {
361
- return rule.ruleDesc
374
+ return getCustomTips(rule.ruleDesc)
362
375
  }
363
- return ['EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
376
+ return ['WEBX_EXCLUSION_ENU_VALUE', fCfgItem.fieldUrl, rule.exprVal]
364
377
  }
365
378
  }
366
379
  }
@@ -384,9 +397,9 @@ function IsIntOk(fCfgItem, paramValue) {
384
397
  }
385
398
  if (!isPass) {
386
399
  if (rule.ruleDesc) {
387
- return rule.ruleDesc
400
+ return getCustomTips(rule.ruleDesc)
388
401
  }
389
- return ['WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
402
+ return ['WEBX_WRONG_REGEX_VALUE', fCfgItem.fieldUrl, exprVal]
390
403
  }
391
404
  }
392
405
  }
@@ -407,16 +420,38 @@ function IsObjOk(fCfgItem, paramValue) {
407
420
  return null
408
421
  }
409
422
 
410
- if (!paramValue) {
411
- return ['NULL_FIELD', fCfgItem.fieldUrl]
423
+ if (!paramValue || paramValue.constructor != Object || !Object.keys(paramValue).length) {
424
+ if (fCfgItem.nullTips) {
425
+ return getCustomTips(fCfgItem.nullTips)
426
+ }
427
+ return ['WEBX_NULL_FIELD', 'map', fCfgItem.fieldUrl]
412
428
  }
413
429
 
414
- if (paramValue.constructor != Object) {
415
- return ['WRONG_OBJ_VALUE', fCfgItem.fieldUrl]
416
- }
430
+ let length = Object.keys(paramValue).length
431
+ for (let idx in fCfgItem.rules) {
432
+ let rule = fCfgItem.rules[idx]
433
+ if (rule.checkType == 'RANGE') {
434
+ let isPass = false
435
+ if (typeof rule.exprVal[0] == 'object') {
436
+ for (let i in rule.exprVal) {
437
+ if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
438
+ isPass = true
439
+ break
440
+ }
441
+ }
442
+ } else {
443
+ if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
444
+ isPass = true
445
+ }
446
+ }
417
447
 
418
- if (!Object.keys(paramValue).length) {
419
- return ['NULL_VALUE', fCfgItem.fieldUrl]
448
+ if (!isPass) {
449
+ if (rule.ruleDesc) {
450
+ return getCustomTips(rule.ruleDesc)
451
+ }
452
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
453
+ }
454
+ }
420
455
  }
421
456
 
422
457
  return null
@@ -435,16 +470,38 @@ function IsListOk(fCfgItem, paramValue) {
435
470
  return null
436
471
  }
437
472
 
438
- if (!paramValue) {
439
- return ['NULL_FIELD', fCfgItem.fieldUrl]
473
+ if (!paramValue || paramValue.constructor != Array || !paramValue.length) {
474
+ if (fCfgItem.nullTips) {
475
+ return getCustomTips(fCfgItem.nullTips)
476
+ }
477
+ return ['WEBX_NULL_FIELD', 'list', fCfgItem.fieldUrl]
440
478
  }
441
479
 
442
- if (paramValue.constructor != Array) {
443
- return ['WRONG_LIST_VALUE', fCfgItem.fieldUrl]
444
- }
480
+ let length = paramValue.length
481
+ for (let idx in fCfgItem.rules) {
482
+ let rule = fCfgItem.rules[idx]
483
+ if (rule.checkType == 'RANGE') {
484
+ let isPass = false
485
+ if (typeof rule.exprVal[0] == 'object') {
486
+ for (let i in rule.exprVal) {
487
+ if (length >= parseInt(rule.exprVal[i][0]) && length <= parseInt(rule.exprVal[i][1])) {
488
+ isPass = true
489
+ break
490
+ }
491
+ }
492
+ } else {
493
+ if (length >= parseInt(rule.exprVal[0]) && length <= parseInt(rule.exprVal[1])) {
494
+ isPass = true
495
+ }
496
+ }
445
497
 
446
- if (!paramValue.length) {
447
- return ['NULL_VALUE', fCfgItem.fieldUrl]
498
+ if (!isPass) {
499
+ if (rule.ruleDesc) {
500
+ return getCustomTips(rule.ruleDesc)
501
+ }
502
+ return ['WEBX_WRONG_RANGE', fCfgItem.fieldUrl, JSON.stringify(rule.exprVal)]
503
+ }
504
+ }
448
505
  }
449
506
 
450
507
  return null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xzwebx-httpfilter",
3
- "version": "1.3.1",
3
+ "version": "2.0.1",
4
4
  "main": "HttpFilter.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"