@cullet/erp-core 1.2.0 → 1.3.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.
- package/dist/app-error.d.cts +108 -0
- package/dist/application/index.cjs +26 -0
- package/dist/application/index.d.cts +2 -0
- package/dist/domain/index.cjs +16 -0
- package/dist/domain/index.d.cts +3 -0
- package/dist/domain-event-contracts.cjs +33 -0
- package/dist/domain-event-contracts.cjs.map +1 -0
- package/dist/domain-event-contracts.d.cts +27 -0
- package/dist/domain-exception.cjs +17 -0
- package/dist/domain-exception.cjs.map +1 -0
- package/dist/domain-exception.d.cts +7 -0
- package/dist/errors/index.cjs +41 -0
- package/dist/errors/index.d.cts +3 -0
- package/dist/exceptions/index.cjs +17 -0
- package/dist/exceptions/index.d.cts +5 -0
- package/dist/exceptions/validation-field.cjs +3 -0
- package/dist/exceptions/validation-field.d.cts +2 -0
- package/dist/gate-engine-registry.cjs +308 -0
- package/dist/gate-engine-registry.cjs.map +1 -0
- package/dist/gate-engine-registry.d.cts +81 -0
- package/dist/gate-types.d.cts +171 -0
- package/dist/gate-v1-payload.schema.cjs +638 -0
- package/dist/gate-v1-payload.schema.cjs.map +1 -0
- package/dist/hashing.cjs +66 -0
- package/dist/hashing.cjs.map +1 -0
- package/dist/immutable.cjs +77 -0
- package/dist/immutable.cjs.map +1 -0
- package/dist/immutable.d.cts +6 -0
- package/dist/index.cjs +163 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/invalid-state-transition-exception.cjs +47 -0
- package/dist/invalid-state-transition-exception.cjs.map +1 -0
- package/dist/invariant-violation-exception.cjs +16 -0
- package/dist/invariant-violation-exception.cjs.map +1 -0
- package/dist/not-found-error.cjs +65 -0
- package/dist/not-found-error.cjs.map +1 -0
- package/dist/outcome.cjs +97 -0
- package/dist/outcome.cjs.map +1 -0
- package/dist/outcome.d.cts +140 -0
- package/dist/parse-gate-payload.d.cts +62 -0
- package/dist/path.d.cts +90 -0
- package/dist/plugin.cjs +79 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.cts +85 -0
- package/dist/plugins/index.cjs +3 -0
- package/dist/plugins/index.d.cts +2 -0
- package/dist/policies/engines/index.cjs +10 -0
- package/dist/policies/engines/index.d.cts +4 -0
- package/dist/policies/engines/v1/gate/index.cjs +91 -0
- package/dist/policies/engines/v1/gate/index.cjs.map +1 -0
- package/dist/policies/engines/v1/gate/index.d.cts +121 -0
- package/dist/policies/index.cjs +41 -0
- package/dist/policies/index.d.cts +7 -0
- package/dist/policy-service.cjs +1190 -0
- package/dist/policy-service.cjs.map +1 -0
- package/dist/policy-service.d.cts +559 -0
- package/dist/result/index.cjs +7 -0
- package/dist/result/index.d.cts +2 -0
- package/dist/result.cjs +135 -0
- package/dist/result.cjs.map +1 -0
- package/dist/ruleset-registry.cjs +47 -0
- package/dist/ruleset-registry.cjs.map +1 -0
- package/dist/rulesets/index.cjs +3 -0
- package/dist/rulesets/index.d.cts +2 -0
- package/dist/temporal-guards.cjs +32 -0
- package/dist/temporal-guards.cjs.map +1 -0
- package/dist/temporal-use-case.cjs +191 -0
- package/dist/temporal-use-case.cjs.map +1 -0
- package/dist/temporal-use-case.d.cts +304 -0
- package/dist/unexpected-error.cjs +208 -0
- package/dist/unexpected-error.cjs.map +1 -0
- package/dist/use-case.cjs +96 -0
- package/dist/use-case.cjs.map +1 -0
- package/dist/uuid-identifier.cjs +65 -0
- package/dist/uuid-identifier.cjs.map +1 -0
- package/dist/uuid-identifier.d.cts +230 -0
- package/dist/validation-code.cjs +60 -0
- package/dist/validation-code.cjs.map +1 -0
- package/dist/validation-code.d.cts +23 -0
- package/dist/validation-error.cjs +1020 -0
- package/dist/validation-error.cjs.map +1 -0
- package/dist/validation-error.d.cts +777 -0
- package/dist/validation-exception.cjs +41 -0
- package/dist/validation-exception.cjs.map +1 -0
- package/dist/validation-exception.d.cts +50 -0
- package/dist/validation-field.cjs +28 -0
- package/dist/validation-field.cjs.map +1 -0
- package/dist/validation-field.d.cts +12 -0
- package/dist/value-object-ruleset.contracts.d.cts +36 -0
- package/dist/value-object.cjs +208 -0
- package/dist/value-object.cjs.map +1 -0
- package/dist/version.d.cts +10 -0
- package/dist/versioning/index.cjs +7 -0
- package/dist/versioning/index.d.cts +3 -0
- package/meta.json +3 -2
- package/package.json +147 -28
- package/src/version.ts +1 -1
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
const require_temporal_guards = require("./temporal-guards.cjs");
|
|
2
|
+
const require_result = require("./result.cjs");
|
|
3
|
+
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
|
+
//#region src/core/policies/engines/v1/condition-schema.ts
|
|
164
|
+
const conditionOpSchema = zod.z.enum([
|
|
165
|
+
"eq",
|
|
166
|
+
"neq",
|
|
167
|
+
"gt",
|
|
168
|
+
"gte",
|
|
169
|
+
"lt",
|
|
170
|
+
"lte",
|
|
171
|
+
"in",
|
|
172
|
+
"notIn",
|
|
173
|
+
"isNull",
|
|
174
|
+
"isNotNull"
|
|
175
|
+
]);
|
|
176
|
+
const conditionLeafNodeSchema = zod.z.object({
|
|
177
|
+
field: zod.z.string().min(1),
|
|
178
|
+
op: conditionOpSchema,
|
|
179
|
+
value: zod.z.unknown(),
|
|
180
|
+
allowNull: zod.z.literal(true).optional()
|
|
181
|
+
}).strict();
|
|
182
|
+
const conditionNodeSchema = zod.z.lazy(() => zod.z.union([
|
|
183
|
+
conditionLeafNodeSchema,
|
|
184
|
+
zod.z.object({ and: zod.z.array(conditionNodeSchema).min(1) }).strict(),
|
|
185
|
+
zod.z.object({ or: zod.z.array(conditionNodeSchema).min(1) }).strict(),
|
|
186
|
+
zod.z.object({ not: conditionNodeSchema }).strict()
|
|
187
|
+
]));
|
|
188
|
+
//#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
|
+
//#region src/core/policies/engines/v1/gate/gate-v1-payload.schema.ts
|
|
562
|
+
var GatePayloadSchemaV1 = class GatePayloadSchemaV1 {
|
|
563
|
+
static {
|
|
564
|
+
this.schema = zod.z.union([zod.z.object({ condition: conditionNodeSchema }).strict(), zod.z.object({
|
|
565
|
+
allowIf: conditionNodeSchema,
|
|
566
|
+
defaultOutcome: zod.z.enum(["ALLOW", "DENY"])
|
|
567
|
+
}).strict()]);
|
|
568
|
+
}
|
|
569
|
+
static parse(payload) {
|
|
570
|
+
const parsed = GatePayloadSchemaV1.schema.safeParse(payload);
|
|
571
|
+
if (!parsed.success) {
|
|
572
|
+
const details = parsed.error.issues.map((issue) => {
|
|
573
|
+
const path = issue.path.join(".");
|
|
574
|
+
return path.length > 0 ? `${path}: ${issue.message}` : issue.message;
|
|
575
|
+
}).join("; ");
|
|
576
|
+
return require_result.Result.err(`Invalid gate payload: ${details}`);
|
|
577
|
+
}
|
|
578
|
+
return require_result.Result.ok(parsed.data);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
const gatePayloadSchema = GatePayloadSchemaV1.schema;
|
|
582
|
+
//#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
|
+
Object.defineProperty(exports, "GatePayloadSchemaV1", {
|
|
596
|
+
enumerable: true,
|
|
597
|
+
get: function() {
|
|
598
|
+
return GatePayloadSchemaV1;
|
|
599
|
+
}
|
|
600
|
+
});
|
|
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
|
+
Object.defineProperty(exports, "conditionLeafNodeSchema", {
|
|
614
|
+
enumerable: true,
|
|
615
|
+
get: function() {
|
|
616
|
+
return conditionLeafNodeSchema;
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
Object.defineProperty(exports, "conditionNodeSchema", {
|
|
620
|
+
enumerable: true,
|
|
621
|
+
get: function() {
|
|
622
|
+
return conditionNodeSchema;
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
Object.defineProperty(exports, "coreConfig", {
|
|
626
|
+
enumerable: true,
|
|
627
|
+
get: function() {
|
|
628
|
+
return coreConfig;
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
Object.defineProperty(exports, "gatePayloadSchema", {
|
|
632
|
+
enumerable: true,
|
|
633
|
+
get: function() {
|
|
634
|
+
return gatePayloadSchema;
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
//# sourceMappingURL=gate-v1-payload.schema.cjs.map
|