perfect-payload 1.0.8 → 1.1.0

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 (3) hide show
  1. package/README.md +55 -27
  2. package/index.js +4 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -16,34 +16,43 @@ This module provides a robust framework for validating data objects based on def
16
16
  usage:
17
17
 
18
18
  ```javascript
19
- import { perfectPayloadV1 } from"perfect-payload";
20
- const {statucCode=400, ...result}=perfectPayloadV1(data,dataValidationRule,validPayloadResponse,inValidPayloadResponse)
19
+ import { perfectPayloadV1 } from "perfect-payload";
20
+ const { statucCode = 400, ...result } = perfectPayloadV1(
21
+ data,
22
+ dataValidationRule,
23
+ validPayloadResponse,
24
+ inValidPayloadResponse
25
+ );
21
26
  ```
22
27
 
23
28
  input:
24
29
 
25
- 1. **data :** your payload object (required *)
26
- 2. **dataValidationRule:** validation rule object (required *)
30
+ 1. **data :** your payload object (required \*)
31
+ 2. **dataValidationRule:** validation rule object (required \*)
27
32
  3. **validPayloadResponse:** response object you want it back on all validation passed (Optional)
28
33
  4. **inValidPayloadResponse:** response object you want it back on any validation fails (Optional)
29
34
 
30
35
  **Default Valid Payload Response:**
31
36
 
32
37
  ```javascript
33
- {
34
- statusCode:200,
35
- valid:true
38
+ {
39
+ statusCode:200,
40
+ valid:true,
41
+ validatedPayload:{...}
36
42
  }
37
43
  ```
38
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
+
39
48
  **Default Invalid Payload Response:**
40
49
 
41
50
  ```javascript
42
- {
43
- statusCode:400,
44
- valid:false,
45
- message:"One or more attribute values are invalid",
46
- errors:["minSalary must be less than maxSalary"]
51
+ {
52
+ statusCode:400,
53
+ valid:false,
54
+ message:"One or more attribute values are invalid",
55
+ errors:["minSalary must be less than maxSalary"]
47
56
  }
48
57
  ```
49
58
 
@@ -178,10 +187,8 @@ sample-2
178
187
  },
179
188
  firstName: {
180
189
  mandatory: true,
181
- allowNull: false,
182
190
  type: "string",
183
191
  minLength: 3,
184
- maxLength: 6,
185
192
  },
186
193
  lastName: {
187
194
  mandatory: false,
@@ -202,7 +209,6 @@ sample-2
202
209
  preventDecimal: true,
203
210
  },
204
211
  email: {
205
- type: "email",
206
212
  regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
207
213
  },
208
214
  githubLink: {
@@ -307,10 +313,10 @@ sample-2
307
313
 
308
314
  ### 2. Usage
309
315
 
310
- #### 1. creating your route with middleware and validation rule:
316
+ #### 1. creating your route with payload validation middleware
311
317
 
312
318
  ```javascript
313
- //Here validatePayload is your middleware function where you're invoking perfect payload
319
+ //Here validatePayload is your middleware function, where you're invoking perfect payload
314
320
  router.post(
315
321
  "/payload-validation",
316
322
  validatePayload({ rule: <your validation rule json object> }),
@@ -318,28 +324,50 @@ router.post(
318
324
  );
319
325
  ```
320
326
 
321
- #### 2. Use perfect-payload in your middleware like below
327
+ #### 2.1 Use perfect-payload in your middleware like below(for MODULE JS)
322
328
 
323
329
  ```javascript
324
330
  import { perfectPayloadV1 } from "perfect-payload";
325
331
 
326
332
  export const validatePayload = ({ rule }) => {
327
333
  return (req, res, next) => {
328
- const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
329
- if (+statusCode >= 200 && +statusCode <= 299) {
330
- next();
331
- } else res.status(statusCode).json(response);
334
+ try {
335
+ const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
336
+ if (+statusCode >= 200 && +statusCode <= 299) {
337
+ req.validatedBody=response?.validatedPayload
338
+ next();
339
+ } else res.status(statusCode).json(response);
340
+ } catch (error) {
341
+ console.error("Error validating payload", error);
342
+ res.status(500).json({ error: "Internal Server Error" });
343
+ }
332
344
  };
333
345
  };
334
346
  ```
335
347
 
336
- #### Usage in venila js code
348
+ #### 2.2 Use perfect-payload in your middleware like below(for COMMON JS)
337
349
 
338
350
  ```javascript
339
- import { perfectPayloadV1 } from "perfect-payload";
340
- const result = perfectPayloadV1(dataObject, ruleObject);
341
- console.log(result?.statusCode)
342
- console.log(result?.isValid)
351
+ function validatePayload({ rule }) {
352
+ return async (req, res, next) => {
353
+ try {
354
+ const { perfectPayloadV1 } = await import("perfect-payload");
355
+ const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
356
+
357
+ if (+statusCode >= 200 && +statusCode <= 299) {
358
+ req.validatedBody=response?.validatedPayload
359
+ next();
360
+ } else {
361
+ res.status(statusCode).json(response);
362
+ }
363
+ } catch (error) {
364
+ console.error("Error validating payload", error);
365
+ res.status(500).json({ error: "Internal Server Error" });
366
+ }
367
+ };
368
+ }
369
+
370
+ module.exports = { validatePayload };
343
371
  ```
344
372
 
345
373
  ---
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,15 @@ export function perfectPayloadV1(
478
479
  default:
479
480
  break;
480
481
  }
482
+
483
+ validatedPayload[attributeName] = attributeValue;
481
484
  }
482
485
  }
483
486
 
484
487
  if (rowErrors?.length) {
485
488
  return { ...inValidPayloadResponse, errors: rowErrors };
486
489
  } else {
487
- return validPayloadResponse;
490
+ return { ...validPayloadResponse, validatedPayload };
488
491
  }
489
492
  }
490
493
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perfect-payload",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "description": "rich json validator",
6
6
  "main": "index.js",