xdbc 1.0.116 → 1.0.118

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/dist/DBC/TYPE.js CHANGED
@@ -76,6 +76,25 @@ export class TYPE extends DBC {
76
76
  check(toCheck) {
77
77
  return TYPE.checkAlgorithm(toCheck, this.type);
78
78
  }
79
+ /**
80
+ * Invokes the {@link TYPE.checkAlgorithm } passing the value **toCheck** and the {@link TYPE.type } .
81
+ *
82
+ * @param toCheck See {@link INSTANCE.checkAlgorithm }.
83
+ * @param type See {@link INSTANCE.checkAlgorithm }.
84
+ * @param id A {@link string } identifying this {@link TYPE } via the {@link DBC.Infringement }-Message.
85
+ *
86
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link TYPE }.
87
+ *
88
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
89
+ static tsCheck(toCheck, type, id = undefined) {
90
+ const result = TYPE.checkAlgorithm(toCheck, type);
91
+ if (result === true) {
92
+ return toCheck;
93
+ }
94
+ else {
95
+ throw new DBC.Infringement(`${id ? `(${id}) ` : ""}${result}`);
96
+ }
97
+ }
79
98
  /**
80
99
  * Creates this {@link TYPE } by setting the protected property {@link TYPE.type } used by {@link TYPE.check }.
81
100
  *
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.116",
2
+ "version": "1.0.118",
3
3
  "name": "xdbc",
4
4
  "scripts": {
5
5
  "docs": "typedoc",
@@ -31,7 +31,8 @@
31
31
  "dependencies": {
32
32
  "@types/reflect-metadata": "^0.1.0",
33
33
  "parcel": "^2.14.4",
34
- "reflect-metadata": "^0.2.2"
34
+ "reflect-metadata": "^0.2.2",
35
+ "zod": "^4.0.5"
35
36
  },
36
37
  "description": "A Typescript Design by Contract Framework",
37
38
  "repository": {
@@ -117,7 +117,7 @@ export class DEFINED extends DBC {
117
117
  *
118
118
  * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }.*/
119
119
  public static tsCheck<CANDIDATE = unknown >( toCheck : CANDIDATE | undefined | null, id : string | undefined = undefined ) : CANDIDATE {
120
- const result = DEFINED.checkAlgorithm(toCheck );
120
+ const result = DEFINED.checkAlgorithm(toCheck);
121
121
 
122
122
  if( result === true ) {
123
123
  return toCheck as CANDIDATE ;
@@ -6,13 +6,13 @@ import { DBC } from "../DBC";
6
6
  * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
7
7
  export class INSTANCE extends DBC {
8
8
  /**
9
- * Checks if the value **toCheck** is complies to the {@link RegExp } **expression**.
9
+ * Checks if the value **toCheck** is an instance of the specified **reference**.
10
10
  *
11
- * @param toCheck The value that has comply to the {@link RegExp } **expression** for this {@link DBC } to be fulfilled.
12
- * @param reference The {@link RegExp } the one **toCheck** has comply to in order for this {@link DBC } to be
13
- * fulfilled.
11
+ * @param toCheck The value that has to be an instance of the **reference** in order for this {@link DBC }
12
+ * to be fulfilled.
13
+ * @param reference The {@link object } the one **toCheck** has to be an instance of.
14
14
  *
15
- * @returns TRUE if the value **toCheck** is of the specified **type**, otherwise FALSE. */
15
+ * @returns TRUE if the value **toCheck** is is an instance of the *reference**, otherwise FALSE. */
16
16
  // biome-ignore lint/suspicious/noExplicitAny: In order to perform an "instanceof" check.
17
17
  public static checkAlgorithm(toCheck: any, reference: any): boolean | string {
18
18
  if (!(toCheck instanceof reference)) {
package/src/DBC/TYPE.ts CHANGED
@@ -110,6 +110,26 @@ export class TYPE extends DBC {
110
110
  public check(toCheck: any) {
111
111
  return TYPE.checkAlgorithm(toCheck, this.type);
112
112
  }
113
+ /**
114
+ * Invokes the {@link TYPE.checkAlgorithm } passing the value **toCheck** and the {@link TYPE.type } .
115
+ *
116
+ * @param toCheck See {@link INSTANCE.checkAlgorithm }.
117
+ * @param type See {@link INSTANCE.checkAlgorithm }.
118
+ * @param id A {@link string } identifying this {@link TYPE } via the {@link DBC.Infringement }-Message.
119
+ *
120
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link TYPE }.
121
+ *
122
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
123
+ public static tsCheck < CANDIDATE = unknown > ( toCheck : any, type : string, id : string | undefined = undefined ) : CANDIDATE {
124
+ const result = TYPE.checkAlgorithm(toCheck, type);
125
+
126
+ if( result === true ) {
127
+ return toCheck ;
128
+ }
129
+ else {
130
+ throw new DBC.Infringement( `${id?`(${id}) `:""}${result as string }`);
131
+ }
132
+ }
113
133
  /**
114
134
  * Creates this {@link TYPE } by setting the protected property {@link TYPE.type } used by {@link TYPE.check }.
115
135
  *
package/src/DBC/ZOD.ts ADDED
@@ -0,0 +1,144 @@
1
+ import { DBC } from "../DBC";
2
+ import { z } from 'zod';
3
+ /**
4
+ * A {@link DBC } defining that the an {@link object }s gotta be an instance of a certain {@link ZOD.schema }.
5
+ *
6
+ * @remarks
7
+ * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
8
+ export class ZOD extends DBC {
9
+ /**
10
+ * Checks if the value **toCheck** complies to the specified {@link z.ZodType }.
11
+ *
12
+ * @param toCheck The value that has to comply to the specified **schema** in order for this {@link DBC }
13
+ * @param schema The {@link z.ZodType } the {@link object } **toCheck** has comply to in order for this {@link DBC } to be
14
+ * fulfilled.
15
+ *
16
+ * @returns TRUE if the value **toCheck** complies to the specified **schema**, otherwise FALSE. */
17
+ // biome-ignore lint/suspicious/noExplicitAny: In order to perform an "instanceof" check.
18
+ public static checkAlgorithm(toCheck: any, schema: z.ZodType): boolean | string {
19
+ if (!schema.safeParse(toCheck).success) {
20
+ return `Value has to be an instance of "${schema}" but is of type "${typeof toCheck}"`;
21
+ }
22
+
23
+ return true;
24
+ }
25
+ /**
26
+ * A parameter-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
27
+ * by the tagged parameter.
28
+ *
29
+ * @param schema See {@link ZOD.checkAlgorithm }.
30
+ * @param path See {@link DBC.decPrecondition }.
31
+ * @param dbc See {@link DBC.decPrecondition }.
32
+ *
33
+ * @returns See {@link DBC.decPrecondition }. */
34
+ public static PRE(
35
+ // biome-ignore lint/suspicious/noExplicitAny: In order to perform an "instanceof" check.
36
+ schema: z.ZodType,
37
+ path: string | undefined = undefined,
38
+ dbc = "WaXCode.DBC",
39
+ ): (
40
+ target: object,
41
+ methodName: string | symbol,
42
+ parameterIndex: number,
43
+ ) => void {
44
+ return DBC.decPrecondition(
45
+ (
46
+ value: object,
47
+ target: object,
48
+ methodName: string,
49
+ parameterIndex: number,
50
+ ) => {
51
+ return ZOD.checkAlgorithm(value, schema);
52
+ },
53
+ dbc,
54
+ path,
55
+ );
56
+ }
57
+ /**
58
+ * A method-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
59
+ * by the tagged method's returnvalue.
60
+ *
61
+ * @param schema See {@link ZOD.checkAlgorithm }.
62
+ * @param path See {@link DBC.Postcondition }.
63
+ * @param dbc See {@link DBC.decPostcondition }.
64
+ *
65
+ * @returns See {@link DBC.decPostcondition }. */
66
+ public static POST(
67
+ // biome-ignore lint/suspicious/noExplicitAny: In order to perform an "instanceof" check.
68
+ schema: z.ZodType,
69
+ path: string | undefined = undefined,
70
+ dbc = "WaXCode.DBC",
71
+ ): (
72
+ target: object,
73
+ propertyKey: string,
74
+ descriptor: PropertyDescriptor,
75
+ ) => PropertyDescriptor {
76
+ return DBC.decPostcondition(
77
+ (value: object, target: object, propertyKey: string) => {
78
+ return ZOD.checkAlgorithm(value, schema);
79
+ },
80
+ dbc,
81
+ path,
82
+ );
83
+ }
84
+ /**
85
+ * A field-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
86
+ * by the tagged method's returnvalue.
87
+ *
88
+ * @param schema See {@link ZOD.checkAlgorithm }.
89
+ * @param path See {@link DBC.decInvariant }.
90
+ * @param dbc See {@link DBC.decInvariant }.
91
+ *
92
+ * @returns See {@link DBC.decInvariant }. */
93
+ public static INVARIANT(
94
+ // biome-ignore lint/suspicious/noExplicitAny: In order to perform an "instanceof" check.
95
+ schema: z.ZodType,
96
+ path: string | undefined = undefined,
97
+ dbc = "WaXCode.DBC",
98
+ ) {
99
+ return DBC.decInvariant([new ZOD(schema)], path, dbc);
100
+ }
101
+ // #endregion Condition checking.
102
+ // #region Referenced Condition checking.
103
+ //
104
+ // For usage in dynamic scenarios (like with AE-DBC).
105
+ //
106
+ /**
107
+ * Invokes the {@link ZOD.checkAlgorithm } passing the value **toCheck** and the {@link ZOD.schema } .
108
+ *
109
+ * @param toCheck See {@link ZOD.checkAlgorithm }.
110
+ *
111
+ * @returns See {@link ZOD.checkAlgorithm}. */
112
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
113
+ public check(toCheck: any) {
114
+ return ZOD.checkAlgorithm(toCheck, this.schema);
115
+ }
116
+ /**
117
+ * Invokes the {@link ZOD.checkAlgorithm } passing the value **toCheck** and the {@link ZOD.schema } .
118
+ *
119
+ * @param toCheck See {@link ZOD.checkAlgorithm }.
120
+ * @param schema See {@link ZOD.checkAlgorithm }.
121
+ * @param id A {@link string } identifying this {@link ZOD } via the {@link DBC.Infringement }-Message.
122
+ *
123
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link ZOD }.
124
+ *
125
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
126
+ public static tsCheck < CANDIDATE = unknown > ( toCheck : any, schema : z.ZodType, id : string | undefined = undefined ) : CANDIDATE {
127
+ const result = ZOD.checkAlgorithm(toCheck, schema);
128
+
129
+ if( result === true ) {
130
+ return toCheck ;
131
+ }
132
+ else {
133
+ throw new DBC.Infringement( `${id?`(${id}) `:""}${result as string }`);
134
+ }
135
+ }
136
+ /**
137
+ * Creates this {@link ZOD } by setting the protected property {@link ZOD.schema } used by {@link ZOD.check }.
138
+ *
139
+ * @param schema See {@link ZOD.check }. */
140
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
141
+ public constructor(protected schema: z.ZodType) {
142
+ super();
143
+ }
144
+ }