perfect-payload 1.0.9 → 1.1.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.
- package/README.md +7 -1
- package/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,10 +37,14 @@ input:
|
|
|
37
37
|
```javascript
|
|
38
38
|
{
|
|
39
39
|
statusCode:200,
|
|
40
|
-
valid:true
|
|
40
|
+
valid:true,
|
|
41
|
+
validatedPayload:{...}
|
|
41
42
|
}
|
|
42
43
|
```
|
|
43
44
|
|
|
45
|
+
**Note:** you can use validatedPayload to overwrite your existing `req.body` or create new req attribute(`req.validatedBody=validatedPayload`) to access in your API logic
|
|
46
|
+
|
|
47
|
+
|
|
44
48
|
**Default Invalid Payload Response:**
|
|
45
49
|
|
|
46
50
|
```javascript
|
|
@@ -330,6 +334,7 @@ export const validatePayload = ({ rule }) => {
|
|
|
330
334
|
try {
|
|
331
335
|
const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
|
|
332
336
|
if (+statusCode >= 200 && +statusCode <= 299) {
|
|
337
|
+
req.validatedBody=response?.validatedPayload
|
|
333
338
|
next();
|
|
334
339
|
} else res.status(statusCode).json(response);
|
|
335
340
|
} catch (error) {
|
|
@@ -350,6 +355,7 @@ function validatePayload({ rule }) {
|
|
|
350
355
|
const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
|
|
351
356
|
|
|
352
357
|
if (+statusCode >= 200 && +statusCode <= 299) {
|
|
358
|
+
req.validatedBody=response?.validatedPayload
|
|
353
359
|
next();
|
|
354
360
|
} else {
|
|
355
361
|
res.status(statusCode).json(response);
|
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export function perfectPayloadV1(
|
|
|
8
8
|
message: "One or more attribute values are invalid",
|
|
9
9
|
}
|
|
10
10
|
) {
|
|
11
|
+
let validatedPayload = {};
|
|
11
12
|
let rowErrors = [];
|
|
12
13
|
for (const attributeName in dataValidationRule) {
|
|
13
14
|
let addNextError = true;
|
|
@@ -478,13 +479,16 @@ export function perfectPayloadV1(
|
|
|
478
479
|
default:
|
|
479
480
|
break;
|
|
480
481
|
}
|
|
482
|
+
|
|
483
|
+
if (rowErrors.length == 0)
|
|
484
|
+
validatedPayload[attributeName] = attributeValue;
|
|
481
485
|
}
|
|
482
486
|
}
|
|
483
487
|
|
|
484
488
|
if (rowErrors?.length) {
|
|
485
489
|
return { ...inValidPayloadResponse, errors: rowErrors };
|
|
486
490
|
} else {
|
|
487
|
-
return validPayloadResponse;
|
|
491
|
+
return { ...validPayloadResponse, validatedPayload };
|
|
488
492
|
}
|
|
489
493
|
}
|
|
490
494
|
|