@atscript/typescript 0.2.9 → 0.2.11
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-e4R5zAfw.cjs → plugin-BNbjc9-d.cjs} +1 -1
- package/dist/{plugin-0ZgIMt1Y.mjs → plugin-CnraKYN3.mjs} +1 -1
- package/dist/realm-runtime.js +477 -10
- 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-C9ov-aSv.cjs → user-text-BrP_qD3T.cjs} +22 -4
- package/dist/{user-text-fz-TpY_H.mjs → user-text-DS0lorlV.mjs} +22 -4
- package/dist/utils.cjs +453 -4
- package/dist/utils.d.ts +104 -3
- package/dist/utils.mjs +451 -6
- 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) {
|
|
@@ -4679,7 +4697,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4679
4697
|
* `inherited`: the normalizer flags of the unions `def` is in.
|
|
4680
4698
|
*/ function holds(def, input, inherited = 0) {
|
|
4681
4699
|
if (input === void 0) return true;
|
|
4682
|
-
if (input === null) return def.optional === true || acceptsNull$
|
|
4700
|
+
if (input === null) return def.optional === true || acceptsNull$2(def);
|
|
4683
4701
|
const type = def.type;
|
|
4684
4702
|
switch (type.kind) {
|
|
4685
4703
|
case "": {
|
|
@@ -4767,6 +4785,338 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4767
4785
|
return result === NO_MATCH ? input : result;
|
|
4768
4786
|
}
|
|
4769
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
|
+
|
|
4770
5120
|
//#endregion
|
|
4771
5121
|
//#region packages/typescript/src/runtime/traverse.ts
|
|
4772
5122
|
/**
|
|
@@ -4846,6 +5196,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4846
5196
|
* @param opts - {@link TJsonSchemaOptions}.
|
|
4847
5197
|
* @returns A JSON Schema object.
|
|
4848
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);
|
|
4849
5204
|
const closed = opts?.closed === true;
|
|
4850
5205
|
const annotations = opts?.annotations !== false;
|
|
4851
5206
|
const enums = opts?.enums !== false;
|
|
@@ -5695,6 +6050,91 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5695
6050
|
return out;
|
|
5696
6051
|
}
|
|
5697
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
|
+
|
|
5698
6138
|
//#endregion
|
|
5699
6139
|
//#region packages/typescript/src/runtime/default-value.ts
|
|
5700
6140
|
/**
|
|
@@ -6713,6 +7153,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6713
7153
|
texts
|
|
6714
7154
|
};
|
|
6715
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
|
+
}
|
|
6716
7176
|
/** `value` translated as `mode` says; the `metadata` objects within it translated too. */ function walk(value, mode, ctx) {
|
|
6717
7177
|
if (Array.isArray(value)) return mapList(value, (item) => walk(item, mode === "metadata" ? void 0 : mode, ctx));
|
|
6718
7178
|
if (typeof value === "string") {
|
|
@@ -6721,6 +7181,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6721
7181
|
return value;
|
|
6722
7182
|
}
|
|
6723
7183
|
if (typeof value !== "object" || value === null) return value;
|
|
7184
|
+
if (ctx.texts === void 0 && typeof mode !== "object") return value;
|
|
6724
7185
|
return mapEntries(value, (key, item) => walk(item, modeOf(key, mode), ctx));
|
|
6725
7186
|
}
|
|
6726
7187
|
/**
|
|
@@ -6735,10 +7196,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6735
7196
|
const out = ctx.t(text);
|
|
6736
7197
|
return typeof out === "string" ? out : void 0;
|
|
6737
7198
|
}
|
|
6738
|
-
/** Adds the translated string literals of expression `source` to `ctx.texts
|
|
6739
|
-
|
|
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)) {
|
|
6740
7203
|
const translated = translate(literal, ctx);
|
|
6741
|
-
if (translated !== void 0 && translated !== literal)
|
|
7204
|
+
if (translated !== void 0 && translated !== literal) texts[literal] = translated;
|
|
6742
7205
|
}
|
|
6743
7206
|
}
|
|
6744
7207
|
/** Single-character escapes and what they stand for (a line continuation stands for nothing). */ const ESCAPES = {
|
|
@@ -7827,7 +8290,9 @@ exports.$ps = $ps;
|
|
|
7827
8290
|
exports.$q = $q;
|
|
7828
8291
|
exports.$sh = $sh;
|
|
7829
8292
|
exports.$ut = $ut;
|
|
8293
|
+
exports.ANTHROPIC_STRICT = ANTHROPIC_STRICT;
|
|
7830
8294
|
exports.LayerMap = LayerMap;
|
|
8295
|
+
exports.OPENAI_STRICT = OPENAI_STRICT;
|
|
7831
8296
|
exports.SERIALIZE_VERSION = SERIALIZE_VERSION;
|
|
7832
8297
|
exports.VALIDATOR_ERROR_CODES = VALIDATOR_ERROR_CODES;
|
|
7833
8298
|
exports.VALIDATOR_MESSAGES = VALIDATOR_MESSAGES;
|
|
@@ -7845,6 +8310,7 @@ exports.createDataFromAnnotatedType = createDataFromAnnotatedType;
|
|
|
7845
8310
|
exports.defineAnnotatedType = defineAnnotatedType;
|
|
7846
8311
|
exports.deserializeAnnotatedType = deserializeAnnotatedType;
|
|
7847
8312
|
exports.detectDiscriminator = detectDiscriminator;
|
|
8313
|
+
exports.dropOptionalNulls = dropOptionalNulls;
|
|
7848
8314
|
exports.flattenAnnotatedType = flattenAnnotatedType;
|
|
7849
8315
|
exports.forAnnotatedType = forAnnotatedType;
|
|
7850
8316
|
exports.formatValidatorMessage = formatValidatorMessage;
|
|
@@ -7866,6 +8332,7 @@ exports.registerUserText = registerUserText;
|
|
|
7866
8332
|
exports.serializeAnnotatedType = serializeAnnotatedType;
|
|
7867
8333
|
exports.throwFeatureDisabled = throwFeatureDisabled;
|
|
7868
8334
|
exports.translateServed = translateServed;
|
|
8335
|
+
exports.translateUserText = translateUserText;
|
|
7869
8336
|
exports.typeHasNormalizers = typeHasNormalizers;
|
|
7870
8337
|
exports.unionDiscriminator = unionDiscriminator;
|
|
7871
8338
|
exports.userTextKeys = userTextKeys;
|
package/dist/test-utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
3
|
-
const require_user_text = require('./user-text-
|
|
2
|
+
const require_plugin = require('./plugin-BNbjc9-d.cjs');
|
|
3
|
+
const require_user_text = require('./user-text-BrP_qD3T.cjs');
|
|
4
4
|
let path = require("path");
|
|
5
5
|
path = require_plugin.__toESM(path);
|
|
6
6
|
let _atscript_core = require("@atscript/core");
|
|
@@ -39,6 +39,13 @@ let fs = require("fs");
|
|
|
39
39
|
* (interpreted or compiled) nor the literal-union shortcut, so it always runs the full
|
|
40
40
|
* error-collecting walk.
|
|
41
41
|
*/ var WalkValidator = class extends require_user_text.Validator {};
|
|
42
|
+
/** Freezes `value` and everything it holds (a spec of a function that must not mutate). */ function deepFreeze(value) {
|
|
43
|
+
if (typeof value === "object" && value !== null) {
|
|
44
|
+
for (const item of Object.values(value)) deepFreeze(item);
|
|
45
|
+
Object.freeze(value);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
42
49
|
/** A seeded random number generator (mulberry32) returning values in `[0, 1)`. */ function rng(seed) {
|
|
43
50
|
return () => {
|
|
44
51
|
seed = seed + 1831565813 | 0;
|
|
@@ -108,6 +115,7 @@ let fs = require("fs");
|
|
|
108
115
|
|
|
109
116
|
//#endregion
|
|
110
117
|
exports.WalkValidator = WalkValidator;
|
|
118
|
+
exports.deepFreeze = deepFreeze;
|
|
111
119
|
exports.denseSchema = denseSchema;
|
|
112
120
|
exports.prepareFixtures = prepareFixtures;
|
|
113
121
|
exports.rng = rng;
|