validup 0.5.1 → 2.0.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/README.md +34 -7
- package/dist/index.d.mts +238 -408
- package/dist/index.mjs +534 -476
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { BaseError,
|
|
1
|
+
import { BaseError, IssueCode, defineIssueGroup, defineIssueItem, isIssue, prefixIssuePath } from "@ebec/core";
|
|
2
2
|
import { distinctArray } from "smob";
|
|
3
3
|
import { arrayToPath, expandPath, getPathValue, pathToArray, setPathValue } from "pathtrace";
|
|
4
|
+
import { op, runTwinAsync, runTwinSync } from "twinop";
|
|
4
5
|
//#region src/utils/has-own-property.ts
|
|
5
6
|
function hasOwnProperty(obj, prop) {
|
|
6
7
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
@@ -16,203 +17,138 @@ function isContainer(input) {
|
|
|
16
17
|
return isObject(input) && typeof input.run === "function" && typeof input.safeRun === "function";
|
|
17
18
|
}
|
|
18
19
|
//#endregion
|
|
19
|
-
//#region src/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
//#region src/container/paths-strict-violation.ts
|
|
21
|
+
/**
|
|
22
|
+
* Thrown by a `Container` run (all variants) when `pathsStrict` is enabled and
|
|
23
|
+
* one or more `pathsToInclude` / `pathsToExclude` entries matched **no mount** —
|
|
24
|
+
* neither an exact key match nor a prefix descent into a container mount. It
|
|
25
|
+
* signals a misconfigured validator graph (typically a mount key that was
|
|
26
|
+
* renamed out from under a caller's static path list), NOT a validation failure
|
|
27
|
+
* of the input.
|
|
28
|
+
*
|
|
29
|
+
* Like `RunSyncViolationError` it is structural, so the per-mount catch and
|
|
30
|
+
* `safeRun` / `safeRunSync` re-throw it verbatim instead of folding it into the
|
|
31
|
+
* issue list or a `Result.failure`. Unlike `RunSyncViolationError` it IS
|
|
32
|
+
* exported from the package barrel — the whole point is that consumers catch it,
|
|
33
|
+
* read `pathsToInclude` / `pathsToExclude`, and surface which paths went stale.
|
|
34
|
+
*
|
|
35
|
+
* The reported paths are **absolute** (prefixed with the owning container's
|
|
36
|
+
* `path` when the violation happens inside a nested container), so a caller sees
|
|
37
|
+
* `user.email` rather than the child-local `email`.
|
|
38
|
+
*/
|
|
39
|
+
var PathsStrictViolationError = class extends Error {
|
|
40
|
+
name = "PathsStrictViolationError";
|
|
41
|
+
/** Absolute `pathsToInclude` entries that matched no mount. */
|
|
42
|
+
pathsToInclude;
|
|
43
|
+
/** Absolute `pathsToExclude` entries that matched no mount. */
|
|
44
|
+
pathsToExclude;
|
|
45
|
+
constructor(input) {
|
|
46
|
+
const pathsToInclude = input.pathsToInclude ?? [];
|
|
47
|
+
const pathsToExclude = input.pathsToExclude ?? [];
|
|
48
|
+
const parts = [];
|
|
49
|
+
if (pathsToInclude.length > 0) parts.push(`pathsToInclude has no mount for: ${pathsToInclude.join(", ")}`);
|
|
50
|
+
if (pathsToExclude.length > 0) parts.push(`pathsToExclude has no mount for: ${pathsToExclude.join(", ")}`);
|
|
51
|
+
super(`pathsStrict: ${parts.join("; ")}`);
|
|
52
|
+
this.pathsToInclude = pathsToInclude;
|
|
53
|
+
this.pathsToExclude = pathsToExclude;
|
|
32
54
|
}
|
|
33
55
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (typeof input.message !== "string") return false;
|
|
43
|
-
if (!isIssuePath(input.path)) return false;
|
|
44
|
-
return typeof input.meta === "undefined" || isObject(input.meta);
|
|
45
|
-
}
|
|
46
|
-
function isIssueItem(input) {
|
|
47
|
-
if (!isBaseIssue(input)) return false;
|
|
48
|
-
if (input.type !== "item") return false;
|
|
49
|
-
return typeof input.code === "string";
|
|
50
|
-
}
|
|
51
|
-
function isIssueGroup(input) {
|
|
52
|
-
if (!isBaseIssue(input)) return false;
|
|
53
|
-
if (input.type !== "group") return false;
|
|
54
|
-
return Array.isArray(input.issues) && input.issues.every((issue) => isIssueItem(issue) || isIssueGroup(issue));
|
|
56
|
+
/**
|
|
57
|
+
* Duck-typed check — robust against `instanceof` mismatches when a duplicate
|
|
58
|
+
* copy of the package exists in the dependency tree, or when the throw crosses
|
|
59
|
+
* a realm boundary. Mirrors the `isValidupError` / `isRunSyncViolation` pattern.
|
|
60
|
+
*/
|
|
61
|
+
function isPathsStrictViolation(e) {
|
|
62
|
+
if (e instanceof PathsStrictViolationError) return true;
|
|
63
|
+
return typeof e === "object" && e !== null && e.name === "PathsStrictViolationError";
|
|
55
64
|
}
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/container/run-sync-violation.ts
|
|
67
|
+
/**
|
|
68
|
+
* Thrown by `Container.runSync` when a validator returns a Promise or a
|
|
69
|
+
* nested container does not implement `runSync`. Marked separately from
|
|
70
|
+
* generic `Error` so the per-mount catch can re-throw it instead of folding
|
|
71
|
+
* it into the issue list (otherwise users would get "Property X is invalid"
|
|
72
|
+
* instead of the structural diagnostic).
|
|
73
|
+
*
|
|
74
|
+
* Internal — not exported from the package barrel.
|
|
75
|
+
*/
|
|
76
|
+
var RunSyncViolationError = class extends Error {
|
|
77
|
+
name = "RunSyncViolationError";
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Duck-typed check — robust against `instanceof` mismatches when a duplicate
|
|
81
|
+
* copy of the package exists in the dependency tree, or when the throw
|
|
82
|
+
* crosses a realm boundary. Mirrors the `isValidupError` pattern.
|
|
83
|
+
*/
|
|
84
|
+
function isRunSyncViolation(e) {
|
|
85
|
+
if (e instanceof RunSyncViolationError) return true;
|
|
86
|
+
return typeof e === "object" && e !== null && e.name === "RunSyncViolationError";
|
|
58
87
|
}
|
|
59
88
|
//#endregion
|
|
60
|
-
//#region src/
|
|
89
|
+
//#region src/container/structural-throw.ts
|
|
61
90
|
/**
|
|
62
|
-
*
|
|
63
|
-
* validator codes onto these so consumer-side translation catalogs
|
|
64
|
-
* (e.g. `@ilingo/validup`) can ship one localized message per code
|
|
65
|
-
* instead of falling back to a generic "invalid value" fallback.
|
|
91
|
+
* Should this thrown value escape the issue-folding path verbatim?
|
|
66
92
|
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* `JSON`, `BASE64`, `STRONG_PASSWORD`, …) cover ground vuelidate
|
|
71
|
-
* predates. Every format code names the format positively (`UUID`,
|
|
72
|
-
* `EMAIL`, `URL`, …) rather than the failure mode (`NOT_UUID`,
|
|
73
|
-
* `INVALID_EMAIL`) so the registry reads consistently.
|
|
93
|
+
* Three unrelated reasons say yes, and every site in `Container` that folds a
|
|
94
|
+
* throw into `Issue[]` has to ask all three. Collapsing them into one predicate
|
|
95
|
+
* keeps the list in a single place — a fourth carve-out is one edit, not three.
|
|
74
96
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* own
|
|
97
|
+
* 1. **The run was cancelled.** `signal.aborted` is a *run-state* read, not a
|
|
98
|
+
* property of `error`. During an aborted run the thrown value is re-raised
|
|
99
|
+
* **as-is** — it is NOT necessarily `signal.reason`, because a mid-flight
|
|
100
|
+
* validator may throw its own error before the next abort check fires. That
|
|
101
|
+
* contract is public (see the `@throws` block on {@link Container.run}), so
|
|
102
|
+
* this leg must stay a signal read and must never become an
|
|
103
|
+
* `isAbortError(error)` test.
|
|
104
|
+
* 2. **`RunSyncViolationError`.** Structural: the caller cannot use `runSync`
|
|
105
|
+
* against this graph. Folding it in would surface "Property X is invalid"
|
|
106
|
+
* instead of the diagnostic.
|
|
107
|
+
* 3. **`PathsStrictViolationError`.** Structural: the validator graph is
|
|
108
|
+
* misconfigured (a `pathsToInclude` / `pathsToExclude` entry matched no
|
|
109
|
+
* mount), not the input. It reaches a parent's per-mount catch because the
|
|
110
|
+
* child `run()` is awaited inside the `try`.
|
|
79
111
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
112
|
+
* Both error legs delegate to the duck-typed guards rather than `instanceof`,
|
|
113
|
+
* so a throw crossing a realm boundary or originating from a duplicate copy of
|
|
114
|
+
* the package is still recognised.
|
|
82
115
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
116
|
+
* **Name caveat.** "Structural" describes legs 2 and 3 exactly; leg 1 is run
|
|
117
|
+
* state, so an aborted run makes this return `true` for *any* value —
|
|
118
|
+
* including a plain validation `ValidupError` from a validator that finished
|
|
119
|
+
* just before the abort landed. The name is kept for continuity with the
|
|
120
|
+
* comments it replaces; read it as "must not be folded into issues", not as a
|
|
121
|
+
* statement about the error's type.
|
|
88
122
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
const IssueCode = {
|
|
94
|
-
/** Generic fallback when no more-specific code applies. `data`: — */
|
|
95
|
-
VALUE_INVALID: "value_invalid",
|
|
96
|
-
/** Every branch of a `oneOf` container failed. `data`: — */
|
|
97
|
-
ONE_OF_FAILED: "one_of_failed",
|
|
98
|
-
/** Value is missing, `undefined`, `null`, or empty per the validator's semantics. `data`: — */
|
|
99
|
-
REQUIRED: "required",
|
|
100
|
-
/** Value contains characters outside the alphabetical set. `data`: — */
|
|
101
|
-
ALPHA: "alpha",
|
|
102
|
-
/** Value contains characters outside the alphanumeric set. `data`: — */
|
|
103
|
-
ALPHA_NUM: "alpha_num",
|
|
104
|
-
/** Value is not a number. `data`: — */
|
|
105
|
-
NUMERIC: "numeric",
|
|
106
|
-
/** Value is not an integer. `data`: — */
|
|
107
|
-
INTEGER: "integer",
|
|
108
|
-
/** Value is not a decimal number. `data`: — */
|
|
109
|
-
DECIMAL: "decimal",
|
|
110
|
-
/** Value is shorter than the configured minimum. `data`: `{ min: number }` */
|
|
111
|
-
MIN_LENGTH: "min_length",
|
|
112
|
-
/** Value is longer than the configured maximum. `data`: `{ max: number }` */
|
|
113
|
-
MAX_LENGTH: "max_length",
|
|
114
|
-
/** Numeric value is below the configured minimum. `data`: `{ min: number }` */
|
|
115
|
-
MIN_VALUE: "min_value",
|
|
116
|
-
/** Numeric value is above the configured maximum. `data`: `{ max: number }` */
|
|
117
|
-
MAX_VALUE: "max_value",
|
|
118
|
-
/** Numeric value falls outside the configured `[min, max]` range. `data`: `{ min: number, max: number }` */
|
|
119
|
-
BETWEEN: "between",
|
|
120
|
-
/** Value is not a valid email address. `data`: — */
|
|
121
|
-
EMAIL: "email",
|
|
122
|
-
/** Value is not a valid URL. `data`: — */
|
|
123
|
-
URL: "url",
|
|
124
|
-
/** Value is not a valid IP address. `data`: — */
|
|
125
|
-
IP_ADDRESS: "ip_address",
|
|
126
|
-
/** Value is not a valid MAC address. `data`: — */
|
|
127
|
-
MAC_ADDRESS: "mac_address",
|
|
128
|
-
/** Value is not a valid UUID. `data`: — */
|
|
129
|
-
UUID: "uuid",
|
|
130
|
-
/** Value is not a valid date / cannot be parsed as a date. `data`: — */
|
|
131
|
-
DATE: "date",
|
|
132
|
-
/**
|
|
133
|
-
* Value does not match the expected regex pattern. `data`:
|
|
134
|
-
* `{ pattern: string }` — source of the regex without flags so
|
|
135
|
-
* catalogs can quote it in human-readable form.
|
|
136
|
-
*/
|
|
137
|
-
PATTERN: "pattern",
|
|
138
|
-
/** Value is not valid JSON. `data`: — */
|
|
139
|
-
JSON: "json",
|
|
140
|
-
/** Value is not valid base64. `data`: — */
|
|
141
|
-
BASE64: "base64",
|
|
142
|
-
/**
|
|
143
|
-
* Value does not meet the configured strong-password rules. `data`:
|
|
144
|
-
* `{ minLength?: number, minLowercase?: number, minUppercase?: number,
|
|
145
|
-
* minNumbers?: number, minSymbols?: number }` — only the keys the
|
|
146
|
-
* adapter chose to surface; missing keys mean "the default for that
|
|
147
|
-
* setting wasn't configured."
|
|
148
|
-
*/
|
|
149
|
-
STRONG_PASSWORD: "strong_password",
|
|
150
|
-
/**
|
|
151
|
-
* Value must equal another named field's value (sibling-field assertion,
|
|
152
|
-
* e.g. password-confirm). `data`: `{ other: string }` — the name of
|
|
153
|
-
* the field being compared against.
|
|
154
|
-
*/
|
|
155
|
-
SAME_AS: "same_as"
|
|
156
|
-
};
|
|
157
|
-
//#endregion
|
|
158
|
-
//#region src/issue/define.ts
|
|
159
|
-
/**
|
|
160
|
-
* Build an `IssueItem`. TS uses the supplied `code` to pick the right
|
|
161
|
-
* `data` requirement at the call site — passing `MIN_LENGTH` without
|
|
162
|
-
* `data: { min }` is a compile error; passing `STRONG_PASSWORD` with
|
|
163
|
-
* `data: { pointsPerUnique: 5 }` is a compile error.
|
|
123
|
+
* Deliberately **not** a type predicate: the abort leg is true for arbitrary
|
|
124
|
+
* values, so `error is RunSyncViolationError | PathsStrictViolationError` would
|
|
125
|
+
* be an unsound narrowing. Every current caller re-throws immediately, but a
|
|
126
|
+
* future one might not.
|
|
164
127
|
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
128
|
+
* Internal — not exported from `container/index.ts`, and therefore not from the
|
|
129
|
+
* package barrel. It composes `isRunSyncViolation`, which is deliberately
|
|
130
|
+
* private; publishing the composite would leak that decision into the public,
|
|
131
|
+
* semver-protected surface.
|
|
167
132
|
*/
|
|
168
|
-
function
|
|
169
|
-
return
|
|
170
|
-
type: "item",
|
|
171
|
-
...data,
|
|
172
|
-
code: data.code || IssueCode.VALUE_INVALID
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
function defineIssueGroup(data) {
|
|
176
|
-
return {
|
|
177
|
-
type: "group",
|
|
178
|
-
...data
|
|
179
|
-
};
|
|
133
|
+
function isStructuralThrow(error, signal) {
|
|
134
|
+
return Boolean(signal?.aborted) || isRunSyncViolation(error) || isPathsStrictViolation(error);
|
|
180
135
|
}
|
|
181
136
|
//#endregion
|
|
182
|
-
//#region src/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
function flattenIssueGroups(issues) {
|
|
190
|
-
const output = [];
|
|
191
|
-
for (const issue of issues) if (isIssueGroup(issue)) {
|
|
192
|
-
output.push(issue);
|
|
193
|
-
output.push(...flattenIssueGroups(issue.issues));
|
|
137
|
+
//#region src/error/base.ts
|
|
138
|
+
var ValidupError = class extends BaseError {
|
|
139
|
+
issues;
|
|
140
|
+
constructor(issues = []) {
|
|
141
|
+
const paths = issues.map((issue) => stringifyPath(issue.path));
|
|
142
|
+
super({ message: buildErrorMessageForAttributes(paths) });
|
|
143
|
+
this.issues = issues;
|
|
194
144
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* Render an issue's user-facing message.
|
|
201
|
-
*
|
|
202
|
-
* Resolution order:
|
|
203
|
-
* 1. If `templates[code]` exists, return `interpolate(template, issue.data)`.
|
|
204
|
-
* 2. Else return `issue.message` (the default English rendering set at
|
|
205
|
-
* construction time).
|
|
206
|
-
* 3. Else return `fallback`.
|
|
207
|
-
*/
|
|
208
|
-
function formatIssue(issue, templates, fallback = "") {
|
|
209
|
-
if (templates && issue.code) {
|
|
210
|
-
const template = templates[issue.code];
|
|
211
|
-
if (typeof template === "string") return interpolate$1(template, issue.data || {});
|
|
145
|
+
toJSON() {
|
|
146
|
+
return {
|
|
147
|
+
...super.toJSON(),
|
|
148
|
+
issues: this.issues
|
|
149
|
+
};
|
|
212
150
|
}
|
|
213
|
-
|
|
214
|
-
return fallback;
|
|
215
|
-
}
|
|
151
|
+
};
|
|
216
152
|
//#endregion
|
|
217
153
|
//#region src/error/check.ts
|
|
218
154
|
function isError(input) {
|
|
@@ -353,7 +289,7 @@ function compose(elements, options = {}) {
|
|
|
353
289
|
});
|
|
354
290
|
if (typeof result !== "undefined") value = result;
|
|
355
291
|
} catch (e) {
|
|
356
|
-
if (bail) throw e;
|
|
292
|
+
if (bail || isStructuralThrow(e, ctx.signal)) throw e;
|
|
357
293
|
for (const issue of errorToIssues(e)) issues.push(issue);
|
|
358
294
|
}
|
|
359
295
|
if (issues.length > 0) throw new ValidupError(issues);
|
|
@@ -390,7 +326,7 @@ function composeAnyOf(elements) {
|
|
|
390
326
|
const result = await invokeComposeElement(element, { ...ctx });
|
|
391
327
|
return typeof result === "undefined" ? ctx.value : result;
|
|
392
328
|
} catch (e) {
|
|
393
|
-
if (ctx.signal
|
|
329
|
+
if (isStructuralThrow(e, ctx.signal)) throw e;
|
|
394
330
|
const wrapped = wrapBranchIssues(e, index);
|
|
395
331
|
for (const issue of wrapped) branchIssues.push(issue);
|
|
396
332
|
}
|
|
@@ -725,30 +661,15 @@ function defineValidator(descriptor) {
|
|
|
725
661
|
return descriptor;
|
|
726
662
|
}
|
|
727
663
|
//#endregion
|
|
728
|
-
//#region src/container/
|
|
729
|
-
/**
|
|
730
|
-
* Thrown by `Container.runSync` when a validator returns a Promise or a
|
|
731
|
-
* nested container does not implement `runSync`. Marked separately from
|
|
732
|
-
* generic `Error` so the per-mount catch can re-throw it instead of folding
|
|
733
|
-
* it into the issue list (otherwise users would get "Property X is invalid"
|
|
734
|
-
* instead of the structural diagnostic).
|
|
735
|
-
*
|
|
736
|
-
* Internal — not exported from the package barrel.
|
|
737
|
-
*/
|
|
738
|
-
var RunSyncViolationError = class extends Error {
|
|
739
|
-
name = "RunSyncViolationError";
|
|
740
|
-
};
|
|
664
|
+
//#region src/container/module.ts
|
|
741
665
|
/**
|
|
742
|
-
*
|
|
743
|
-
*
|
|
744
|
-
*
|
|
666
|
+
* Structural thenable probe. Used by the sync side of the validator effect to
|
|
667
|
+
* reject the one thing a synchronous graph cannot tolerate: a validator that
|
|
668
|
+
* returned a promise.
|
|
745
669
|
*/
|
|
746
|
-
function
|
|
747
|
-
|
|
748
|
-
return typeof e === "object" && e !== null && e.name === "RunSyncViolationError";
|
|
670
|
+
function isThenable(input) {
|
|
671
|
+
return isObject(input) && typeof input.then === "function";
|
|
749
672
|
}
|
|
750
|
-
//#endregion
|
|
751
|
-
//#region src/container/module.ts
|
|
752
673
|
var Container = class {
|
|
753
674
|
options;
|
|
754
675
|
items;
|
|
@@ -860,7 +781,36 @@ var Container = class {
|
|
|
860
781
|
*/
|
|
861
782
|
async run(data = {}, options = {}) {
|
|
862
783
|
if (options.parallel) return this.runParallel(data, options);
|
|
784
|
+
return runTwinAsync(this.runBody(data, options));
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* The single mount-resolution loop behind {@link Container.run} and
|
|
788
|
+
* {@link Container.runSync}.
|
|
789
|
+
*
|
|
790
|
+
* Written as a **twin body** (the `twinop` package): the two impure edges,
|
|
791
|
+
* invoking a validator and descending into a nested container, are yielded
|
|
792
|
+
* as `op(asyncThunk, syncThunk)` pairs, and the driver chosen by the public
|
|
793
|
+
* method (`runTwinAsync` / `runTwinSync`) executes the matching side. Effect
|
|
794
|
+
* errors are thrown back in at the `yield` site, so the cache-write and
|
|
795
|
+
* issue-collection `try`/`catch` blocks below run identically in both
|
|
796
|
+
* variants, with no second copy of the loop to keep in lockstep.
|
|
797
|
+
*
|
|
798
|
+
* The sync thunks own the two structural probes that only `runSync` needs:
|
|
799
|
+
* a nested container that doesn't implement `runSync`, and a validator that
|
|
800
|
+
* returned a thenable. Both throw `RunSyncViolationError`, which the async
|
|
801
|
+
* side cannot produce.
|
|
802
|
+
*
|
|
803
|
+
* `runParallel` deliberately does NOT share this body — a generator is
|
|
804
|
+
* sequential by construction, and expressing "launch every mount, then
|
|
805
|
+
* settle" through it would mean yielding batches and giving up the
|
|
806
|
+
* chain-read. It shares the extracted per-key helpers instead
|
|
807
|
+
* (`prepareMountKey` / `resolveOptionalDirective` / `buildChildRunOptions` /
|
|
808
|
+
* `buildValidatorContext`).
|
|
809
|
+
*/
|
|
810
|
+
*runBody(data, options) {
|
|
863
811
|
const { pathsToInclude, pathsToExclude } = this.resolveContainerFilters(options);
|
|
812
|
+
const pathsStrict = this.resolvePathsStrict(options);
|
|
813
|
+
this.assertPathsStrict(pathsStrict, data, pathsToInclude, pathsToExclude, options.path);
|
|
864
814
|
const output = {};
|
|
865
815
|
const issues = [];
|
|
866
816
|
let itemCount = 0;
|
|
@@ -872,36 +822,42 @@ var Container = class {
|
|
|
872
822
|
let pathCount = 0;
|
|
873
823
|
let pathFailed = false;
|
|
874
824
|
const branchStart = issues.length;
|
|
875
|
-
|
|
825
|
+
let keys = [];
|
|
826
|
+
try {
|
|
827
|
+
keys = item.path ? expandPath(data, item.path) : [""];
|
|
828
|
+
} catch (e) {
|
|
829
|
+
const attribution = this.describeKey(item.path || "");
|
|
830
|
+
this.collectExecutionFailure({
|
|
831
|
+
error: e,
|
|
832
|
+
item,
|
|
833
|
+
value: void 0,
|
|
834
|
+
keyParts: attribution.keyParts,
|
|
835
|
+
pathRelative: attribution.pathRelative,
|
|
836
|
+
issues,
|
|
837
|
+
signal: options.signal
|
|
838
|
+
});
|
|
839
|
+
pathCount = 1;
|
|
840
|
+
pathFailed = true;
|
|
841
|
+
}
|
|
876
842
|
for (const key of keys) {
|
|
877
|
-
|
|
878
|
-
const pathRelative = keyParts.at(-1);
|
|
879
|
-
const pathAbsolute = [...options.path ? options.path : [], ...keyParts];
|
|
843
|
+
let plan;
|
|
880
844
|
let value;
|
|
881
|
-
if (key.length > 0) value = hasOwnProperty(output, key) ? output[key] : getPathValue(data, key);
|
|
882
|
-
else value = data;
|
|
883
|
-
const filter = resolvePathFilter(pathsToInclude, pathsToExclude, key, item.type === "container");
|
|
884
|
-
if (filter.skip) continue;
|
|
885
845
|
try {
|
|
886
|
-
|
|
887
|
-
if (
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
846
|
+
plan = this.prepareMountKey(item, key, options, pathsToInclude, pathsToExclude);
|
|
847
|
+
if (key.length > 0) value = hasOwnProperty(output, key) ? output[key] : getPathValue(data, key);
|
|
848
|
+
else value = data;
|
|
849
|
+
if (plan.filter.skip) continue;
|
|
850
|
+
const optional = this.resolveOptionalDirective(item, value, options);
|
|
851
|
+
if (optional.skip) {
|
|
852
|
+
if (optional.write) output[key] = optional.value;
|
|
892
853
|
} else if (item.type === "container") {
|
|
893
|
-
const
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
context: options.context,
|
|
901
|
-
signal: options.signal,
|
|
902
|
-
cache: options.cache,
|
|
903
|
-
optionalValue: options.optionalValue,
|
|
904
|
-
...hasOwnProperty(options, "optionalAs") ? { optionalAs: options.optionalAs } : {}
|
|
854
|
+
const child = item.data;
|
|
855
|
+
const childInput = isObject(value) ? value : {};
|
|
856
|
+
const childOptions = this.buildChildRunOptions(options, key, plan, pathsStrict);
|
|
857
|
+
const tmp = yield* op(() => child.run(childInput, childOptions), () => {
|
|
858
|
+
const childRunSync = child.runSync;
|
|
859
|
+
if (typeof childRunSync !== "function") throw new RunSyncViolationError(`runSync: nested container at "${key || "<root>"}" does not implement runSync`);
|
|
860
|
+
return childRunSync.call(child, childInput, childOptions);
|
|
905
861
|
});
|
|
906
862
|
const tmpKeys = Object.keys(tmp);
|
|
907
863
|
for (const tmpKey of tmpKeys) output[this.mergePaths(key, tmpKey)] = tmp[tmpKey];
|
|
@@ -912,39 +868,40 @@ var Container = class {
|
|
|
912
868
|
group: options.group
|
|
913
869
|
};
|
|
914
870
|
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
915
|
-
if (cached)
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
871
|
+
if (cached) {
|
|
872
|
+
if (cached.ok) output[key] = cached.value;
|
|
873
|
+
else throw cached.error;
|
|
874
|
+
} else {
|
|
875
|
+
const validator = item.data;
|
|
876
|
+
const ctx = this.buildValidatorContext(key, plan, value, data, options);
|
|
877
|
+
try {
|
|
878
|
+
const result = yield* op(() => validator(ctx), () => {
|
|
879
|
+
const outcome = validator(ctx);
|
|
880
|
+
if (isThenable(outcome)) throw new RunSyncViolationError(`runSync: validator at "${key || "<root>"}" returned a Promise`);
|
|
881
|
+
return outcome;
|
|
882
|
+
});
|
|
883
|
+
output[key] = result;
|
|
884
|
+
this.writeCachedOutcome(item, key, snapshot, {
|
|
885
|
+
ok: true,
|
|
886
|
+
value: result
|
|
887
|
+
}, options.cache, options.signal);
|
|
888
|
+
} catch (e) {
|
|
889
|
+
if (!isStructuralThrow(e, options.signal)) this.writeCachedOutcome(item, key, snapshot, {
|
|
890
|
+
ok: false,
|
|
891
|
+
error: e
|
|
892
|
+
}, options.cache, options.signal);
|
|
893
|
+
throw e;
|
|
894
|
+
}
|
|
939
895
|
}
|
|
940
896
|
}
|
|
941
897
|
} catch (e) {
|
|
898
|
+
const attribution = plan || this.describeKey(key);
|
|
942
899
|
this.collectExecutionFailure({
|
|
943
900
|
error: e,
|
|
944
901
|
item,
|
|
945
902
|
value,
|
|
946
|
-
keyParts,
|
|
947
|
-
pathRelative,
|
|
903
|
+
keyParts: attribution.keyParts,
|
|
904
|
+
pathRelative: attribution.pathRelative,
|
|
948
905
|
issues,
|
|
949
906
|
signal: options.signal
|
|
950
907
|
});
|
|
@@ -966,9 +923,17 @@ var Container = class {
|
|
|
966
923
|
* Parallel-execution variant of `run()`. All mounts kick off their
|
|
967
924
|
* promises eagerly and the results are merged after `Promise.allSettled`.
|
|
968
925
|
* See `ContainerRunOptions.parallel` for the trade-off note.
|
|
926
|
+
*
|
|
927
|
+
* Shares every per-key helper with the sequential twin body
|
|
928
|
+
* (`prepareMountKey` / `resolveOptionalDirective` / `buildChildRunOptions` /
|
|
929
|
+
* `buildValidatorContext` / the cache pair) but keeps its own scheduling
|
|
930
|
+
* loop — see the note on {@link Container.runBody} for why it can't be a
|
|
931
|
+
* third twin driver.
|
|
969
932
|
*/
|
|
970
933
|
async runParallel(data, options) {
|
|
971
934
|
const { pathsToInclude, pathsToExclude } = this.resolveContainerFilters(options);
|
|
935
|
+
const pathsStrict = this.resolvePathsStrict(options);
|
|
936
|
+
this.assertPathsStrict(pathsStrict, data, pathsToInclude, pathsToExclude, options.path);
|
|
972
937
|
const output = {};
|
|
973
938
|
const issues = [];
|
|
974
939
|
const itemGroups = [];
|
|
@@ -978,84 +943,44 @@ var Container = class {
|
|
|
978
943
|
if (!this.isItemGroupIncluded(item, options.group)) continue;
|
|
979
944
|
const tasks = [];
|
|
980
945
|
let syncPathCount = 0;
|
|
981
|
-
|
|
946
|
+
let keys = [];
|
|
947
|
+
try {
|
|
948
|
+
keys = item.path ? expandPath(data, item.path) : [""];
|
|
949
|
+
} catch (e) {
|
|
950
|
+
const attribution = this.describeKey(item.path || "");
|
|
951
|
+
tasks.push({
|
|
952
|
+
key: item.path || "",
|
|
953
|
+
keyParts: attribution.keyParts,
|
|
954
|
+
pathRelative: attribution.pathRelative,
|
|
955
|
+
value: void 0,
|
|
956
|
+
promise: this.ownRejection(Promise.reject(e)),
|
|
957
|
+
kind: item.type
|
|
958
|
+
});
|
|
959
|
+
}
|
|
982
960
|
for (const key of keys) {
|
|
983
|
-
|
|
984
|
-
const pathRelative = keyParts.at(-1);
|
|
985
|
-
const pathAbsolute = [...options.path ? options.path : [], ...keyParts];
|
|
961
|
+
let plan;
|
|
986
962
|
let value;
|
|
987
|
-
if (key.length > 0) value = getPathValue(data, key);
|
|
988
|
-
else value = data;
|
|
989
|
-
const filter = resolvePathFilter(pathsToInclude, pathsToExclude, key, item.type === "container");
|
|
990
|
-
if (filter.skip) continue;
|
|
991
|
-
const resolvedOptionalValue = item.options.optionalValue ?? options.optionalValue ?? this.options.optionalValue;
|
|
992
|
-
if (typeof item.options.optional === "function" ? item.options.optional(value) : item.options.optional && isOptionalValue(value, resolvedOptionalValue)) {
|
|
993
|
-
if (hasOwnProperty(item.options, "optionalAs")) output[key] = item.options.optionalAs;
|
|
994
|
-
else if (hasOwnProperty(options, "optionalAs")) output[key] = options.optionalAs;
|
|
995
|
-
else if (hasOwnProperty(this.options, "optionalAs")) output[key] = this.options.optionalAs;
|
|
996
|
-
else if (item.options.optionalInclude) output[key] = value;
|
|
997
|
-
syncPathCount++;
|
|
998
|
-
continue;
|
|
999
|
-
}
|
|
1000
963
|
let promise;
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
cache: options.cache,
|
|
1012
|
-
optionalValue: options.optionalValue,
|
|
1013
|
-
...hasOwnProperty(options, "optionalAs") ? { optionalAs: options.optionalAs } : {}
|
|
1014
|
-
});
|
|
1015
|
-
else {
|
|
1016
|
-
const snapshot = {
|
|
1017
|
-
value,
|
|
1018
|
-
context: options.context,
|
|
1019
|
-
group: options.group
|
|
1020
|
-
};
|
|
1021
|
-
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
1022
|
-
if (cached) promise = cached.ok ? Promise.resolve(cached.value) : Promise.reject(cached.error);
|
|
1023
|
-
else {
|
|
1024
|
-
const itemData = item.data;
|
|
1025
|
-
const captureItem = item;
|
|
1026
|
-
const captureKey = key;
|
|
1027
|
-
const captureSnapshot = snapshot;
|
|
1028
|
-
promise = (async () => {
|
|
1029
|
-
try {
|
|
1030
|
-
const result = await itemData({
|
|
1031
|
-
key,
|
|
1032
|
-
path: pathAbsolute,
|
|
1033
|
-
value,
|
|
1034
|
-
data,
|
|
1035
|
-
group: options.group,
|
|
1036
|
-
context: options.context,
|
|
1037
|
-
signal: options.signal,
|
|
1038
|
-
cache: options.cache
|
|
1039
|
-
});
|
|
1040
|
-
this.writeCachedOutcome(captureItem, captureKey, captureSnapshot, {
|
|
1041
|
-
ok: true,
|
|
1042
|
-
value: result
|
|
1043
|
-
}, options.cache, options.signal);
|
|
1044
|
-
return result;
|
|
1045
|
-
} catch (e) {
|
|
1046
|
-
this.writeCachedOutcome(captureItem, captureKey, captureSnapshot, {
|
|
1047
|
-
ok: false,
|
|
1048
|
-
error: e
|
|
1049
|
-
}, options.cache, options.signal);
|
|
1050
|
-
throw e;
|
|
1051
|
-
}
|
|
1052
|
-
})();
|
|
964
|
+
try {
|
|
965
|
+
plan = this.prepareMountKey(item, key, options, pathsToInclude, pathsToExclude);
|
|
966
|
+
if (key.length > 0) value = getPathValue(data, key);
|
|
967
|
+
else value = data;
|
|
968
|
+
if (plan.filter.skip) continue;
|
|
969
|
+
const optional = this.resolveOptionalDirective(item, value, options);
|
|
970
|
+
if (optional.skip) {
|
|
971
|
+
if (optional.write) output[key] = optional.value;
|
|
972
|
+
syncPathCount++;
|
|
973
|
+
continue;
|
|
1053
974
|
}
|
|
975
|
+
promise = this.ownRejection(this.dispatchParallelMount(item, key, plan, value, data, options, pathsStrict));
|
|
976
|
+
} catch (e) {
|
|
977
|
+
promise = this.ownRejection(Promise.reject(e));
|
|
1054
978
|
}
|
|
979
|
+
const attribution = plan || this.describeKey(key);
|
|
1055
980
|
tasks.push({
|
|
1056
981
|
key,
|
|
1057
|
-
keyParts,
|
|
1058
|
-
pathRelative,
|
|
982
|
+
keyParts: attribution.keyParts,
|
|
983
|
+
pathRelative: attribution.pathRelative,
|
|
1059
984
|
value,
|
|
1060
985
|
promise,
|
|
1061
986
|
kind: item.type
|
|
@@ -1078,12 +1003,13 @@ var Container = class {
|
|
|
1078
1003
|
const branchStart = issues.length;
|
|
1079
1004
|
for (const [j, task] of tasks.entries()) {
|
|
1080
1005
|
const result = settled[j];
|
|
1081
|
-
if (result.status === "fulfilled")
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1006
|
+
if (result.status === "fulfilled") {
|
|
1007
|
+
if (task.kind === "container") {
|
|
1008
|
+
const tmp = result.value;
|
|
1009
|
+
const tmpKeys = Object.keys(tmp);
|
|
1010
|
+
for (const tmpKey of tmpKeys) output[this.mergePaths(task.key, tmpKey)] = tmp[tmpKey];
|
|
1011
|
+
} else output[task.key] = result.value;
|
|
1012
|
+
} else {
|
|
1087
1013
|
this.collectExecutionFailure({
|
|
1088
1014
|
error: result.reason,
|
|
1089
1015
|
item,
|
|
@@ -1107,6 +1033,58 @@ var Container = class {
|
|
|
1107
1033
|
options.signal?.throwIfAborted();
|
|
1108
1034
|
return this.finalizeOutput(output, options, issues, errorCount, itemCount);
|
|
1109
1035
|
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Claim ownership of a scheduled mount's rejection.
|
|
1038
|
+
*
|
|
1039
|
+
* `runParallel` creates every mount's promise eagerly and only attaches
|
|
1040
|
+
* `Promise.allSettled` after the whole scheduling loop has run. Any
|
|
1041
|
+
* rejection settling in that window — or, worse, any throw that escapes
|
|
1042
|
+
* the loop before `allSettled` is reached at all — is unowned, and Node's
|
|
1043
|
+
* default `--unhandled-rejections=throw` (this package requires Node >=
|
|
1044
|
+
* 24) terminates the process.
|
|
1045
|
+
*
|
|
1046
|
+
* The no-op subscriber below is a *second* consumer, discarded
|
|
1047
|
+
* immediately; the original promise is returned unchanged, so
|
|
1048
|
+
* `Promise.allSettled` still observes the real settlement and no outcome
|
|
1049
|
+
* is swallowed.
|
|
1050
|
+
*/
|
|
1051
|
+
ownRejection(promise) {
|
|
1052
|
+
promise.catch(() => {});
|
|
1053
|
+
return promise;
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Create the in-flight promise for one scheduled mount. Extracted from
|
|
1057
|
+
* `runParallel`'s scheduling loop so the loop body reads as
|
|
1058
|
+
* "resolve, gate, dispatch" and the pre-dispatch `try` stays legible.
|
|
1059
|
+
*/
|
|
1060
|
+
dispatchParallelMount(item, key, plan, value, data, options, pathsStrict) {
|
|
1061
|
+
if (item.type === "container") return item.data.run(isObject(value) ? value : {}, this.buildChildRunOptions(options, key, plan, pathsStrict, true));
|
|
1062
|
+
const snapshot = {
|
|
1063
|
+
value,
|
|
1064
|
+
context: options.context,
|
|
1065
|
+
group: options.group
|
|
1066
|
+
};
|
|
1067
|
+
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
1068
|
+
if (cached) return cached.ok ? Promise.resolve(cached.value) : Promise.reject(cached.error);
|
|
1069
|
+
const validator = item.data;
|
|
1070
|
+
const ctx = this.buildValidatorContext(key, plan, value, data, options);
|
|
1071
|
+
return (async () => {
|
|
1072
|
+
try {
|
|
1073
|
+
const result = await validator(ctx);
|
|
1074
|
+
this.writeCachedOutcome(item, key, snapshot, {
|
|
1075
|
+
ok: true,
|
|
1076
|
+
value: result
|
|
1077
|
+
}, options.cache, options.signal);
|
|
1078
|
+
return result;
|
|
1079
|
+
} catch (e) {
|
|
1080
|
+
if (!isStructuralThrow(e, options.signal)) this.writeCachedOutcome(item, key, snapshot, {
|
|
1081
|
+
ok: false,
|
|
1082
|
+
error: e
|
|
1083
|
+
}, options.cache, options.signal);
|
|
1084
|
+
throw e;
|
|
1085
|
+
}
|
|
1086
|
+
})();
|
|
1087
|
+
}
|
|
1110
1088
|
async safeRun(input = {}, options = {}) {
|
|
1111
1089
|
try {
|
|
1112
1090
|
return {
|
|
@@ -1147,110 +1125,7 @@ var Container = class {
|
|
|
1147
1125
|
* already-aborted run.
|
|
1148
1126
|
*/
|
|
1149
1127
|
runSync(data = {}, options = {}) {
|
|
1150
|
-
|
|
1151
|
-
const output = {};
|
|
1152
|
-
const issues = [];
|
|
1153
|
-
let itemCount = 0;
|
|
1154
|
-
let errorCount = 0;
|
|
1155
|
-
for (let i = 0; i < this.items.length; i++) {
|
|
1156
|
-
options.signal?.throwIfAborted();
|
|
1157
|
-
const item = this.items[i];
|
|
1158
|
-
if (!this.isItemGroupIncluded(item, options.group)) continue;
|
|
1159
|
-
let pathCount = 0;
|
|
1160
|
-
let pathFailed = false;
|
|
1161
|
-
const branchStart = issues.length;
|
|
1162
|
-
const keys = item.path ? expandPath(data, item.path) : [""];
|
|
1163
|
-
for (const key of keys) {
|
|
1164
|
-
const keyParts = key ? pathToArray(key) : [];
|
|
1165
|
-
const pathRelative = keyParts.at(-1);
|
|
1166
|
-
const pathAbsolute = [...options.path ? options.path : [], ...keyParts];
|
|
1167
|
-
let value;
|
|
1168
|
-
if (key.length > 0) value = hasOwnProperty(output, key) ? output[key] : getPathValue(data, key);
|
|
1169
|
-
else value = data;
|
|
1170
|
-
const filter = resolvePathFilter(pathsToInclude, pathsToExclude, key, item.type === "container");
|
|
1171
|
-
if (filter.skip) continue;
|
|
1172
|
-
try {
|
|
1173
|
-
const resolvedOptionalValue = item.options.optionalValue ?? options.optionalValue ?? this.options.optionalValue;
|
|
1174
|
-
if (typeof item.options.optional === "function" ? item.options.optional(value) : item.options.optional && isOptionalValue(value, resolvedOptionalValue)) {
|
|
1175
|
-
if (hasOwnProperty(item.options, "optionalAs")) output[key] = item.options.optionalAs;
|
|
1176
|
-
else if (hasOwnProperty(options, "optionalAs")) output[key] = options.optionalAs;
|
|
1177
|
-
else if (hasOwnProperty(this.options, "optionalAs")) output[key] = this.options.optionalAs;
|
|
1178
|
-
else if (item.options.optionalInclude) output[key] = value;
|
|
1179
|
-
} else if (item.type === "container") {
|
|
1180
|
-
const childRunSync = item.data.runSync;
|
|
1181
|
-
if (typeof childRunSync !== "function") throw new RunSyncViolationError(`runSync: nested container at "${key || "<root>"}" does not implement runSync`);
|
|
1182
|
-
const tmp = childRunSync.call(item.data, isObject(value) ? value : {}, {
|
|
1183
|
-
group: options.group,
|
|
1184
|
-
flat: true,
|
|
1185
|
-
path: pathAbsolute,
|
|
1186
|
-
pathsToInclude: filter.pathsToInclude,
|
|
1187
|
-
pathsToExclude: filter.pathsToExclude,
|
|
1188
|
-
defaults: resolveDefaults(options.defaults, key),
|
|
1189
|
-
context: options.context,
|
|
1190
|
-
signal: options.signal,
|
|
1191
|
-
cache: options.cache,
|
|
1192
|
-
optionalValue: options.optionalValue,
|
|
1193
|
-
...hasOwnProperty(options, "optionalAs") ? { optionalAs: options.optionalAs } : {}
|
|
1194
|
-
});
|
|
1195
|
-
const tmpKeys = Object.keys(tmp);
|
|
1196
|
-
for (const tmpKey of tmpKeys) output[this.mergePaths(key, tmpKey)] = tmp[tmpKey];
|
|
1197
|
-
} else if (item.type === "validator") {
|
|
1198
|
-
const snapshot = {
|
|
1199
|
-
value,
|
|
1200
|
-
context: options.context,
|
|
1201
|
-
group: options.group
|
|
1202
|
-
};
|
|
1203
|
-
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
1204
|
-
if (cached) if (cached.ok) output[key] = cached.value;
|
|
1205
|
-
else throw cached.error;
|
|
1206
|
-
else try {
|
|
1207
|
-
const result = item.data({
|
|
1208
|
-
key,
|
|
1209
|
-
path: pathAbsolute,
|
|
1210
|
-
value,
|
|
1211
|
-
data,
|
|
1212
|
-
group: options.group,
|
|
1213
|
-
context: options.context,
|
|
1214
|
-
signal: options.signal,
|
|
1215
|
-
cache: options.cache
|
|
1216
|
-
});
|
|
1217
|
-
if (result !== null && typeof result === "object" && typeof result.then === "function") throw new RunSyncViolationError(`runSync: validator at "${key || "<root>"}" returned a Promise`);
|
|
1218
|
-
output[key] = result;
|
|
1219
|
-
this.writeCachedOutcome(item, key, snapshot, {
|
|
1220
|
-
ok: true,
|
|
1221
|
-
value: result
|
|
1222
|
-
}, options.cache, options.signal);
|
|
1223
|
-
} catch (e) {
|
|
1224
|
-
if (!isRunSyncViolation(e)) this.writeCachedOutcome(item, key, snapshot, {
|
|
1225
|
-
ok: false,
|
|
1226
|
-
error: e
|
|
1227
|
-
}, options.cache, options.signal);
|
|
1228
|
-
throw e;
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
} catch (e) {
|
|
1232
|
-
this.collectExecutionFailure({
|
|
1233
|
-
error: e,
|
|
1234
|
-
item,
|
|
1235
|
-
value,
|
|
1236
|
-
keyParts,
|
|
1237
|
-
pathRelative,
|
|
1238
|
-
issues,
|
|
1239
|
-
signal: options.signal
|
|
1240
|
-
});
|
|
1241
|
-
pathFailed = true;
|
|
1242
|
-
}
|
|
1243
|
-
pathCount++;
|
|
1244
|
-
}
|
|
1245
|
-
if (pathCount > 0) {
|
|
1246
|
-
itemCount++;
|
|
1247
|
-
if (pathFailed) {
|
|
1248
|
-
errorCount++;
|
|
1249
|
-
this.wrapBranchForOneOf(issues, branchStart, item, i);
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
}
|
|
1253
|
-
return this.finalizeOutput(output, options, issues, errorCount, itemCount);
|
|
1128
|
+
return runTwinSync(this.runBody(data, options));
|
|
1254
1129
|
}
|
|
1255
1130
|
safeRunSync(input = {}, options = {}) {
|
|
1256
1131
|
try {
|
|
@@ -1263,6 +1138,142 @@ var Container = class {
|
|
|
1263
1138
|
}
|
|
1264
1139
|
}
|
|
1265
1140
|
/**
|
|
1141
|
+
* Resolve everything a single `(mount, expanded key)` pair needs before
|
|
1142
|
+
* dispatch: the split path parts, the absolute path handed to validators
|
|
1143
|
+
* and child containers, and the include/exclude verdict.
|
|
1144
|
+
*
|
|
1145
|
+
* Shared verbatim by the twin body and `runParallel`. The mount's input
|
|
1146
|
+
* `value` is deliberately NOT read here — see {@link MountKeyPlan}.
|
|
1147
|
+
*/
|
|
1148
|
+
prepareMountKey(item, key, options, pathsToInclude, pathsToExclude) {
|
|
1149
|
+
const keyParts = key ? pathToArray(key) : [];
|
|
1150
|
+
return {
|
|
1151
|
+
keyParts,
|
|
1152
|
+
pathRelative: keyParts.at(-1),
|
|
1153
|
+
pathAbsolute: [...options.path ? options.path : [], ...keyParts],
|
|
1154
|
+
filter: resolvePathFilter(pathsToInclude, pathsToExclude, key, item.type === "container")
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Resolve the optional gate and — when it fires — what (if anything) the
|
|
1159
|
+
* skipped key contributes to the output.
|
|
1160
|
+
*
|
|
1161
|
+
* `optional` is the gate ("may this mount be skipped?"); `optionalValue` is
|
|
1162
|
+
* the definition of absent, resolved mount → run → container. A predicate
|
|
1163
|
+
* `optional` wins over the atom vocabulary entirely.
|
|
1164
|
+
*
|
|
1165
|
+
* The write directive follows the same three layers, but keys off
|
|
1166
|
+
* `optionalAs` **presence** rather than its value — `{ optionalAs:
|
|
1167
|
+
* undefined }` at any layer is a deliberate "emit `undefined`". Only when
|
|
1168
|
+
* no layer declares `optionalAs` does the mount-level `optionalInclude`
|
|
1169
|
+
* fallback (copy the input through) apply.
|
|
1170
|
+
*/
|
|
1171
|
+
resolveOptionalDirective(item, value, options) {
|
|
1172
|
+
const resolvedOptionalValue = item.options.optionalValue ?? options.optionalValue ?? this.options.optionalValue;
|
|
1173
|
+
if (!(typeof item.options.optional === "function" ? item.options.optional(value) : item.options.optional && isOptionalValue(value, resolvedOptionalValue))) return {
|
|
1174
|
+
skip: false,
|
|
1175
|
+
write: false
|
|
1176
|
+
};
|
|
1177
|
+
let write = true;
|
|
1178
|
+
let writeValue;
|
|
1179
|
+
if (hasOwnProperty(item.options, "optionalAs")) writeValue = item.options.optionalAs;
|
|
1180
|
+
else if (hasOwnProperty(options, "optionalAs")) writeValue = options.optionalAs;
|
|
1181
|
+
else if (hasOwnProperty(this.options, "optionalAs")) writeValue = this.options.optionalAs;
|
|
1182
|
+
else if (item.options.optionalInclude) writeValue = value;
|
|
1183
|
+
else write = false;
|
|
1184
|
+
return {
|
|
1185
|
+
skip: true,
|
|
1186
|
+
write,
|
|
1187
|
+
value: writeValue
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Re-resolve a mount's `optional` declaration for the `meta.optional`
|
|
1192
|
+
* stamp applied by {@link Container.collectExecutionFailure}.
|
|
1193
|
+
*
|
|
1194
|
+
* Split out from `resolveOptionalDirective` because the two calls answer
|
|
1195
|
+
* different questions at different times: the directive decides whether to
|
|
1196
|
+
* *run* the mount, this decides whether to *tag* the mount's issues. The
|
|
1197
|
+
* split is what lets this one contain a throw the gate cannot.
|
|
1198
|
+
*
|
|
1199
|
+
* A predicate that throws degrades to `false` (don't tag) rather than
|
|
1200
|
+
* propagating. This is load-bearing, not defensive: the caller is a
|
|
1201
|
+
* `catch` handler with no `try` above it inside the run loop, and the very
|
|
1202
|
+
* throw it is folding may be the predicate's own. Re-raising here escapes
|
|
1203
|
+
* the error path entirely, discarding **every issue collected so far** and
|
|
1204
|
+
* replacing them with one path-less item — so a mainstream idiom like
|
|
1205
|
+
* `optional: (v) => v.trim().length === 0` would, on the first absent
|
|
1206
|
+
* field, wipe out every other field's validation error.
|
|
1207
|
+
*
|
|
1208
|
+
* `false` is the conservative fallback: the mount's issues surface at full
|
|
1209
|
+
* severity instead of being downgraded to a warning by consumers such as
|
|
1210
|
+
* `@validup/vue`'s `getSeverity`. The predicate's failure is not swallowed
|
|
1211
|
+
* — it is already being folded into an issue by the caller.
|
|
1212
|
+
*/
|
|
1213
|
+
resolveOptionalStamp(item, value) {
|
|
1214
|
+
if (typeof item.options.optional === "function") try {
|
|
1215
|
+
return Boolean(item.options.optional(value));
|
|
1216
|
+
} catch {
|
|
1217
|
+
return false;
|
|
1218
|
+
}
|
|
1219
|
+
return item.options.optional === true;
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Attribution fallback for a throw raised before a {@link MountKeyPlan}
|
|
1223
|
+
* exists — path expansion, key preparation, or the mount's value read.
|
|
1224
|
+
*
|
|
1225
|
+
* Produces the same `keyParts` / `pathRelative` pair `prepareMountKey`
|
|
1226
|
+
* would have, so a pre-dispatch failure is attributed to the mount that
|
|
1227
|
+
* caused it rather than surfacing path-less. For an item-level failure
|
|
1228
|
+
* (path expansion) the caller passes the *unexpanded* mount path, so a
|
|
1229
|
+
* glob mount is identified by its literal pattern (`items.*.name`) — the
|
|
1230
|
+
* keys it would have expanded to are precisely what could not be computed.
|
|
1231
|
+
*/
|
|
1232
|
+
describeKey(key) {
|
|
1233
|
+
const keyParts = key ? pathToArray(key) : [];
|
|
1234
|
+
return {
|
|
1235
|
+
keyParts,
|
|
1236
|
+
pathRelative: keyParts.at(-1)
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Build the option bag forwarded into a nested container's `run` /
|
|
1241
|
+
* `runSync`. Single source of truth for what a child inherits, so a new
|
|
1242
|
+
* run option is threaded downward in one place instead of three.
|
|
1243
|
+
*/
|
|
1244
|
+
buildChildRunOptions(options, key, plan, pathsStrict, parallel = false) {
|
|
1245
|
+
return {
|
|
1246
|
+
group: options.group,
|
|
1247
|
+
flat: true,
|
|
1248
|
+
path: plan.pathAbsolute,
|
|
1249
|
+
pathsToInclude: plan.filter.pathsToInclude,
|
|
1250
|
+
pathsToExclude: plan.filter.pathsToExclude,
|
|
1251
|
+
...pathsStrict && key.length > 0 ? { pathsStrict: true } : {},
|
|
1252
|
+
defaults: resolveDefaults(options.defaults, key),
|
|
1253
|
+
context: options.context,
|
|
1254
|
+
signal: options.signal,
|
|
1255
|
+
cache: options.cache,
|
|
1256
|
+
...parallel ? { parallel: true } : {},
|
|
1257
|
+
optionalValue: options.optionalValue,
|
|
1258
|
+
...hasOwnProperty(options, "optionalAs") ? { optionalAs: options.optionalAs } : {}
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
1262
|
+
* Build the context object handed to a mounted validator.
|
|
1263
|
+
*/
|
|
1264
|
+
buildValidatorContext(key, plan, value, data, options) {
|
|
1265
|
+
return {
|
|
1266
|
+
key,
|
|
1267
|
+
path: plan.pathAbsolute,
|
|
1268
|
+
value,
|
|
1269
|
+
data,
|
|
1270
|
+
group: options.group,
|
|
1271
|
+
context: options.context,
|
|
1272
|
+
signal: options.signal,
|
|
1273
|
+
cache: options.cache
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1266
1277
|
* Lookup helper for the per-mount result cache.
|
|
1267
1278
|
*
|
|
1268
1279
|
* Returns `undefined` (forcing a real run) for any of:
|
|
@@ -1316,21 +1327,73 @@ var Container = class {
|
|
|
1316
1327
|
};
|
|
1317
1328
|
}
|
|
1318
1329
|
/**
|
|
1319
|
-
*
|
|
1320
|
-
*
|
|
1321
|
-
*
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1330
|
+
* Resolve the effective `pathsStrict` flag. Run-level wins over
|
|
1331
|
+
* container-level; unset anywhere is `false` (the silent, back-compatible
|
|
1332
|
+
* default).
|
|
1333
|
+
*/
|
|
1334
|
+
resolvePathsStrict(options) {
|
|
1335
|
+
return options.pathsStrict ?? this.options.pathsStrict ?? false;
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* Structural pre-flight for `pathsStrict`. Verifies every resolved
|
|
1339
|
+
* `pathsToInclude` / `pathsToExclude` entry is satisfied by *this*
|
|
1340
|
+
* container — either an exact match against an expanded mount key, or a
|
|
1341
|
+
* prefix descent into a container mount (whose child re-checks the
|
|
1342
|
+
* forwarded remainder, since `pathsStrict` threads downward). Anything
|
|
1343
|
+
* unmatched throws {@link PathsStrictViolationError} with absolute paths.
|
|
1344
|
+
*
|
|
1345
|
+
* No-ops unless strict is on AND at least one filter list is present, so
|
|
1346
|
+
* the overhead on the common (non-strict) path is a single `??` chain.
|
|
1347
|
+
*
|
|
1348
|
+
* Notes on the match rules (kept in lockstep with `resolvePathFilter`):
|
|
1349
|
+
* - Keyless container mounts share the parent namespace and receive the
|
|
1350
|
+
* filter list verbatim, so a path could be owned by the keyless child
|
|
1351
|
+
* OR by a keyed sibling here — the parent can't tell without recursing.
|
|
1352
|
+
* To avoid false positives it treats every entry as satisfied when any
|
|
1353
|
+
* keyless container is present, and `pathsStrict` is NOT forwarded into
|
|
1354
|
+
* keyless children (that would throw for parent-sibling paths). Net:
|
|
1355
|
+
* keyless subtrees are a strict blind spot ("out of scope" per the issue).
|
|
1356
|
+
* - Group filtering is intentionally ignored: a mount excluded from the
|
|
1357
|
+
* active group still exists, so a path targeting it is not "stale".
|
|
1358
|
+
* - Expansion uses the same `expandPath(data, item.path)` the run loop
|
|
1359
|
+
* uses, so glob mounts are matched against the keys they actually
|
|
1360
|
+
* expand to for the given `data`.
|
|
1326
1361
|
*/
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1362
|
+
assertPathsStrict(strict, data, pathsToInclude, pathsToExclude, parentPath) {
|
|
1363
|
+
if (!strict) return;
|
|
1364
|
+
const hasInclude = typeof pathsToInclude !== "undefined" && pathsToInclude.length > 0;
|
|
1365
|
+
const hasExclude = typeof pathsToExclude !== "undefined" && pathsToExclude.length > 0;
|
|
1366
|
+
if (!hasInclude && !hasExclude) return;
|
|
1367
|
+
const expanded = [];
|
|
1368
|
+
let hasKeylessContainer = false;
|
|
1369
|
+
for (const item of this.items) {
|
|
1370
|
+
const keys = item.path ? expandPath(data, item.path) : [""];
|
|
1371
|
+
const itemIsContainer = item.type === "container";
|
|
1372
|
+
if (itemIsContainer && keys.some((key) => key.length === 0)) hasKeylessContainer = true;
|
|
1373
|
+
expanded.push({
|
|
1374
|
+
keys,
|
|
1375
|
+
isContainer: itemIsContainer
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
const isSatisfied = (path) => {
|
|
1379
|
+
if (hasKeylessContainer) return true;
|
|
1380
|
+
for (const entry of expanded) for (const key of entry.keys) {
|
|
1381
|
+
if (key.length === 0) continue;
|
|
1382
|
+
if (path === key) return true;
|
|
1383
|
+
if (entry.isContainer && path.startsWith(`${key}.`)) return true;
|
|
1384
|
+
}
|
|
1385
|
+
return false;
|
|
1331
1386
|
};
|
|
1332
|
-
|
|
1333
|
-
|
|
1387
|
+
const toAbsolute = (path) => {
|
|
1388
|
+
if (!parentPath || parentPath.length === 0) return path;
|
|
1389
|
+
return stringifyPath([...parentPath, ...pathToArray(path)]);
|
|
1390
|
+
};
|
|
1391
|
+
const unmatchedInclude = hasInclude ? pathsToInclude.filter((path) => !isSatisfied(path)).map(toAbsolute) : [];
|
|
1392
|
+
const unmatchedExclude = hasExclude ? pathsToExclude.filter((path) => !isSatisfied(path)).map(toAbsolute) : [];
|
|
1393
|
+
if (unmatchedInclude.length > 0 || unmatchedExclude.length > 0) throw new PathsStrictViolationError({
|
|
1394
|
+
pathsToInclude: unmatchedInclude,
|
|
1395
|
+
pathsToExclude: unmatchedExclude
|
|
1396
|
+
});
|
|
1334
1397
|
}
|
|
1335
1398
|
/**
|
|
1336
1399
|
* Translate a throw raised by one execution step (one validator or
|
|
@@ -1356,9 +1419,8 @@ var Container = class {
|
|
|
1356
1419
|
*/
|
|
1357
1420
|
collectExecutionFailure(context) {
|
|
1358
1421
|
const { error, item, value, keyParts, pathRelative, issues, signal } = context;
|
|
1359
|
-
if (signal
|
|
1360
|
-
|
|
1361
|
-
const isOptionalMount = typeof item.options.optional === "function" ? Boolean(item.options.optional(value)) : item.options.optional === true;
|
|
1422
|
+
if (isStructuralThrow(error, signal)) throw error;
|
|
1423
|
+
const isOptionalMount = this.resolveOptionalStamp(item, value);
|
|
1362
1424
|
const markOptional = (issue) => {
|
|
1363
1425
|
if (!isOptionalMount) return issue;
|
|
1364
1426
|
issue.meta = {
|
|
@@ -1375,7 +1437,7 @@ var Container = class {
|
|
|
1375
1437
|
};
|
|
1376
1438
|
const childIssues = [];
|
|
1377
1439
|
if (isValidupError(error)) for (let i = 0; i < error.issues.length; i++) {
|
|
1378
|
-
const prefixed =
|
|
1440
|
+
const prefixed = prefixIssuePath(error.issues[i], keyParts);
|
|
1379
1441
|
childIssues.push(item.type === "validator" ? markOptionalDeep(prefixed) : prefixed);
|
|
1380
1442
|
}
|
|
1381
1443
|
else if (isError(error)) childIssues.push(markOptional(defineIssueItem({
|
|
@@ -1386,14 +1448,15 @@ var Container = class {
|
|
|
1386
1448
|
path: keyParts,
|
|
1387
1449
|
message: typeof error === "string" && error.length > 0 ? error : `Non-Error throw: ${String(error)}`
|
|
1388
1450
|
})));
|
|
1389
|
-
if (pathRelative)
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1451
|
+
if (pathRelative) {
|
|
1452
|
+
if (item.type === "container" || childIssues.length > 1) issues.push(markOptional(defineIssueGroup({
|
|
1453
|
+
message: buildErrorMessageForAttribute(String(pathRelative)),
|
|
1454
|
+
data: { name: String(pathRelative) },
|
|
1455
|
+
path: keyParts,
|
|
1456
|
+
issues: childIssues
|
|
1457
|
+
})));
|
|
1458
|
+
else issues.push(...childIssues);
|
|
1459
|
+
} else issues.push(...childIssues);
|
|
1397
1460
|
}
|
|
1398
1461
|
/**
|
|
1399
1462
|
* For `oneOf` containers, wrap the issues produced by a single branch
|
|
@@ -1439,25 +1502,14 @@ var Container = class {
|
|
|
1439
1502
|
return temp;
|
|
1440
1503
|
}
|
|
1441
1504
|
wrapSafeRunError(e, options) {
|
|
1442
|
-
if (options.signal
|
|
1443
|
-
if (isRunSyncViolation(e)) throw e;
|
|
1505
|
+
if (isStructuralThrow(e, options.signal)) throw e;
|
|
1444
1506
|
if (isValidupError(e)) return {
|
|
1445
1507
|
success: false,
|
|
1446
1508
|
error: e
|
|
1447
1509
|
};
|
|
1448
|
-
if (isError(e)) return {
|
|
1449
|
-
success: false,
|
|
1450
|
-
error: new ValidupError([defineIssueItem({
|
|
1451
|
-
path: [],
|
|
1452
|
-
message: e.message
|
|
1453
|
-
})])
|
|
1454
|
-
};
|
|
1455
1510
|
return {
|
|
1456
1511
|
success: false,
|
|
1457
|
-
error: new ValidupError(
|
|
1458
|
-
path: [],
|
|
1459
|
-
message: typeof e === "string" && e.length > 0 ? e : `Non-Error throw: ${String(e)}`
|
|
1460
|
-
})])
|
|
1512
|
+
error: new ValidupError(errorToIssues(e))
|
|
1461
1513
|
};
|
|
1462
1514
|
}
|
|
1463
1515
|
isItemGroupIncluded(item, group) {
|
|
@@ -1551,6 +1603,12 @@ var Builder = class Builder {
|
|
|
1551
1603
|
pathsToExclude: [...this.options.pathsToExclude ?? [], ...paths]
|
|
1552
1604
|
}, this.steps);
|
|
1553
1605
|
}
|
|
1606
|
+
pathsStrict(strict = true) {
|
|
1607
|
+
return new Builder({
|
|
1608
|
+
...this.options,
|
|
1609
|
+
pathsStrict: strict
|
|
1610
|
+
}, this.steps);
|
|
1611
|
+
}
|
|
1554
1612
|
build() {
|
|
1555
1613
|
const container = new Container(this.options);
|
|
1556
1614
|
for (const step of this.steps) if (step.kind === "nest") container.mount(step.key, step.options, step.child);
|
|
@@ -1631,6 +1689,6 @@ var ResultCache = class {
|
|
|
1631
1689
|
}
|
|
1632
1690
|
};
|
|
1633
1691
|
//#endregion
|
|
1634
|
-
export { Builder, Container, GroupKey,
|
|
1692
|
+
export { Builder, Container, GroupKey, OptionalValue, PathsStrictViolationError, ResultCache, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineSchema, defineValidator, errorToIssues, hasOwnProperty, isContainer, isError, isObject, isOptionalValue, isPathsStrictViolation, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
|
|
1635
1693
|
|
|
1636
1694
|
//# sourceMappingURL=index.mjs.map
|