@atscript/typescript 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +44 -1
- package/dist/index.mjs +2 -2
- package/dist/{plugin-lDfE9qm8.cjs → plugin-BNbjc9-d.cjs} +1 -1
- package/dist/{plugin-DUInOy5v.mjs → plugin-CnraKYN3.mjs} +1 -1
- package/dist/realm-runtime.js +660 -60
- package/dist/test-utils.cjs +10 -2
- package/dist/test-utils.d.ts +57 -3
- package/dist/test-utils.mjs +10 -3
- package/dist/{user-text-BvyErcKK.cjs → user-text-BrP_qD3T.cjs} +28 -4
- package/dist/{user-text-DZR5hfLa.mjs → user-text-DS0lorlV.mjs} +23 -5
- package/dist/utils.cjs +637 -55
- package/dist/utils.d.ts +116 -9
- package/dist/utils.mjs +634 -56
- package/package.json +6 -6
package/dist/realm-runtime.js
CHANGED
|
@@ -2040,21 +2040,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2040
2040
|
}
|
|
2041
2041
|
return regex;
|
|
2042
2042
|
}
|
|
2043
|
+
/** Whether `def` carries `@meta.required`. */ function isRequired(def) {
|
|
2044
|
+
return !!filledMeta(def.metadata)?.get("meta.required");
|
|
2045
|
+
}
|
|
2043
2046
|
/**
|
|
2044
2047
|
* `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
|
|
2045
2048
|
* type does not itself accept `null`. Shared by the walk and the fast pre-check.
|
|
2046
2049
|
*/ function rejectsNullAsRequired(def) {
|
|
2047
|
-
|
|
2048
|
-
return metadata !== void 0 && !!metadata.get("meta.required") && !acceptsNull$1(def);
|
|
2050
|
+
return isRequired(def) && !acceptsNull$2(def);
|
|
2049
2051
|
}
|
|
2050
|
-
function acceptsNull$
|
|
2052
|
+
function acceptsNull$2(def) {
|
|
2051
2053
|
const type = def.type;
|
|
2052
2054
|
if (type.kind === "") {
|
|
2053
2055
|
const designType = type.designType;
|
|
2054
2056
|
return designType === "null" || designType === "any";
|
|
2055
2057
|
}
|
|
2056
2058
|
if (type.kind === "union") {
|
|
2057
|
-
for (const item of type.items) if (item.optional || acceptsNull$
|
|
2059
|
+
for (const item of type.items) if (item.optional || acceptsNull$2(item)) return true;
|
|
2058
2060
|
}
|
|
2059
2061
|
return false;
|
|
2060
2062
|
}
|
|
@@ -2967,9 +2969,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2967
2969
|
/** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */ mismatch(def, code, params) {
|
|
2968
2970
|
return this.fail(code, params, expectMessage(def));
|
|
2969
2971
|
}
|
|
2970
|
-
/**
|
|
2972
|
+
/**
|
|
2973
|
+
* The `@meta.required` error of `def`: its message, else its `@expect.message`, else the code's
|
|
2974
|
+
* default.
|
|
2975
|
+
*/ required(def) {
|
|
2971
2976
|
const isBoolean = def.type.kind === "" && def.type.designType === "boolean";
|
|
2972
|
-
return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")));
|
|
2977
|
+
return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")) ?? expectMessage(def));
|
|
2973
2978
|
}
|
|
2974
2979
|
/**
|
|
2975
2980
|
* Adds `error`, already made, to the errors of the current level (as {@link error} does);
|
|
@@ -3040,9 +3045,22 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3040
3045
|
const decided = this.preCheck(def, value);
|
|
3041
3046
|
if (decided !== void 0) return decided;
|
|
3042
3047
|
}
|
|
3048
|
+
if ((value === void 0 || value === null) && isRequired(def)) return this.missing(def, value);
|
|
3043
3049
|
return this.validateAnnotatedType(def, value);
|
|
3044
3050
|
}
|
|
3045
3051
|
/**
|
|
3052
|
+
* A missing value (`undefined`, or `null`) of a `@meta.required` `def`: when `def` rejects it,
|
|
3053
|
+
* the `@meta.required` error replaces the type error the walk reports (the same verdict). A
|
|
3054
|
+
* bare `@meta.required` keeps the `@expect.message` text the type error had.
|
|
3055
|
+
*/ missing(def, value) {
|
|
3056
|
+
this.stackErrors.push(null);
|
|
3057
|
+
const passed = this.validateAnnotatedType(def, value);
|
|
3058
|
+
this.stackErrors.pop();
|
|
3059
|
+
if (passed) return true;
|
|
3060
|
+
this.limitExceeded = false;
|
|
3061
|
+
return this.required(def);
|
|
3062
|
+
}
|
|
3063
|
+
/**
|
|
3046
3064
|
* What {@link validateSafe} decides of `def` (replaced) before its own validation: an
|
|
3047
3065
|
* optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
|
|
3048
3066
|
*/ preCheck(def, value) {
|
|
@@ -4012,13 +4030,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4012
4030
|
for (let i = 0; i < keys.length; i++) {
|
|
4013
4031
|
const key = keys[i];
|
|
4014
4032
|
const input = value[key];
|
|
4015
|
-
|
|
4016
|
-
if (!def) {
|
|
4017
|
-
for (const { pattern, def: patternDef } of type.propsPatterns) if (pattern.test(key)) {
|
|
4018
|
-
def = patternDef;
|
|
4019
|
-
break;
|
|
4020
|
-
}
|
|
4021
|
-
}
|
|
4033
|
+
const def = propOf(type, key);
|
|
4022
4034
|
const mapped = def ? step(def, input, ctx) : input;
|
|
4023
4035
|
if (mapped === NO_MATCH) return NO_MATCH;
|
|
4024
4036
|
if (out) setOwn(out, key, mapped);
|
|
@@ -4030,6 +4042,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4030
4042
|
}
|
|
4031
4043
|
return out ?? value;
|
|
4032
4044
|
}
|
|
4045
|
+
/** The def of prop `key` of object `type`: its declared prop, else the first pattern `key` matches. */ function propOf(type, key) {
|
|
4046
|
+
const def = type.props.get(key);
|
|
4047
|
+
if (def) return def;
|
|
4048
|
+
for (const p of type.propsPatterns) if (p.pattern.test(key)) return p.def;
|
|
4049
|
+
}
|
|
4033
4050
|
/**
|
|
4034
4051
|
* Maps array / tuple items with `step`, against `of` (an array's item type) or `of[i]` (a tuple's
|
|
4035
4052
|
* items). Copy-on-first-change, as {@link mapProps}.
|
|
@@ -4449,12 +4466,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4449
4466
|
* backtracking). A scalar branch takes what it parses; an object (tuple)
|
|
4450
4467
|
* branch takes a plain object (an array of its length) unless a literal
|
|
4451
4468
|
* property (item) — `kind: 'cat'`, `kind: 'cat' | null` — holds a value that
|
|
4452
|
-
* does not coerce to it
|
|
4453
|
-
* literal and of the unions it is in.
|
|
4454
|
-
*
|
|
4455
|
-
*
|
|
4456
|
-
*
|
|
4457
|
-
*
|
|
4469
|
+
* does not coerce to it (`"2"` coerces to a `2` literal), a string compared
|
|
4470
|
+
* after the normalizers of the literal and of the unions it is in. A scalar
|
|
4471
|
+
* takes the first branch that takes it. An object or array takes the only
|
|
4472
|
+
* branch that takes it, else the one it fits best, judged in this order
|
|
4473
|
+
* (never looking deeper than its own keys and items): its props (items) hold
|
|
4474
|
+
* values of their kinds (a string that parses for a `number` prop, `null`
|
|
4475
|
+
* where `null` is allowed); it has no undeclared key; it has every required
|
|
4476
|
+
* prop (the last two can be waived by `partial` / `unknownProps`, a wrong
|
|
4477
|
+
* kind cannot); a nested union's branches count as the union's own. Ties go
|
|
4478
|
+
* to the branch an object's discriminator names by the literal itself, then
|
|
4479
|
+
* to declared order. When no branch takes the value, it is returned
|
|
4480
|
+
* untouched.
|
|
4458
4481
|
* - Objects recurse into props when the input is a plain object (covers
|
|
4459
4482
|
* `@Query()` DTOs where every field arrives as a string), whatever their
|
|
4460
4483
|
* literal props hold unless tried as a union branch.
|
|
@@ -4539,34 +4562,161 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4539
4562
|
}
|
|
4540
4563
|
}
|
|
4541
4564
|
/**
|
|
4542
|
-
* The
|
|
4543
|
-
*
|
|
4544
|
-
|
|
4545
|
-
|
|
4565
|
+
* The score bits of a union branch for an object (array), most weighty first (see {@link score} and
|
|
4566
|
+
* {@link coerceForType}): props (items) hold their kinds; no undeclared key; required props present.
|
|
4567
|
+
*/ const HOLDS = 4;
|
|
4568
|
+
const DECLARED = 2;
|
|
4569
|
+
const COMPLETE = 1;
|
|
4570
|
+
const BEST = DECLARED | 5;
|
|
4571
|
+
/** The score of a branch not scored yet (the only taker so far, see {@link TPick}). */ const UNSCORED = -1;
|
|
4572
|
+
/**
|
|
4573
|
+
* The branch of union `items` that takes `value` (see {@link takes}); `undefined` when none does.
|
|
4574
|
+
* A scalar: the first taker. An object or array: of the leaf branches (see {@link leavesOf}), the
|
|
4575
|
+
* only taker, else the best scored one (see {@link score}); the branch the discriminator names is
|
|
4576
|
+
* tried first, then declared order, and a tie keeps the earlier one.
|
|
4546
4577
|
*/ function branchOf(items, value) {
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4578
|
+
if (!isPlainObject$1(value) && !Array.isArray(value)) return items.find((item) => takes(item, value));
|
|
4579
|
+
const leaves = leavesOf(items);
|
|
4580
|
+
const pick = {
|
|
4581
|
+
branch: void 0,
|
|
4582
|
+
score: UNSCORED,
|
|
4583
|
+
keys: void 0,
|
|
4584
|
+
values: void 0
|
|
4585
|
+
};
|
|
4586
|
+
const named = Array.isArray(value) ? void 0 : discriminatedBranch(leaves, value);
|
|
4587
|
+
if (named) consider(named, value, pick);
|
|
4588
|
+
for (const leaf of leaves) {
|
|
4589
|
+
if (pick.score === BEST) break;
|
|
4590
|
+
if (leaf !== named) consider(leaf, value, pick);
|
|
4591
|
+
}
|
|
4592
|
+
return pick.branch;
|
|
4593
|
+
}
|
|
4594
|
+
/** {@link leavesOf} by union items: valid until the next type tree write (see `typeWriteCount`). */ let leavesCache = /* @__PURE__ */ new WeakMap();
|
|
4595
|
+
let leavesStamp = -1;
|
|
4596
|
+
/**
|
|
4597
|
+
* The branches of union `items` that are not unions, a nested union's in its place, in declared
|
|
4598
|
+
* order, so `(A | B) | C` and `A | B | C` rank alike: `items` itself when none is a union, else
|
|
4599
|
+
* the same array for the same items (the discriminator over them is decided once).
|
|
4600
|
+
*/ function leavesOf(items) {
|
|
4601
|
+
if (!items.some(isUnion)) return items;
|
|
4602
|
+
const writes = typeWriteCount();
|
|
4603
|
+
if (writes !== leavesStamp) {
|
|
4604
|
+
leavesStamp = writes;
|
|
4605
|
+
leavesCache = /* @__PURE__ */ new WeakMap();
|
|
4606
|
+
}
|
|
4607
|
+
let leaves = leavesCache.get(items);
|
|
4608
|
+
if (!leaves) {
|
|
4609
|
+
leaves = [];
|
|
4610
|
+
collectLeaves(items, leaves);
|
|
4611
|
+
leavesCache.set(items, leaves);
|
|
4612
|
+
}
|
|
4613
|
+
return leaves;
|
|
4614
|
+
}
|
|
4615
|
+
function isUnion(def) {
|
|
4616
|
+
return def.type.kind === "union";
|
|
4617
|
+
}
|
|
4618
|
+
function collectLeaves(items, out) {
|
|
4619
|
+
for (const item of items) if (isUnion(item)) collectLeaves(item.type.items, out);
|
|
4620
|
+
else out.push(item);
|
|
4621
|
+
}
|
|
4622
|
+
/** Takes leaf branch `def` for `pick` when it takes `value` and scores above the best so far. */ function consider(def, value, pick) {
|
|
4623
|
+
if (!takes(def, value)) return;
|
|
4624
|
+
if (!pick.branch) {
|
|
4625
|
+
pick.branch = def;
|
|
4626
|
+
return;
|
|
4627
|
+
}
|
|
4628
|
+
if (pick.score === UNSCORED) {
|
|
4629
|
+
pick.score = score(pick.branch, value, pick);
|
|
4630
|
+
if (pick.score === BEST) return;
|
|
4631
|
+
}
|
|
4632
|
+
const s = score(def, value, pick);
|
|
4633
|
+
if (s > pick.score) {
|
|
4634
|
+
pick.branch = def;
|
|
4635
|
+
pick.score = s;
|
|
4556
4636
|
}
|
|
4557
|
-
return items.find((item) => takes(item, value));
|
|
4558
4637
|
}
|
|
4559
4638
|
/**
|
|
4560
|
-
*
|
|
4561
|
-
*
|
|
4562
|
-
*
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
if (def.type.kind === "union") return def.type.items.some((item) => fits(item, value));
|
|
4639
|
+
* The bits (see {@link HOLDS}) leaf branch `def`, which takes object (array) `value`, scores; once
|
|
4640
|
+
* it cannot beat `pick.score`, any score not above it. Only an object branch can miss
|
|
4641
|
+
* {@link DECLARED} or {@link COMPLETE}.
|
|
4642
|
+
*/ function score(def, value, pick) {
|
|
4643
|
+
var _pick, _pick1;
|
|
4566
4644
|
const objects = [];
|
|
4567
|
-
if (!objectsOf(def, objects)) return
|
|
4568
|
-
|
|
4569
|
-
|
|
4645
|
+
if (Array.isArray(value) || !objectsOf(def, objects)) return (holdsAll(def, value) ? HOLDS : 0) | 3;
|
|
4646
|
+
const floor = pick.score;
|
|
4647
|
+
let bits = BEST;
|
|
4648
|
+
const keys = (_pick = pick).keys ?? (_pick.keys = Object.keys(value));
|
|
4649
|
+
const values = (_pick1 = pick).values ?? (_pick1.values = keys.map((key) => value[key]));
|
|
4650
|
+
for (let i = 0; i < keys.length; i++) {
|
|
4651
|
+
const key = keys[i];
|
|
4652
|
+
let declared = false;
|
|
4653
|
+
for (const o of objects) {
|
|
4654
|
+
const prop = propOf(o, key);
|
|
4655
|
+
if (prop) {
|
|
4656
|
+
declared = true;
|
|
4657
|
+
if (bits & HOLDS && !holds(prop, values[i])) bits &= ~HOLDS;
|
|
4658
|
+
}
|
|
4659
|
+
}
|
|
4660
|
+
if (!declared) bits &= ~DECLARED;
|
|
4661
|
+
if (bits <= floor) return bits;
|
|
4662
|
+
if (bits <= COMPLETE) break;
|
|
4663
|
+
}
|
|
4664
|
+
for (const o of objects) for (const [key, prop] of o.props) if (!prop.optional && !Object.hasOwn(value, key)) return bits & ~COMPLETE;
|
|
4665
|
+
return bits;
|
|
4666
|
+
}
|
|
4667
|
+
/**
|
|
4668
|
+
* Whether each prop (pattern prop) of branch `def` that object `value` has, or each item of array
|
|
4669
|
+
* `value`, holds a value of its kind (see {@link holds}). Looks at `value`'s own keys and items
|
|
4670
|
+
* only. `def` takes `value` (see {@link takes}).
|
|
4671
|
+
*/ function holdsAll(def, value) {
|
|
4672
|
+
const type = def.type;
|
|
4673
|
+
switch (type.kind) {
|
|
4674
|
+
case "object": {
|
|
4675
|
+
const o = type;
|
|
4676
|
+
const record = value;
|
|
4677
|
+
return Object.keys(record).every((key) => {
|
|
4678
|
+
const prop = propOf(o, key);
|
|
4679
|
+
return !prop || holds(prop, record[key]);
|
|
4680
|
+
});
|
|
4681
|
+
}
|
|
4682
|
+
case "array": {
|
|
4683
|
+
const of = type.of;
|
|
4684
|
+
return value.every((item) => holds(of, item));
|
|
4685
|
+
}
|
|
4686
|
+
case "tuple": return type.items.every((item, i) => holds(item, value[i]));
|
|
4687
|
+
case "union": return type.items.some((item) => takes(item, value) && holdsAll(item, value));
|
|
4688
|
+
case "intersection": return type.items.every((item) => holdsAll(item, value));
|
|
4689
|
+
default: return true;
|
|
4690
|
+
}
|
|
4691
|
+
}
|
|
4692
|
+
/**
|
|
4693
|
+
* Whether `input` is of the kind of prop (item) `def`, never looking inside it: a scalar that
|
|
4694
|
+
* coerces to the prop's scalar type, or to one of its literals (a string after the normalizers on
|
|
4695
|
+
* the literal's way, see `literalNodes`); a plain object for an object, an array for an array (of
|
|
4696
|
+
* the tuple's length); `null` where the prop takes `null` (optional or a nullable type).
|
|
4697
|
+
* `inherited`: the normalizer flags of the unions `def` is in.
|
|
4698
|
+
*/ function holds(def, input, inherited = 0) {
|
|
4699
|
+
if (input === void 0) return true;
|
|
4700
|
+
if (input === null) return def.optional === true || acceptsNull$2(def);
|
|
4701
|
+
const type = def.type;
|
|
4702
|
+
switch (type.kind) {
|
|
4703
|
+
case "": {
|
|
4704
|
+
const final = type;
|
|
4705
|
+
return final.value === void 0 ? scalar(final.designType, input) !== NO_MATCH : coercesTo(def, flagsOf(def, inherited), input);
|
|
4706
|
+
}
|
|
4707
|
+
case "object": return isPlainObject$1(input);
|
|
4708
|
+
case "array": return Array.isArray(input);
|
|
4709
|
+
case "tuple": return Array.isArray(input) && input.length === type.items.length;
|
|
4710
|
+
case "union": {
|
|
4711
|
+
const flags = flagsOf(def, inherited);
|
|
4712
|
+
return type.items.some((item) => holds(item, input, flags));
|
|
4713
|
+
}
|
|
4714
|
+
case "intersection": {
|
|
4715
|
+
const flags = flagsOf(def, inherited);
|
|
4716
|
+
return type.items.every((item) => holds(item, input, flags));
|
|
4717
|
+
}
|
|
4718
|
+
default: return false;
|
|
4719
|
+
}
|
|
4570
4720
|
}
|
|
4571
4721
|
/** Collects the objects `def` is (an object, an intersection of objects); `false` for others. */ function objectsOf(def, out) {
|
|
4572
4722
|
const type = def.type;
|
|
@@ -4588,7 +4738,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4588
4738
|
case "": return coerce(def, value) !== NO_MATCH;
|
|
4589
4739
|
case "object":
|
|
4590
4740
|
if (!isPlainObject$1(value)) return false;
|
|
4591
|
-
for (const [key, prop] of type.props) if (Object.hasOwn(value, key) && !holdsLiteral(prop, value[key])) return false;
|
|
4741
|
+
for (const [key, prop] of type.props) if (mayBeLiteral(prop) && Object.hasOwn(value, key) && !holdsLiteral(prop, value[key])) return false;
|
|
4592
4742
|
return true;
|
|
4593
4743
|
case "array": return Array.isArray(value);
|
|
4594
4744
|
case "tuple": {
|
|
@@ -4600,6 +4750,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4600
4750
|
default: return false;
|
|
4601
4751
|
}
|
|
4602
4752
|
}
|
|
4753
|
+
/** Whether `def` can be a literal prop (see `literalNodes`): a literal or a union. */ function mayBeLiteral(def) {
|
|
4754
|
+
const type = def.type;
|
|
4755
|
+
return type.kind === "" ? type.value !== void 0 : type.kind === "union";
|
|
4756
|
+
}
|
|
4603
4757
|
/**
|
|
4604
4758
|
* Whether prop (item) `def` of a union branch can hold `input`: anything, but a literal prop
|
|
4605
4759
|
* (see `literalNodes`: `kind: 'cat'`, `kind: 'cat' | null`) only a value that coerces to one of
|
|
@@ -4608,32 +4762,361 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4608
4762
|
*/ function holdsLiteral(def, input) {
|
|
4609
4763
|
if (input === void 0 || input === null) return true;
|
|
4610
4764
|
const literals = literalNodes(def);
|
|
4611
|
-
return !literals || literals.some((literal) => coercesTo(literal, input));
|
|
4765
|
+
return !literals || literals.some((literal) => coercesTo(literal.node, literal.flags, input));
|
|
4612
4766
|
}
|
|
4613
|
-
/**
|
|
4614
|
-
|
|
4615
|
-
|
|
4767
|
+
/**
|
|
4768
|
+
* Whether `input` coerces to the value of literal `node`, a string after normalizer `flags` (see
|
|
4769
|
+
* `holdsLiteral`).
|
|
4770
|
+
*/ function coercesTo(node, flags, input) {
|
|
4771
|
+
return coerce(node, typeof input === "string" ? applyFlags(input, flags) : input) !== NO_MATCH;
|
|
4616
4772
|
}
|
|
4617
4773
|
/**
|
|
4618
|
-
* The branch of union `items`
|
|
4619
|
-
*
|
|
4620
|
-
* union has no discriminator or
|
|
4774
|
+
* The branch of union `items` whose discriminator literal is exactly the one object `value` holds
|
|
4775
|
+
* (`"1"` names a `'1'` literal, not a `1`, though it makes a `1` branch a taker, see {@link takes});
|
|
4776
|
+
* `undefined` when the union has no discriminator or no branch's literal is that value.
|
|
4621
4777
|
*/ function discriminatedBranch(items, value) {
|
|
4622
4778
|
const disc = unionDiscriminator(items);
|
|
4623
|
-
if (disc === void 0) return;
|
|
4624
|
-
const tag =
|
|
4625
|
-
if (tag === void 0 || tag === null) return;
|
|
4779
|
+
if (disc === void 0 || !Object.hasOwn(value, disc.property)) return;
|
|
4780
|
+
const tag = value[disc.property];
|
|
4626
4781
|
for (const [index, literal] of disc.branches) if (literal === tag) return items[index];
|
|
4627
|
-
for (const [index] of disc.branches) {
|
|
4628
|
-
const prop = items[index].type.props.get(disc.property);
|
|
4629
|
-
if (prop && holdsLiteral(prop, tag)) return items[index];
|
|
4630
|
-
}
|
|
4631
4782
|
}
|
|
4632
4783
|
/** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
|
|
4633
4784
|
const result = coerce(def, input);
|
|
4634
4785
|
return result === NO_MATCH ? input : result;
|
|
4635
4786
|
}
|
|
4636
4787
|
|
|
4788
|
+
//#endregion
|
|
4789
|
+
//#region packages/typescript/src/runtime/json-schema-strict.ts
|
|
4790
|
+
/** The keywords every provider takes: the shape of the schema, its notes and literals. */ const BASE_KEYWORDS = {
|
|
4791
|
+
type: true,
|
|
4792
|
+
properties: true,
|
|
4793
|
+
required: true,
|
|
4794
|
+
additionalProperties: true,
|
|
4795
|
+
items: true,
|
|
4796
|
+
anyOf: true,
|
|
4797
|
+
$ref: true,
|
|
4798
|
+
$defs: true,
|
|
4799
|
+
enum: true,
|
|
4800
|
+
const: true,
|
|
4801
|
+
title: true,
|
|
4802
|
+
description: true
|
|
4803
|
+
};
|
|
4804
|
+
/** The string formats both providers list. */ const FORMATS = new Set([
|
|
4805
|
+
"date-time",
|
|
4806
|
+
"time",
|
|
4807
|
+
"date",
|
|
4808
|
+
"duration",
|
|
4809
|
+
"email",
|
|
4810
|
+
"hostname",
|
|
4811
|
+
"ipv4",
|
|
4812
|
+
"ipv6",
|
|
4813
|
+
"uuid"
|
|
4814
|
+
]);
|
|
4815
|
+
/**
|
|
4816
|
+
* OpenAI Structured Outputs / `strict: true` function calling: every property required
|
|
4817
|
+
* (optional ones nullable), objects closed, an object root, no `allOf`; patterns without
|
|
4818
|
+
* lookaround, backreferences or modifier groups, numeric bounds and `minItems` / `maxItems`
|
|
4819
|
+
* kept; string lengths, `uniqueItems` and key counts dropped. Recursion is allowed.
|
|
4820
|
+
*/ const OPENAI_STRICT = {
|
|
4821
|
+
name: "openai",
|
|
4822
|
+
keywords: {
|
|
4823
|
+
...BASE_KEYWORDS,
|
|
4824
|
+
pattern: (value) => decodablePattern(value),
|
|
4825
|
+
format: (value) => FORMATS.has(value),
|
|
4826
|
+
minimum: true,
|
|
4827
|
+
maximum: true,
|
|
4828
|
+
exclusiveMinimum: true,
|
|
4829
|
+
exclusiveMaximum: true,
|
|
4830
|
+
multipleOf: true,
|
|
4831
|
+
minItems: true,
|
|
4832
|
+
maxItems: true
|
|
4833
|
+
},
|
|
4834
|
+
requireAll: true,
|
|
4835
|
+
collapseAllOf: true,
|
|
4836
|
+
describeDropped: true,
|
|
4837
|
+
allowRecursion: true,
|
|
4838
|
+
objectRoot: true
|
|
4839
|
+
};
|
|
4840
|
+
/**
|
|
4841
|
+
* Anthropic strict tool use / structured outputs: objects closed, an object root (a tool's
|
|
4842
|
+
* `input_schema`), no recursion; simple patterns kept (no lookaround, backreferences, word
|
|
4843
|
+
* boundaries or large `{n,m}`), `minItems` only `0` / `1`; numeric bounds, string lengths,
|
|
4844
|
+
* `maxItems`, `uniqueItems` and key counts dropped. `allOf` is allowed
|
|
4845
|
+
* except with a `$ref` (collapsed). Optional properties stay optional.
|
|
4846
|
+
*/ const ANTHROPIC_STRICT = {
|
|
4847
|
+
name: "anthropic",
|
|
4848
|
+
keywords: {
|
|
4849
|
+
...BASE_KEYWORDS,
|
|
4850
|
+
allOf: true,
|
|
4851
|
+
pattern: (value) => simplePattern(value),
|
|
4852
|
+
format: (value) => FORMATS.has(value) || value === "uri",
|
|
4853
|
+
minItems: (value) => value === 0 || value === 1
|
|
4854
|
+
},
|
|
4855
|
+
requireAll: false,
|
|
4856
|
+
collapseAllOf: true,
|
|
4857
|
+
describeDropped: true,
|
|
4858
|
+
allowRecursion: false,
|
|
4859
|
+
objectRoot: true
|
|
4860
|
+
};
|
|
4861
|
+
/** The largest `{n,m}` bound a simple pattern has (see {@link simplePattern}). */ const SIMPLE_BOUND = 100;
|
|
4862
|
+
/**
|
|
4863
|
+
* A pattern without lookaround, backreferences, word boundaries, Unicode property escapes,
|
|
4864
|
+
* modifier groups or `{n,m}` bounds above {@link SIMPLE_BOUND} (Anthropic's simple patterns).
|
|
4865
|
+
* Named groups are fine; `\b` in a class is a backspace.
|
|
4866
|
+
*/ function simplePattern(pattern) {
|
|
4867
|
+
return scanPattern(pattern, true);
|
|
4868
|
+
}
|
|
4869
|
+
/** A pattern without lookaround, backreferences or modifier groups (OpenAI). */ function decodablePattern(pattern) {
|
|
4870
|
+
return scanPattern(pattern, false);
|
|
4871
|
+
}
|
|
4872
|
+
/** {@link simplePattern} (`simple`), else {@link decodablePattern}. */ function scanPattern(pattern, simple) {
|
|
4873
|
+
let inClass = false;
|
|
4874
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
4875
|
+
const c = pattern[i];
|
|
4876
|
+
if (c === "\\") {
|
|
4877
|
+
const next = pattern[++i];
|
|
4878
|
+
if (next === void 0 || /[1-9k]/.test(next)) return false;
|
|
4879
|
+
if (simple && (/[pPB]/.test(next) || next === "b" && !inClass)) return false;
|
|
4880
|
+
} else if (inClass) inClass = c !== "]";
|
|
4881
|
+
else if (c === "[") inClass = true;
|
|
4882
|
+
else if (c === "(" && pattern[i + 1] === "?") {
|
|
4883
|
+
const kind = pattern[i + 2];
|
|
4884
|
+
const named = kind === "<" && pattern[i + 3] !== "=" && pattern[i + 3] !== "!";
|
|
4885
|
+
if (kind !== ":" && !named) return false;
|
|
4886
|
+
} else if (simple && c === "{") {
|
|
4887
|
+
const bounds = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(i));
|
|
4888
|
+
if (bounds && (Number(bounds[1]) > SIMPLE_BOUND || Number(bounds[2] || 0) > SIMPLE_BOUND)) return false;
|
|
4889
|
+
}
|
|
4890
|
+
}
|
|
4891
|
+
return true;
|
|
4892
|
+
}
|
|
4893
|
+
/** The `$defs` name a `$ref` this builder writes points to. */ function refName(ref) {
|
|
4894
|
+
return ref.slice(8);
|
|
4895
|
+
}
|
|
4896
|
+
/** The `$defs` names `schema` refers to (through any keyword but `$defs`). */ function refsOf(schema, out = /* @__PURE__ */ new Set()) {
|
|
4897
|
+
if (Array.isArray(schema)) for (const item of schema) refsOf(item, out);
|
|
4898
|
+
else if (schema && typeof schema === "object") {
|
|
4899
|
+
for (const [key, value] of Object.entries(schema)) if (key === "$ref" && typeof value === "string") out.add(refName(value));
|
|
4900
|
+
else if (key !== "$defs") refsOf(value, out);
|
|
4901
|
+
}
|
|
4902
|
+
return out;
|
|
4903
|
+
}
|
|
4904
|
+
/** The refs of each `$defs` entry, by name. */ function edgesOf(defs) {
|
|
4905
|
+
return new Map(Object.entries(defs).map(([name, def]) => [name, refsOf(def)]));
|
|
4906
|
+
}
|
|
4907
|
+
/** The `$defs` names reachable from `from` through `edges`. */ function reachable(from, edges) {
|
|
4908
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4909
|
+
const stack = [...from];
|
|
4910
|
+
for (let name = stack.pop(); name !== void 0; name = stack.pop()) if (!seen.has(name)) {
|
|
4911
|
+
seen.add(name);
|
|
4912
|
+
stack.push(...edges.get(name) ?? []);
|
|
4913
|
+
}
|
|
4914
|
+
return seen;
|
|
4915
|
+
}
|
|
4916
|
+
/** The first `$defs` entry that refers to itself, through any others; `undefined`: none. */ function recursiveDef(defs) {
|
|
4917
|
+
const edges = edgesOf(defs);
|
|
4918
|
+
return [...edges.keys()].find((name) => reachable(edges.get(name), edges).has(name));
|
|
4919
|
+
}
|
|
4920
|
+
/** `n` `one`(s): `1 item`, `2 items`. */ function count(n, one, many = `${one}s`) {
|
|
4921
|
+
return `${n} ${n === 1 ? one : many}`;
|
|
4922
|
+
}
|
|
4923
|
+
/** A dropped keyword in plain words (`pattern` `\S`: the `@meta.required` check). */ function describe(keyword, value) {
|
|
4924
|
+
switch (keyword) {
|
|
4925
|
+
case "minLength": return `At least ${count(value, "character")}.`;
|
|
4926
|
+
case "maxLength": return `At most ${count(value, "character")}.`;
|
|
4927
|
+
case "pattern": return value === "\\S" ? "Must not be blank." : `Must match the pattern ${JSON.stringify(value)}.`;
|
|
4928
|
+
case "format": return `Format: ${value}.`;
|
|
4929
|
+
case "minimum": return `Minimum ${value}.`;
|
|
4930
|
+
case "maximum": return `Maximum ${value}.`;
|
|
4931
|
+
case "exclusiveMinimum": return `Greater than ${value}.`;
|
|
4932
|
+
case "exclusiveMaximum": return `Less than ${value}.`;
|
|
4933
|
+
case "multipleOf": return `A multiple of ${value}.`;
|
|
4934
|
+
case "minItems": return `At least ${count(value, "item")}.`;
|
|
4935
|
+
case "maxItems": return `At most ${count(value, "item")}.`;
|
|
4936
|
+
case "uniqueItems": return value ? "Items must be unique." : "";
|
|
4937
|
+
case "minProperties": return `At least ${count(value, "property", "properties")}.`;
|
|
4938
|
+
case "maxProperties": return `At most ${count(value, "property", "properties")}.`;
|
|
4939
|
+
default: return `${keyword}: ${JSON.stringify(value)}.`;
|
|
4940
|
+
}
|
|
4941
|
+
}
|
|
4942
|
+
/** A schema `type` of objects (`'object'`, or a type list with it: an optional object). */ function isObjectType(type) {
|
|
4943
|
+
return type === "object" || Array.isArray(type) && type.includes("object");
|
|
4944
|
+
}
|
|
4945
|
+
/** Whether `schema` accepts `null` as it is (a `null` type, value or `anyOf` member). */ function acceptsNull$1(schema) {
|
|
4946
|
+
if ("const" in schema) return schema.const === null;
|
|
4947
|
+
if (Array.isArray(schema.enum)) return schema.enum.includes(null);
|
|
4948
|
+
if (schema.type !== void 0) return schema.type === "null" || Array.isArray(schema.type) && schema.type.includes("null");
|
|
4949
|
+
return Array.isArray(schema.anyOf) && schema.anyOf.some(acceptsNull$1);
|
|
4950
|
+
}
|
|
4951
|
+
/**
|
|
4952
|
+
* `schema` also accepting `null`: `type: [t, 'null']` for a plain typed schema, else an `anyOf`
|
|
4953
|
+
* with a `null` member (the title / description stay beside it).
|
|
4954
|
+
*/ function orNull(schema) {
|
|
4955
|
+
if (acceptsNull$1(schema)) return schema;
|
|
4956
|
+
if (schema.type !== void 0 && !("const" in schema) && schema.enum === void 0) return {
|
|
4957
|
+
...schema,
|
|
4958
|
+
type: [schema.type, "null"].flat()
|
|
4959
|
+
};
|
|
4960
|
+
const { title, description, ...body } = schema;
|
|
4961
|
+
const notes = {};
|
|
4962
|
+
if (title !== void 0) notes.title = title;
|
|
4963
|
+
if (description !== void 0) notes.description = description;
|
|
4964
|
+
return {
|
|
4965
|
+
anyOf: [...Object.keys(body).length === 1 && Array.isArray(body.anyOf) ? body.anyOf : [body], { type: "null" }],
|
|
4966
|
+
...notes
|
|
4967
|
+
};
|
|
4968
|
+
}
|
|
4969
|
+
/**
|
|
4970
|
+
* Object schema `out` with every property required (`requireAll`): those not in `required`
|
|
4971
|
+
* (optional) made nullable.
|
|
4972
|
+
*/ function requireAll(out, required) {
|
|
4973
|
+
const properties = out.properties ?? {};
|
|
4974
|
+
const keys = Object.keys(properties);
|
|
4975
|
+
const requiredKeys = new Set(required);
|
|
4976
|
+
for (const key of keys) if (!requiredKeys.has(key)) properties[key] = orNull(properties[key]);
|
|
4977
|
+
if (keys.length > 0) out.required = keys;
|
|
4978
|
+
else delete out.required;
|
|
4979
|
+
}
|
|
4980
|
+
/** Whether `schema` carries the `@meta.required` check of a string (the pattern `\S`). */ function hasBlankCheck(schema) {
|
|
4981
|
+
return schema.pattern === "\\S" || Array.isArray(schema.allOf) && schema.allOf.some((item) => item.pattern === "\\S");
|
|
4982
|
+
}
|
|
4983
|
+
/**
|
|
4984
|
+
* `out` with the `dropped` keywords described in its `description` (`blank`: it has the
|
|
4985
|
+
* `@meta.required` check, kept or described, which says what its `minLength: 1` would). A lone
|
|
4986
|
+
* `$ref` takes no keywords beside it: it moves into an `anyOf` of its own.
|
|
4987
|
+
*/ function described(out, dropped, blank) {
|
|
4988
|
+
const notes = dropped.filter(([key, value]) => !(blank && key === "minLength" && value === 1)).map(([key, value]) => describe(key, value)).filter(Boolean).join(" ");
|
|
4989
|
+
if (!notes) return out;
|
|
4990
|
+
let schema = out;
|
|
4991
|
+
if (schema.$ref !== void 0) {
|
|
4992
|
+
const { $ref, ...rest } = schema;
|
|
4993
|
+
schema = {
|
|
4994
|
+
anyOf: [{ $ref }],
|
|
4995
|
+
...rest
|
|
4996
|
+
};
|
|
4997
|
+
}
|
|
4998
|
+
schema.description = schema.description ? `${schema.description}\n\n${notes}` : notes;
|
|
4999
|
+
return schema;
|
|
5000
|
+
}
|
|
5001
|
+
/**
|
|
5002
|
+
* `schema` (built closed by {@link buildJsonSchema}) rewritten to `profile` (see
|
|
5003
|
+
* {@link TStrictProfile}). Throws for a shape the profile can't take.
|
|
5004
|
+
*/ function strictSchema(schema, profile) {
|
|
5005
|
+
const fail = (what) => {
|
|
5006
|
+
throw new Error(`buildJsonSchema (strict: ${profile.name}): ${what}`);
|
|
5007
|
+
};
|
|
5008
|
+
const defs = schema.$defs ?? {};
|
|
5009
|
+
if (!profile.allowRecursion) {
|
|
5010
|
+
const recursive = recursiveDef(defs);
|
|
5011
|
+
if (recursive !== void 0) fail(`recursive type "${recursive}" is not supported`);
|
|
5012
|
+
}
|
|
5013
|
+
if (profile.objectRoot && schema.type !== "object") fail("the root must be an object type");
|
|
5014
|
+
const allowed = (keyword, value) => {
|
|
5015
|
+
const rule = profile.keywords[keyword];
|
|
5016
|
+
return rule === true || rule !== void 0 && rule(value);
|
|
5017
|
+
};
|
|
5018
|
+
/** `$defs` entries being inlined (a use of an entry inside itself stays a reference). */ const inlining = /* @__PURE__ */ new Set();
|
|
5019
|
+
/**
|
|
5020
|
+
* `s` rewritten; the constraints it drops are described on it. `part`: a subschema of an
|
|
5021
|
+
* `allOf` (a constraint of the schema it sits in, no type of its own).
|
|
5022
|
+
*/ const rewrite = (s, at, part = false) => {
|
|
5023
|
+
/** The dropped keywords and their values (described at the end). */ const dropped = [];
|
|
5024
|
+
/** The `$defs` entries inlined here (see `collapse`). */ const entered = [];
|
|
5025
|
+
let source = profile.collapseAllOf ? collapse(s, dropped, entered) : s;
|
|
5026
|
+
if (source.oneOf && !profile.keywords.oneOf) {
|
|
5027
|
+
const { oneOf, discriminator: _, ...rest } = source;
|
|
5028
|
+
source = {
|
|
5029
|
+
...rest,
|
|
5030
|
+
anyOf: oneOf
|
|
5031
|
+
};
|
|
5032
|
+
}
|
|
5033
|
+
const where = at || "the root";
|
|
5034
|
+
if (Array.isArray(source.items)) fail(`a tuple (at ${where}) has no strict schema`);
|
|
5035
|
+
if (source.patternProperties !== void 0) fail(`a record (at ${where}) has no strict schema; use an array of key/value objects`);
|
|
5036
|
+
if (!part && source.type === void 0 && source.anyOf === void 0 && source.allOf === void 0 && source.$ref === void 0 && source.not === void 0 && !("const" in source) && source.enum === void 0) fail(`a value of any type (at ${where}) has no strict schema`);
|
|
5037
|
+
const out = {};
|
|
5038
|
+
for (const [key, value] of Object.entries(source)) {
|
|
5039
|
+
if (key === "discriminator" && !profile.keywords.discriminator) continue;
|
|
5040
|
+
if (!allowed(key, value)) {
|
|
5041
|
+
if (key === "allOf" || key === "not") fail(`${key === "allOf" ? "an intersection" : "an impossible type"} (at ${where}) has no strict schema`);
|
|
5042
|
+
dropped.push([key, value]);
|
|
5043
|
+
continue;
|
|
5044
|
+
}
|
|
5045
|
+
out[key] = rewriteValue(key, value, at);
|
|
5046
|
+
}
|
|
5047
|
+
for (const name of entered) inlining.delete(name);
|
|
5048
|
+
if (isObjectType(out.type)) {
|
|
5049
|
+
out.additionalProperties = false;
|
|
5050
|
+
if (profile.requireAll) requireAll(out, source.required);
|
|
5051
|
+
}
|
|
5052
|
+
return profile.describeDropped && dropped.length > 0 ? described(out, dropped, hasBlankCheck(s) || hasBlankCheck(source)) : out;
|
|
5053
|
+
};
|
|
5054
|
+
/** The value of keyword `key` rewritten (its subschemas). */ const rewriteValue = (key, value, at) => {
|
|
5055
|
+
switch (key) {
|
|
5056
|
+
case "properties": return Object.fromEntries(Object.entries(value).map(([name, p]) => [name, rewrite(p, at ? `${at}.${name}` : name)]));
|
|
5057
|
+
case "items": return rewrite(value, `${at}[]`);
|
|
5058
|
+
case "anyOf": return value.map((item) => rewrite(item, at));
|
|
5059
|
+
case "allOf": return value.map((item) => rewrite(item, at, true));
|
|
5060
|
+
case "$defs": return Object.fromEntries(Object.entries(value).map(([name, d]) => {
|
|
5061
|
+
inlining.add(name);
|
|
5062
|
+
const entry = rewrite(d, `$defs.${name}`);
|
|
5063
|
+
inlining.delete(name);
|
|
5064
|
+
return [name, entry];
|
|
5065
|
+
}));
|
|
5066
|
+
default: return value;
|
|
5067
|
+
}
|
|
5068
|
+
};
|
|
5069
|
+
/**
|
|
5070
|
+
* `s` with its `allOf` collapsed (see `collapseAllOf`): what it drops in `dropped`, the
|
|
5071
|
+
* `$defs` entries it inlines in `entered` (and `inlining`, until `s` is rewritten).
|
|
5072
|
+
*/ const collapse = (s, dropped, entered) => {
|
|
5073
|
+
if (!Array.isArray(s.allOf)) return s;
|
|
5074
|
+
const { allOf, ...rest } = s;
|
|
5075
|
+
const ref = allOf.length === 1 && Object.keys(allOf[0]).length === 1 ? allOf[0].$ref : void 0;
|
|
5076
|
+
if (typeof ref === "string") {
|
|
5077
|
+
const name = refName(ref);
|
|
5078
|
+
const target = defs[name];
|
|
5079
|
+
if (target === void 0 || inlining.has(name)) return {
|
|
5080
|
+
anyOf: [{ $ref: ref }],
|
|
5081
|
+
...rest
|
|
5082
|
+
};
|
|
5083
|
+
inlining.add(name);
|
|
5084
|
+
entered.push(name);
|
|
5085
|
+
return collapse({
|
|
5086
|
+
...target,
|
|
5087
|
+
...rest
|
|
5088
|
+
}, dropped, entered);
|
|
5089
|
+
}
|
|
5090
|
+
if (!allOf.every((item) => Object.keys(item).length === 1 && "pattern" in item)) return s;
|
|
5091
|
+
if (profile.keywords.allOf) {
|
|
5092
|
+
const kept = allOf.filter((item) => {
|
|
5093
|
+
if (allowed("pattern", item.pattern)) return true;
|
|
5094
|
+
dropped.push(["pattern", item.pattern]);
|
|
5095
|
+
return false;
|
|
5096
|
+
});
|
|
5097
|
+
if (kept.length > 1 || kept.length === 1 && rest.pattern !== void 0) return {
|
|
5098
|
+
...rest,
|
|
5099
|
+
allOf: kept
|
|
5100
|
+
};
|
|
5101
|
+
return kept.length === 1 ? {
|
|
5102
|
+
...rest,
|
|
5103
|
+
pattern: kept[0].pattern
|
|
5104
|
+
} : rest;
|
|
5105
|
+
}
|
|
5106
|
+
const out = { ...rest };
|
|
5107
|
+
for (const { pattern } of allOf) if (out.pattern === void 0 && allowed("pattern", pattern)) out.pattern = pattern;
|
|
5108
|
+
else dropped.push(["pattern", pattern]);
|
|
5109
|
+
return out;
|
|
5110
|
+
};
|
|
5111
|
+
const out = rewrite(schema, "");
|
|
5112
|
+
if (out.$defs) {
|
|
5113
|
+
const used = reachable(refsOf(out), edgesOf(out.$defs));
|
|
5114
|
+
for (const name of Object.keys(out.$defs)) if (!used.has(name)) delete out.$defs[name];
|
|
5115
|
+
if (Object.keys(out.$defs).length === 0) delete out.$defs;
|
|
5116
|
+
}
|
|
5117
|
+
return out;
|
|
5118
|
+
}
|
|
5119
|
+
|
|
4637
5120
|
//#endregion
|
|
4638
5121
|
//#region packages/typescript/src/runtime/traverse.ts
|
|
4639
5122
|
/**
|
|
@@ -4713,6 +5196,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4713
5196
|
* @param opts - {@link TJsonSchemaOptions}.
|
|
4714
5197
|
* @returns A JSON Schema object.
|
|
4715
5198
|
*/ function buildJsonSchema(type, opts) {
|
|
5199
|
+
if (opts?.strict) return strictSchema(buildJsonSchema(type, {
|
|
5200
|
+
...opts,
|
|
5201
|
+
strict: void 0,
|
|
5202
|
+
closed: true
|
|
5203
|
+
}), opts.strict);
|
|
4716
5204
|
const closed = opts?.closed === true;
|
|
4717
5205
|
const annotations = opts?.annotations !== false;
|
|
4718
5206
|
const enums = opts?.enums !== false;
|
|
@@ -5562,6 +6050,91 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5562
6050
|
return out;
|
|
5563
6051
|
}
|
|
5564
6052
|
|
|
6053
|
+
//#endregion
|
|
6054
|
+
//#region packages/typescript/src/runtime/drop-optional-nulls.ts
|
|
6055
|
+
/**
|
|
6056
|
+
* What `value` looks like without the `null` values the model sends for fields it leaves out under
|
|
6057
|
+
* a `requireAll` profile (`OPENAI_STRICT`), before it goes to the validator. The validator
|
|
6058
|
+
* takes a missing optional field, but not `null` on an optional `@meta.required` one.
|
|
6059
|
+
*
|
|
6060
|
+
* A `null` is dropped only from an optional field whose type does not accept `null`: for
|
|
6061
|
+
* `x?: string | null` it stays (`null` and a missing field mean different things, e.g. "clear"
|
|
6062
|
+
* and "keep" in a patch). In a union, the branch its discriminator picks applies; else every
|
|
6063
|
+
* branch does, and a key is dropped only when every branch (and, in an intersection, every part)
|
|
6064
|
+
* that declares it has it optional and not accepting `null`: one that needs the `null` keeps it.
|
|
6065
|
+
*
|
|
6066
|
+
* Copy-on-change: `value` is never mutated (a frozen one works); the same reference comes back
|
|
6067
|
+
* when nothing is dropped, else a copy with the changed objects and arrays rebuilt.
|
|
6068
|
+
*/ function dropOptionalNulls(type, value) {
|
|
6069
|
+
return dropNulls([type], value);
|
|
6070
|
+
}
|
|
6071
|
+
/** The discriminator of a union, found once per union type. */ const discriminators = /* @__PURE__ */ new WeakMap();
|
|
6072
|
+
function discriminatorOf(union) {
|
|
6073
|
+
let disc = discriminators.get(union);
|
|
6074
|
+
if (disc === void 0) {
|
|
6075
|
+
disc = detectDiscriminator(union.items);
|
|
6076
|
+
discriminators.set(union, disc);
|
|
6077
|
+
}
|
|
6078
|
+
return disc;
|
|
6079
|
+
}
|
|
6080
|
+
/**
|
|
6081
|
+
* The defs `value` is checked against, unions and intersections expanded: the branch a
|
|
6082
|
+
* discriminator picks, else every branch.
|
|
6083
|
+
*/ function expand(defs, value, out) {
|
|
6084
|
+
for (const def of defs) {
|
|
6085
|
+
const type = def.type;
|
|
6086
|
+
if (type.kind === "union" || type.kind === "intersection") {
|
|
6087
|
+
const union = type;
|
|
6088
|
+
const isObject = value !== null && typeof value === "object" && !Array.isArray(value);
|
|
6089
|
+
const disc = type.kind === "union" && isObject ? discriminatorOf(union) : null;
|
|
6090
|
+
const picked = disc ? disc.indexMapping[String(value[disc.propertyName])] : void 0;
|
|
6091
|
+
expand(picked === void 0 ? union.items : [union.items[picked]], value, out);
|
|
6092
|
+
} else out.push(def);
|
|
6093
|
+
}
|
|
6094
|
+
}
|
|
6095
|
+
/** {@link dropOptionalNulls} of `value` as every one of `defs` (alternatives or parts). */ function dropNulls(defs, value) {
|
|
6096
|
+
if (value === null || typeof value !== "object") return value;
|
|
6097
|
+
const leaves = [];
|
|
6098
|
+
expand(defs, value, leaves);
|
|
6099
|
+
if (Array.isArray(value)) return dropNullsOfArray(leaves, value);
|
|
6100
|
+
let out;
|
|
6101
|
+
for (const key of Object.keys(value)) {
|
|
6102
|
+
const current = value[key];
|
|
6103
|
+
if (current !== null && typeof current !== "object") continue;
|
|
6104
|
+
const declared = [];
|
|
6105
|
+
for (const leaf of leaves) {
|
|
6106
|
+
const prop = leaf.type.kind === "object" ? leaf.type.props.get(key) : void 0;
|
|
6107
|
+
if (prop !== void 0 && !isPhantomType(prop)) declared.push(prop);
|
|
6108
|
+
}
|
|
6109
|
+
if (declared.length === 0) continue;
|
|
6110
|
+
if (current === null) {
|
|
6111
|
+
if (declared.every((prop) => prop.optional && !acceptsNull$2(prop))) {
|
|
6112
|
+
out ?? (out = { ...value });
|
|
6113
|
+
delete out[key];
|
|
6114
|
+
}
|
|
6115
|
+
continue;
|
|
6116
|
+
}
|
|
6117
|
+
const next = dropNulls(declared, current);
|
|
6118
|
+
if (next !== current) {
|
|
6119
|
+
out ?? (out = { ...value });
|
|
6120
|
+
out[key] = next;
|
|
6121
|
+
}
|
|
6122
|
+
}
|
|
6123
|
+
return out ?? value;
|
|
6124
|
+
}
|
|
6125
|
+
/** {@link dropNulls} of an array `value`: the items of every array and tuple among `leaves`. */ function dropNullsOfArray(leaves, value) {
|
|
6126
|
+
return mapList(value, (item, i) => {
|
|
6127
|
+
if (item === null || typeof item !== "object") return item;
|
|
6128
|
+
const declared = [];
|
|
6129
|
+
for (const leaf of leaves) {
|
|
6130
|
+
const type = leaf.type;
|
|
6131
|
+
if (type.kind === "array") declared.push(type.of);
|
|
6132
|
+
else if (type.kind === "tuple" && i < type.items.length) declared.push(type.items[i]);
|
|
6133
|
+
}
|
|
6134
|
+
return dropNulls(declared, item);
|
|
6135
|
+
});
|
|
6136
|
+
}
|
|
6137
|
+
|
|
5565
6138
|
//#endregion
|
|
5566
6139
|
//#region packages/typescript/src/runtime/default-value.ts
|
|
5567
6140
|
/**
|
|
@@ -6580,6 +7153,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6580
7153
|
texts
|
|
6581
7154
|
};
|
|
6582
7155
|
}
|
|
7156
|
+
/**
|
|
7157
|
+
* Translates the value of one metadata key (live, as a compiled type holds it — not serialized)
|
|
7158
|
+
* with `t`, by the rules {@link translateServed} applies to serialized metadata, so a client
|
|
7159
|
+
* translating compiled types reads one catalogue with the server: a text key's string (each item
|
|
7160
|
+
* of a `multiple` list; the named fields of a value object, e.g. `message` of `meta.required`) is
|
|
7161
|
+
* replaced by `t(text)` — except a primitive's built-in format message, which is reported by its
|
|
7162
|
+
* code. Returns `value` itself when the key holds no user text or nothing is translated; it is
|
|
7163
|
+
* never changed.
|
|
7164
|
+
*
|
|
7165
|
+
* Expressions are left as they are: their string literals are translated in a served type's
|
|
7166
|
+
* `texts`, and a client translates what an expression returns (by exact match), not its source.
|
|
7167
|
+
*
|
|
7168
|
+
* @example
|
|
7169
|
+
* ```ts
|
|
7170
|
+
* const label = translateUserText('meta.label', 'Street', text => dict[text]) // dict['Street']
|
|
7171
|
+
* ```
|
|
7172
|
+
*/ function translateUserText(key, value, t) {
|
|
7173
|
+
const mode = userTextOf(key);
|
|
7174
|
+
return mode === void 0 ? value : walk(value, mode, { t });
|
|
7175
|
+
}
|
|
6583
7176
|
/** `value` translated as `mode` says; the `metadata` objects within it translated too. */ function walk(value, mode, ctx) {
|
|
6584
7177
|
if (Array.isArray(value)) return mapList(value, (item) => walk(item, mode === "metadata" ? void 0 : mode, ctx));
|
|
6585
7178
|
if (typeof value === "string") {
|
|
@@ -6588,6 +7181,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6588
7181
|
return value;
|
|
6589
7182
|
}
|
|
6590
7183
|
if (typeof value !== "object" || value === null) return value;
|
|
7184
|
+
if (ctx.texts === void 0 && typeof mode !== "object") return value;
|
|
6591
7185
|
return mapEntries(value, (key, item) => walk(item, modeOf(key, mode), ctx));
|
|
6592
7186
|
}
|
|
6593
7187
|
/**
|
|
@@ -6602,10 +7196,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6602
7196
|
const out = ctx.t(text);
|
|
6603
7197
|
return typeof out === "string" ? out : void 0;
|
|
6604
7198
|
}
|
|
6605
|
-
/** Adds the translated string literals of expression `source` to `ctx.texts
|
|
6606
|
-
|
|
7199
|
+
/** Adds the translated string literals of expression `source` to `ctx.texts` (a served type only). */ function collectLiterals(source, ctx) {
|
|
7200
|
+
const { texts } = ctx;
|
|
7201
|
+
if (!texts) return;
|
|
7202
|
+
for (const literal of stringLiterals(source)) if (!Object.hasOwn(texts, literal)) {
|
|
6607
7203
|
const translated = translate(literal, ctx);
|
|
6608
|
-
if (translated !== void 0 && translated !== literal)
|
|
7204
|
+
if (translated !== void 0 && translated !== literal) texts[literal] = translated;
|
|
6609
7205
|
}
|
|
6610
7206
|
}
|
|
6611
7207
|
/** Single-character escapes and what they stand for (a line continuation stands for nothing). */ const ESCAPES = {
|
|
@@ -7694,7 +8290,9 @@ exports.$ps = $ps;
|
|
|
7694
8290
|
exports.$q = $q;
|
|
7695
8291
|
exports.$sh = $sh;
|
|
7696
8292
|
exports.$ut = $ut;
|
|
8293
|
+
exports.ANTHROPIC_STRICT = ANTHROPIC_STRICT;
|
|
7697
8294
|
exports.LayerMap = LayerMap;
|
|
8295
|
+
exports.OPENAI_STRICT = OPENAI_STRICT;
|
|
7698
8296
|
exports.SERIALIZE_VERSION = SERIALIZE_VERSION;
|
|
7699
8297
|
exports.VALIDATOR_ERROR_CODES = VALIDATOR_ERROR_CODES;
|
|
7700
8298
|
exports.VALIDATOR_MESSAGES = VALIDATOR_MESSAGES;
|
|
@@ -7712,6 +8310,7 @@ exports.createDataFromAnnotatedType = createDataFromAnnotatedType;
|
|
|
7712
8310
|
exports.defineAnnotatedType = defineAnnotatedType;
|
|
7713
8311
|
exports.deserializeAnnotatedType = deserializeAnnotatedType;
|
|
7714
8312
|
exports.detectDiscriminator = detectDiscriminator;
|
|
8313
|
+
exports.dropOptionalNulls = dropOptionalNulls;
|
|
7715
8314
|
exports.flattenAnnotatedType = flattenAnnotatedType;
|
|
7716
8315
|
exports.forAnnotatedType = forAnnotatedType;
|
|
7717
8316
|
exports.formatValidatorMessage = formatValidatorMessage;
|
|
@@ -7733,6 +8332,7 @@ exports.registerUserText = registerUserText;
|
|
|
7733
8332
|
exports.serializeAnnotatedType = serializeAnnotatedType;
|
|
7734
8333
|
exports.throwFeatureDisabled = throwFeatureDisabled;
|
|
7735
8334
|
exports.translateServed = translateServed;
|
|
8335
|
+
exports.translateUserText = translateUserText;
|
|
7736
8336
|
exports.typeHasNormalizers = typeHasNormalizers;
|
|
7737
8337
|
exports.unionDiscriminator = unionDiscriminator;
|
|
7738
8338
|
exports.userTextKeys = userTextKeys;
|