@cargolift-cdi/lib-payload 0.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 (43) hide show
  1. package/dist/cjs/index.js +20 -0
  2. package/dist/cjs/index.js.map +1 -0
  3. package/dist/cjs/package.json +1 -0
  4. package/dist/cjs/payload-validation/payload-validation.js +313 -0
  5. package/dist/cjs/payload-validation/payload-validation.js.map +1 -0
  6. package/dist/cjs/payload-validation/validation.interface.js +3 -0
  7. package/dist/cjs/payload-validation/validation.interface.js.map +1 -0
  8. package/dist/cjs/response-template/response-template.interface.js +2 -0
  9. package/dist/cjs/response-template/response-template.interface.js.map +1 -0
  10. package/dist/cjs/response-template/response-template.js +74 -0
  11. package/dist/cjs/response-template/response-template.js.map +1 -0
  12. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  13. package/dist/cjs/util/util.js +24 -0
  14. package/dist/cjs/util/util.js.map +1 -0
  15. package/dist/esm/index.js +4 -0
  16. package/dist/esm/index.js.map +1 -0
  17. package/dist/esm/payload-validation/payload-validation.js +309 -0
  18. package/dist/esm/payload-validation/payload-validation.js.map +1 -0
  19. package/dist/esm/payload-validation/validation.interface.js +2 -0
  20. package/dist/esm/payload-validation/validation.interface.js.map +1 -0
  21. package/dist/esm/payload-validation.js +309 -0
  22. package/dist/esm/payload-validation.js.map +1 -0
  23. package/dist/esm/response-template/response-template.interface.js +2 -0
  24. package/dist/esm/response-template/response-template.interface.js.map +1 -0
  25. package/dist/esm/response-template/response-template.js +70 -0
  26. package/dist/esm/response-template/response-template.js.map +1 -0
  27. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
  28. package/dist/esm/types/schema.js +2 -0
  29. package/dist/esm/types/schema.js.map +1 -0
  30. package/dist/esm/types/types.js +2 -0
  31. package/dist/esm/types/types.js.map +1 -0
  32. package/dist/esm/util/util.js +20 -0
  33. package/dist/esm/util/util.js.map +1 -0
  34. package/dist/types/index.d.ts +3 -0
  35. package/dist/types/payload-validation/payload-validation.d.ts +19 -0
  36. package/dist/types/payload-validation/validation.interface.d.ts +30 -0
  37. package/dist/types/payload-validation.d.ts +20 -0
  38. package/dist/types/response-template/response-template.d.ts +21 -0
  39. package/dist/types/response-template/response-template.interface.d.ts +0 -0
  40. package/dist/types/types/schema.d.ts +22 -0
  41. package/dist/types/types/types.d.ts +8 -0
  42. package/dist/types/util/util.d.ts +3 -0
  43. package/package.json +54 -0
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/types/schema.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ export class PayloadUtil {
2
+ static normalize(payload) {
3
+ if (payload === null || payload === undefined) {
4
+ return undefined;
5
+ }
6
+ if (typeof payload === "object") {
7
+ return payload;
8
+ }
9
+ if (typeof payload === "string") {
10
+ try {
11
+ return JSON.parse(payload);
12
+ }
13
+ catch {
14
+ return undefined;
15
+ }
16
+ }
17
+ return undefined;
18
+ }
19
+ }
20
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/util/util.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,WAAW;IACf,MAAM,CAAC,SAAS,CAAC,OAAgB;QACtC,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC9C,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,OAAO,OAA0C,CAAC;QACpD,CAAC;QACD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CAGF"}
@@ -0,0 +1,3 @@
1
+ export * from "./payload-validation/payload-validation.js";
2
+ export * from "./response-template/response-template.js";
3
+ export * from "./util/util.js";
@@ -0,0 +1,19 @@
1
+ import { Schema, ValidationResult } from "./validation.interface.js";
2
+ export declare class PayloadValidation {
3
+ run(rawPayload: any, schema: Schema): Promise<ValidationResult>;
4
+ private applyValidation;
5
+ private validateRule;
6
+ private validateRequired;
7
+ private validateType;
8
+ private validateLength;
9
+ private validateRange;
10
+ private validateArraySize;
11
+ private validateAllowedValues;
12
+ private validatePattern;
13
+ private pushError;
14
+ private resolvePathValues;
15
+ private parseSegment;
16
+ private checkType;
17
+ parsePayload(raw: any): any;
18
+ private detectType;
19
+ }
@@ -0,0 +1,30 @@
1
+ interface SchemaValidation {
2
+ field: string;
3
+ type: "string" | "number" | "boolean" | "date" | "object" | "array" | "email";
4
+ required?: boolean;
5
+ length?: {
6
+ min?: number;
7
+ max?: number;
8
+ };
9
+ range?: {
10
+ min?: number;
11
+ max?: number;
12
+ };
13
+ minItems?: number;
14
+ maxItems?: number;
15
+ allowedValues?: string[] | number[];
16
+ pattern?: string;
17
+ message?: string;
18
+ }
19
+ export interface Schema {
20
+ validations: SchemaValidation[];
21
+ }
22
+ export interface ValidationResult {
23
+ success: boolean;
24
+ errors: {
25
+ field: string;
26
+ error: string;
27
+ }[];
28
+ errorsList: string[];
29
+ }
30
+ export {};
@@ -0,0 +1,20 @@
1
+ import { ExecutionResult } from "./types/types.js";
2
+ import { Schema } from "./types/schema.js";
3
+ export declare class PayloadValidation {
4
+ run(rawPayload: any, schema: Schema): Promise<ExecutionResult>;
5
+ private applyValidation;
6
+ private validateRule;
7
+ private validateRequired;
8
+ private validateType;
9
+ private validateLength;
10
+ private validateRange;
11
+ private validateArraySize;
12
+ private validateAllowedValues;
13
+ private validatePattern;
14
+ private pushError;
15
+ private resolvePathValues;
16
+ private parseSegment;
17
+ private checkType;
18
+ parsePayload(raw: any): any;
19
+ private detectType;
20
+ }
@@ -0,0 +1,21 @@
1
+ export interface ResponseTemplateFieldConfig {
2
+ path: string;
3
+ equals?: string | number | boolean | null;
4
+ fallback?: string;
5
+ }
6
+ export interface HttpResponseTemplate {
7
+ success?: ResponseTemplateFieldConfig | ResponseTemplateFieldConfig[];
8
+ message?: string | string[];
9
+ details?: string | string[];
10
+ }
11
+ export interface ResponseTemplateResult {
12
+ success: boolean;
13
+ message?: string | string[];
14
+ details?: string | string[];
15
+ }
16
+ export declare class ResponseTemplate {
17
+ validate(template: HttpResponseTemplate, payload: any): ResponseTemplateResult | undefined;
18
+ private evaluateSuccess;
19
+ private extractTemplateValue;
20
+ private toPathArray;
21
+ }
@@ -0,0 +1,22 @@
1
+ interface SchemaValidation {
2
+ field: string;
3
+ type: "string" | "number" | "boolean" | "date" | "object" | "array" | "email";
4
+ required?: boolean;
5
+ length?: {
6
+ min?: number;
7
+ max?: number;
8
+ };
9
+ range?: {
10
+ min?: number;
11
+ max?: number;
12
+ };
13
+ minItems?: number;
14
+ maxItems?: number;
15
+ allowedValues?: string[] | number[];
16
+ pattern?: string;
17
+ message?: string;
18
+ }
19
+ export interface Schema {
20
+ validations: SchemaValidation[];
21
+ }
22
+ export {};
@@ -0,0 +1,8 @@
1
+ export interface ExecutionResult {
2
+ success: boolean;
3
+ errors: {
4
+ field: string;
5
+ error: string;
6
+ }[];
7
+ errorsList: string[];
8
+ }
@@ -0,0 +1,3 @@
1
+ export declare class PayloadUtil {
2
+ static normalize(payload: unknown): Record<string, any> | unknown[] | undefined;
3
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@cargolift-cdi/lib-payload",
3
+ "private": false,
4
+ "version": "0.1.0",
5
+ "description": "Lib de tratamento e validação de payloads para serviços do middleware da Cargolift CDI",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/types/index.d.ts",
20
+ "import": "./dist/esm/index.js",
21
+ "require": "./dist/cjs/index.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "main": "./dist/cjs/index.js",
26
+ "module": "./dist/esm/index.js",
27
+ "types": "./dist/types/index.d.ts",
28
+ "scripts": {
29
+ "build": "npm run build:clean && npm run build:esm && npm run build:cjs",
30
+ "build:clean": "",
31
+ "build:esm": "tsc -p tsconfig.esm.json",
32
+ "build:cjs": "tsc -p tsconfig.cjs.json && node -e \"require('fs').mkdirSync('dist/cjs',{recursive:true}); require('fs').writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}')\"",
33
+ "build:smoke": "tsc -p .",
34
+ "dev": "tsc -p . -w",
35
+ "smoke": "npm run build:smoke && node ./dist/smoke/smoke-run.js",
36
+ "smoke_debug": "npm run build:smoke && node ./dist/smoke/smoke-debug.js",
37
+ "prepare": "npm run build",
38
+ "prepublishOnly": "npm run build && npm test",
39
+ "test": "vitest run --reporter=dot",
40
+ "test:watch": "vitest"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^24.7.0",
47
+ "typescript": "^5.9.3",
48
+ "vitest": "^3.2.4"
49
+ },
50
+ "dependencies": {
51
+ "@cargolift-cdi/common": "^1.0.62",
52
+ "@cargolift-cdi/types": "^0.1.67"
53
+ }
54
+ }