xdbc 1.0.217 → 1.0.218

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 (89) hide show
  1. package/.gitattributes +8 -0
  2. package/.vscode/settings.json +3 -3
  3. package/.vscode/tasks.json +23 -23
  4. package/ASSESSMENT.md +249 -0
  5. package/README.md +131 -1
  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 +481 -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 +40 -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 +87 -0
  38. package/dist/DBC/DOM.js +223 -0
  39. package/dist/DBC/EQ/DIFFERENT.js +34 -0
  40. package/dist/DBC/EQ.js +101 -0
  41. package/dist/DBC/HasAttribute.js +101 -0
  42. package/dist/DBC/IF.js +96 -0
  43. package/dist/DBC/INSTANCE.js +122 -0
  44. package/dist/DBC/JSON.OP.js +120 -0
  45. package/dist/DBC/JSON.Parse.js +104 -0
  46. package/dist/DBC/OR.js +125 -0
  47. package/dist/DBC/REGEX.js +136 -0
  48. package/dist/DBC/TYPE.js +112 -0
  49. package/dist/DBC/UNDEFINED.js +87 -0
  50. package/dist/DBC/ZOD.js +99 -0
  51. package/dist/DBC.d.ts +18 -4
  52. package/dist/DBC.js +645 -0
  53. package/dist/Demo.d.ts +10 -0
  54. package/dist/Demo.js +713 -0
  55. package/dist/bundle.js +6140 -405
  56. package/dist/index.d.ts +22 -0
  57. package/dist/index.js +22 -0
  58. package/jest.config.js +32 -32
  59. package/package.json +71 -55
  60. package/src/DBC/AE.ts +269 -288
  61. package/src/DBC/ARR/PLAIN_OBJECT.ts +122 -133
  62. package/src/DBC/ARRAY.ts +117 -127
  63. package/src/DBC/COMPARISON/GREATER.ts +41 -46
  64. package/src/DBC/COMPARISON/GREATER_OR_EQUAL.ts +41 -45
  65. package/src/DBC/COMPARISON/LESS.ts +41 -45
  66. package/src/DBC/COMPARISON/LESS_OR_EQUAL.ts +41 -45
  67. package/src/DBC/COMPARISON.ts +149 -159
  68. package/src/DBC/DEFINED.ts +117 -122
  69. package/src/DBC/DOM.ts +291 -0
  70. package/src/DBC/EQ/DIFFERENT.ts +51 -57
  71. package/src/DBC/EQ.ts +154 -163
  72. package/src/DBC/HasAttribute.ts +149 -154
  73. package/src/DBC/IF.ts +173 -179
  74. package/src/DBC/INSTANCE.ts +168 -171
  75. package/src/DBC/JSON.OP.ts +178 -186
  76. package/src/DBC/JSON.Parse.ts +150 -157
  77. package/src/DBC/OR.ts +183 -187
  78. package/src/DBC/REGEX.ts +195 -196
  79. package/src/DBC/TYPE.ts +142 -149
  80. package/src/DBC/UNDEFINED.ts +115 -117
  81. package/src/DBC/ZOD.ts +130 -135
  82. package/src/DBC.ts +902 -904
  83. package/src/Demo.ts +537 -404
  84. package/src/index.ts +22 -0
  85. package/tsconfig.json +18 -18
  86. package/tsconfig.test.json +7 -7
  87. package/typedoc.json +16 -16
  88. package/webpack.config.js +27 -27
  89. package/Assessment.md +0 -507
@@ -0,0 +1,34 @@
1
+ import { EQ } from "../EQ";
2
+ /**
3
+ * DIFFERENT class for inequality comparisons.
4
+ *
5
+ * This class extends EQ and provides methods to check if a value is different (not equal)
6
+ * from a specified equivalent value. It inverts the equality check by always passing
7
+ * `true` for the invert parameter to the parent EQ class methods.
8
+ *
9
+ * @remarks
10
+ * The class provides precondition (PRE), postcondition (POST), and invariant (INVARIANT)
11
+ * checks for Design by Contract programming patterns.
12
+ *
13
+ * @see {@link COMPARISON}
14
+ * @see {@link EQ}
15
+ */
16
+ export class DIFFERENT extends EQ {
17
+ /** See {@link EQ.PRE }. Always inverts equality check. */
18
+ static PRE(equivalent, _invert = false, path = undefined, hint = undefined, dbc = undefined) {
19
+ return EQ.PRE(equivalent, true, path, hint, dbc);
20
+ }
21
+ /** See {@link EQ.POST }. Always inverts equality check. */
22
+ static POST(equivalent, _invert = false, path = undefined, hint = undefined, dbc = undefined) {
23
+ return EQ.POST(equivalent, true, path, hint, dbc);
24
+ }
25
+ /** See {@link EQ.INVARIANT }. Always inverts equality check. */
26
+ static INVARIANT(equivalent, _invert = false, path = undefined, hint = undefined, dbc = undefined) {
27
+ return EQ.INVARIANT(equivalent, true, path, hint, dbc);
28
+ }
29
+ /** See {@link EQ.constructor }. */
30
+ constructor(equivalent) {
31
+ super(equivalent, true);
32
+ this.equivalent = equivalent;
33
+ }
34
+ }
package/dist/DBC/EQ.js ADDED
@@ -0,0 +1,101 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that two {@link object }s gotta be equal.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
7
+ export class EQ extends DBC {
8
+ // #region Condition checking.
9
+ /**
10
+ * Checks if the value **toCheck** is equal to the specified **equivalent**.
11
+ *
12
+ * @param toCheck The value that has to be equal to it's possible **equivalent** for this {@link DBC } to be fulfilled.
13
+ * @param equivalent The {@link object } the one **toCheck** has to be equal to in order for this {@link DBC } to be
14
+ * fulfilled.
15
+ *
16
+ * @returns TRUE if the value **toCheck** and the **equivalent** are equal to each other, otherwise FALSE. */
17
+ static checkAlgorithm(toCheck, equivalent, invert) {
18
+ if (!invert && equivalent !== toCheck) {
19
+ return `Value has to be equal to "${equivalent}"`;
20
+ }
21
+ if (invert && equivalent === toCheck) {
22
+ return `Value must not be equal to "${equivalent}"`;
23
+ }
24
+ return true;
25
+ }
26
+ /**
27
+ * A parameter-decorator factory using the {@link EQ.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
28
+ * by the tagged parameter.
29
+ *
30
+ * @param equivalent See {@link EQ.checkAlgorithm }.
31
+ * @param path See {@link DBC.decPrecondition }.
32
+ * @param dbc See {@link DBC.decPrecondition }.
33
+ *
34
+ * @returns See {@link DBC.decPrecondition }. */
35
+ static PRE(equivalent, invert = false, path = undefined, hint = undefined, dbc = undefined) {
36
+ return DBC.createPRE(EQ.checkAlgorithm, [equivalent, invert], dbc, path, hint);
37
+ }
38
+ /**
39
+ * A method-decorator factory using the {@link EQ.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
40
+ * by the tagged method's returnvalue.
41
+ *
42
+ * @param equivalent See {@link EQ.checkAlgorithm }.
43
+ * @param path See {@link DBC.Postcondition }.
44
+ * @param dbc See {@link DBC.decPostcondition }.
45
+ *
46
+ * @returns See {@link DBC.decPostcondition }. */
47
+ static POST(equivalent, invert = false, path = undefined, hint = undefined, dbc = undefined) {
48
+ return DBC.createPOST(EQ.checkAlgorithm, [equivalent, invert], dbc, path, hint);
49
+ }
50
+ /**
51
+ * A field-decorator factory using the {@link EQ.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
52
+ * by the tagged field.
53
+ *
54
+ * @param equivalent See {@link EQ.checkAlgorithm }.
55
+ * @param path See {@link DBC.decInvariant }.
56
+ * @param dbc See {@link DBC.decInvariant }.
57
+ *
58
+ * @returns See {@link DBC.decInvariant }. */
59
+ static INVARIANT(equivalent, invert = false, path = undefined, hint = undefined, dbc = undefined) {
60
+ return DBC.createINVARIANT(EQ, [equivalent, invert], dbc, path, hint);
61
+ }
62
+ // #endregion Condition checking.
63
+ // #region Referenced Condition checking.
64
+ //
65
+ // For usage in dynamic scenarios (like with AE-DBC).
66
+ //
67
+ /**
68
+ * Invokes the {@link EQ.checkAlgorithm } passing the value **toCheck**, {@link EQ.equivalent } and {@link EQ.invert }.
69
+ *
70
+ * @param toCheck See {@link EQ.checkAlgorithm }.
71
+ *
72
+ * @returns See {@link EQ.checkAlgorithm}. */
73
+ check(toCheck) {
74
+ return EQ.checkAlgorithm(toCheck, this.equivalent, this.invert);
75
+ }
76
+ /**
77
+ * Invokes the {@link EQ.checkAlgorithm } passing the value **toCheck** and the specified **type** .
78
+ *
79
+ * @param toCheck See {@link EQ.checkAlgorithm }.
80
+ *
81
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link EQ }.
82
+ *
83
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link EQ }.*/
84
+ static tsCheck(toCheck, equivalent, hint = undefined, id = undefined, dbc = undefined) {
85
+ const result = EQ.checkAlgorithm(toCheck, equivalent, false);
86
+ if (result === true) {
87
+ return toCheck;
88
+ }
89
+ DBC.reportTsCheckInfringement(`${id ? `(${id}) ` : ""}${result} ${hint ? `✨ ${hint} ✨` : ""}`, dbc);
90
+ return toCheck;
91
+ }
92
+ /**
93
+ * Creates this {@link EQ } by setting the protected property {@link EQ.equivalent } used by {@link EQ.check }.
94
+ *
95
+ * @param equivalent See {@link EQ.check }. */
96
+ constructor(equivalent, invert = false) {
97
+ super();
98
+ this.equivalent = equivalent;
99
+ this.invert = invert;
100
+ }
101
+ }
@@ -0,0 +1,101 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that a {@link HTMLElement } gotta have a certain attribute set.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
7
+ export class HasAttribute extends DBC {
8
+ // #region Condition checking.
9
+ /**
10
+ * Checks if the {@link HTMLElement } **toCheck** has the attribute **toCheckFor**.
11
+ *
12
+ * @param toCheckFor name of the attribute to check for whether it is set or not.
13
+ *
14
+ * @returns TRUE if the {@link HTMLElement } **toCheck** has set the attribute **toCheckFor**,
15
+ * otherwise a proper errormessage. */
16
+ static checkAlgorithm(toCheck, toCheckFor, invert) {
17
+ if (!(toCheck instanceof HTMLElement)) {
18
+ return `The object to check for whether it has the attribute "${toCheckFor}" is not a HTMLElement. It is of type "${typeof toCheck}".`;
19
+ }
20
+ if (!invert && !toCheck.hasAttribute(toCheckFor)) {
21
+ return `Required Attribute "${toCheckFor}" is not set.`;
22
+ }
23
+ if (invert && toCheck.hasAttribute(toCheckFor)) {
24
+ return `Forbidden Attribute "${toCheckFor}" is set.`;
25
+ }
26
+ return true;
27
+ }
28
+ /**
29
+ * A parameter-decorator factory using the {@link HasAttribute.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
30
+ * by the tagged parameter.
31
+ *
32
+ * @param toCheckFor See {@link HasAttribute.checkAlgorithm }.
33
+ * @param path See {@link DBC.decPrecondition }.
34
+ * @param dbc See {@link DBC.decPrecondition }.
35
+ *
36
+ * @returns See {@link DBC.decPrecondition }. */
37
+ static PRE(toCheckFor, invert = false, path = undefined, dbc = "WaXCode.DBC") {
38
+ return DBC.createPRE(HasAttribute.checkAlgorithm, [toCheckFor, invert], dbc, path);
39
+ }
40
+ /**
41
+ * A method-decorator factory using the {@link HasAttribute.checkAlgorithm } to determine whether this {@link DBC } is
42
+ * fulfilled by the tagged method's returnvalue.
43
+ *
44
+ * @param toCheckFor See {@link HasAttribute.checkAlgorithm }.
45
+ * @param path See {@link DBC.Postcondition }.
46
+ * @param dbc See {@link DBC.decPostcondition }.
47
+ *
48
+ * @returns See {@link DBC.decPostcondition }. */
49
+ static POST(toCheckFor, invert = false, path = undefined, dbc = "WaXCode.DBC") {
50
+ return DBC.createPOST(HasAttribute.checkAlgorithm, [toCheckFor, invert], dbc, path);
51
+ }
52
+ /**
53
+ * A field-decorator factory using the {@link hasAttribute.checkAlgorithm } to determine whether this {@link DBC } is
54
+ * fulfilled by the tagged field.
55
+ *
56
+ * @param toCheckFor See {@link hasAttribute.checkAlgorithm }.
57
+ * @param path See {@link DBC.decInvariant }.
58
+ * @param dbc See {@link DBC.decInvariant }.
59
+ *
60
+ * @returns See {@link DBC.decInvariant }. */
61
+ static INVARIANT(toCheckFor, invert = false, path = undefined, dbc = "WaXCode.DBC") {
62
+ return DBC.createINVARIANT(HasAttribute, [toCheckFor, invert], dbc, path);
63
+ }
64
+ /**
65
+ * A field-decorator factory using the {@link hasAttribute.checkAlgorithm } to determine whether this {@link DBC } is
66
+ * fulfilled by the tagged field's class instance.
67
+ *
68
+ * @param toCheckFor See {@link hasAttribute.checkAlgorithm }.
69
+ * @param path See {@link DBC.decInvariant }.
70
+ * @param dbc See {@link DBC.decInvariant }.
71
+ *
72
+ * @returns See {@link DBC.decInvariant }. */
73
+ static cINVARIANT(toCheckFor, invert = false, path = undefined, dbc = "WaXCode.DBC") {
74
+ return DBC.decClassInvariant([new HasAttribute(toCheckFor, invert)], path, dbc);
75
+ }
76
+ // #endregion Condition checking.
77
+ // #region Referenced Condition checking.
78
+ //
79
+ // For usage in dynamic scenarios (like with AE-DBC).
80
+ //
81
+ /**
82
+ * Invokes the {@link hasAttribute.checkAlgorithm } passing the value **toCheck**, {@link hasAttribute.equivalent } and
83
+ * {@link hasAttribute.invert }.
84
+ *
85
+ * @param toCheck See {@link EQ.checkAlgorithm }.
86
+ *
87
+ * @returns See {@link EQ.checkAlgorithm}. */
88
+ check(toCheck) {
89
+ return HasAttribute.checkAlgorithm(toCheck, this.toCheckFor, this.invert);
90
+ }
91
+ /**
92
+ * Creates this {@link DBC } by setting the protected property {@link hasAttribute.equivalent } used by
93
+ * {@link hasAttribute.check }.
94
+ *
95
+ * @param toCheckFor See {@link hasAttribute.check }. */
96
+ constructor(toCheckFor, invert = false) {
97
+ super();
98
+ this.toCheckFor = toCheckFor;
99
+ this.invert = invert;
100
+ }
101
+ }
package/dist/DBC/IF.js ADDED
@@ -0,0 +1,96 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } defining that an {@link object } has also to comply to a certain {@link DBC } if it complies to
4
+ * another specified one.
5
+ *
6
+ * @remarks
7
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
8
+ export class IF extends DBC {
9
+ // #region Condition checking.
10
+ /**
11
+ * Checks if the value **toCheck** complies to the specified **condition** and if so does also comply to the one **inCase**.
12
+ *
13
+ * @param toCheck The value that has to be equal to it's possible **equivalent** for this {@link DBC } to be fulfilled.
14
+ * @param condition The contract **toCheck** has to comply to in order to also have to comply to the one **inCase**.
15
+ * @param inCase The contract **toCheck** has to also comply to if it complies to **condition**.
16
+ *
17
+ * @returns TRUE if the value **toCheck** and the **equivalent** are equal to each other, otherwise FALSE. */
18
+ static checkAlgorithm(toCheck, condition, inCase, invert = false) {
19
+ if (toCheck === undefined || toCheck === null)
20
+ return true;
21
+ if (invert &&
22
+ condition.check(toCheck) !== true &&
23
+ inCase.check(toCheck) !== true) {
24
+ return "In case that the value does not comply to the condition, it also has to comply to the required contract";
25
+ }
26
+ if (!invert &&
27
+ condition.check(toCheck) === true &&
28
+ inCase.check(toCheck) !== true) {
29
+ return "In case that the value complies to the condition, it has to comply to the required contract";
30
+ }
31
+ return true;
32
+ }
33
+ /**
34
+ * A parameter-decorator factory using the {@link EQ.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
35
+ * by the tagged parameter.
36
+ *
37
+ * @param condition See {@link IF.checkAlgorithm }.
38
+ * @param inCase See {@link IF.checkAlgorithm }.
39
+ * @param path See {@link DBC.decPrecondition }.
40
+ * @param dbc See {@link DBC.decPrecondition }.
41
+ *
42
+ * @returns See {@link DBC.decPrecondition }. */
43
+ static PRE(condition, inCase, path = undefined, invert = false, hint = undefined, dbc = undefined) {
44
+ return DBC.createPRE(IF.checkAlgorithm, [condition, inCase, invert], dbc, path, hint);
45
+ }
46
+ /**
47
+ * A method-decorator factory using the {@link IF.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
48
+ * by the tagged method's returnvalue.
49
+ *
50
+ * @param condition See {@link IF.checkAlgorithm }.
51
+ * @param inCase See {@link IF.checkAlgorithm }.
52
+ * @param path See {@link DBC.Postcondition }.
53
+ * @param dbc See {@link DBC.decPostcondition }.
54
+ *
55
+ * @returns See {@link DBC.decPostcondition }. */
56
+ static POST(condition, inCase, path = undefined, invert = false, hint = undefined, dbc = undefined) {
57
+ return DBC.createPOST(IF.checkAlgorithm, [condition, inCase, invert], dbc, path, hint);
58
+ }
59
+ /**
60
+ * A field-decorator factory using the {@link IF.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
61
+ * by the tagged field.
62
+ *
63
+ * @param condition See {@link IF.checkAlgorithm }.
64
+ * @param inCase See {@link IF.checkAlgorithm }.
65
+ * @param path See {@link DBC.decInvariant }.
66
+ * @param dbc See {@link DBC.decInvariant }.
67
+ *
68
+ * @returns See {@link DBC.decInvariant }. */
69
+ static INVARIANT(condition, inCase, path = undefined, invert = false, hint = undefined, dbc = undefined) {
70
+ return DBC.createINVARIANT(IF, [condition, inCase, invert], dbc, path, hint);
71
+ }
72
+ // #endregion Condition checking.
73
+ // #region Referenced Condition checking.
74
+ //
75
+ // For usage in dynamic scenarios (like with AE-DBC).
76
+ //
77
+ /**
78
+ * Invokes the {@link IF.checkAlgorithm } passing the value **toCheck**, {@link IF.equivalent } and {@link IF.invert }.
79
+ *
80
+ * @param toCheck See {@link IF.checkAlgorithm }.
81
+ *
82
+ * @returns See {@link IF.checkAlgorithm}. */
83
+ check(toCheck) {
84
+ return IF.checkAlgorithm(toCheck, this.condition, this.inCase, this.invert);
85
+ }
86
+ /**
87
+ * Creates this {@link IF } by setting the protected property {@link IF.equivalent } used by {@link IF.check }.
88
+ *
89
+ * @param equivalent See {@link IF.check }. */
90
+ constructor(condition, inCase, invert = false) {
91
+ super();
92
+ this.condition = condition;
93
+ this.inCase = inCase;
94
+ this.invert = invert;
95
+ }
96
+ }
@@ -0,0 +1,122 @@
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 INSTANCE.reference }.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
7
+ export class INSTANCE extends DBC {
8
+ /**
9
+ * Checks if the value **toCheck** is an instance of the specified **reference**.
10
+ *
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
+ *
15
+ * @returns TRUE if the value **toCheck** is is an instance of the *reference**, **undefined** or **null**, otherwise FALSE. */
16
+ static checkAlgorithm(toCheck, ...references) {
17
+ if (toCheck === null || toCheck === undefined) {
18
+ return true;
19
+ }
20
+ for (const ref of references) {
21
+ if (toCheck instanceof ref) {
22
+ return true;
23
+ }
24
+ }
25
+ return `Value has to be an instance of "${references.map((ref) => ref.name || ref).join(", ")}" but is of type "${typeof toCheck}"`;
26
+ }
27
+ /**
28
+ * A parameter-decorator factory using the {@link INSTANCE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
29
+ * by the tagged parameter.
30
+ *
31
+ * @param reference See {@link INSTANCE.checkAlgorithm }.
32
+ * @param path See {@link DBC.decPrecondition }.
33
+ * @param dbc See {@link DBC.decPrecondition }.
34
+ *
35
+ * @returns See {@link DBC.decPrecondition }. */
36
+ static PRE(reference, path = undefined, hint = undefined, dbc = undefined) {
37
+ const refs = Array.isArray(reference) ? reference : [reference];
38
+ return DBC.createPRE(INSTANCE.checkAlgorithm, refs, dbc, path, hint);
39
+ }
40
+ /**
41
+ * A method-decorator factory using the {@link INSTANCE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
42
+ * by the tagged method's returnvalue.
43
+ *
44
+ * @param reference See {@link INSTANCE.checkAlgorithm }.
45
+ * @param path See {@link DBC.Postcondition }.
46
+ * @param dbc See {@link DBC.decPostcondition }.
47
+ *
48
+ * @returns See {@link DBC.decPostcondition }. */
49
+ static POST(reference, path = undefined, hint = undefined, dbc = undefined) {
50
+ const refs = Array.isArray(reference) ? reference : [reference];
51
+ return DBC.createPOST(INSTANCE.checkAlgorithm, refs, dbc, path, hint);
52
+ }
53
+ /**
54
+ * A field-decorator factory using the {@link INSTANCE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
55
+ * by the tagged method's returnvalue.
56
+ *
57
+ * @param reference See {@link INSTANCE.checkAlgorithm }.
58
+ * @param path See {@link DBC.decInvariant }.
59
+ * @param dbc See {@link DBC.decInvariant }.
60
+ *
61
+ * @returns See {@link DBC.decInvariant }. */
62
+ static INVARIANT(reference, path = undefined, hint = undefined, dbc = undefined) {
63
+ return DBC.createINVARIANT(INSTANCE, [reference], dbc, path, hint);
64
+ }
65
+ // #endregion Condition checking.
66
+ // #region Referenced Condition checking.
67
+ //
68
+ // For usage in dynamic scenarios (like with AE-DBC).
69
+ //
70
+ /**
71
+ * Invokes the {@link INSTANCE.checkAlgorithm } passing the value **toCheck** and the {@link INSTANCE.reference } .
72
+ *
73
+ * @param toCheck See {@link INSTANCE.checkAlgorithm }.
74
+ *
75
+ * @returns See {@link INSTANCE.checkAlgorithm}. */
76
+ check(toCheck) {
77
+ return Array.isArray(this.reference)
78
+ ? INSTANCE.checkAlgorithm(toCheck, ...this.reference)
79
+ : INSTANCE.checkAlgorithm(toCheck, this.reference);
80
+ }
81
+ /**
82
+ * Type-safe check that validates if a value is an instance of a specified reference.
83
+ *
84
+ * @param toCheck The value to check for instance validity.
85
+ * @param reference The {@link object } the one **toCheck** has to be an instance of.
86
+ * @param hint An optional {@link string } providing extra information in case of an infringement.
87
+ * @param id A {@link string } identifying this {@link INSTANCE } via the {@link DBC.Infringement }-Message.
88
+ *
89
+ * @returns The **CANDIDATE** **toCheck** if it fulfills this {@link INSTANCE }.
90
+ *
91
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link INSTANCE }. */
92
+ static tsCheck(toCheck, reference, hint = undefined, id = undefined, dbc = undefined) {
93
+ return INSTANCE.tsCheckMulti(toCheck, [reference], hint, id, dbc);
94
+ }
95
+ /**
96
+ * Invokes the {@link INSTANCE.checkAlgorithm } passing the value **toCheck** and the {@link INSTANCE.reference } .
97
+ *
98
+ * @param toCheck See {@link INSTANCE.checkAlgorithm }.
99
+ * @param reference See {@link INSTANCE.checkAlgorithm }.
100
+ * @param hint An optional {@link string } providing extra information in case of an infringement.
101
+ * @param id A {@link string } identifying this {@link INSTANCE } via the {@link DBC.Infringement }-Message.
102
+ *
103
+ * @returns The **CANDIDATE** **toCheck** doesn't fulfill this {@link INSTANCE }.
104
+ *
105
+ * @throws A {@link DBC.Infringement } if the **CANDIDATE** **toCheck** does not fulfill this {@link DEFINED }. */
106
+ static tsCheckMulti(toCheck, references, hint = undefined, id = undefined, dbc = undefined) {
107
+ const result = INSTANCE.checkAlgorithm(toCheck, ...references);
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 INSTANCE } by setting the protected property {@link INSTANCE.reference } used by {@link INSTANCE.check }.
116
+ *
117
+ * @param reference See {@link INSTANCE.check }. */
118
+ constructor(reference) {
119
+ super();
120
+ this.reference = reference;
121
+ }
122
+ }
@@ -0,0 +1,120 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } demanding that an {@link object } has specific properties of specific types.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
7
+ export class JSON_OP extends DBC {
8
+ // #region Condition checking.
9
+ /**
10
+ * Checks if the object **toCheck** has the **necessaryProperties** of necessary type.
11
+ *
12
+ * @param toCheck The {@link object } to check for the necessary properties.
13
+ * @param necessaryProperties The **{ name : string, type : string }**s defining the properties and type the {@link object } to
14
+ * check needs to have.
15
+ * @param checkElements Indicates if **toCheck** is an iterable object of which all elements have to be checked.
16
+ * Elements will only be checked if **toCheck** is iterable, otherwise **toCheck** itself
17
+ * will be checked.
18
+ *
19
+ * @returns TRUE if the value **toCheck** or it's elements, if **checkElements** is TRUE, has all **necessaryProperties**, otherwise a {@link string } to report the infringement. */
20
+ static checkAlgorithm(toCheck, necessaryProperties, checkElements) {
21
+ if (toCheck === undefined || toCheck === null) {
22
+ return `[ UNDEFINED or NULL received instead of object with following properties: ${JSON.stringify(necessaryProperties)} ]`;
23
+ }
24
+ for (const property of necessaryProperties)
25
+ if (checkElements && typeof toCheck[Symbol.iterator] === "function") {
26
+ for (const element of toCheck) {
27
+ if (
28
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
29
+ !element.hasOwnProperty(property.name) ||
30
+ typeof element[property.name] !== property.type) {
31
+ return `[ Object "${JSON.stringify(element)}" in Array "${JSON.stringify(toCheck)}" does not contain the necessary property "${property.name}" of type "${property.type}"]`;
32
+ }
33
+ }
34
+ }
35
+ else {
36
+ if (
37
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
38
+ !toCheck.hasOwnProperty(property.name) ||
39
+ typeof toCheck[property.name] !== property.type) {
40
+ return `[ Object does not contain the necessary property "${property.name}" of type "${property.type}"]`;
41
+ }
42
+ }
43
+ return true;
44
+ }
45
+ /**
46
+ * A parameter-decorator factory using the {@link JSON_OP.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
47
+ * by the tagged parameter.
48
+ *
49
+ * @param expression See {@link JSON.checkAlgorithm }.
50
+ * @param path See {@link DBC.decPrecondition }.
51
+ * @param dbc See {@link DBC.decPrecondition }.
52
+ *
53
+ * @returns See {@link DBC.decPrecondition }. */
54
+ static PRE(necessaryProperties, checkElements = false, path = undefined, dbc = "WaXCode.DBC") {
55
+ return DBC.createPRE(JSON_OP.checkAlgorithm, [necessaryProperties, checkElements], dbc, path);
56
+ }
57
+ /**
58
+ * A method-decorator factory using the {@link JSON_OP.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
59
+ * by the tagged method's returnvalue.
60
+ *
61
+ * @param expression See {@link JSON.checkAlgorithm }.
62
+ * @param path See {@link DBC.Postcondition }.
63
+ * @param dbc See {@link DBC.decPostcondition }.
64
+ *
65
+ * @returns See {@link DBC.decPostcondition }. */
66
+ static POST(necessaryProperties, checkElements = false, path = undefined, dbc = "WaXCode.DBC") {
67
+ return DBC.createPOST(JSON_OP.checkAlgorithm, [necessaryProperties, checkElements], dbc, path);
68
+ }
69
+ /**
70
+ * A field-decorator factory using the {@link JSON_OP.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
71
+ * by the tagged field.
72
+ *
73
+ * @param expression See {@link JSON.checkAlgorithm }.
74
+ * @param path See {@link DBC.decInvariant }.
75
+ * @param dbc See {@link DBC.decInvariant }.
76
+ *
77
+ * @returns See {@link DBC.decPostcondition }. */
78
+ static INVARIANT(necessaryProperties, checkElements = false, path = undefined, dbc = "WaXCode.DBC") {
79
+ return DBC.createINVARIANT(JSON_OP, [necessaryProperties, checkElements], dbc, path);
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 JSON_OP.checkAlgorithm } passing the value **toCheck**, {@link JSON_OP.necessaryProperties } and {@link JSON_OP.checkElements }.
88
+ *
89
+ * @param toCheck See {@link JSON_OP.checkAlgorithm }.
90
+ *
91
+ * @returns See {@link JSON_OP.checkAlgorithm}. */
92
+ check(toCheck) {
93
+ return JSON_OP.checkAlgorithm(toCheck, this.necessaryProperties, this.checkElements);
94
+ }
95
+ /**
96
+ * Creates this {@link JSON_OP } by setting the protected property {@link JSON_OP.necessaryProperties } and {@link checkElements } used by {@link JSON_OP.check }.
97
+ *
98
+ * @param necessaryProperties See {@link JSON_OP.check }.
99
+ * @param checkElements See {@link JSON_OP.check }. */
100
+ constructor(necessaryProperties, checkElements = false) {
101
+ super();
102
+ this.necessaryProperties = necessaryProperties;
103
+ this.checkElements = checkElements;
104
+ }
105
+ // #endregion Referenced Condition checking.
106
+ // #region In-Method checking.
107
+ /**
108
+ * Invokes the {@link JSON_OP.checkAlgorithm } passing the value **toCheck**, {@link JSON_OP.necessaryProperties } and {@link JSON_OP.checkElements }.
109
+ *
110
+ * @param toCheck See {@link JSON_OP.checkAlgorithm} }.
111
+ * @param necessaryProperties See {@link JSON_OP.checkAlgorithm} }.
112
+ * @param checkElements See {@link JSON_OP.checkAlgorithm} }.
113
+ */
114
+ static check(toCheck, necessaryProperties, checkElements = false, dbc = undefined) {
115
+ const checkResult = JSON_OP.checkAlgorithm(toCheck, necessaryProperties, checkElements);
116
+ if (typeof checkResult === "string") {
117
+ DBC.reportTsCheckInfringement(checkResult, dbc);
118
+ }
119
+ }
120
+ }
@@ -0,0 +1,104 @@
1
+ import { DBC } from "../DBC";
2
+ /**
3
+ * A {@link DBC } demanding that a {@link string } is {@link JSON.parse}able.
4
+ *
5
+ * @remarks
6
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
7
+ export class JSON_Parse extends DBC {
8
+ // #region Condition checking.
9
+ /**
10
+ * Tries to {@link JSON.parse } the {@link string } **toCheck** invoking the **receptor** with the result, if so.
11
+ *
12
+ * @param toCheck The {@link string } to be {@link JSON.parse }d.
13
+ * @param receptor The **( json : object ) => void** to receive the {@link JSON.parse }d {@link string } **toCheck**.
14
+ * check needs to have.
15
+ *
16
+ * @returns TRUE if the value **toCheck** is a valid JSON, otherwise a {@link string } to report the infringement. */
17
+ static checkAlgorithm(toCheck, receptor) {
18
+ let parsed;
19
+ try {
20
+ parsed = JSON.parse(toCheck);
21
+ }
22
+ catch (X) {
23
+ return `[ Following string is not a valid JSON: ${toCheck}]`;
24
+ }
25
+ if (receptor) {
26
+ receptor(parsed);
27
+ }
28
+ return true;
29
+ }
30
+ /**
31
+ * A parameter-decorator factory using the {@link JSON_Parse.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
32
+ * by the tagged parameter.
33
+ *
34
+ * @param receptor See {@link JSON.checkAlgorithm }.
35
+ * @param path See {@link DBC.decPrecondition }.
36
+ * @param dbc See {@link DBC.decPrecondition }.
37
+ *
38
+ * @returns See {@link DBC.decPrecondition }. */
39
+ static PRE(receptor, path = undefined, hint = undefined, dbc = undefined) {
40
+ return DBC.createPRE(JSON_Parse.checkAlgorithm, [receptor], dbc, path, hint);
41
+ }
42
+ /**
43
+ * A method-decorator factory using the {@link JSON_Parse.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
44
+ * by the tagged method's returnvalue.
45
+ *
46
+ * @param expression See {@link JSON.checkAlgorithm }.
47
+ * @param path See {@link DBC.Postcondition }.
48
+ * @param dbc See {@link DBC.decPostcondition }.
49
+ *
50
+ * @returns See {@link DBC.decPostcondition }. */
51
+ static POST(receptor, path = undefined, hint = undefined, dbc = undefined) {
52
+ return DBC.createPOST(JSON_Parse.checkAlgorithm, [receptor], dbc, path, hint);
53
+ }
54
+ /**
55
+ * A field-decorator factory using the {@link JSON_Parse.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
56
+ * by the tagged field.
57
+ *
58
+ * @param expression See {@link JSON.checkAlgorithm }.
59
+ * @param path See {@link DBC.decInvariant }.
60
+ * @param dbc See {@link DBC.decInvariant }.
61
+ *
62
+ * @returns See {@link DBC.decInvariant }. */
63
+ static INVARIANT(receptor, path = undefined, hint = undefined, dbc = undefined) {
64
+ return DBC.createINVARIANT(JSON_Parse, [receptor], dbc, path, hint);
65
+ }
66
+ // #endregion Condition checking.
67
+ // #region Referenced Condition checking.
68
+ //
69
+ // For usage in dynamic scenarios (like with AE-DBC).
70
+ //
71
+ /**
72
+ * Invokes the {@link JSON_Parse.checkAlgorithm } passing the value **toCheck** and {@link JSON_Parse.receptor }.
73
+ *
74
+ * @param toCheck See {@link JSON_Parse.checkAlgorithm }.
75
+ *
76
+ * @returns See {@link JSON_Parse.checkAlgorithm}. */
77
+ check(toCheck) {
78
+ return JSON_Parse.checkAlgorithm(toCheck, this.receptor);
79
+ }
80
+ /**
81
+ * Creates this {@link JSON_Parse } by setting the protected property {@link JSON_Parse.necessaryProperties } and {@link checkElements } used by {@link JSON_Parse.check }.
82
+ *
83
+ * @param necessaryProperties See {@link JSON_Parse.check }.
84
+ * @param checkElements See {@link JSON_Parse.check }. */
85
+ constructor(receptor = undefined) {
86
+ super();
87
+ this.receptor = receptor;
88
+ }
89
+ // #endregion Referenced Condition checking.
90
+ // #region In-Method checking.
91
+ /**
92
+ * Invokes the {@link JSON_Parse.checkAlgorithm } passing the value **toCheck**, {@link JSON_Parse.necessaryProperties } and {@link JSON_Parse.checkElements }.
93
+ *
94
+ * @param toCheck See {@link JSON_Parse.checkAlgorithm} }.
95
+ * @param necessaryProperties See {@link JSON_Parse.checkAlgorithm} }.
96
+ * @param checkElements See {@link JSON_Parse.checkAlgorithm} }.
97
+ */
98
+ static check(toCheck, receptor, dbc = undefined) {
99
+ const checkResult = JSON_Parse.checkAlgorithm(toCheck, receptor);
100
+ if (typeof checkResult === "string") {
101
+ DBC.reportTsCheckInfringement(checkResult, dbc);
102
+ }
103
+ }
104
+ }