@cullet/erp-core 1.3.0 → 1.5.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 (197) hide show
  1. package/KIT_CONTEXT.md +11 -3
  2. package/README.md +160 -0
  3. package/dist/abac/index.cjs +7 -0
  4. package/dist/abac/index.d.cts +2 -0
  5. package/dist/abac/index.d.ts +2 -0
  6. package/dist/abac/index.js +2 -0
  7. package/dist/app-error.cjs +201 -0
  8. package/dist/app-error.cjs.map +1 -0
  9. package/dist/app-error.js +178 -0
  10. package/dist/app-error.js.map +1 -0
  11. package/dist/application/index.cjs +2 -1
  12. package/dist/application/index.d.cts +2 -1
  13. package/dist/application/index.d.ts +2 -1
  14. package/dist/application/index.js +2 -1
  15. package/dist/authorization-error.cjs +157 -0
  16. package/dist/authorization-error.cjs.map +1 -0
  17. package/dist/authorization-error.d.cts +113 -0
  18. package/dist/authorization-error.d.ts +113 -0
  19. package/dist/authorization-error.js +152 -0
  20. package/dist/authorization-error.js.map +1 -0
  21. package/dist/authorizer.port.d.cts +130 -0
  22. package/dist/authorizer.port.d.ts +130 -0
  23. package/dist/composite-authorizer.d.cts +216 -0
  24. package/dist/composite-authorizer.d.ts +216 -0
  25. package/dist/condition-evaluator.cjs +579 -0
  26. package/dist/condition-evaluator.cjs.map +1 -0
  27. package/dist/condition-evaluator.js +550 -0
  28. package/dist/condition-evaluator.js.map +1 -0
  29. package/dist/{gate-types.d.ts → core-config.d.cts} +78 -77
  30. package/dist/{gate-types.d.cts → core-config.d.ts} +78 -77
  31. package/dist/domain/index.cjs +2 -1
  32. package/dist/domain/index.d.cts +2 -1
  33. package/dist/domain/index.d.ts +2 -1
  34. package/dist/domain/index.js +2 -1
  35. package/dist/domain-event-contracts.cjs +3 -2
  36. package/dist/domain-event-contracts.cjs.map +1 -1
  37. package/dist/domain-event-contracts.js +2 -1
  38. package/dist/domain-event-contracts.js.map +1 -1
  39. package/dist/entity.cjs +122 -0
  40. package/dist/entity.cjs.map +1 -0
  41. package/dist/entity.js +111 -0
  42. package/dist/entity.js.map +1 -0
  43. package/dist/errors/index.cjs +10 -7
  44. package/dist/errors/index.d.cts +2 -1
  45. package/dist/errors/index.d.ts +2 -1
  46. package/dist/errors/index.js +6 -3
  47. package/dist/gate-engine-registry.cjs +5 -4
  48. package/dist/gate-engine-registry.cjs.map +1 -1
  49. package/dist/gate-engine-registry.d.cts +3 -2
  50. package/dist/gate-engine-registry.d.ts +3 -2
  51. package/dist/gate-engine-registry.js +2 -1
  52. package/dist/gate-engine-registry.js.map +1 -1
  53. package/dist/gate-v1-payload.schema.cjs +0 -562
  54. package/dist/gate-v1-payload.schema.cjs.map +1 -1
  55. package/dist/gate-v1-payload.schema.js +1 -533
  56. package/dist/gate-v1-payload.schema.js.map +1 -1
  57. package/dist/hashing.cjs +2 -44
  58. package/dist/hashing.cjs.map +1 -1
  59. package/dist/hashing.js +2 -38
  60. package/dist/hashing.js.map +1 -1
  61. package/dist/index.cjs +36 -17
  62. package/dist/index.cjs.map +1 -1
  63. package/dist/index.d.cts +19 -12
  64. package/dist/index.d.ts +19 -12
  65. package/dist/index.js +17 -10
  66. package/dist/index.js.map +1 -1
  67. package/dist/not-found-error.cjs +5 -5
  68. package/dist/not-found-error.cjs.map +1 -1
  69. package/dist/not-found-error.js +1 -1
  70. package/dist/outcome.d.cts +1 -83
  71. package/dist/outcome.d.ts +1 -83
  72. package/dist/parse-gate-payload.d.cts +2 -2
  73. package/dist/parse-gate-payload.d.ts +2 -2
  74. package/dist/path.d.cts +2 -2
  75. package/dist/path.d.ts +2 -2
  76. package/dist/policies/engines/index.d.cts +1 -1
  77. package/dist/policies/engines/index.d.ts +1 -1
  78. package/dist/policies/engines/v1/gate/index.cjs +5 -4
  79. package/dist/policies/engines/v1/gate/index.cjs.map +1 -1
  80. package/dist/policies/engines/v1/gate/index.d.cts +4 -2
  81. package/dist/policies/engines/v1/gate/index.d.ts +4 -2
  82. package/dist/policies/engines/v1/gate/index.js +2 -1
  83. package/dist/policies/engines/v1/gate/index.js.map +1 -1
  84. package/dist/policies/index.cjs +5 -5
  85. package/dist/policies/index.d.cts +3 -2
  86. package/dist/policies/index.d.ts +3 -2
  87. package/dist/policies/index.js +2 -2
  88. package/dist/policy-bridge.cjs +437 -0
  89. package/dist/policy-bridge.cjs.map +1 -0
  90. package/dist/policy-bridge.d.cts +185 -0
  91. package/dist/policy-bridge.d.ts +185 -0
  92. package/dist/policy-bridge.js +396 -0
  93. package/dist/policy-bridge.js.map +1 -0
  94. package/dist/policy-service.cjs +241 -240
  95. package/dist/policy-service.cjs.map +1 -1
  96. package/dist/policy-service.d.cts +2 -2
  97. package/dist/policy-service.d.ts +2 -2
  98. package/dist/policy-service.js +240 -239
  99. package/dist/policy-service.js.map +1 -1
  100. package/dist/rbac/index.cjs +9 -0
  101. package/dist/rbac/index.d.cts +3 -0
  102. package/dist/rbac/index.d.ts +3 -0
  103. package/dist/rbac/index.js +2 -0
  104. package/dist/requested-by.cjs +54 -0
  105. package/dist/requested-by.cjs.map +1 -0
  106. package/dist/requested-by.d.cts +25 -0
  107. package/dist/requested-by.d.ts +25 -0
  108. package/dist/requested-by.js +49 -0
  109. package/dist/requested-by.js.map +1 -0
  110. package/dist/result/index.d.cts +2 -1
  111. package/dist/result/index.d.ts +2 -1
  112. package/dist/result.d.cts +84 -0
  113. package/dist/result.d.ts +84 -0
  114. package/dist/rule.cjs +327 -0
  115. package/dist/rule.cjs.map +1 -0
  116. package/dist/rule.js +298 -0
  117. package/dist/rule.js.map +1 -0
  118. package/dist/stable-stringify.cjs +47 -0
  119. package/dist/stable-stringify.cjs.map +1 -0
  120. package/dist/stable-stringify.js +42 -0
  121. package/dist/stable-stringify.js.map +1 -0
  122. package/dist/temporal-use-case.cjs +6 -53
  123. package/dist/temporal-use-case.cjs.map +1 -1
  124. package/dist/temporal-use-case.d.cts +11 -28
  125. package/dist/temporal-use-case.d.ts +11 -28
  126. package/dist/temporal-use-case.js +7 -48
  127. package/dist/temporal-use-case.js.map +1 -1
  128. package/dist/unexpected-error.cjs +3 -192
  129. package/dist/unexpected-error.cjs.map +1 -1
  130. package/dist/unexpected-error.js +2 -167
  131. package/dist/unexpected-error.js.map +1 -1
  132. package/dist/use-case.cjs +2 -6
  133. package/dist/use-case.cjs.map +1 -1
  134. package/dist/use-case.js +2 -6
  135. package/dist/use-case.js.map +1 -1
  136. package/dist/uuid-identifier.cjs +2 -7
  137. package/dist/uuid-identifier.cjs.map +1 -1
  138. package/dist/uuid-identifier.d.cts +2 -85
  139. package/dist/uuid-identifier.d.ts +2 -85
  140. package/dist/uuid-identifier.js +1 -6
  141. package/dist/uuid-identifier.js.map +1 -1
  142. package/dist/uuid.cjs +18 -0
  143. package/dist/uuid.cjs.map +1 -0
  144. package/dist/uuid.js +13 -0
  145. package/dist/uuid.js.map +1 -0
  146. package/dist/validation-error.cjs +33 -166
  147. package/dist/validation-error.cjs.map +1 -1
  148. package/dist/validation-error.d.cts +12 -103
  149. package/dist/validation-error.d.ts +12 -103
  150. package/dist/validation-error.js +7 -134
  151. package/dist/validation-error.js.map +1 -1
  152. package/dist/value-object.cjs +12 -126
  153. package/dist/value-object.cjs.map +1 -1
  154. package/dist/value-object.d.cts +97 -0
  155. package/dist/value-object.d.ts +97 -0
  156. package/dist/value-object.js +13 -115
  157. package/dist/value-object.js.map +1 -1
  158. package/meta.json +20 -4
  159. package/package.json +27 -1
  160. package/src/abac/index.ts +1 -0
  161. package/src/core/abac/abac-request.ts +29 -0
  162. package/src/core/abac/attributes.ts +39 -0
  163. package/src/core/abac/authorizer.ts +108 -0
  164. package/src/core/abac/combining.ts +63 -0
  165. package/src/core/abac/composite-authorizer.ts +45 -0
  166. package/src/core/abac/domain/policy-set.ts +52 -0
  167. package/src/core/abac/domain/rule.ts +197 -0
  168. package/src/core/abac/index.ts +21 -0
  169. package/src/core/application/commands/requested-by.ts +3 -4
  170. package/src/core/application/ports/abac-authorizer.port.ts +20 -0
  171. package/src/core/application/ports/authorizer.port.ts +22 -0
  172. package/src/core/application/queries/query.ts +6 -1
  173. package/src/core/application/use-case.ts +1 -1
  174. package/src/core/domain/entity.ts +1 -1
  175. package/src/core/domain/uuid-identifier.ts +1 -8
  176. package/src/core/domain/value-object.ts +12 -3
  177. package/src/core/errors/authorization-error.ts +26 -0
  178. package/src/core/errors/error-codes.ts +1 -0
  179. package/src/core/errors/utils/json-safe.ts +20 -11
  180. package/src/core/index.ts +7 -0
  181. package/src/core/policies/engines/v1/condition-evaluator.ts +56 -1
  182. package/src/core/rbac/access-request.ts +32 -0
  183. package/src/core/rbac/authorizer.ts +91 -0
  184. package/src/core/rbac/domain/grant.ts +96 -0
  185. package/src/core/rbac/domain/permission-set.ts +67 -0
  186. package/src/core/rbac/domain/permission.ts +119 -0
  187. package/src/core/rbac/domain/role.ts +101 -0
  188. package/src/core/rbac/domain/scope.ts +84 -0
  189. package/src/core/rbac/index.ts +15 -0
  190. package/src/core/rbac/policy-bridge.ts +44 -0
  191. package/src/core/shared/hashing.ts +1 -55
  192. package/src/core/shared/stable-stringify.ts +57 -0
  193. package/src/core/shared/uuid.ts +11 -0
  194. package/src/examples/application/authorize-cancel-order-abac.example.ts +107 -0
  195. package/src/examples/application/authorize-cancel-order.example.ts +101 -0
  196. package/src/rbac/index.ts +1 -0
  197. package/src/version.ts +1 -1
@@ -0,0 +1,579 @@
1
+ const require_temporal_guards = require("./temporal-guards.cjs");
2
+ const require_result = require("./result.cjs");
3
+ //#region src/core/config/silent-policy-reporter.ts
4
+ var SilentPolicyReporter = class {
5
+ report(_event) {}
6
+ };
7
+ //#endregion
8
+ //#region src/core/config/core-config.ts
9
+ function reportSafely(policyReporter, event) {
10
+ try {
11
+ policyReporter.report(event);
12
+ } catch {}
13
+ }
14
+ /**
15
+ * Centraliza a configuracao pura do core sem depender de implementacoes
16
+ * concretas de logging, tracing ou report.
17
+ */
18
+ var CoreConfig = class {
19
+ #initialReporter;
20
+ #currentReporter;
21
+ constructor(options = {}) {
22
+ const reporter = options.observability?.reporter ?? new SilentPolicyReporter();
23
+ this.#initialReporter = reporter;
24
+ this.#currentReporter = reporter;
25
+ }
26
+ configure(options) {
27
+ const reporter = options.observability?.reporter;
28
+ if (reporter) this.#currentReporter = reporter;
29
+ return this;
30
+ }
31
+ reset() {
32
+ this.#currentReporter = this.#initialReporter;
33
+ return this;
34
+ }
35
+ getPolicyReporter() {
36
+ return this.#currentReporter;
37
+ }
38
+ getConditionEvaluationOptions(engineVersion) {
39
+ return {
40
+ reporter: this.#bridgeToConditionReporter(this.#currentReporter),
41
+ engineVersion
42
+ };
43
+ }
44
+ #bridgeToConditionReporter(policyReporter) {
45
+ return {
46
+ warn(report) {
47
+ reportSafely(policyReporter, {
48
+ kind: "condition-eval",
49
+ ...report
50
+ });
51
+ },
52
+ error(report) {
53
+ reportSafely(policyReporter, {
54
+ kind: "condition-eval",
55
+ ...report
56
+ });
57
+ }
58
+ };
59
+ }
60
+ };
61
+ //#endregion
62
+ //#region src/core/config/core-config.instance.ts
63
+ /**
64
+ * Shared instance for the application composition root.
65
+ * Use `new CoreConfig()` for isolated runtimes, tests, or multi-tenant hosts.
66
+ *
67
+ * The default reporter is silent so the core has no runtime logging dependency.
68
+ * Hosts can swap in their own `PolicyReporter` via
69
+ * `coreConfig.configure({ observability: { reporter } })`.
70
+ */
71
+ const coreConfig = new CoreConfig({ observability: { reporter: new SilentPolicyReporter() } });
72
+ //#endregion
73
+ //#region src/core/policies/context/path.ts
74
+ /**
75
+ * Utilities for reading/writing values at dot-separated paths
76
+ * in plain objects, e.g. "installment.dueDate".
77
+ */
78
+ var PolicyContextPath = class PolicyContextPath {
79
+ static {
80
+ this.FORBIDDEN_SEGMENTS = new Set([
81
+ "__proto__",
82
+ "prototype",
83
+ "constructor"
84
+ ]);
85
+ }
86
+ static resolve(obj, path) {
87
+ const segmentsResult = PolicyContextPath.parseSegments(path);
88
+ if (segmentsResult.isErr()) return require_result.Result.err(segmentsResult.errorOrNull());
89
+ const segments = segmentsResult.getOrNull();
90
+ let current = obj;
91
+ for (const segment of segments) {
92
+ if (current === null || current === void 0 || typeof current !== "object") return require_result.Result.ok({
93
+ found: false,
94
+ value: void 0
95
+ });
96
+ const record = current;
97
+ if (!Object.prototype.hasOwnProperty.call(record, segment)) return require_result.Result.ok({
98
+ found: false,
99
+ value: void 0
100
+ });
101
+ current = record[segment];
102
+ }
103
+ return require_result.Result.ok({
104
+ found: true,
105
+ value: current
106
+ });
107
+ }
108
+ static parseSegments(path) {
109
+ if (path.trim().length === 0) return require_result.Result.err("Path cannot be empty");
110
+ const segments = path.split(".");
111
+ if (segments.some((segment) => segment.length === 0)) return require_result.Result.err(`Path "${path}" contains an empty segment`);
112
+ const forbiddenSegment = segments.find((segment) => PolicyContextPath.FORBIDDEN_SEGMENTS.has(segment));
113
+ if (forbiddenSegment !== void 0) return require_result.Result.err(`Path "${path}" contains forbidden segment "${forbiddenSegment}"`);
114
+ return require_result.Result.ok(segments);
115
+ }
116
+ static isPlainRecord(value) {
117
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
118
+ const prototype = Object.getPrototypeOf(value);
119
+ return prototype === Object.prototype || prototype === null;
120
+ }
121
+ static getOrAbsent(obj, path) {
122
+ const resolvedPath = PolicyContextPath.resolve(obj, path);
123
+ if (resolvedPath.isErr()) return require_result.Result.err("absent");
124
+ const { found, value } = resolvedPath.getOrNull();
125
+ if (!found) return require_result.Result.err("absent");
126
+ return require_result.Result.ok(value);
127
+ }
128
+ static has(obj, path) {
129
+ const resolvedPath = PolicyContextPath.resolve(obj, path);
130
+ if (resolvedPath.isErr()) return false;
131
+ return resolvedPath.getOrNull().found;
132
+ }
133
+ static set(obj, path, value) {
134
+ const segmentsResult = PolicyContextPath.parseSegments(path);
135
+ if (segmentsResult.isErr()) return require_result.Result.err(segmentsResult.errorOrNull());
136
+ const segments = segmentsResult.getOrNull();
137
+ let current = obj;
138
+ for (let i = 0; i < segments.length - 1; i++) {
139
+ const seg = segments[i];
140
+ const next = current[seg];
141
+ if (next === void 0) {
142
+ const container = Object.create(null);
143
+ current[seg] = container;
144
+ current = container;
145
+ continue;
146
+ }
147
+ if (!PolicyContextPath.isPlainRecord(next)) return require_result.Result.err(`Cannot create nested path "${path}" because "${segments.slice(0, i + 1).join(".")}" already contains a non-plain object`);
148
+ current = next;
149
+ }
150
+ current[segments[segments.length - 1]] = value;
151
+ return require_result.Result.ok(void 0);
152
+ }
153
+ };
154
+ //#endregion
155
+ //#region src/core/policies/utils/date.ts
156
+ var PolicyDateUtils = class {
157
+ static isValid(value) {
158
+ return require_temporal_guards.isValidDate(value);
159
+ }
160
+ };
161
+ //#endregion
162
+ //#region src/core/policies/engines/v1/condition-evaluator.ts
163
+ const CONDITION_EVAL_THROWN_TAG = "CONDITION_EVAL_THREW";
164
+ const NULLISH_NUMERIC_OPERAND_NOT_ALLOWED_TAG = "NULLISH_NUMERIC_OPERAND_NOT_ALLOWED";
165
+ const NULLISH_DATE_OPERAND_NOT_ALLOWED_TAG = "NULLISH_DATE_OPERAND_NOT_ALLOWED";
166
+ const INVALID_DATE_OPERAND_TAG = "INVALID_DATE_OPERAND";
167
+ const INVALID_NUMERIC_OPERAND_TAG = "INVALID_NUMERIC_OPERAND";
168
+ const INVALID_SET_OPERAND_TAG = "INVALID_SET_OPERAND";
169
+ const EMPTY_OR_CONDITION_TAG = "EMPTY_OR_CONDITION";
170
+ const EMPTY_AND_CONDITION_TAG = "EMPTY_AND_CONDITION";
171
+ const ISO_8601_UTC_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
172
+ var ConditionEvaluatorV1 = class ConditionEvaluatorV1 {
173
+ constructor(context, options) {
174
+ this.context = context;
175
+ this.options = options;
176
+ }
177
+ static isLeafNode(node) {
178
+ return "field" in node && "op" in node;
179
+ }
180
+ static isAndNode(node) {
181
+ return "and" in node;
182
+ }
183
+ static isOrNode(node) {
184
+ return "or" in node;
185
+ }
186
+ static isNotNode(node) {
187
+ return "not" in node;
188
+ }
189
+ static isRelationalOperator(op) {
190
+ return op === "gt" || op === "gte" || op === "lt" || op === "lte";
191
+ }
192
+ static describeError(error) {
193
+ if (error instanceof Error) return {
194
+ name: error.name,
195
+ message: error.message
196
+ };
197
+ return {
198
+ name: "NonErrorThrown",
199
+ message: String(error)
200
+ };
201
+ }
202
+ static buildNullishNumericOperandMessage(node, actual) {
203
+ const resolvedValue = actual === null ? "null" : "undefined";
204
+ return `${NULLISH_NUMERIC_OPERAND_NOT_ALLOWED_TAG}: "${node.field}" resolved to ${resolvedValue} for numeric operator "${node.op}"`;
205
+ }
206
+ static buildNullishDateOperandMessage(node, actual) {
207
+ const resolvedValue = actual === null ? "null" : "undefined";
208
+ return `${NULLISH_DATE_OPERAND_NOT_ALLOWED_TAG}: "${node.field}" resolved to ${resolvedValue} for date comparison operator "${node.op}"`;
209
+ }
210
+ static buildInvalidDateOperandMessage(node) {
211
+ return `${INVALID_DATE_OPERAND_TAG}: "${node.field}" with operator "${node.op}" requires Date or ISO 8601 UTC string operands`;
212
+ }
213
+ static buildInvalidNumericOperandMessage(node) {
214
+ return `${INVALID_NUMERIC_OPERAND_TAG}: "${node.field}" with operator "${node.op}" requires numeric operands`;
215
+ }
216
+ static buildInvalidSetOperandMessage(node) {
217
+ return `${INVALID_SET_OPERAND_TAG}: "${node.field}" with operator "${node.op}" requires an array operand`;
218
+ }
219
+ static buildEmptyOrConditionMessage() {
220
+ return `${EMPTY_OR_CONDITION_TAG}: OR nodes must contain at least one child condition`;
221
+ }
222
+ static buildEmptyAndConditionMessage() {
223
+ return `${EMPTY_AND_CONDITION_TAG}: AND nodes must contain at least one child condition`;
224
+ }
225
+ static parseComparableDate(value) {
226
+ if (value instanceof Date) {
227
+ if (!PolicyDateUtils.isValid(value)) return require_result.Result.err("Invalid Date instance");
228
+ return require_result.Result.ok(value);
229
+ }
230
+ if (typeof value !== "string" || !ISO_8601_UTC_DATE_PATTERN.test(value)) return null;
231
+ const parsed = new Date(value);
232
+ if (!PolicyDateUtils.isValid(parsed) || parsed.toISOString() !== value) return require_result.Result.err("Invalid ISO 8601 UTC date string");
233
+ return require_result.Result.ok(parsed);
234
+ }
235
+ static evaluateRelationalNumbers(actual, op, expected) {
236
+ switch (op) {
237
+ case "gt": return actual > expected;
238
+ case "gte": return actual >= expected;
239
+ case "lt": return actual < expected;
240
+ case "lte": return actual <= expected;
241
+ }
242
+ }
243
+ buildReport(params) {
244
+ return {
245
+ occurredAt: /* @__PURE__ */ new Date(),
246
+ level: params.level,
247
+ tag: params.tag,
248
+ message: params.message,
249
+ engineVersion: this.options.engineVersion,
250
+ details: params.details
251
+ };
252
+ }
253
+ conditionEvalErr(node, error) {
254
+ const cause = ConditionEvaluatorV1.describeError(error);
255
+ const message = `${CONDITION_EVAL_THROWN_TAG}: ${cause.name}: ${cause.message}`;
256
+ this.options.reporter.error(this.buildReport({
257
+ level: "error",
258
+ tag: CONDITION_EVAL_THROWN_TAG,
259
+ message,
260
+ details: {
261
+ node,
262
+ cause
263
+ }
264
+ }));
265
+ return require_result.Result.err(message);
266
+ }
267
+ static containsInvalidDate(value) {
268
+ if (value instanceof Date) return !PolicyDateUtils.isValid(value);
269
+ if (Array.isArray(value)) return value.some((item) => ConditionEvaluatorV1.containsInvalidDate(item));
270
+ return false;
271
+ }
272
+ static normalizeDateOperand(value) {
273
+ if (value instanceof Date) return value.toISOString();
274
+ if (Array.isArray(value)) return value.map((item) => ConditionEvaluatorV1.normalizeDateOperand(item));
275
+ return value;
276
+ }
277
+ reportDateOperandError(node, actual) {
278
+ const message = ConditionEvaluatorV1.buildInvalidDateOperandMessage(node);
279
+ this.options.reporter.error(this.buildReport({
280
+ level: "error",
281
+ tag: INVALID_DATE_OPERAND_TAG,
282
+ message,
283
+ details: {
284
+ field: node.field,
285
+ op: node.op,
286
+ actual,
287
+ expected: node.value,
288
+ node
289
+ }
290
+ }));
291
+ return require_result.Result.err(message);
292
+ }
293
+ reportInvalidNumericOperand(node, actual) {
294
+ const message = ConditionEvaluatorV1.buildInvalidNumericOperandMessage(node);
295
+ this.options.reporter.error(this.buildReport({
296
+ level: "error",
297
+ tag: INVALID_NUMERIC_OPERAND_TAG,
298
+ message,
299
+ details: {
300
+ field: node.field,
301
+ op: node.op,
302
+ actual,
303
+ expected: node.value,
304
+ node
305
+ }
306
+ }));
307
+ return require_result.Result.err(message);
308
+ }
309
+ reportInvalidSetOperand(node, actual) {
310
+ const message = ConditionEvaluatorV1.buildInvalidSetOperandMessage(node);
311
+ this.options.reporter.error(this.buildReport({
312
+ level: "error",
313
+ tag: INVALID_SET_OPERAND_TAG,
314
+ message,
315
+ details: {
316
+ field: node.field,
317
+ op: node.op,
318
+ actual,
319
+ expected: node.value,
320
+ node
321
+ }
322
+ }));
323
+ return require_result.Result.err(message);
324
+ }
325
+ evaluateDateRelationalNode(node, actual) {
326
+ if (!ConditionEvaluatorV1.isRelationalOperator(node.op)) return null;
327
+ const actualDateResult = ConditionEvaluatorV1.parseComparableDate(actual);
328
+ const expectedDateResult = ConditionEvaluatorV1.parseComparableDate(node.value);
329
+ if (actualDateResult === null && expectedDateResult === null) return null;
330
+ const allowsNull = node.allowNull === true;
331
+ if (actual === null) {
332
+ if (allowsNull) return require_result.Result.ok(false);
333
+ const message = ConditionEvaluatorV1.buildNullishDateOperandMessage(node, actual);
334
+ this.options.reporter.error(this.buildReport({
335
+ level: "error",
336
+ tag: NULLISH_DATE_OPERAND_NOT_ALLOWED_TAG,
337
+ message,
338
+ details: {
339
+ field: node.field,
340
+ op: node.op,
341
+ actual,
342
+ expected: node.value,
343
+ allowNull: allowsNull,
344
+ node
345
+ }
346
+ }));
347
+ return require_result.Result.err(message);
348
+ }
349
+ if (actual === void 0) {
350
+ const message = ConditionEvaluatorV1.buildNullishDateOperandMessage(node, actual);
351
+ this.options.reporter.error(this.buildReport({
352
+ level: "error",
353
+ tag: NULLISH_DATE_OPERAND_NOT_ALLOWED_TAG,
354
+ message,
355
+ details: {
356
+ field: node.field,
357
+ op: node.op,
358
+ actual,
359
+ expected: node.value,
360
+ allowNull: allowsNull,
361
+ node
362
+ }
363
+ }));
364
+ return require_result.Result.err(message);
365
+ }
366
+ if (actualDateResult === null || actualDateResult.isErr() || expectedDateResult === null || expectedDateResult.isErr()) return this.reportDateOperandError(node, actual);
367
+ return require_result.Result.ok(ConditionEvaluatorV1.evaluateRelationalNumbers(actualDateResult.getOrNull().getTime(), node.op, expectedDateResult.getOrNull().getTime()));
368
+ }
369
+ evaluateOperator(actual, op, expected) {
370
+ switch (op) {
371
+ case "eq": return actual === expected;
372
+ case "neq": return actual !== expected;
373
+ case "gt": return typeof actual === "number" && typeof expected === "number" && ConditionEvaluatorV1.evaluateRelationalNumbers(actual, op, expected);
374
+ case "gte": return typeof actual === "number" && typeof expected === "number" && ConditionEvaluatorV1.evaluateRelationalNumbers(actual, op, expected);
375
+ case "lt": return typeof actual === "number" && typeof expected === "number" && ConditionEvaluatorV1.evaluateRelationalNumbers(actual, op, expected);
376
+ case "lte": return typeof actual === "number" && typeof expected === "number" && ConditionEvaluatorV1.evaluateRelationalNumbers(actual, op, expected);
377
+ case "in": return Array.isArray(expected) && expected.includes(actual);
378
+ case "notIn": return Array.isArray(expected) && !expected.includes(actual);
379
+ case "isNull": return actual === null;
380
+ case "isNotNull": return actual !== null && actual !== void 0;
381
+ }
382
+ }
383
+ evaluateNumericRelationalNode(node, actual) {
384
+ const allowsNull = node.allowNull === true;
385
+ if (actual === void 0 || actual === null && !allowsNull) {
386
+ const message = ConditionEvaluatorV1.buildNullishNumericOperandMessage(node, actual);
387
+ this.options.reporter.error(this.buildReport({
388
+ level: "error",
389
+ tag: NULLISH_NUMERIC_OPERAND_NOT_ALLOWED_TAG,
390
+ message,
391
+ details: {
392
+ field: node.field,
393
+ op: node.op,
394
+ actual,
395
+ expected: node.value,
396
+ allowNull: allowsNull,
397
+ node
398
+ }
399
+ }));
400
+ return require_result.Result.err(message);
401
+ }
402
+ if (actual === null) return require_result.Result.ok(false);
403
+ if (typeof actual !== "number" || typeof node.value !== "number") return this.reportInvalidNumericOperand(node, actual);
404
+ return require_result.Result.ok(this.evaluateOperator(actual, node.op, node.value));
405
+ }
406
+ evaluateLeafNode(node) {
407
+ const actualResult = PolicyContextPath.getOrAbsent(this.context, node.field);
408
+ if (actualResult.isErr()) {
409
+ this.options.reporter.warn(this.buildReport({
410
+ level: "warn",
411
+ tag: "MISSING_CONTEXT_FIELD",
412
+ message: `MISSING_CONTEXT_FIELD: "${node.field}" not found in context`,
413
+ details: {
414
+ field: node.field,
415
+ node
416
+ }
417
+ }));
418
+ return require_result.Result.err(`MISSING_CONTEXT_FIELD: "${node.field}" not found in context`);
419
+ }
420
+ try {
421
+ const actual = actualResult.getOrThrow();
422
+ if (ConditionEvaluatorV1.isRelationalOperator(node.op)) {
423
+ const dateResult = this.evaluateDateRelationalNode(node, actual);
424
+ if (dateResult !== null) return dateResult;
425
+ return this.evaluateNumericRelationalNode(node, actual);
426
+ }
427
+ if ((node.op === "in" || node.op === "notIn") && !Array.isArray(node.value)) return this.reportInvalidSetOperand(node, actual);
428
+ if (node.op === "eq" || node.op === "neq" || node.op === "in" || node.op === "notIn") {
429
+ if (ConditionEvaluatorV1.containsInvalidDate(actual) || ConditionEvaluatorV1.containsInvalidDate(node.value)) return this.reportDateOperandError(node, actual);
430
+ return require_result.Result.ok(this.evaluateOperator(ConditionEvaluatorV1.normalizeDateOperand(actual), node.op, ConditionEvaluatorV1.normalizeDateOperand(node.value)));
431
+ }
432
+ return require_result.Result.ok(this.evaluateOperator(actual, node.op, node.value));
433
+ } catch (error) {
434
+ return this.conditionEvalErr(node, error);
435
+ }
436
+ }
437
+ buildTrace(params) {
438
+ return {
439
+ conditionPath: params.conditionPath,
440
+ decisiveNode: params.decisiveNode,
441
+ lastEvaluatedLeaf: params.lastEvaluatedLeaf,
442
+ lastEvaluatedLeafPath: params.lastEvaluatedLeafPath,
443
+ lastEvaluatedLeafResult: params.lastEvaluatedLeafResult
444
+ };
445
+ }
446
+ evaluateWithTraceInternal(node, path) {
447
+ if (ConditionEvaluatorV1.isLeafNode(node)) {
448
+ const leafResult = this.evaluateLeafNode(node);
449
+ if (leafResult.isErr()) return require_result.Result.err(leafResult.errorOrNull());
450
+ const matched = leafResult.getOrNull();
451
+ return require_result.Result.ok({
452
+ matched,
453
+ trace: this.buildTrace({
454
+ conditionPath: path,
455
+ decisiveNode: node,
456
+ lastEvaluatedLeaf: node,
457
+ lastEvaluatedLeafPath: path,
458
+ lastEvaluatedLeafResult: matched
459
+ })
460
+ });
461
+ }
462
+ if (ConditionEvaluatorV1.isAndNode(node)) {
463
+ if (node.and.length === 0) return require_result.Result.err(ConditionEvaluatorV1.buildEmptyAndConditionMessage());
464
+ let lastTracedChild = null;
465
+ for (const [index, child] of node.and.entries()) {
466
+ const childPath = `${path}.and[${index}]`;
467
+ const childResult = this.evaluateWithTraceInternal(child, childPath);
468
+ if (childResult.isErr()) return require_result.Result.err(childResult.errorOrNull());
469
+ const tracedChild = childResult.getOrNull();
470
+ lastTracedChild = tracedChild;
471
+ if (!tracedChild.matched) return require_result.Result.ok({
472
+ matched: false,
473
+ trace: this.buildTrace({
474
+ conditionPath: childPath,
475
+ decisiveNode: child,
476
+ lastEvaluatedLeaf: tracedChild.trace.lastEvaluatedLeaf,
477
+ lastEvaluatedLeafPath: tracedChild.trace.lastEvaluatedLeafPath,
478
+ lastEvaluatedLeafResult: tracedChild.trace.lastEvaluatedLeafResult
479
+ })
480
+ });
481
+ }
482
+ return require_result.Result.ok({
483
+ matched: true,
484
+ trace: lastTracedChild.trace
485
+ });
486
+ }
487
+ if (ConditionEvaluatorV1.isOrNode(node)) {
488
+ let lastTrace = null;
489
+ for (const [index, child] of node.or.entries()) {
490
+ const childResult = this.evaluateWithTraceInternal(child, `${path}.or[${index}]`);
491
+ if (childResult.isErr()) return require_result.Result.err(childResult.errorOrNull());
492
+ const tracedChild = childResult.getOrNull();
493
+ lastTrace = tracedChild.trace;
494
+ if (tracedChild.matched) return require_result.Result.ok({
495
+ matched: true,
496
+ trace: tracedChild.trace
497
+ });
498
+ }
499
+ if (lastTrace === null) return require_result.Result.err(ConditionEvaluatorV1.buildEmptyOrConditionMessage());
500
+ return require_result.Result.ok({
501
+ matched: false,
502
+ trace: lastTrace
503
+ });
504
+ }
505
+ if (ConditionEvaluatorV1.isNotNode(node)) {
506
+ const childPath = `${path}.not`;
507
+ const childResult = this.evaluateWithTraceInternal(node.not, childPath);
508
+ if (childResult.isErr()) return require_result.Result.err(childResult.errorOrNull());
509
+ const tracedChild = childResult.getOrNull();
510
+ return require_result.Result.ok({
511
+ matched: !tracedChild.matched,
512
+ trace: this.buildTrace({
513
+ conditionPath: path,
514
+ decisiveNode: node,
515
+ lastEvaluatedLeaf: tracedChild.trace.lastEvaluatedLeaf,
516
+ lastEvaluatedLeafPath: tracedChild.trace.lastEvaluatedLeafPath,
517
+ lastEvaluatedLeafResult: tracedChild.trace.lastEvaluatedLeafResult
518
+ })
519
+ });
520
+ }
521
+ return this.conditionEvalErr(node, /* @__PURE__ */ new TypeError("Invalid condition node shape"));
522
+ }
523
+ evaluate(node) {
524
+ try {
525
+ const result = this.evaluateWithTraceInternal(node, "$");
526
+ if (result.isErr()) return require_result.Result.err(result.errorOrNull());
527
+ return require_result.Result.ok(result.getOrNull().matched);
528
+ } catch (error) {
529
+ return this.conditionEvalErr(node, error);
530
+ }
531
+ }
532
+ evaluateWithTrace(node) {
533
+ try {
534
+ return this.evaluateWithTraceInternal(node, "$");
535
+ } catch (error) {
536
+ return this.conditionEvalErr(node, error);
537
+ }
538
+ }
539
+ static extractFields(node) {
540
+ if (ConditionEvaluatorV1.isLeafNode(node)) return [node.field];
541
+ if (ConditionEvaluatorV1.isAndNode(node)) return node.and.flatMap((child) => ConditionEvaluatorV1.extractFields(child));
542
+ if (ConditionEvaluatorV1.isOrNode(node)) return node.or.flatMap((child) => ConditionEvaluatorV1.extractFields(child));
543
+ if (ConditionEvaluatorV1.isNotNode(node)) return ConditionEvaluatorV1.extractFields(node.not);
544
+ return [];
545
+ }
546
+ };
547
+ //#endregion
548
+ Object.defineProperty(exports, "ConditionEvaluatorV1", {
549
+ enumerable: true,
550
+ get: function() {
551
+ return ConditionEvaluatorV1;
552
+ }
553
+ });
554
+ Object.defineProperty(exports, "CoreConfig", {
555
+ enumerable: true,
556
+ get: function() {
557
+ return CoreConfig;
558
+ }
559
+ });
560
+ Object.defineProperty(exports, "PolicyContextPath", {
561
+ enumerable: true,
562
+ get: function() {
563
+ return PolicyContextPath;
564
+ }
565
+ });
566
+ Object.defineProperty(exports, "SilentPolicyReporter", {
567
+ enumerable: true,
568
+ get: function() {
569
+ return SilentPolicyReporter;
570
+ }
571
+ });
572
+ Object.defineProperty(exports, "coreConfig", {
573
+ enumerable: true,
574
+ get: function() {
575
+ return coreConfig;
576
+ }
577
+ });
578
+
579
+ //# sourceMappingURL=condition-evaluator.cjs.map