validup 0.5.1 → 1.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 +18 -0
- package/dist/index.d.mts +282 -27
- package/dist/index.mjs +519 -265
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BaseError, interpolate, interpolate as interpolate$1 } 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,6 +17,123 @@ function isContainer(input) {
|
|
|
16
17
|
return isObject(input) && typeof input.run === "function" && typeof input.safeRun === "function";
|
|
17
18
|
}
|
|
18
19
|
//#endregion
|
|
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;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
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";
|
|
64
|
+
}
|
|
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";
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/container/structural-throw.ts
|
|
90
|
+
/**
|
|
91
|
+
* Should this thrown value escape the issue-folding path verbatim?
|
|
92
|
+
*
|
|
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.
|
|
96
|
+
*
|
|
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`.
|
|
111
|
+
*
|
|
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.
|
|
115
|
+
*
|
|
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.
|
|
122
|
+
*
|
|
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.
|
|
127
|
+
*
|
|
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.
|
|
132
|
+
*/
|
|
133
|
+
function isStructuralThrow(error, signal) {
|
|
134
|
+
return Boolean(signal?.aborted) || isRunSyncViolation(error) || isPathsStrictViolation(error);
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
19
137
|
//#region src/error/base.ts
|
|
20
138
|
var ValidupError = class extends BaseError {
|
|
21
139
|
issues;
|
|
@@ -353,7 +471,7 @@ function compose(elements, options = {}) {
|
|
|
353
471
|
});
|
|
354
472
|
if (typeof result !== "undefined") value = result;
|
|
355
473
|
} catch (e) {
|
|
356
|
-
if (bail) throw e;
|
|
474
|
+
if (bail || isStructuralThrow(e, ctx.signal)) throw e;
|
|
357
475
|
for (const issue of errorToIssues(e)) issues.push(issue);
|
|
358
476
|
}
|
|
359
477
|
if (issues.length > 0) throw new ValidupError(issues);
|
|
@@ -390,7 +508,7 @@ function composeAnyOf(elements) {
|
|
|
390
508
|
const result = await invokeComposeElement(element, { ...ctx });
|
|
391
509
|
return typeof result === "undefined" ? ctx.value : result;
|
|
392
510
|
} catch (e) {
|
|
393
|
-
if (ctx.signal
|
|
511
|
+
if (isStructuralThrow(e, ctx.signal)) throw e;
|
|
394
512
|
const wrapped = wrapBranchIssues(e, index);
|
|
395
513
|
for (const issue of wrapped) branchIssues.push(issue);
|
|
396
514
|
}
|
|
@@ -725,30 +843,15 @@ function defineValidator(descriptor) {
|
|
|
725
843
|
return descriptor;
|
|
726
844
|
}
|
|
727
845
|
//#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
|
-
};
|
|
846
|
+
//#region src/container/module.ts
|
|
741
847
|
/**
|
|
742
|
-
*
|
|
743
|
-
*
|
|
744
|
-
*
|
|
848
|
+
* Structural thenable probe. Used by the sync side of the validator effect to
|
|
849
|
+
* reject the one thing a synchronous graph cannot tolerate: a validator that
|
|
850
|
+
* returned a promise.
|
|
745
851
|
*/
|
|
746
|
-
function
|
|
747
|
-
|
|
748
|
-
return typeof e === "object" && e !== null && e.name === "RunSyncViolationError";
|
|
852
|
+
function isThenable(input) {
|
|
853
|
+
return isObject(input) && typeof input.then === "function";
|
|
749
854
|
}
|
|
750
|
-
//#endregion
|
|
751
|
-
//#region src/container/module.ts
|
|
752
855
|
var Container = class {
|
|
753
856
|
options;
|
|
754
857
|
items;
|
|
@@ -860,7 +963,36 @@ var Container = class {
|
|
|
860
963
|
*/
|
|
861
964
|
async run(data = {}, options = {}) {
|
|
862
965
|
if (options.parallel) return this.runParallel(data, options);
|
|
966
|
+
return runTwinAsync(this.runBody(data, options));
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* The single mount-resolution loop behind {@link Container.run} and
|
|
970
|
+
* {@link Container.runSync}.
|
|
971
|
+
*
|
|
972
|
+
* Written as a **twin body** (the `twinop` package): the two impure edges,
|
|
973
|
+
* invoking a validator and descending into a nested container, are yielded
|
|
974
|
+
* as `op(asyncThunk, syncThunk)` pairs, and the driver chosen by the public
|
|
975
|
+
* method (`runTwinAsync` / `runTwinSync`) executes the matching side. Effect
|
|
976
|
+
* errors are thrown back in at the `yield` site, so the cache-write and
|
|
977
|
+
* issue-collection `try`/`catch` blocks below run identically in both
|
|
978
|
+
* variants, with no second copy of the loop to keep in lockstep.
|
|
979
|
+
*
|
|
980
|
+
* The sync thunks own the two structural probes that only `runSync` needs:
|
|
981
|
+
* a nested container that doesn't implement `runSync`, and a validator that
|
|
982
|
+
* returned a thenable. Both throw `RunSyncViolationError`, which the async
|
|
983
|
+
* side cannot produce.
|
|
984
|
+
*
|
|
985
|
+
* `runParallel` deliberately does NOT share this body — a generator is
|
|
986
|
+
* sequential by construction, and expressing "launch every mount, then
|
|
987
|
+
* settle" through it would mean yielding batches and giving up the
|
|
988
|
+
* chain-read. It shares the extracted per-key helpers instead
|
|
989
|
+
* (`prepareMountKey` / `resolveOptionalDirective` / `buildChildRunOptions` /
|
|
990
|
+
* `buildValidatorContext`).
|
|
991
|
+
*/
|
|
992
|
+
*runBody(data, options) {
|
|
863
993
|
const { pathsToInclude, pathsToExclude } = this.resolveContainerFilters(options);
|
|
994
|
+
const pathsStrict = this.resolvePathsStrict(options);
|
|
995
|
+
this.assertPathsStrict(pathsStrict, data, pathsToInclude, pathsToExclude, options.path);
|
|
864
996
|
const output = {};
|
|
865
997
|
const issues = [];
|
|
866
998
|
let itemCount = 0;
|
|
@@ -872,36 +1004,42 @@ var Container = class {
|
|
|
872
1004
|
let pathCount = 0;
|
|
873
1005
|
let pathFailed = false;
|
|
874
1006
|
const branchStart = issues.length;
|
|
875
|
-
|
|
1007
|
+
let keys = [];
|
|
1008
|
+
try {
|
|
1009
|
+
keys = item.path ? expandPath(data, item.path) : [""];
|
|
1010
|
+
} catch (e) {
|
|
1011
|
+
const attribution = this.describeKey(item.path || "");
|
|
1012
|
+
this.collectExecutionFailure({
|
|
1013
|
+
error: e,
|
|
1014
|
+
item,
|
|
1015
|
+
value: void 0,
|
|
1016
|
+
keyParts: attribution.keyParts,
|
|
1017
|
+
pathRelative: attribution.pathRelative,
|
|
1018
|
+
issues,
|
|
1019
|
+
signal: options.signal
|
|
1020
|
+
});
|
|
1021
|
+
pathCount = 1;
|
|
1022
|
+
pathFailed = true;
|
|
1023
|
+
}
|
|
876
1024
|
for (const key of keys) {
|
|
877
|
-
|
|
878
|
-
const pathRelative = keyParts.at(-1);
|
|
879
|
-
const pathAbsolute = [...options.path ? options.path : [], ...keyParts];
|
|
1025
|
+
let plan;
|
|
880
1026
|
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
1027
|
try {
|
|
886
|
-
|
|
887
|
-
if (
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
1028
|
+
plan = this.prepareMountKey(item, key, options, pathsToInclude, pathsToExclude);
|
|
1029
|
+
if (key.length > 0) value = hasOwnProperty(output, key) ? output[key] : getPathValue(data, key);
|
|
1030
|
+
else value = data;
|
|
1031
|
+
if (plan.filter.skip) continue;
|
|
1032
|
+
const optional = this.resolveOptionalDirective(item, value, options);
|
|
1033
|
+
if (optional.skip) {
|
|
1034
|
+
if (optional.write) output[key] = optional.value;
|
|
892
1035
|
} 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 } : {}
|
|
1036
|
+
const child = item.data;
|
|
1037
|
+
const childInput = isObject(value) ? value : {};
|
|
1038
|
+
const childOptions = this.buildChildRunOptions(options, key, plan, pathsStrict);
|
|
1039
|
+
const tmp = yield* op(() => child.run(childInput, childOptions), () => {
|
|
1040
|
+
const childRunSync = child.runSync;
|
|
1041
|
+
if (typeof childRunSync !== "function") throw new RunSyncViolationError(`runSync: nested container at "${key || "<root>"}" does not implement runSync`);
|
|
1042
|
+
return childRunSync.call(child, childInput, childOptions);
|
|
905
1043
|
});
|
|
906
1044
|
const tmpKeys = Object.keys(tmp);
|
|
907
1045
|
for (const tmpKey of tmpKeys) output[this.mergePaths(key, tmpKey)] = tmp[tmpKey];
|
|
@@ -914,37 +1052,37 @@ var Container = class {
|
|
|
914
1052
|
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
915
1053
|
if (cached) if (cached.ok) output[key] = cached.value;
|
|
916
1054
|
else throw cached.error;
|
|
917
|
-
else
|
|
918
|
-
const
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
}
|
|
938
|
-
throw e;
|
|
1055
|
+
else {
|
|
1056
|
+
const validator = item.data;
|
|
1057
|
+
const ctx = this.buildValidatorContext(key, plan, value, data, options);
|
|
1058
|
+
try {
|
|
1059
|
+
const result = yield* op(() => validator(ctx), () => {
|
|
1060
|
+
const outcome = validator(ctx);
|
|
1061
|
+
if (isThenable(outcome)) throw new RunSyncViolationError(`runSync: validator at "${key || "<root>"}" returned a Promise`);
|
|
1062
|
+
return outcome;
|
|
1063
|
+
});
|
|
1064
|
+
output[key] = result;
|
|
1065
|
+
this.writeCachedOutcome(item, key, snapshot, {
|
|
1066
|
+
ok: true,
|
|
1067
|
+
value: result
|
|
1068
|
+
}, options.cache, options.signal);
|
|
1069
|
+
} catch (e) {
|
|
1070
|
+
if (!isStructuralThrow(e, options.signal)) this.writeCachedOutcome(item, key, snapshot, {
|
|
1071
|
+
ok: false,
|
|
1072
|
+
error: e
|
|
1073
|
+
}, options.cache, options.signal);
|
|
1074
|
+
throw e;
|
|
1075
|
+
}
|
|
939
1076
|
}
|
|
940
1077
|
}
|
|
941
1078
|
} catch (e) {
|
|
1079
|
+
const attribution = plan || this.describeKey(key);
|
|
942
1080
|
this.collectExecutionFailure({
|
|
943
1081
|
error: e,
|
|
944
1082
|
item,
|
|
945
1083
|
value,
|
|
946
|
-
keyParts,
|
|
947
|
-
pathRelative,
|
|
1084
|
+
keyParts: attribution.keyParts,
|
|
1085
|
+
pathRelative: attribution.pathRelative,
|
|
948
1086
|
issues,
|
|
949
1087
|
signal: options.signal
|
|
950
1088
|
});
|
|
@@ -966,9 +1104,17 @@ var Container = class {
|
|
|
966
1104
|
* Parallel-execution variant of `run()`. All mounts kick off their
|
|
967
1105
|
* promises eagerly and the results are merged after `Promise.allSettled`.
|
|
968
1106
|
* See `ContainerRunOptions.parallel` for the trade-off note.
|
|
1107
|
+
*
|
|
1108
|
+
* Shares every per-key helper with the sequential twin body
|
|
1109
|
+
* (`prepareMountKey` / `resolveOptionalDirective` / `buildChildRunOptions` /
|
|
1110
|
+
* `buildValidatorContext` / the cache pair) but keeps its own scheduling
|
|
1111
|
+
* loop — see the note on {@link Container.runBody} for why it can't be a
|
|
1112
|
+
* third twin driver.
|
|
969
1113
|
*/
|
|
970
1114
|
async runParallel(data, options) {
|
|
971
1115
|
const { pathsToInclude, pathsToExclude } = this.resolveContainerFilters(options);
|
|
1116
|
+
const pathsStrict = this.resolvePathsStrict(options);
|
|
1117
|
+
this.assertPathsStrict(pathsStrict, data, pathsToInclude, pathsToExclude, options.path);
|
|
972
1118
|
const output = {};
|
|
973
1119
|
const issues = [];
|
|
974
1120
|
const itemGroups = [];
|
|
@@ -978,84 +1124,44 @@ var Container = class {
|
|
|
978
1124
|
if (!this.isItemGroupIncluded(item, options.group)) continue;
|
|
979
1125
|
const tasks = [];
|
|
980
1126
|
let syncPathCount = 0;
|
|
981
|
-
|
|
1127
|
+
let keys = [];
|
|
1128
|
+
try {
|
|
1129
|
+
keys = item.path ? expandPath(data, item.path) : [""];
|
|
1130
|
+
} catch (e) {
|
|
1131
|
+
const attribution = this.describeKey(item.path || "");
|
|
1132
|
+
tasks.push({
|
|
1133
|
+
key: item.path || "",
|
|
1134
|
+
keyParts: attribution.keyParts,
|
|
1135
|
+
pathRelative: attribution.pathRelative,
|
|
1136
|
+
value: void 0,
|
|
1137
|
+
promise: this.ownRejection(Promise.reject(e)),
|
|
1138
|
+
kind: item.type
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
982
1141
|
for (const key of keys) {
|
|
983
|
-
|
|
984
|
-
const pathRelative = keyParts.at(-1);
|
|
985
|
-
const pathAbsolute = [...options.path ? options.path : [], ...keyParts];
|
|
1142
|
+
let plan;
|
|
986
1143
|
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
1144
|
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
|
-
})();
|
|
1145
|
+
try {
|
|
1146
|
+
plan = this.prepareMountKey(item, key, options, pathsToInclude, pathsToExclude);
|
|
1147
|
+
if (key.length > 0) value = getPathValue(data, key);
|
|
1148
|
+
else value = data;
|
|
1149
|
+
if (plan.filter.skip) continue;
|
|
1150
|
+
const optional = this.resolveOptionalDirective(item, value, options);
|
|
1151
|
+
if (optional.skip) {
|
|
1152
|
+
if (optional.write) output[key] = optional.value;
|
|
1153
|
+
syncPathCount++;
|
|
1154
|
+
continue;
|
|
1053
1155
|
}
|
|
1156
|
+
promise = this.ownRejection(this.dispatchParallelMount(item, key, plan, value, data, options, pathsStrict));
|
|
1157
|
+
} catch (e) {
|
|
1158
|
+
promise = this.ownRejection(Promise.reject(e));
|
|
1054
1159
|
}
|
|
1160
|
+
const attribution = plan || this.describeKey(key);
|
|
1055
1161
|
tasks.push({
|
|
1056
1162
|
key,
|
|
1057
|
-
keyParts,
|
|
1058
|
-
pathRelative,
|
|
1163
|
+
keyParts: attribution.keyParts,
|
|
1164
|
+
pathRelative: attribution.pathRelative,
|
|
1059
1165
|
value,
|
|
1060
1166
|
promise,
|
|
1061
1167
|
kind: item.type
|
|
@@ -1107,6 +1213,58 @@ var Container = class {
|
|
|
1107
1213
|
options.signal?.throwIfAborted();
|
|
1108
1214
|
return this.finalizeOutput(output, options, issues, errorCount, itemCount);
|
|
1109
1215
|
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Claim ownership of a scheduled mount's rejection.
|
|
1218
|
+
*
|
|
1219
|
+
* `runParallel` creates every mount's promise eagerly and only attaches
|
|
1220
|
+
* `Promise.allSettled` after the whole scheduling loop has run. Any
|
|
1221
|
+
* rejection settling in that window — or, worse, any throw that escapes
|
|
1222
|
+
* the loop before `allSettled` is reached at all — is unowned, and Node's
|
|
1223
|
+
* default `--unhandled-rejections=throw` (this package requires Node >=
|
|
1224
|
+
* 24) terminates the process.
|
|
1225
|
+
*
|
|
1226
|
+
* The no-op subscriber below is a *second* consumer, discarded
|
|
1227
|
+
* immediately; the original promise is returned unchanged, so
|
|
1228
|
+
* `Promise.allSettled` still observes the real settlement and no outcome
|
|
1229
|
+
* is swallowed.
|
|
1230
|
+
*/
|
|
1231
|
+
ownRejection(promise) {
|
|
1232
|
+
promise.catch(() => {});
|
|
1233
|
+
return promise;
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* Create the in-flight promise for one scheduled mount. Extracted from
|
|
1237
|
+
* `runParallel`'s scheduling loop so the loop body reads as
|
|
1238
|
+
* "resolve, gate, dispatch" and the pre-dispatch `try` stays legible.
|
|
1239
|
+
*/
|
|
1240
|
+
dispatchParallelMount(item, key, plan, value, data, options, pathsStrict) {
|
|
1241
|
+
if (item.type === "container") return item.data.run(isObject(value) ? value : {}, this.buildChildRunOptions(options, key, plan, pathsStrict, true));
|
|
1242
|
+
const snapshot = {
|
|
1243
|
+
value,
|
|
1244
|
+
context: options.context,
|
|
1245
|
+
group: options.group
|
|
1246
|
+
};
|
|
1247
|
+
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
1248
|
+
if (cached) return cached.ok ? Promise.resolve(cached.value) : Promise.reject(cached.error);
|
|
1249
|
+
const validator = item.data;
|
|
1250
|
+
const ctx = this.buildValidatorContext(key, plan, value, data, options);
|
|
1251
|
+
return (async () => {
|
|
1252
|
+
try {
|
|
1253
|
+
const result = await validator(ctx);
|
|
1254
|
+
this.writeCachedOutcome(item, key, snapshot, {
|
|
1255
|
+
ok: true,
|
|
1256
|
+
value: result
|
|
1257
|
+
}, options.cache, options.signal);
|
|
1258
|
+
return result;
|
|
1259
|
+
} catch (e) {
|
|
1260
|
+
if (!isStructuralThrow(e, options.signal)) this.writeCachedOutcome(item, key, snapshot, {
|
|
1261
|
+
ok: false,
|
|
1262
|
+
error: e
|
|
1263
|
+
}, options.cache, options.signal);
|
|
1264
|
+
throw e;
|
|
1265
|
+
}
|
|
1266
|
+
})();
|
|
1267
|
+
}
|
|
1110
1268
|
async safeRun(input = {}, options = {}) {
|
|
1111
1269
|
try {
|
|
1112
1270
|
return {
|
|
@@ -1147,110 +1305,7 @@ var Container = class {
|
|
|
1147
1305
|
* already-aborted run.
|
|
1148
1306
|
*/
|
|
1149
1307
|
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);
|
|
1308
|
+
return runTwinSync(this.runBody(data, options));
|
|
1254
1309
|
}
|
|
1255
1310
|
safeRunSync(input = {}, options = {}) {
|
|
1256
1311
|
try {
|
|
@@ -1263,6 +1318,142 @@ var Container = class {
|
|
|
1263
1318
|
}
|
|
1264
1319
|
}
|
|
1265
1320
|
/**
|
|
1321
|
+
* Resolve everything a single `(mount, expanded key)` pair needs before
|
|
1322
|
+
* dispatch: the split path parts, the absolute path handed to validators
|
|
1323
|
+
* and child containers, and the include/exclude verdict.
|
|
1324
|
+
*
|
|
1325
|
+
* Shared verbatim by the twin body and `runParallel`. The mount's input
|
|
1326
|
+
* `value` is deliberately NOT read here — see {@link MountKeyPlan}.
|
|
1327
|
+
*/
|
|
1328
|
+
prepareMountKey(item, key, options, pathsToInclude, pathsToExclude) {
|
|
1329
|
+
const keyParts = key ? pathToArray(key) : [];
|
|
1330
|
+
return {
|
|
1331
|
+
keyParts,
|
|
1332
|
+
pathRelative: keyParts.at(-1),
|
|
1333
|
+
pathAbsolute: [...options.path ? options.path : [], ...keyParts],
|
|
1334
|
+
filter: resolvePathFilter(pathsToInclude, pathsToExclude, key, item.type === "container")
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* Resolve the optional gate and — when it fires — what (if anything) the
|
|
1339
|
+
* skipped key contributes to the output.
|
|
1340
|
+
*
|
|
1341
|
+
* `optional` is the gate ("may this mount be skipped?"); `optionalValue` is
|
|
1342
|
+
* the definition of absent, resolved mount → run → container. A predicate
|
|
1343
|
+
* `optional` wins over the atom vocabulary entirely.
|
|
1344
|
+
*
|
|
1345
|
+
* The write directive follows the same three layers, but keys off
|
|
1346
|
+
* `optionalAs` **presence** rather than its value — `{ optionalAs:
|
|
1347
|
+
* undefined }` at any layer is a deliberate "emit `undefined`". Only when
|
|
1348
|
+
* no layer declares `optionalAs` does the mount-level `optionalInclude`
|
|
1349
|
+
* fallback (copy the input through) apply.
|
|
1350
|
+
*/
|
|
1351
|
+
resolveOptionalDirective(item, value, options) {
|
|
1352
|
+
const resolvedOptionalValue = item.options.optionalValue ?? options.optionalValue ?? this.options.optionalValue;
|
|
1353
|
+
if (!(typeof item.options.optional === "function" ? item.options.optional(value) : item.options.optional && isOptionalValue(value, resolvedOptionalValue))) return {
|
|
1354
|
+
skip: false,
|
|
1355
|
+
write: false
|
|
1356
|
+
};
|
|
1357
|
+
let write = true;
|
|
1358
|
+
let writeValue;
|
|
1359
|
+
if (hasOwnProperty(item.options, "optionalAs")) writeValue = item.options.optionalAs;
|
|
1360
|
+
else if (hasOwnProperty(options, "optionalAs")) writeValue = options.optionalAs;
|
|
1361
|
+
else if (hasOwnProperty(this.options, "optionalAs")) writeValue = this.options.optionalAs;
|
|
1362
|
+
else if (item.options.optionalInclude) writeValue = value;
|
|
1363
|
+
else write = false;
|
|
1364
|
+
return {
|
|
1365
|
+
skip: true,
|
|
1366
|
+
write,
|
|
1367
|
+
value: writeValue
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Re-resolve a mount's `optional` declaration for the `meta.optional`
|
|
1372
|
+
* stamp applied by {@link Container.collectExecutionFailure}.
|
|
1373
|
+
*
|
|
1374
|
+
* Split out from `resolveOptionalDirective` because the two calls answer
|
|
1375
|
+
* different questions at different times: the directive decides whether to
|
|
1376
|
+
* *run* the mount, this decides whether to *tag* the mount's issues. The
|
|
1377
|
+
* split is what lets this one contain a throw the gate cannot.
|
|
1378
|
+
*
|
|
1379
|
+
* A predicate that throws degrades to `false` (don't tag) rather than
|
|
1380
|
+
* propagating. This is load-bearing, not defensive: the caller is a
|
|
1381
|
+
* `catch` handler with no `try` above it inside the run loop, and the very
|
|
1382
|
+
* throw it is folding may be the predicate's own. Re-raising here escapes
|
|
1383
|
+
* the error path entirely, discarding **every issue collected so far** and
|
|
1384
|
+
* replacing them with one path-less item — so a mainstream idiom like
|
|
1385
|
+
* `optional: (v) => v.trim().length === 0` would, on the first absent
|
|
1386
|
+
* field, wipe out every other field's validation error.
|
|
1387
|
+
*
|
|
1388
|
+
* `false` is the conservative fallback: the mount's issues surface at full
|
|
1389
|
+
* severity instead of being downgraded to a warning by consumers such as
|
|
1390
|
+
* `@validup/vue`'s `getSeverity`. The predicate's failure is not swallowed
|
|
1391
|
+
* — it is already being folded into an issue by the caller.
|
|
1392
|
+
*/
|
|
1393
|
+
resolveOptionalStamp(item, value) {
|
|
1394
|
+
if (typeof item.options.optional === "function") try {
|
|
1395
|
+
return Boolean(item.options.optional(value));
|
|
1396
|
+
} catch {
|
|
1397
|
+
return false;
|
|
1398
|
+
}
|
|
1399
|
+
return item.options.optional === true;
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Attribution fallback for a throw raised before a {@link MountKeyPlan}
|
|
1403
|
+
* exists — path expansion, key preparation, or the mount's value read.
|
|
1404
|
+
*
|
|
1405
|
+
* Produces the same `keyParts` / `pathRelative` pair `prepareMountKey`
|
|
1406
|
+
* would have, so a pre-dispatch failure is attributed to the mount that
|
|
1407
|
+
* caused it rather than surfacing path-less. For an item-level failure
|
|
1408
|
+
* (path expansion) the caller passes the *unexpanded* mount path, so a
|
|
1409
|
+
* glob mount is identified by its literal pattern (`items.*.name`) — the
|
|
1410
|
+
* keys it would have expanded to are precisely what could not be computed.
|
|
1411
|
+
*/
|
|
1412
|
+
describeKey(key) {
|
|
1413
|
+
const keyParts = key ? pathToArray(key) : [];
|
|
1414
|
+
return {
|
|
1415
|
+
keyParts,
|
|
1416
|
+
pathRelative: keyParts.at(-1)
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Build the option bag forwarded into a nested container's `run` /
|
|
1421
|
+
* `runSync`. Single source of truth for what a child inherits, so a new
|
|
1422
|
+
* run option is threaded downward in one place instead of three.
|
|
1423
|
+
*/
|
|
1424
|
+
buildChildRunOptions(options, key, plan, pathsStrict, parallel = false) {
|
|
1425
|
+
return {
|
|
1426
|
+
group: options.group,
|
|
1427
|
+
flat: true,
|
|
1428
|
+
path: plan.pathAbsolute,
|
|
1429
|
+
pathsToInclude: plan.filter.pathsToInclude,
|
|
1430
|
+
pathsToExclude: plan.filter.pathsToExclude,
|
|
1431
|
+
...pathsStrict && key.length > 0 ? { pathsStrict: true } : {},
|
|
1432
|
+
defaults: resolveDefaults(options.defaults, key),
|
|
1433
|
+
context: options.context,
|
|
1434
|
+
signal: options.signal,
|
|
1435
|
+
cache: options.cache,
|
|
1436
|
+
...parallel ? { parallel: true } : {},
|
|
1437
|
+
optionalValue: options.optionalValue,
|
|
1438
|
+
...hasOwnProperty(options, "optionalAs") ? { optionalAs: options.optionalAs } : {}
|
|
1439
|
+
};
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Build the context object handed to a mounted validator.
|
|
1443
|
+
*/
|
|
1444
|
+
buildValidatorContext(key, plan, value, data, options) {
|
|
1445
|
+
return {
|
|
1446
|
+
key,
|
|
1447
|
+
path: plan.pathAbsolute,
|
|
1448
|
+
value,
|
|
1449
|
+
data,
|
|
1450
|
+
group: options.group,
|
|
1451
|
+
context: options.context,
|
|
1452
|
+
signal: options.signal,
|
|
1453
|
+
cache: options.cache
|
|
1454
|
+
};
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1266
1457
|
* Lookup helper for the per-mount result cache.
|
|
1267
1458
|
*
|
|
1268
1459
|
* Returns `undefined` (forcing a real run) for any of:
|
|
@@ -1316,6 +1507,75 @@ var Container = class {
|
|
|
1316
1507
|
};
|
|
1317
1508
|
}
|
|
1318
1509
|
/**
|
|
1510
|
+
* Resolve the effective `pathsStrict` flag. Run-level wins over
|
|
1511
|
+
* container-level; unset anywhere is `false` (the silent, back-compatible
|
|
1512
|
+
* default).
|
|
1513
|
+
*/
|
|
1514
|
+
resolvePathsStrict(options) {
|
|
1515
|
+
return options.pathsStrict ?? this.options.pathsStrict ?? false;
|
|
1516
|
+
}
|
|
1517
|
+
/**
|
|
1518
|
+
* Structural pre-flight for `pathsStrict`. Verifies every resolved
|
|
1519
|
+
* `pathsToInclude` / `pathsToExclude` entry is satisfied by *this*
|
|
1520
|
+
* container — either an exact match against an expanded mount key, or a
|
|
1521
|
+
* prefix descent into a container mount (whose child re-checks the
|
|
1522
|
+
* forwarded remainder, since `pathsStrict` threads downward). Anything
|
|
1523
|
+
* unmatched throws {@link PathsStrictViolationError} with absolute paths.
|
|
1524
|
+
*
|
|
1525
|
+
* No-ops unless strict is on AND at least one filter list is present, so
|
|
1526
|
+
* the overhead on the common (non-strict) path is a single `??` chain.
|
|
1527
|
+
*
|
|
1528
|
+
* Notes on the match rules (kept in lockstep with `resolvePathFilter`):
|
|
1529
|
+
* - Keyless container mounts share the parent namespace and receive the
|
|
1530
|
+
* filter list verbatim, so a path could be owned by the keyless child
|
|
1531
|
+
* OR by a keyed sibling here — the parent can't tell without recursing.
|
|
1532
|
+
* To avoid false positives it treats every entry as satisfied when any
|
|
1533
|
+
* keyless container is present, and `pathsStrict` is NOT forwarded into
|
|
1534
|
+
* keyless children (that would throw for parent-sibling paths). Net:
|
|
1535
|
+
* keyless subtrees are a strict blind spot ("out of scope" per the issue).
|
|
1536
|
+
* - Group filtering is intentionally ignored: a mount excluded from the
|
|
1537
|
+
* active group still exists, so a path targeting it is not "stale".
|
|
1538
|
+
* - Expansion uses the same `expandPath(data, item.path)` the run loop
|
|
1539
|
+
* uses, so glob mounts are matched against the keys they actually
|
|
1540
|
+
* expand to for the given `data`.
|
|
1541
|
+
*/
|
|
1542
|
+
assertPathsStrict(strict, data, pathsToInclude, pathsToExclude, parentPath) {
|
|
1543
|
+
if (!strict) return;
|
|
1544
|
+
const hasInclude = typeof pathsToInclude !== "undefined" && pathsToInclude.length > 0;
|
|
1545
|
+
const hasExclude = typeof pathsToExclude !== "undefined" && pathsToExclude.length > 0;
|
|
1546
|
+
if (!hasInclude && !hasExclude) return;
|
|
1547
|
+
const expanded = [];
|
|
1548
|
+
let hasKeylessContainer = false;
|
|
1549
|
+
for (const item of this.items) {
|
|
1550
|
+
const keys = item.path ? expandPath(data, item.path) : [""];
|
|
1551
|
+
const itemIsContainer = item.type === "container";
|
|
1552
|
+
if (itemIsContainer && keys.some((key) => key.length === 0)) hasKeylessContainer = true;
|
|
1553
|
+
expanded.push({
|
|
1554
|
+
keys,
|
|
1555
|
+
isContainer: itemIsContainer
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
const isSatisfied = (path) => {
|
|
1559
|
+
if (hasKeylessContainer) return true;
|
|
1560
|
+
for (const entry of expanded) for (const key of entry.keys) {
|
|
1561
|
+
if (key.length === 0) continue;
|
|
1562
|
+
if (path === key) return true;
|
|
1563
|
+
if (entry.isContainer && path.startsWith(`${key}.`)) return true;
|
|
1564
|
+
}
|
|
1565
|
+
return false;
|
|
1566
|
+
};
|
|
1567
|
+
const toAbsolute = (path) => {
|
|
1568
|
+
if (!parentPath || parentPath.length === 0) return path;
|
|
1569
|
+
return stringifyPath([...parentPath, ...pathToArray(path)]);
|
|
1570
|
+
};
|
|
1571
|
+
const unmatchedInclude = hasInclude ? pathsToInclude.filter((path) => !isSatisfied(path)).map(toAbsolute) : [];
|
|
1572
|
+
const unmatchedExclude = hasExclude ? pathsToExclude.filter((path) => !isSatisfied(path)).map(toAbsolute) : [];
|
|
1573
|
+
if (unmatchedInclude.length > 0 || unmatchedExclude.length > 0) throw new PathsStrictViolationError({
|
|
1574
|
+
pathsToInclude: unmatchedInclude,
|
|
1575
|
+
pathsToExclude: unmatchedExclude
|
|
1576
|
+
});
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1319
1579
|
* Prepend `keyParts` to a child issue's `path` and — when the child is
|
|
1320
1580
|
* an `IssueGroup` — recurse into its nested issues so every leaf carries
|
|
1321
1581
|
* the parent prefix. Without recursion, a child container that already
|
|
@@ -1356,9 +1616,8 @@ var Container = class {
|
|
|
1356
1616
|
*/
|
|
1357
1617
|
collectExecutionFailure(context) {
|
|
1358
1618
|
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;
|
|
1619
|
+
if (isStructuralThrow(error, signal)) throw error;
|
|
1620
|
+
const isOptionalMount = this.resolveOptionalStamp(item, value);
|
|
1362
1621
|
const markOptional = (issue) => {
|
|
1363
1622
|
if (!isOptionalMount) return issue;
|
|
1364
1623
|
issue.meta = {
|
|
@@ -1439,25 +1698,14 @@ var Container = class {
|
|
|
1439
1698
|
return temp;
|
|
1440
1699
|
}
|
|
1441
1700
|
wrapSafeRunError(e, options) {
|
|
1442
|
-
if (options.signal
|
|
1443
|
-
if (isRunSyncViolation(e)) throw e;
|
|
1701
|
+
if (isStructuralThrow(e, options.signal)) throw e;
|
|
1444
1702
|
if (isValidupError(e)) return {
|
|
1445
1703
|
success: false,
|
|
1446
1704
|
error: e
|
|
1447
1705
|
};
|
|
1448
|
-
if (isError(e)) return {
|
|
1449
|
-
success: false,
|
|
1450
|
-
error: new ValidupError([defineIssueItem({
|
|
1451
|
-
path: [],
|
|
1452
|
-
message: e.message
|
|
1453
|
-
})])
|
|
1454
|
-
};
|
|
1455
1706
|
return {
|
|
1456
1707
|
success: false,
|
|
1457
|
-
error: new ValidupError(
|
|
1458
|
-
path: [],
|
|
1459
|
-
message: typeof e === "string" && e.length > 0 ? e : `Non-Error throw: ${String(e)}`
|
|
1460
|
-
})])
|
|
1708
|
+
error: new ValidupError(errorToIssues(e))
|
|
1461
1709
|
};
|
|
1462
1710
|
}
|
|
1463
1711
|
isItemGroupIncluded(item, group) {
|
|
@@ -1551,6 +1799,12 @@ var Builder = class Builder {
|
|
|
1551
1799
|
pathsToExclude: [...this.options.pathsToExclude ?? [], ...paths]
|
|
1552
1800
|
}, this.steps);
|
|
1553
1801
|
}
|
|
1802
|
+
pathsStrict(strict = true) {
|
|
1803
|
+
return new Builder({
|
|
1804
|
+
...this.options,
|
|
1805
|
+
pathsStrict: strict
|
|
1806
|
+
}, this.steps);
|
|
1807
|
+
}
|
|
1554
1808
|
build() {
|
|
1555
1809
|
const container = new Container(this.options);
|
|
1556
1810
|
for (const step of this.steps) if (step.kind === "nest") container.mount(step.key, step.options, step.child);
|
|
@@ -1631,6 +1885,6 @@ var ResultCache = class {
|
|
|
1631
1885
|
}
|
|
1632
1886
|
};
|
|
1633
1887
|
//#endregion
|
|
1634
|
-
export { Builder, Container, GroupKey, IssueCode, OptionalValue, ResultCache, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineIssueGroup, defineIssueItem, defineSchema, defineValidator, errorToIssues, flattenIssueGroups, flattenIssueItems, formatIssue, hasOwnProperty, interpolate, isContainer, isError, isIssue, isIssueGroup, isIssueItem, isObject, isOptionalValue, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
|
|
1888
|
+
export { Builder, Container, GroupKey, IssueCode, OptionalValue, PathsStrictViolationError, ResultCache, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineIssueGroup, defineIssueItem, defineSchema, defineValidator, errorToIssues, flattenIssueGroups, flattenIssueItems, formatIssue, hasOwnProperty, interpolate, isContainer, isError, isIssue, isIssueGroup, isIssueItem, isObject, isOptionalValue, isPathsStrictViolation, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
|
|
1635
1889
|
|
|
1636
1890
|
//# sourceMappingURL=index.mjs.map
|