xdbc 1.0.217 → 1.0.219

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 (120) hide show
  1. package/.gitattributes +16 -8
  2. package/.vscode/settings.json +3 -3
  3. package/.vscode/tasks.json +23 -23
  4. package/ASSESSMENT.md +249 -0
  5. package/README.md +538 -408
  6. package/__tests__/DBC/AE.test.ts +62 -62
  7. package/__tests__/DBC/ARRAY.test.ts +91 -91
  8. package/__tests__/DBC/DEFINED.test.ts +53 -53
  9. package/__tests__/DBC/DOM.test.ts +786 -0
  10. package/__tests__/DBC/Decorators.test.ts +367 -367
  11. package/__tests__/DBC/EQ.test.ts +13 -13
  12. package/__tests__/DBC/GREATER.test.ts +31 -31
  13. package/__tests__/DBC/HasAttribute.test.ts +60 -60
  14. package/__tests__/DBC/IF.test.ts +62 -62
  15. package/__tests__/DBC/INSTANCE.test.ts +13 -13
  16. package/__tests__/DBC/JSON.OP.test.ts +47 -47
  17. package/__tests__/DBC/JSON.Parse.test.ts +17 -17
  18. package/__tests__/DBC/OR.test.ts +14 -14
  19. package/__tests__/DBC/PLAIN_OBJECT.test.ts +109 -109
  20. package/__tests__/DBC/REGEX.test.ts +17 -17
  21. package/__tests__/DBC/TYPE.test.ts +13 -13
  22. package/__tests__/DBC/UNDEFINED.test.ts +45 -45
  23. package/__tests__/DBC/ZOD.test.ts +54 -54
  24. package/__tests__/DBC/onInfringement.test.ts +262 -0
  25. package/biome.json +45 -40
  26. package/dist/DBC/AE.js +172 -0
  27. package/dist/DBC/ARR/PLAIN_OBJECT.d.ts +0 -3
  28. package/dist/DBC/ARR/PLAIN_OBJECT.js +95 -0
  29. package/dist/DBC/ARRAY.d.ts +0 -3
  30. package/dist/DBC/ARRAY.js +90 -0
  31. package/dist/DBC/COMPARISON/GREATER.js +21 -0
  32. package/dist/DBC/COMPARISON/GREATER_OR_EQUAL.js +21 -0
  33. package/dist/DBC/COMPARISON/LESS.js +21 -0
  34. package/dist/DBC/COMPARISON/LESS_OR_EQUAL.js +21 -0
  35. package/dist/DBC/COMPARISON.js +98 -0
  36. package/dist/DBC/DEFINED.js +87 -0
  37. package/dist/DBC/DOM.d.ts +123 -0
  38. package/dist/DBC/DOM.js +362 -0
  39. package/dist/DBC/EQ/DIFFERENT.js +34 -0
  40. package/dist/DBC/EQ.js +101 -0
  41. package/dist/DBC/GREATER.js +99 -0
  42. package/dist/DBC/HasAttribute.js +101 -0
  43. package/dist/DBC/IF.js +96 -0
  44. package/dist/DBC/INSTANCE.js +122 -0
  45. package/dist/DBC/JSON.OP.js +120 -0
  46. package/dist/DBC/JSON.Parse.js +104 -0
  47. package/dist/DBC/OR.js +125 -0
  48. package/dist/DBC/REGEX.js +136 -0
  49. package/dist/DBC/TYPE.js +112 -0
  50. package/dist/DBC/UNDEFINED.js +87 -0
  51. package/dist/DBC/ZOD.js +99 -0
  52. package/dist/DBC.d.ts +18 -4
  53. package/dist/DBC.js +645 -0
  54. package/dist/Demo.d.ts +10 -0
  55. package/dist/Demo.js +713 -0
  56. package/dist/Test.html +18 -0
  57. package/dist/bundle.js +6140 -405
  58. package/dist/index.d.ts +22 -0
  59. package/dist/index.html +18 -0
  60. package/dist/index.js +22 -0
  61. package/docs/assets/highlight.css +22 -22
  62. package/docs/assets/icons.js +17 -17
  63. package/docs/assets/main.js +60 -60
  64. package/docs/assets/style.css +1640 -1640
  65. package/docs/classes/DBC.DBC.html +98 -98
  66. package/docs/classes/DBC_AE.AE.html +160 -160
  67. package/docs/classes/DBC_EQ.EQ.html +131 -131
  68. package/docs/classes/DBC_GREATER.GREATER.html +139 -139
  69. package/docs/classes/DBC_INSTANCE.INSTANCE.html +130 -130
  70. package/docs/classes/DBC_JSON.OP.JSON_OP.html +138 -138
  71. package/docs/classes/DBC_JSON.Parse.JSON_Parse.html +129 -129
  72. package/docs/classes/DBC_OR.OR.html +137 -137
  73. package/docs/classes/DBC_REGEX.REGEX.html +136 -136
  74. package/docs/classes/DBC_TYPE.TYPE.html +130 -130
  75. package/docs/classes/Demo.Demo.html +14 -14
  76. package/docs/hierarchy.html +1 -1
  77. package/docs/index.html +1 -1
  78. package/docs/modules/DBC.html +1 -1
  79. package/docs/modules/DBC_AE.html +1 -1
  80. package/docs/modules/DBC_EQ.html +1 -1
  81. package/docs/modules/DBC_GREATER.html +1 -1
  82. package/docs/modules/DBC_INSTANCE.html +1 -1
  83. package/docs/modules/DBC_JSON.OP.html +1 -1
  84. package/docs/modules/DBC_JSON.Parse.html +1 -1
  85. package/docs/modules/DBC_OR.html +1 -1
  86. package/docs/modules/DBC_REGEX.html +1 -1
  87. package/docs/modules/DBC_TYPE.html +1 -1
  88. package/docs/modules/Demo.html +1 -1
  89. package/jest.config.js +32 -32
  90. package/package.json +71 -55
  91. package/src/DBC/AE.ts +269 -288
  92. package/src/DBC/ARR/PLAIN_OBJECT.ts +122 -133
  93. package/src/DBC/ARRAY.ts +117 -127
  94. package/src/DBC/COMPARISON/GREATER.ts +41 -46
  95. package/src/DBC/COMPARISON/GREATER_OR_EQUAL.ts +41 -45
  96. package/src/DBC/COMPARISON/LESS.ts +41 -45
  97. package/src/DBC/COMPARISON/LESS_OR_EQUAL.ts +41 -45
  98. package/src/DBC/COMPARISON.ts +149 -159
  99. package/src/DBC/DEFINED.ts +117 -122
  100. package/src/DBC/DOM.ts +453 -0
  101. package/src/DBC/EQ/DIFFERENT.ts +51 -57
  102. package/src/DBC/EQ.ts +154 -163
  103. package/src/DBC/HasAttribute.ts +149 -154
  104. package/src/DBC/IF.ts +173 -179
  105. package/src/DBC/INSTANCE.ts +168 -171
  106. package/src/DBC/JSON.OP.ts +178 -186
  107. package/src/DBC/JSON.Parse.ts +150 -157
  108. package/src/DBC/OR.ts +183 -187
  109. package/src/DBC/REGEX.ts +195 -196
  110. package/src/DBC/TYPE.ts +142 -149
  111. package/src/DBC/UNDEFINED.ts +115 -117
  112. package/src/DBC/ZOD.ts +130 -135
  113. package/src/DBC.ts +902 -904
  114. package/src/Demo.ts +537 -404
  115. package/src/index.ts +22 -0
  116. package/tsconfig.json +18 -18
  117. package/tsconfig.test.json +7 -7
  118. package/typedoc.json +16 -16
  119. package/webpack.config.js +27 -27
  120. package/Assessment.md +0 -507
@@ -0,0 +1,136 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } providing {@link REGEX }-contracts and standard {@link RegExp } for common use cases in {@link REGEX.stdExp }.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
7
+ export class REGEX extends DBC {
8
+ static get stdExp() {
9
+ if (!REGEX._stdExp) {
10
+ REGEX._stdExp = {
11
+ htmlAttributeName: /^[a-zA-Z_:][a-zA-Z0-9_.:-]*$/,
12
+ eMail: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i,
13
+ property: /^[$_A-Za-z][$_A-Za-z0-9]*$/,
14
+ url: /^(?:https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i,
15
+ keyPath: /^([a-zA-Z_$][a-zA-Z0-9_$]*\.)*[a-zA-Z_$][a-zA-Z0-9_$]*$/,
16
+ date: /^\d{1,4}[.\/-]\d{1,2}[.\/-]\d{1,4}$/i,
17
+ dateFormat: /^((D{1,2}[./-]M{1,2}[./-]Y{1,4})|(M{1,2}[./-]D{1,2}[./-]Y{1,4})|Y{1,4}[./-]D{1,2}[./-]M{1,2}|(Y{1,4}[./-]M{1,2}[./-]D{1,2}))$/i,
18
+ cssSelector: /^(?:\*|#[\w-]+|\.[\w-]+|(?:[\w-]+|\*)(?::(?:[\w-]+(?:\([\w-]+\))?)+)?(?:\[(?:[\w-]+(?:(?:=|~=|\|=|\*=|\$=|\^=)\s*(?:"[^"]*"|'[^']*'|[\w-]+)\s*)?)?\])+|\[\s*[\w-]+\s*=\s*(?:"[^"]*"|'[^']*'|[\w-]+)\s*\])(?:,\s*(?:\*|#[\w-]+|\.[\w-]+|(?:[\w-]+|\*)(?::(?:[\w-]+(?:\([\w-]+\))?)+)?(?:\[(?:[\w-]+(?:(?:=|~=|\|=|\*=|\$=|\^=)\s*(?:"[^"]*"|'[^']*'|[\w-]+)\s*)?)?\])+|\[\s*[\w-]+\s*=\s*(?:"[^"]*"|'[^']*'|[\w-]+)\s*\]))*$/,
19
+ boolean: /^(TRUE|FALSE)$/i,
20
+ colorCodeHEX: /^#([A-Fa-f\d]{3,4}|[A-Fa-f\d]{6}|[A-Fa-f\d]{8})$/i,
21
+ simpleHotkey: /^((Alt|Ctrl|Shift|Meta)\+)+[a-z\d]$/i,
22
+ bcp47: /^(?:[a-z]{2,3}(?:-[a-z]{3}){0,3}|[a-z]{4}|[a-z]{5,8})(?:-[a-z]{4})?(?:-[a-z]{2}|-[0-9]{3})?(?:-[a-z0-9]{5,8}|-[0-9][a-z0-9]{3})*(?:-[0-9a-wy-z](?:-[a-z0-9]{2,8})+)*(?:-x(?:-[a-z0-9]{1,8})+)?$|^x(?:-[a-z0-9]{1,8})+$/i,
23
+ };
24
+ }
25
+ return REGEX._stdExp;
26
+ }
27
+ // #region Condition checking.
28
+ /**
29
+ * Checks if the value **toCheck** is complies to the {@link RegExp } **expression**.
30
+ *
31
+ * @param toCheck The value that has comply to the {@link RegExp } **expression** for this {@link DBC } to be fulfilled.
32
+ * @param expression The {@link RegExp } the one **toCheck** has comply to in order for this {@link DBC } to be
33
+ * fulfilled.
34
+ *
35
+ * @returns TRUE if the value **toCheck** complies with the {@link RegExp } **expression**, otherwise FALSE. */
36
+ static checkAlgorithm(toCheck, expression) {
37
+ if (toCheck === undefined || toCheck === null || toCheck === "")
38
+ return true;
39
+ if (!expression.test(toCheck)) {
40
+ return `Value has to comply to regular expression "${expression}"`;
41
+ }
42
+ return true;
43
+ }
44
+ /**
45
+ * A parameter-decorator factory using the {@link REGEX.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
46
+ * by the tagged parameter.
47
+ *
48
+ * @param expression See {@link REGEX.checkAlgorithm }.
49
+ * @param path See {@link DBC.decPrecondition }.
50
+ * @param dbc See {@link DBC.decPrecondition }.
51
+ *
52
+ * @returns See {@link DBC.decPrecondition }. */
53
+ static PRE(expression, path = undefined, hint = undefined, dbc = undefined) {
54
+ return DBC.createPRE(REGEX.checkAlgorithm, [expression], dbc, path, hint);
55
+ }
56
+ /**
57
+ * A method-decorator factory using the {@link REGEX.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
58
+ * by the tagged method's returnvalue.
59
+ *
60
+ * @param expression See {@link REGEX.checkAlgorithm }.
61
+ * @param path See {@link DBC.Postcondition }.
62
+ * @param dbc See {@link DBC.decPostcondition }.
63
+ * @param hint See {@link DBC.decPostcondition }.
64
+ *
65
+ * @returns See {@link DBC.decPostcondition }. */
66
+ static POST(expression, path = undefined, hint = undefined, dbc = undefined) {
67
+ return DBC.createPOST(REGEX.checkAlgorithm, [expression], dbc, path, hint);
68
+ }
69
+ /**
70
+ * A field-decorator factory using the {@link REGEX.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
71
+ * by the tagged field.
72
+ *
73
+ * @param expression See {@link REGEX.checkAlgorithm }.
74
+ * @param path See {@link DBC.decInvariant }.
75
+ * @param dbc See {@link DBC.decInvariant }.
76
+ * @param hint See {@link DBC.decInvariant }.
77
+ * @returns See {@link DBC.decInvariant }. */
78
+ static INVARIANT(expression, path = undefined, hint = undefined, dbc = undefined) {
79
+ return DBC.createINVARIANT(REGEX, [expression], dbc, path, hint);
80
+ }
81
+ // #endregion Condition checking.
82
+ // #region Referenced Condition checking.
83
+ //
84
+ // For usage in dynamic scenarios (like with AE-DBC).
85
+ //
86
+ /**
87
+ * Invokes the {@link REGEX.checkAlgorithm } passing the value **toCheck** and {@link REGEX.equivalent }.
88
+ *
89
+ * @param toCheck See {@link REGEX.checkAlgorithm }.
90
+ *
91
+ * @returns See {@link EQ.checkAlgorithm}. */
92
+ check(toCheck) {
93
+ return REGEX.checkAlgorithm(toCheck, this.expression);
94
+ }
95
+ /**
96
+ * Type-safe check that validates a value against a regular expression and returns it as the specified type.
97
+ *
98
+ * @param toCheck The value to check against the regular expression.
99
+ * @param expression The regular expression to validate against.
100
+ * @param hint Optional hint message to include in the error if validation fails.
101
+ * @param id Optional identifier to include in the error message.
102
+ *
103
+ * @returns The validated value cast to the CANDIDATE type.
104
+ *
105
+ * @throws {@link DBC.Infringement} if the value does not match the regular expression. */
106
+ static tsCheck(toCheck, expression, hint = undefined, id = undefined, dbc = undefined) {
107
+ const result = REGEX.checkAlgorithm(toCheck, expression);
108
+ if (result === true) {
109
+ return toCheck;
110
+ }
111
+ DBC.reportTsCheckInfringement(`${id ? `(${id}) ` : ""}${result}${hint ? ` ✨ ${hint} ✨` : ""}`, dbc);
112
+ return toCheck;
113
+ }
114
+ /**
115
+ * Creates this {@link REGEX } by setting the protected property {@link REGEX.expression } used by {@link REGEX.check }.
116
+ *
117
+ * @param expression See {@link REGEX.check }. */
118
+ constructor(expression) {
119
+ super();
120
+ this.expression = expression;
121
+ }
122
+ // #endregion Referenced Condition checking.
123
+ // #region In-Method checking.
124
+ /**
125
+ * Invokes the {@link REGEX.checkAlgorithm } passing the value **toCheck** and {@link REGEX.expression }.
126
+ *
127
+ * @param toCheck See {@link REGEX.checkAlgorithm}.
128
+ * @param expression See {@link REGEX.checkAlgorithm}.
129
+ */
130
+ static check(toCheck, expression, dbc = undefined) {
131
+ const checkResult = REGEX.checkAlgorithm(toCheck, expression);
132
+ if (typeof checkResult === "string") {
133
+ DBC.reportTsCheckInfringement(checkResult, dbc);
134
+ }
135
+ }
136
+ }
@@ -0,0 +1,112 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that an {@link object }s gotta be of certain {@link TYPE.type }.
4
+ *
5
+ * @remarks
6
+ * Author: Salvatore Callari (Callari@WaXCode.net) / 2025
7
+ * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
8
+ export class TYPE extends DBC {
9
+ /**
10
+ * Checks if the value **toCheck** is of the **type** specified.
11
+ *
12
+ * @param toCheck The {@link Object } which's **type** to check.
13
+ * @param type The type the {@link object} **toCheck** has to be of. Can be a single type or multiple types separated by "|".
14
+ *
15
+ * @returns TRUE if the value **toCheck** is of the specified **type**, otherwise FALSE. */
16
+ static checkAlgorithm(toCheck, type) {
17
+ if (toCheck === undefined || toCheck === null)
18
+ return true;
19
+ const types = type.split("|").map((t) => t.trim());
20
+ const actualType = typeof toCheck;
21
+ // #region Check if the actual type matches at least one of the specified types
22
+ const isValid = types.some((t) => actualType === t);
23
+ if (!isValid) {
24
+ if (types.length === 1) {
25
+ return `Value has to be of type "${type}" but is of type "${actualType}"`;
26
+ }
27
+ return `Value has to be of type "${types.join(" | ")}" but is of type "${actualType}"`;
28
+ }
29
+ // #endregion Check if the actual type matches at least one of the specified types
30
+ return true;
31
+ }
32
+ /**
33
+ * A parameter-decorator factory using the {@link TYPE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
34
+ * by the tagged parameter.
35
+ *
36
+ * @param type See {@link TYPE.checkAlgorithm }.
37
+ * @param path A ::-separated list of dotted paths to check. Each path points to a property within the parameter value.
38
+ * Undefined properties are skipped. See {@link DBC.decPrecondition }.
39
+ * @param dbc See {@link DBC.decPrecondition }.
40
+ *
41
+ * @returns See {@link DBC.decPrecondition }. */
42
+ static PRE(type, path = undefined, hint = undefined, dbc = undefined) {
43
+ return DBC.createPRE(TYPE.checkAlgorithm, [type], dbc, path, hint);
44
+ }
45
+ /**
46
+ * A method-decorator factory using the {@link TYPE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
47
+ * by the tagged method's returnvalue.
48
+ *
49
+ * @param type See {@link TYPE.checkAlgorithm }.
50
+ * @param path A ::-separated list of dotted paths to check. Each path points to a property within the parameter value.
51
+ * Undefined properties are skipped. See {@link DBC.decPrecondition }.
52
+ * @param dbc See {@link DBC.decPostcondition }.
53
+ *
54
+ * @returns See {@link DBC.decPostcondition }. */
55
+ static POST(type, path = undefined, hint = undefined, dbc = undefined) {
56
+ return DBC.createPOST(TYPE.checkAlgorithm, [type], dbc, path, hint);
57
+ }
58
+ /**
59
+ * A field-decorator factory using the {@link TYPE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
60
+ * by the tagged field.
61
+ *
62
+ * @param type See {@link TYPE.checkAlgorithm }.
63
+ * @param path A ::-separated list of dotted paths to check. Each path points to a property within the parameter value.
64
+ * Undefined properties are skipped. See {@link DBC.decPrecondition }.
65
+ * @param dbc See {@link DBC.decInvariant }.
66
+ *
67
+ * @returns See {@link DBC.decInvariant }. */
68
+ static INVARIANT(type, path = undefined, hint = undefined, dbc = undefined) {
69
+ return DBC.createINVARIANT(TYPE, [type], dbc, path, hint);
70
+ }
71
+ // #endregion Condition checking.
72
+ // #region Referenced Condition checking.
73
+ //
74
+ // For usage in dynamic scenarios (like with AE-DBC).
75
+ //
76
+ /**
77
+ * Invokes the {@link TYPE.checkAlgorithm } passing the value **toCheck** and the {@link TYPE.type } .
78
+ *
79
+ * @param toCheck See {@link TYPE.checkAlgorithm }.
80
+ *
81
+ * @returns See {@link TYPE.checkAlgorithm}. */
82
+ check(toCheck) {
83
+ return TYPE.checkAlgorithm(toCheck, this.type);
84
+ }
85
+ /**
86
+ * Invokes the {@link TYPE.checkAlgorithm } passing the value **toCheck** and the {@link TYPE.type } .
87
+ *
88
+ * @param toCheck See {@link TYPE.checkAlgorithm }.
89
+ * @param type See {@link TYPE.checkAlgorithm }.
90
+ * @param hint An optional {@link string } providing extra information in case of an infringement.
91
+ * @param id A {@link string } identifying this {@link TYPE } via the {@link DBC.Infringement }-Message.
92
+ *
93
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link TYPE }.
94
+ *
95
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
96
+ static tsCheck(toCheck, type, hint = undefined, id = undefined, dbc = undefined) {
97
+ const result = TYPE.checkAlgorithm(toCheck, type);
98
+ if (result === true) {
99
+ return toCheck;
100
+ }
101
+ DBC.reportTsCheckInfringement(`${id ? `(${id}) ` : ""}${result}${hint ? ` ✨ ${hint} ✨` : ""}`, dbc);
102
+ return toCheck;
103
+ }
104
+ /**
105
+ * Creates this {@link TYPE } by setting the protected property {@link TYPE.type } used by {@link TYPE.check }.
106
+ *
107
+ * @param type See {@link TYPE.check }. */
108
+ constructor(type) {
109
+ super();
110
+ this.type = type;
111
+ }
112
+ }
@@ -0,0 +1,87 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that an {@link object }s must be **undefined**.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
7
+ export class UNDEFINED extends DBC {
8
+ /**
9
+ * Checks if the value **toCheck** is undefined.
10
+ *
11
+ * @param toCheck The {@link Object } to check.
12
+ *
13
+ * @returns TRUE if the value **toCheck** is of the specified **type**, otherwise FALSE. */
14
+ static checkAlgorithm(toCheck) {
15
+ if (toCheck !== undefined) {
16
+ return `Value must be UNDEFINED but it is ${typeof toCheck}`;
17
+ }
18
+ return true;
19
+ }
20
+ /**
21
+ * A parameter-decorator factory using the {@link UNDEFINED.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
22
+ * by the tagged parameter.
23
+ *
24
+ * @param path See {@link DBC.decPrecondition }.
25
+ * @param dbc See {@link DBC.decPrecondition }.
26
+ * @param hint See {@link DBC.decPrecondition }.
27
+ *
28
+ * @returns See {@link DBC.decPrecondition }. */
29
+ static PRE(path = undefined, hint = undefined, dbc = undefined) {
30
+ return DBC.createPRE(UNDEFINED.checkAlgorithm, [], dbc, path, hint);
31
+ }
32
+ /**
33
+ * A method-decorator factory using the {@link UNDEFINED.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
34
+ * by the tagged method's returnvalue.
35
+ *
36
+ * @param path See {@link DBC.Postcondition }.
37
+ * @param dbc See {@link DBC.decPostcondition }.
38
+ * @param hint See {@link DBC.decPostcondition }.
39
+ *
40
+ * @returns See {@link DBC.decPostcondition }. */
41
+ static POST(path = undefined, hint = undefined, dbc = undefined) {
42
+ return DBC.createPOST(UNDEFINED.checkAlgorithm, [], dbc, path, hint);
43
+ }
44
+ /**
45
+ * A field-decorator factory using the {@link UNDEFINED.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
46
+ * by the tagged field.
47
+ *
48
+ * @param path See {@link DBC.decInvariant }.
49
+ * @param dbc See {@link DBC.decInvariant }.
50
+ * @param hint See {@link DBC.decInvariant }.
51
+ *
52
+ * @returns See {@link DBC.decInvariant }. */
53
+ static INVARIANT(type, path = undefined, hint = undefined, dbc = undefined) {
54
+ return DBC.createINVARIANT(UNDEFINED, [], dbc, path, hint);
55
+ }
56
+ // #endregion Condition checking.
57
+ // #region Referenced Condition checking.
58
+ //
59
+ // For usage in dynamic scenarios (like with AE-DBC).
60
+ //
61
+ /**
62
+ * Invokes the {@link UNDEFINED.checkAlgorithm } passing the value **toCheck** and the {@link UNDEFINED.type } .
63
+ *
64
+ * @param toCheck See {@link UNDEFINED.checkAlgorithm }.
65
+ *
66
+ * @returns See {@link UNDEFINED.checkAlgorithm}. */
67
+ check(toCheck) {
68
+ return UNDEFINED.checkAlgorithm(toCheck);
69
+ }
70
+ /**
71
+ * Invokes the {@link UNDEFINED.checkAlgorithm } passing the value **toCheck** and the {@link UNDEFINED.type } .
72
+ *
73
+ * @param toCheck See {@link UNDEFINED.checkAlgorithm }.
74
+ * @param id A {@link string } identifying this {@link INSTANCE } via the {@link DBC.Infringement }-Message.
75
+ *
76
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link UNDEFINED }.
77
+ *
78
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link UNDEFINED }.*/
79
+ static tsCheck(toCheck, id = undefined, dbc = undefined) {
80
+ const result = UNDEFINED.checkAlgorithm(toCheck);
81
+ if (result === true) {
82
+ return toCheck;
83
+ }
84
+ DBC.reportTsCheckInfringement(`${id ? `(${id}) ` : ""}${result}`, dbc);
85
+ return toCheck;
86
+ }
87
+ }
@@ -0,0 +1,99 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that the an {@link object }s gotta be an instance of a certain {@link ZOD.schema }.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
7
+ export class ZOD extends DBC {
8
+ /**
9
+ * Checks if the value **toCheck** complies to the specified {@link z.ZodType }.
10
+ *
11
+ * @param toCheck The value that has to comply to the specified **schema** in order for this {@link DBC }
12
+ * @param schema The {@link z.ZodType } the {@link object } **toCheck** has comply to in order for this {@link DBC } to be
13
+ * fulfilled.
14
+ *
15
+ * @returns TRUE if the value **toCheck** complies to the specified **schema**, otherwise FALSE. */
16
+ static checkAlgorithm(toCheck, schema) {
17
+ const result = schema.safeParse(toCheck);
18
+ if (!result.success) {
19
+ return `Value does not comply to the specified schema. Received: "${JSON.stringify(toCheck)}". Errors: ${result.error.message}`;
20
+ }
21
+ return true;
22
+ }
23
+ /**
24
+ * A parameter-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
25
+ * by the tagged parameter.
26
+ *
27
+ * @param schema See {@link ZOD.checkAlgorithm }.
28
+ * @param path See {@link DBC.decPrecondition }.
29
+ * @param dbc See {@link DBC.decPrecondition }.
30
+ *
31
+ * @returns See {@link DBC.decPrecondition }. */
32
+ static PRE(schema, path = undefined, dbc = "WaXCode.DBC") {
33
+ return DBC.createPRE(ZOD.checkAlgorithm, [schema], dbc, path);
34
+ }
35
+ /**
36
+ * A method-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
37
+ * by the tagged method's returnvalue.
38
+ *
39
+ * @param schema See {@link ZOD.checkAlgorithm }.
40
+ * @param path See {@link DBC.Postcondition }.
41
+ * @param dbc See {@link DBC.decPostcondition }.
42
+ *
43
+ * @returns See {@link DBC.decPostcondition }. */
44
+ static POST(schema, path = undefined, dbc = "WaXCode.DBC") {
45
+ return DBC.createPOST(ZOD.checkAlgorithm, [schema], dbc, path);
46
+ }
47
+ /**
48
+ * A field-decorator factory using the {@link ZOD.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
49
+ * by the tagged method's returnvalue.
50
+ *
51
+ * @param schema See {@link ZOD.checkAlgorithm }.
52
+ * @param path See {@link DBC.decInvariant }.
53
+ * @param dbc See {@link DBC.decInvariant }.
54
+ *
55
+ * @returns See {@link DBC.decInvariant }. */
56
+ static INVARIANT(schema, path = undefined, dbc = "WaXCode.DBC") {
57
+ return DBC.createINVARIANT(ZOD, [schema], dbc, path);
58
+ }
59
+ // #endregion Condition checking.
60
+ // #region Referenced Condition checking.
61
+ //
62
+ // For usage in dynamic scenarios (like with AE-DBC).
63
+ //
64
+ /**
65
+ * Invokes the {@link ZOD.checkAlgorithm } passing the value **toCheck** and the {@link ZOD.schema } .
66
+ *
67
+ * @param toCheck See {@link ZOD.checkAlgorithm }.
68
+ *
69
+ * @returns See {@link ZOD.checkAlgorithm}. */
70
+ check(toCheck) {
71
+ return ZOD.checkAlgorithm(toCheck, this.schema);
72
+ }
73
+ /**
74
+ * Invokes the {@link ZOD.checkAlgorithm } passing the value **toCheck** and the {@link ZOD.schema } .
75
+ *
76
+ * @param toCheck See {@link ZOD.checkAlgorithm }.
77
+ * @param schema See {@link ZOD.checkAlgorithm }.
78
+ * @param id A {@link string } identifying this {@link ZOD } via the {@link DBC.Infringement }-Message.
79
+ *
80
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link ZOD }.
81
+ *
82
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
83
+ static tsCheck(toCheck, schema, id = undefined, dbc = undefined) {
84
+ const result = ZOD.checkAlgorithm(toCheck, schema);
85
+ if (result === true) {
86
+ return toCheck;
87
+ }
88
+ DBC.reportTsCheckInfringement(`${id ? `(${id}) ` : ""}${result}`, dbc);
89
+ return toCheck;
90
+ }
91
+ /**
92
+ * Creates this {@link ZOD } by setting the protected property {@link ZOD.schema } used by {@link ZOD.check }.
93
+ *
94
+ * @param schema See {@link ZOD.check }. */
95
+ constructor(schema) {
96
+ super();
97
+ this.schema = schema;
98
+ }
99
+ }
package/dist/DBC.d.ts CHANGED
@@ -11,6 +11,12 @@ export declare class DBC {
11
11
  private static evictIfNeeded;
12
12
  private static getHost;
13
13
  private static getDBC;
14
+ /**
15
+ * Returns the registered {@link DBC } instance at the specified path.
16
+ * Throws if no instance is registered there.
17
+ *
18
+ * @param path The dotted path to look up (default: `"WaXCode.DBC"`). */
19
+ static getRegistered(path?: string): DBC;
14
20
  /** Stores all request for parameter values registered by {@link decPrecondition }. */
15
21
  static paramValueRequests: Map<string, Map<number, Array<(value: any) => undefined>>>;
16
22
  /**
@@ -48,8 +54,8 @@ export declare class DBC {
48
54
  * @returns When used as a **method** decorator: the (modified) {@link PropertyDescriptor }.
49
55
  * When used as a **class** decorator: a replacement constructor that performs precondition checks. */
50
56
  static ParamvalueProvider(target: object, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
51
- static ParamvalueProvider<T extends new (...args: any[]) => any>(constructor: T): T;
52
- static ParamvalueProvider<T extends abstract new (...args: any[]) => any>(constructor: T): T;
57
+ static ParamvalueProvider<T extends new (...args: any[]) => any>(ctor: T): T;
58
+ static ParamvalueProvider<T extends abstract new (...args: any[]) => any>(ctor: T): T;
53
59
  /**
54
60
  * A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.
55
61
  * This invariant aims to check the instance of the class not the value to be get or set.
@@ -148,6 +154,10 @@ export declare class DBC {
148
154
  infringementSettings: {
149
155
  throwException: boolean;
150
156
  logToConsole: boolean;
157
+ onInfringement: ((infringement: InstanceType<typeof DBC.Infringement>, context: {
158
+ type: "precondition" | "postcondition" | "invariant";
159
+ value: unknown;
160
+ }) => void) | undefined;
151
161
  };
152
162
  /** Sanitizes a value for safe inclusion in error messages. */
153
163
  private static sanitize;
@@ -157,7 +167,7 @@ export declare class DBC {
157
167
  *
158
168
  * @param message The {@link string } describing the infringement and it's provenience.
159
169
  * @param violator The {@link string } describing or naming the violator. */
160
- protected reportInfringement(message: string, violator: string, target: object, value: unknown, path: string | undefined, hint?: string | undefined): undefined;
170
+ protected reportInfringement(message: string, violator: string, target: object, value: unknown, path: string | undefined, hint?: string | undefined, type?: "precondition" | "postcondition" | "invariant"): undefined;
161
171
  /**
162
172
  * Reports a parameter-infringement via {@link reportInfringement } also generating a proper {@link string }-wrapper
163
173
  * for the given "message","method", parameter-"index" & value.
@@ -193,7 +203,7 @@ export declare class DBC {
193
203
  *
194
204
  * @param message The fully formatted infringement message.
195
205
  * @param dbc The path to the DBC instance. Defaults to `"WaXCode.DBC"`. */
196
- static reportTsCheckInfringement(message: string, dbc?: string | undefined): void;
206
+ static reportTsCheckInfringement(message: string, dbc?: string | undefined, value?: unknown): void;
197
207
  /** An {@link Error } to be thrown whenever an infringement is detected. */
198
208
  static Infringement: {
199
209
  new (message: string): {
@@ -220,6 +230,10 @@ export declare class DBC {
220
230
  constructor(infringementSettings?: {
221
231
  throwException: boolean;
222
232
  logToConsole: boolean;
233
+ onInfringement?: (infringement: InstanceType<typeof DBC.Infringement>, context: {
234
+ type: "precondition" | "postcondition" | "invariant";
235
+ value: unknown;
236
+ }) => void;
223
237
  }, executionSettings?: {
224
238
  checkPreconditions: boolean;
225
239
  checkPostconditions: boolean;