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