perfect-payload 1.0.8 → 1.0.9

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/README.md +49 -27
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -16,34 +16,39 @@ 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
36
41
  }
37
42
  ```
38
43
 
39
44
  **Default Invalid Payload Response:**
40
45
 
41
46
  ```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"]
47
+ {
48
+ statusCode:400,
49
+ valid:false,
50
+ message:"One or more attribute values are invalid",
51
+ errors:["minSalary must be less than maxSalary"]
47
52
  }
48
53
  ```
49
54
 
@@ -178,10 +183,8 @@ sample-2
178
183
  },
179
184
  firstName: {
180
185
  mandatory: true,
181
- allowNull: false,
182
186
  type: "string",
183
187
  minLength: 3,
184
- maxLength: 6,
185
188
  },
186
189
  lastName: {
187
190
  mandatory: false,
@@ -202,7 +205,6 @@ sample-2
202
205
  preventDecimal: true,
203
206
  },
204
207
  email: {
205
- type: "email",
206
208
  regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
207
209
  },
208
210
  githubLink: {
@@ -307,10 +309,10 @@ sample-2
307
309
 
308
310
  ### 2. Usage
309
311
 
310
- #### 1. creating your route with middleware and validation rule:
312
+ #### 1. creating your route with payload validation middleware
311
313
 
312
314
  ```javascript
313
- //Here validatePayload is your middleware function where you're invoking perfect payload
315
+ //Here validatePayload is your middleware function, where you're invoking perfect payload
314
316
  router.post(
315
317
  "/payload-validation",
316
318
  validatePayload({ rule: <your validation rule json object> }),
@@ -318,28 +320,48 @@ router.post(
318
320
  );
319
321
  ```
320
322
 
321
- #### 2. Use perfect-payload in your middleware like below
323
+ #### 2.1 Use perfect-payload in your middleware like below(for MODULE JS)
322
324
 
323
325
  ```javascript
324
326
  import { perfectPayloadV1 } from "perfect-payload";
325
327
 
326
328
  export const validatePayload = ({ rule }) => {
327
329
  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);
330
+ try {
331
+ const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
332
+ if (+statusCode >= 200 && +statusCode <= 299) {
333
+ next();
334
+ } else res.status(statusCode).json(response);
335
+ } catch (error) {
336
+ console.error("Error validating payload", error);
337
+ res.status(500).json({ error: "Internal Server Error" });
338
+ }
332
339
  };
333
340
  };
334
341
  ```
335
342
 
336
- #### Usage in venila js code
343
+ #### 2.2 Use perfect-payload in your middleware like below(for COMMON JS)
337
344
 
338
345
  ```javascript
339
- import { perfectPayloadV1 } from "perfect-payload";
340
- const result = perfectPayloadV1(dataObject, ruleObject);
341
- console.log(result?.statusCode)
342
- console.log(result?.isValid)
346
+ function validatePayload({ rule }) {
347
+ return async (req, res, next) => {
348
+ try {
349
+ const { perfectPayloadV1 } = await import("perfect-payload");
350
+ const { statusCode, ...response } = perfectPayloadV1(req?.body, rule);
351
+
352
+ if (+statusCode >= 200 && +statusCode <= 299) {
353
+ next();
354
+ } else {
355
+ res.status(statusCode).json(response);
356
+ }
357
+ } catch (error) {
358
+ console.error("Error validating payload", error);
359
+ res.status(500).json({ error: "Internal Server Error" });
360
+ }
361
+ };
362
+ }
363
+
364
+ module.exports = { validatePayload };
343
365
  ```
344
366
 
345
367
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perfect-payload",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "description": "rich json validator",
6
6
  "main": "index.js",