perfect-payload 1.0.6 → 1.0.8
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/README.md +10 -7
- package/index.js +35 -96
- package/package.json +36 -36
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ input:
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
**Note:** If inValidPayloadResponse is passed, then it will be returned along with errors property(avoid
|
|
50
|
+
**Note:** If inValidPayloadResponse is passed, then it will be returned along with errors property(avoid error attribute in your inValidPayloadResponse object)
|
|
51
51
|
|
|
52
52
|
## 2. Available Validation Attributes
|
|
53
53
|
|
|
@@ -57,7 +57,7 @@ These attributes define the rules that can be applied to each field in the data
|
|
|
57
57
|
- **allowNull**: Allows `null` values.
|
|
58
58
|
- **allowEmptyObject**: Allows empty objects `{}`.
|
|
59
59
|
- **allowEmptyArray**: Allows empty arrays `[]`.
|
|
60
|
-
- **
|
|
60
|
+
- **elementConstraints**: Defines the constraints of elements in an array (refer to [Available ](#available-types)[Attributes](#available-validation-attributes) section).
|
|
61
61
|
- **regex**: Applies custom regular expression validation.
|
|
62
62
|
- **type**: Specifies the type of the field (refer to [Available Types](#available-types) section).
|
|
63
63
|
- **minLength**: Minimum length for string values.
|
|
@@ -82,7 +82,7 @@ These types can be used in the `type` attribute to specify the expected data typ
|
|
|
82
82
|
- **uuid**: Valid UUIDs (supports all versions).
|
|
83
83
|
- **uuidv1/uuidv3/uuidv4/uuidv5**: Version-specific UUID validation.
|
|
84
84
|
- **objectId**: Valid MongoDB ObjectIds.
|
|
85
|
-
- **array**: Validates arrays, with support for element type validation using `
|
|
85
|
+
- **array**: Validates arrays, with support for element type validation using `elementConstraints`.
|
|
86
86
|
- **object**: Validates objects, including nested objects using `objectAttr`.
|
|
87
87
|
|
|
88
88
|
## 4. Custom Error Message Attributes
|
|
@@ -93,7 +93,7 @@ You can define custom error messages for various validation failures using these
|
|
|
93
93
|
- **allowNullError**: Custom message when a `null` value is not allowed.
|
|
94
94
|
- **emptyObjectError**: Custom message when an empty object `{}` is not allowed.
|
|
95
95
|
- **emptyArrayError**: Custom message when an empty array `[]` is not allowed.
|
|
96
|
-
- **
|
|
96
|
+
- **elementConstraintsError**: Custom message when array elements do not match the passed constraints.
|
|
97
97
|
- **regexError**: Custom message when a value does not match the specified regex pattern.
|
|
98
98
|
- **typeError**: Custom message when a value does not match the specified type.
|
|
99
99
|
- **minLengthError**: Custom message when a string value is shorter than the minimum length.
|
|
@@ -111,7 +111,7 @@ If not explicitly specified, the following default values are applied:
|
|
|
111
111
|
- **allowNull**: `true` (allows `null` values).
|
|
112
112
|
- **allowEmptyObject**: `true` (allows empty objects `{}`).
|
|
113
113
|
- **allowEmptyArray**: `true` (allows empty arrays `[]`).
|
|
114
|
-
- **
|
|
114
|
+
- **elementConstraints**: Ignored.
|
|
115
115
|
- **regex**: Ignored.
|
|
116
116
|
- **type**: Ignored.
|
|
117
117
|
- **minLength**: Ignored.
|
|
@@ -165,7 +165,6 @@ sample-1
|
|
|
165
165
|
sample-2
|
|
166
166
|
|
|
167
167
|
```javascript
|
|
168
|
-
|
|
169
168
|
{
|
|
170
169
|
id: {
|
|
171
170
|
mandatory: true,
|
|
@@ -218,8 +217,12 @@ sample-2
|
|
|
218
217
|
},
|
|
219
218
|
allMarks: {
|
|
220
219
|
type: "array",
|
|
221
|
-
elementType: "string",
|
|
222
220
|
allowEmptyArray: false,
|
|
221
|
+
elementConstraints: {
|
|
222
|
+
type: "number",
|
|
223
|
+
allowNull: false,
|
|
224
|
+
range: "0-100",
|
|
225
|
+
},
|
|
223
226
|
},
|
|
224
227
|
totalScore: {
|
|
225
228
|
type: "number",
|
package/index.js
CHANGED
|
@@ -1,72 +1,3 @@
|
|
|
1
|
-
//-----------AVAILABLE VALIATION ATTRIBUTES-----------
|
|
2
|
-
// mandatory : required data
|
|
3
|
-
// allowNull : allow null
|
|
4
|
-
// allowEmptyObject : allow {}
|
|
5
|
-
// allowEmptyArray : allow []
|
|
6
|
-
// elementType : define array elements type(values: AVAILABLE TYPE Section)
|
|
7
|
-
// regex : custom regex validation
|
|
8
|
-
// type : refer AVAILABLE TYPE Section
|
|
9
|
-
// minLength : for string length
|
|
10
|
-
// maxLength : for string length
|
|
11
|
-
// preventDecimal : decimal/fraction value nor allowed
|
|
12
|
-
// min : for minimum number value
|
|
13
|
-
// max : for maximum number value
|
|
14
|
-
// range : for number, between 2 range
|
|
15
|
-
// objectAttr : for nested object validation
|
|
16
|
-
// dependency : for dependent field validation(ex: min and max salary,etc. customizable)
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//--------AVAILABLE TYPE-----------
|
|
20
|
-
// number :
|
|
21
|
-
// string :
|
|
22
|
-
// boolean :
|
|
23
|
-
// email :
|
|
24
|
-
// url :
|
|
25
|
-
// enum : array of values(heterogeneous values supported)
|
|
26
|
-
// uuid/uuidv1/uuidv3/uuidv4/uuidv5 : "uuid" for all version and version specific type for specific version
|
|
27
|
-
// objectId :
|
|
28
|
-
// array : can validate element tyoe with elementType attribute(PENDING)
|
|
29
|
-
// object : supports nested object validation as well with objectAttr attribute
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
// ---------Custom Error Message Attributes-----------
|
|
33
|
-
// mandatoryError : string
|
|
34
|
-
// allowNullError : string
|
|
35
|
-
// emptyObjectError : string
|
|
36
|
-
// emptyArrayError : string
|
|
37
|
-
// elementTypeError : string
|
|
38
|
-
// regexError : string
|
|
39
|
-
// typeError : string
|
|
40
|
-
// minLengthError : string
|
|
41
|
-
// maxLengthError : string
|
|
42
|
-
// preventDecimalError : string
|
|
43
|
-
// minError : string
|
|
44
|
-
// maxError : string
|
|
45
|
-
// rangeError : string
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//--------DEFAULT VALUES IF NOT SPECIFIED-----------
|
|
49
|
-
// mandatory : false
|
|
50
|
-
// allowNull : true
|
|
51
|
-
// allowEmptyObject : true
|
|
52
|
-
// allowEmptyArray : true
|
|
53
|
-
// elementType : ignored
|
|
54
|
-
// regex : ignored
|
|
55
|
-
// type : ignored
|
|
56
|
-
// minLength : ignored
|
|
57
|
-
// maxLength : ignored
|
|
58
|
-
// preventDecimal : false(allows both decimal and integer)
|
|
59
|
-
// min : ignored
|
|
60
|
-
// max : ignored
|
|
61
|
-
// range : ignored
|
|
62
|
-
// objectAttr : ignored
|
|
63
|
-
// dependency : ignored
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
|
|
70
1
|
export function perfectPayloadV1(
|
|
71
2
|
data = {},
|
|
72
3
|
dataValidationRule = {},
|
|
@@ -105,11 +36,7 @@ export function perfectPayloadV1(
|
|
|
105
36
|
break;
|
|
106
37
|
case "allowNull":
|
|
107
38
|
//allowNull value check
|
|
108
|
-
if (
|
|
109
|
-
addNextError &&
|
|
110
|
-
ruleName === "allowNull" &&
|
|
111
|
-
attributeValue == null
|
|
112
|
-
) {
|
|
39
|
+
if (addNextError && attributeValue == null) {
|
|
113
40
|
if (!dataValidationRule?.[attributeName]?.[ruleName]) {
|
|
114
41
|
addNextError = false;
|
|
115
42
|
rowErrors.push(
|
|
@@ -121,7 +48,7 @@ export function perfectPayloadV1(
|
|
|
121
48
|
break;
|
|
122
49
|
case "allowEmptyObject":
|
|
123
50
|
//allowEmptyObject check
|
|
124
|
-
if (addNextError
|
|
51
|
+
if (addNextError) {
|
|
125
52
|
if (
|
|
126
53
|
!attributeRules?.["allowEmptyObject"] &&
|
|
127
54
|
Object.keys(attributeValue).length == 0
|
|
@@ -137,7 +64,7 @@ export function perfectPayloadV1(
|
|
|
137
64
|
break;
|
|
138
65
|
case "allowEmptyArray":
|
|
139
66
|
//allowEmptyArray check
|
|
140
|
-
if (addNextError
|
|
67
|
+
if (addNextError) {
|
|
141
68
|
if (
|
|
142
69
|
!attributeRules?.["allowEmptyArray"] &&
|
|
143
70
|
isArray(attributeValue) &&
|
|
@@ -152,15 +79,25 @@ export function perfectPayloadV1(
|
|
|
152
79
|
}
|
|
153
80
|
|
|
154
81
|
break;
|
|
155
|
-
case "
|
|
82
|
+
case "elementConstraints":
|
|
156
83
|
//check array ele type
|
|
157
|
-
if (addNextError
|
|
84
|
+
if (addNextError) {
|
|
158
85
|
if (isArray(attributeValue) && attributeValue?.length > 0) {
|
|
159
|
-
|
|
160
|
-
|
|
86
|
+
let elementError = false;
|
|
87
|
+
for (const element of attributeValue) {
|
|
88
|
+
const { statusCode = 200, errors } = perfectPayloadV1(
|
|
89
|
+
{ [attributeName]: element },
|
|
90
|
+
{ [attributeName]: attributeRules[ruleName] }
|
|
91
|
+
);
|
|
92
|
+
if (statusCode == 400) {
|
|
93
|
+
elementError = errors?.[0];
|
|
94
|
+
// addNextError = false;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (elementError) {
|
|
161
99
|
rowErrors.push(
|
|
162
|
-
attributeRules?.["
|
|
163
|
-
`invalid type values in ${attributePath}`
|
|
100
|
+
attributeRules?.["elementConstraintsError"] || elementError
|
|
164
101
|
);
|
|
165
102
|
addNextError = false;
|
|
166
103
|
}
|
|
@@ -170,7 +107,7 @@ export function perfectPayloadV1(
|
|
|
170
107
|
break;
|
|
171
108
|
case "regex":
|
|
172
109
|
//custom regex check
|
|
173
|
-
if (addNextError
|
|
110
|
+
if (addNextError) {
|
|
174
111
|
if (!isPassedRegex(attributeRules[ruleName], attributeValue)) {
|
|
175
112
|
rowErrors.push(
|
|
176
113
|
attributeRules?.["regexError"] ||
|
|
@@ -182,7 +119,7 @@ export function perfectPayloadV1(
|
|
|
182
119
|
break;
|
|
183
120
|
case "type":
|
|
184
121
|
//value type check
|
|
185
|
-
if (addNextError
|
|
122
|
+
if (addNextError) {
|
|
186
123
|
if (attributeValue == null && nullAllowed) {
|
|
187
124
|
addNextError = true;
|
|
188
125
|
} else {
|
|
@@ -358,7 +295,7 @@ export function perfectPayloadV1(
|
|
|
358
295
|
break;
|
|
359
296
|
case "minLength":
|
|
360
297
|
//minimum string length check
|
|
361
|
-
if (addNextError
|
|
298
|
+
if (addNextError) {
|
|
362
299
|
if (attributeValue == null && nullAllowed) {
|
|
363
300
|
addNextError = true;
|
|
364
301
|
} else if (typeof attributeValue == "string") {
|
|
@@ -369,15 +306,16 @@ export function perfectPayloadV1(
|
|
|
369
306
|
);
|
|
370
307
|
addNextError = false;
|
|
371
308
|
} else {
|
|
372
|
-
|
|
373
|
-
|
|
309
|
+
rowErrors.push(
|
|
310
|
+
`${attributePath} value should be a string type(minLength specified)`
|
|
374
311
|
);
|
|
312
|
+
addNextError = false;
|
|
375
313
|
}
|
|
376
314
|
}
|
|
377
315
|
break;
|
|
378
316
|
case "maxLength":
|
|
379
317
|
//max string length check
|
|
380
|
-
if (addNextError
|
|
318
|
+
if (addNextError) {
|
|
381
319
|
if (attributeValue == null && nullAllowed) {
|
|
382
320
|
addNextError = true;
|
|
383
321
|
} else if (typeof attributeValue == "string") {
|
|
@@ -388,15 +326,16 @@ export function perfectPayloadV1(
|
|
|
388
326
|
);
|
|
389
327
|
addNextError = false;
|
|
390
328
|
} else {
|
|
391
|
-
|
|
392
|
-
|
|
329
|
+
rowErrors.push(
|
|
330
|
+
`${attributePath} value should be a string type(maxLength specified)`
|
|
393
331
|
);
|
|
332
|
+
addNextError = false;
|
|
394
333
|
}
|
|
395
334
|
}
|
|
396
335
|
break;
|
|
397
336
|
case "preventDecimal":
|
|
398
337
|
//preventFraction
|
|
399
|
-
if (addNextError
|
|
338
|
+
if (addNextError) {
|
|
400
339
|
if (attributeValue == null && nullAllowed) {
|
|
401
340
|
addNextError = true;
|
|
402
341
|
} else if (!isNumber(attributeValue)) {
|
|
@@ -415,7 +354,7 @@ export function perfectPayloadV1(
|
|
|
415
354
|
break;
|
|
416
355
|
case "min":
|
|
417
356
|
//minimum value check
|
|
418
|
-
if (addNextError
|
|
357
|
+
if (addNextError) {
|
|
419
358
|
if (attributeValue == null && nullAllowed) {
|
|
420
359
|
addNextError = true;
|
|
421
360
|
} else if (!isNumber(attributeValue)) {
|
|
@@ -438,7 +377,7 @@ export function perfectPayloadV1(
|
|
|
438
377
|
break;
|
|
439
378
|
case "max":
|
|
440
379
|
//maximum value check
|
|
441
|
-
if (addNextError
|
|
380
|
+
if (addNextError) {
|
|
442
381
|
if (attributeValue == null && nullAllowed) {
|
|
443
382
|
addNextError = true;
|
|
444
383
|
} else if (!isNumber(attributeValue)) {
|
|
@@ -461,7 +400,7 @@ export function perfectPayloadV1(
|
|
|
461
400
|
break;
|
|
462
401
|
case "range":
|
|
463
402
|
//value range check
|
|
464
|
-
if (addNextError
|
|
403
|
+
if (addNextError) {
|
|
465
404
|
if (attributeValue == null && nullAllowed) {
|
|
466
405
|
addNextError = true;
|
|
467
406
|
} else if (!isNumber(attributeValue)) {
|
|
@@ -487,7 +426,7 @@ export function perfectPayloadV1(
|
|
|
487
426
|
break;
|
|
488
427
|
case "objectAttr":
|
|
489
428
|
//nested object attributes check
|
|
490
|
-
if (addNextError
|
|
429
|
+
if (addNextError) {
|
|
491
430
|
const allObjectAttr = Object.keys(attributeRules?.[ruleName]);
|
|
492
431
|
const objectAttrRules = {};
|
|
493
432
|
for (const attr of allObjectAttr) {
|
|
@@ -509,7 +448,7 @@ export function perfectPayloadV1(
|
|
|
509
448
|
break;
|
|
510
449
|
case "dependency":
|
|
511
450
|
//dependency check
|
|
512
|
-
if (addNextError
|
|
451
|
+
if (addNextError) {
|
|
513
452
|
const allDependencyAttr = Object.keys(attributeRules?.[ruleName]);
|
|
514
453
|
for (const attr of allDependencyAttr) {
|
|
515
454
|
if (
|
package/package.json
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "perfect-payload",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"test": "jest"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/kiranpoojary/perfect-payload.git"
|
|
13
|
-
},
|
|
14
|
-
"keywords": [
|
|
15
|
-
"validation",
|
|
16
|
-
"payload",
|
|
17
|
-
"api",
|
|
18
|
-
"data",
|
|
19
|
-
"request-body",
|
|
20
|
-
"response",
|
|
21
|
-
"express",
|
|
22
|
-
"validate"
|
|
23
|
-
],
|
|
24
|
-
"author": "Kiran Poojary",
|
|
25
|
-
"license": "ISC",
|
|
26
|
-
"bugs": {
|
|
27
|
-
"url": "https://github.com/kiranpoojary/perfect-payload/issues"
|
|
28
|
-
},
|
|
29
|
-
"homepage": "https://github.com/kiranpoojary/perfect-payload#readme",
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@babel/core": "^7.25.2",
|
|
32
|
-
"@babel/preset-env": "^7.25.4",
|
|
33
|
-
"babel-jest": "^29.7.0",
|
|
34
|
-
"jest": "^29.7.0"
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "perfect-payload",
|
|
3
|
+
"version": "1.0.8",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "rich json validator",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "jest"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/kiranpoojary/perfect-payload.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"validation",
|
|
16
|
+
"payload",
|
|
17
|
+
"api",
|
|
18
|
+
"data",
|
|
19
|
+
"request-body",
|
|
20
|
+
"response",
|
|
21
|
+
"express",
|
|
22
|
+
"validate"
|
|
23
|
+
],
|
|
24
|
+
"author": "Kiran Poojary",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/kiranpoojary/perfect-payload/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/kiranpoojary/perfect-payload#readme",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@babel/core": "^7.25.2",
|
|
32
|
+
"@babel/preset-env": "^7.25.4",
|
|
33
|
+
"babel-jest": "^29.7.0",
|
|
34
|
+
"jest": "^29.7.0"
|
|
35
|
+
}
|
|
36
|
+
}
|