schema-shield 0.0.4 → 0.0.6

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/lib/utils.ts CHANGED
@@ -1,36 +1,83 @@
1
1
  import { CompiledSchema } from "./index";
2
2
 
3
+ interface ErrorTree {
4
+ message: string;
5
+ keyword: string;
6
+ item?: string | number;
7
+ schemaPath: string;
8
+ instancePath: string;
9
+ data?: any;
10
+ cause?: ErrorTree;
11
+ }
12
+
3
13
  export class ValidationError extends Error {
4
14
  message: string;
5
- item: string | number;
15
+ item?: string | number;
6
16
  keyword: string;
7
- cause: ValidationError;
8
- path: string = "";
17
+ cause?: ValidationError;
18
+ schemaPath: string = "";
19
+ instancePath: string = "";
9
20
  data?: any;
10
21
  schema?: CompiledSchema;
11
22
 
12
- private _getCause(pointer = "#") {
13
- const path =
14
- pointer +
15
- "/" +
16
- this.keyword +
17
- (typeof this.item !== "undefined" ? "/" + this.item : "");
23
+ private _getCause(pointer = "#", instancePointer = "#"): ValidationError {
24
+ let schemaPath = `${pointer}/${this.keyword}`;
25
+ let instancePath = `${instancePointer}`;
26
+ if (typeof this.item !== "undefined") {
27
+ if (typeof this.item === "string" && this.item in this.schema) {
28
+ schemaPath += `/${this.item}`;
29
+ }
30
+ instancePath += `/${this.item}`;
31
+ }
32
+
33
+ this.instancePath = instancePath;
34
+ this.schemaPath = schemaPath;
18
35
 
19
- if (!this.cause) {
20
- this.path = path;
36
+ // If there is no cause or the cause is not a ValidationError, return this
37
+ if (!this.cause || !(this.cause instanceof ValidationError)) {
21
38
  return this;
22
39
  }
23
40
 
24
- return this.cause._getCause(path);
41
+ return this.cause._getCause(schemaPath, instancePath);
25
42
  }
26
43
 
27
- getCause() {
44
+ getCause(): ValidationError {
28
45
  return this._getCause();
29
46
  }
47
+
48
+ private _getTree(): ErrorTree {
49
+ const tree: ErrorTree = {
50
+ message: this.message,
51
+ keyword: this.keyword,
52
+ item: this.item,
53
+ schemaPath: this.schemaPath,
54
+ instancePath: this.instancePath,
55
+ data: this.data
56
+ };
57
+
58
+ if (this.cause) {
59
+ tree.cause = this.cause._getTree();
60
+ }
61
+
62
+ return tree;
63
+ }
64
+
65
+ getTree(): ErrorTree {
66
+ this.getCause();
67
+ return this._getTree();
68
+ }
69
+
70
+ getPath() {
71
+ const cause = this.getCause();
72
+ return {
73
+ schemaPath: cause.schemaPath,
74
+ instancePath: cause.instancePath
75
+ };
76
+ }
30
77
  }
31
78
 
32
79
  export interface DefineErrorOptions {
33
- item?: any; // Final item in the path
80
+ item?: any; // Final item in the schemaPath
34
81
  cause?: ValidationError; // Cause of the error
35
82
  data?: any; // Data that caused the error
36
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schema-shield",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "A fast library that protects your JSON schema from invalid data.",
5
5
  "repository": "git@github.com:Masquerade-Circus/schema-shield.git",
6
6
  "author": "Masquerade <christian@masquerade-circus.net>",
@@ -89,6 +89,7 @@
89
89
  "@release-it/conventional-changelog": "^5.1.1",
90
90
  "@typescript-eslint/eslint-plugin": "^5.56.0",
91
91
  "@typescript-eslint/parser": "^5.56.0",
92
+ "ajv": "^8.12.0",
92
93
  "cz-conventional-changelog": "^3.3.0",
93
94
  "esbuild": "^0.17.12",
94
95
  "eslint": "^8.36.0",