@arbol-ai/maximus 8.1.0 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics/analytics.d.ts +16 -16
- package/dist/analytics/analytics.js +3 -3
- package/dist/body.d.ts +2 -2
- package/dist/body.js +2 -1
- package/dist/capture/index.d.ts +1 -1
- package/dist/capture/index.js +3 -4
- package/dist/chunks/{define-app-BxGZdNmS.js → define-app-CQpWqqqE.js} +33 -14
- package/dist/chunks/destination-5xTX0b7E.js +65 -0
- package/dist/chunks/{digest-C7UGt760.js → digest-C51F1bhA.js} +1 -2
- package/dist/chunks/{errors-CWgE9uvM.js → errors-BoutSmiy.js} +2 -3
- package/dist/chunks/{execution-BTiAWA4n.js → execution-Bpq6s4nA.js} +9 -8
- package/dist/chunks/{generate-C-9FKW0o.js → generate-Dg88LzLU.js} +1 -1
- package/dist/chunks/{handler-iebR-urt.js → handler-DSpyrWc-.js} +6 -2
- package/dist/chunks/{http-layout-BvlbD__1.js → http-layout-BcYH4I2w.js} +1 -1
- package/dist/chunks/{http-paths-DMBxwpp6.js → http-paths-D043DkJv.js} +50 -81
- package/dist/chunks/http-routes-B6tCsnxB.js +29 -0
- package/dist/chunks/{openapi-BF3eCpux.js → openapi-D2rds7vy.js} +27 -116
- package/dist/chunks/{openapi-file-RcXoq0AF.js → openapi-file-BK8zET5U.js} +24 -6
- package/dist/chunks/{pages-BCksUeRf.js → pages-B7ysoFAM.js} +1 -1
- package/dist/chunks/result-CF4OzG9O.js +29 -0
- package/dist/chunks/{runtime-D5eR9-Es.js → runtime-8OpxAvsI.js} +132 -7
- package/dist/chunks/schemas-_UJcx_uY.js +813 -0
- package/dist/chunks/{server-B9C8KPwa.js → server-CVhTLNj2.js} +37 -101
- package/dist/chunks/{src-C-4z443M.js → src-B_LthUVU.js} +39 -18
- package/dist/chunks/tasks-C873C3le.js +100 -0
- package/dist/chunks/tasks-DYdY-dBI.js +267 -0
- package/dist/chunks/{types-Dqo3oGig.d.ts → types-BfGa2aJT.d.ts} +346 -138
- package/dist/chunks/validation-CpzAxj2_.js +201 -0
- package/dist/chunks/{workers-types-BgjyDtGd.d.ts → workers-types-Cxf-5cH7.d.ts} +6 -1
- package/dist/client.d.ts +92 -0
- package/dist/client.js +300 -0
- package/dist/diagnostics.d.ts +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/http-paths.d.ts +1 -1
- package/dist/http-paths.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +11 -9
- package/dist/internals.d.ts +1 -1
- package/dist/internals.js +4 -4
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/tasks.d.ts +33 -0
- package/dist/mcp/tasks.js +3 -0
- package/dist/project/config.d.ts +2 -2
- package/dist/project/config.js +3 -2
- package/dist/project/index.d.ts +2 -2
- package/dist/project/index.js +3 -3
- package/dist/schemas.d.ts +2 -2
- package/dist/schemas.js +3 -3
- package/dist/standard-failures.d.ts +1 -1
- package/dist/tooling/cli.js +2 -2
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +6 -5
- package/dist/webhooks.d.ts +13 -2
- package/dist/webhooks.js +38 -1
- package/dist/workers/runtime.d.ts +1 -1
- package/dist/workers/runtime.js +1 -1
- package/package.json +15 -3
- package/skills/maximus/SKILL.md +7 -4
- package/skills/maximus/references/routes-and-tools.md +9 -1
- package/dist/chunks/result-CKgZHrnj.js +0 -56
- package/dist/chunks/schemas-BOFxZP1c.js +0 -227
- package/dist/chunks/validation-DMu0rkMQ.js +0 -670
|
@@ -1,670 +0,0 @@
|
|
|
1
|
-
import { t as isRecord } from "./record-DQUDTpEA.js";
|
|
2
|
-
import { FormatRegistry, Kind, TransformKind, Type } from "@sinclair/typebox";
|
|
3
|
-
import { AssertError, HasTransform, TransformDecode, TransformDecodeError, Value } from "@sinclair/typebox/value";
|
|
4
|
-
import { fullFormats } from "ajv-formats/dist/formats.js";
|
|
5
|
-
|
|
6
|
-
// src/internal/schema-markers.ts
|
|
7
|
-
/**
|
|
8
|
-
* The markers input codecs leave on a schema, which the codecs and the reader of secret fields share.
|
|
9
|
-
*/
|
|
10
|
-
/** Whether an object drops unknown keys when read with `stripUnknown`. */
|
|
11
|
-
const STRIP = Symbol("maximus.schema.strip");
|
|
12
|
-
/** The source schema of a `pipeSchema`, which the wire value is first read as. */
|
|
13
|
-
const SOURCE = Symbol("maximus.schema.source");
|
|
14
|
-
|
|
15
|
-
// src/secrets.ts
|
|
16
|
-
/** What `visit` answers for a value that must not stay. */
|
|
17
|
-
const REMOVED = Symbol("maximus.schema.removed");
|
|
18
|
-
/** Stands for a schema that says nothing about a value, such as an unknown key of a loose object. */
|
|
19
|
-
const UNDESCRIBED = Object.freeze({});
|
|
20
|
-
/** Each schema's tree, read on first use. */
|
|
21
|
-
const trees = /* @__PURE__ */ new WeakMap();
|
|
22
|
-
/** Each `patternProperties` key, compiled once: patterns come from schemas, which are finite. */
|
|
23
|
-
const patterns = /* @__PURE__ */ new Map();
|
|
24
|
-
/** A keyword's value as a list of schemas. */
|
|
25
|
-
function schemaList(value) {
|
|
26
|
-
return Array.isArray(value) ? value : [];
|
|
27
|
-
}
|
|
28
|
-
/** A keyword's value as schemas by name. */
|
|
29
|
-
function schemaMap(value) {
|
|
30
|
-
return isRecord(value) ? value : {};
|
|
31
|
-
}
|
|
32
|
-
/** The schemas directly below a schema, whichever keyword holds them. */
|
|
33
|
-
function childSchemas(node) {
|
|
34
|
-
return [
|
|
35
|
-
...Object.values(schemaMap(node.properties)),
|
|
36
|
-
...Object.values(schemaMap(node.patternProperties)),
|
|
37
|
-
...Object.values(schemaMap(node.$defs)),
|
|
38
|
-
node.additionalProperties,
|
|
39
|
-
node.additionalItems,
|
|
40
|
-
...[node.items].flat(),
|
|
41
|
-
...schemaList(node.anyOf),
|
|
42
|
-
...schemaList(node.oneOf),
|
|
43
|
-
...schemaList(node.allOf),
|
|
44
|
-
node[SOURCE]
|
|
45
|
-
];
|
|
46
|
-
}
|
|
47
|
-
/** A schema's tree: what `$ref` may name in it, and whether it marks anything secret. */
|
|
48
|
-
function treeOf(schema) {
|
|
49
|
-
const known = trees.get(schema);
|
|
50
|
-
if (known) return known;
|
|
51
|
-
const definitions = /* @__PURE__ */ new Map();
|
|
52
|
-
/** Adds one more definition of a name; two of one name both apply. */
|
|
53
|
-
const define = (name, node) => {
|
|
54
|
-
definitions.set(name, [...definitions.get(name) ?? [], node]);
|
|
55
|
-
};
|
|
56
|
-
let secret = false;
|
|
57
|
-
const seen = /* @__PURE__ */ new Set();
|
|
58
|
-
const pending = [schema];
|
|
59
|
-
while (pending.length > 0) {
|
|
60
|
-
const node = pending.pop();
|
|
61
|
-
if (!isRecord(node) || seen.has(node)) continue;
|
|
62
|
-
seen.add(node);
|
|
63
|
-
secret ||= node.secret === true;
|
|
64
|
-
if (typeof node.$id === "string") define(node.$id, node);
|
|
65
|
-
for (const [name, definition] of Object.entries(schemaMap(node.$defs))) if (isRecord(definition)) define(name, definition);
|
|
66
|
-
pending.push(...childSchemas(node));
|
|
67
|
-
}
|
|
68
|
-
const tree = {
|
|
69
|
-
definitions,
|
|
70
|
-
secret
|
|
71
|
-
};
|
|
72
|
-
trees.set(schema, tree);
|
|
73
|
-
return tree;
|
|
74
|
-
}
|
|
75
|
-
/** Whether a schema marks any value secret, at any depth. */
|
|
76
|
-
function holdsSecrets(schema) {
|
|
77
|
-
return treeOf(schema).secret;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Adds to `leaves` the schemas a value of `schema` may match, through every reference and union member, and
|
|
81
|
-
* answers false when one of them marks the value secret or names nothing this tree defines: a value that
|
|
82
|
-
* cannot be shown to be clean is removed.
|
|
83
|
-
*/
|
|
84
|
-
function collectLeaves(schema, tree, leaves, seen) {
|
|
85
|
-
if (seen.has(schema) || !isRecord(schema)) return true;
|
|
86
|
-
seen.add(schema);
|
|
87
|
-
const node = schema;
|
|
88
|
-
if (node.secret === true) return false;
|
|
89
|
-
const reference = typeof node.$ref === "string" ? node.$ref.replace("#/$defs/", "") : void 0;
|
|
90
|
-
const targets = reference === void 0 ? [] : tree.definitions.get(reference) ?? [];
|
|
91
|
-
if (reference !== void 0 && targets.length === 0) return false;
|
|
92
|
-
const members = [
|
|
93
|
-
...targets,
|
|
94
|
-
...schemaList(node.anyOf),
|
|
95
|
-
...schemaList(node.oneOf),
|
|
96
|
-
...schemaList(node.allOf)
|
|
97
|
-
];
|
|
98
|
-
if (!members.every((member) => collectLeaves(member, tree, leaves, seen))) return false;
|
|
99
|
-
if (members.length === 0 || node.type !== void 0 || node.properties !== void 0 || node.patternProperties !== void 0) leaves.push(node);
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
/** Whether a schema's `type` admits a value. A TypeBox type outside JSON, such as `Date`, admits anything. */
|
|
103
|
-
function typeAdmits(type, value) {
|
|
104
|
-
switch (type) {
|
|
105
|
-
case "string":
|
|
106
|
-
case "boolean": return typeof value === type;
|
|
107
|
-
case "number": return typeof value === "number";
|
|
108
|
-
case "integer": return Number.isInteger(value);
|
|
109
|
-
case "null": return value === null;
|
|
110
|
-
case "array": return Array.isArray(value);
|
|
111
|
-
case "object": return isRecord(value);
|
|
112
|
-
default: return true;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/** Whether a schema holds no opinion on what is inside a value: nothing below it can be marked. */
|
|
116
|
-
function describesNothing(leaf) {
|
|
117
|
-
return leaf.properties === void 0 && leaf.patternProperties === void 0 && leaf.items === void 0 && (leaf.additionalProperties === void 0 || leaf.additionalProperties === true) && leaf[STRIP] !== true;
|
|
118
|
-
}
|
|
119
|
-
/** The schemas of an array's element at `position`: a tuple's position, or a list's one schema of items. */
|
|
120
|
-
function itemSchemas(leaf, position) {
|
|
121
|
-
if (!Array.isArray(leaf.items)) return leaf.items === void 0 ? [UNDESCRIBED] : [leaf.items];
|
|
122
|
-
if (position < leaf.items.length) return [leaf.items[position]];
|
|
123
|
-
return isRecord(leaf.additionalItems) ? [leaf.additionalItems] : [];
|
|
124
|
-
}
|
|
125
|
-
/** A copy of an array without what its schemas mark: a list loses the element, a tuple keeps its positions. */
|
|
126
|
-
function visitArray(leaves, value, tree) {
|
|
127
|
-
const tuple = leaves.some((leaf) => Array.isArray(leaf.items));
|
|
128
|
-
const kept = [];
|
|
129
|
-
for (const [position, element] of value.entries()) {
|
|
130
|
-
const schemas = leaves.flatMap((leaf) => itemSchemas(leaf, position));
|
|
131
|
-
const visited = schemas.length ? visit(schemas, element, tree) : REMOVED;
|
|
132
|
-
if (visited !== REMOVED) kept.push(visited);
|
|
133
|
-
else if (tuple) kept.push(null);
|
|
134
|
-
}
|
|
135
|
-
return kept;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* The schemas one object schema gives a key: its property and the patterns the key matches, else its
|
|
139
|
-
* additional properties. None when the schema refuses the key: a strict object, or one that strips unknown
|
|
140
|
-
* keys.
|
|
141
|
-
*/
|
|
142
|
-
function fieldSchemas(leaf, key) {
|
|
143
|
-
const declared = [...Object.hasOwn(schemaMap(leaf.properties), key) ? [schemaMap(leaf.properties)[key]] : [], ...Object.entries(schemaMap(leaf.patternProperties)).filter(([pattern]) => compiled(pattern).test(key)).map(([, schema]) => schema)];
|
|
144
|
-
if (declared.length) return declared;
|
|
145
|
-
if (leaf.additionalProperties === false || leaf[STRIP] === true) return [];
|
|
146
|
-
return isRecord(leaf.additionalProperties) ? [leaf.additionalProperties] : [UNDESCRIBED];
|
|
147
|
-
}
|
|
148
|
-
/** A pattern of `patternProperties`, compiled once and without flags, as TypeBox compiles it to check a value. */
|
|
149
|
-
function compiled(pattern) {
|
|
150
|
-
let expression = patterns.get(pattern);
|
|
151
|
-
if (!expression) {
|
|
152
|
-
expression = new RegExp(pattern);
|
|
153
|
-
patterns.set(pattern, expression);
|
|
154
|
-
}
|
|
155
|
-
return expression;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* A copy of an object without what its schemas mark, nor a key every schema refuses. `Object.fromEntries`
|
|
159
|
-
* keeps an own `__proto__` key a plain key, where assigning it would set the copy's prototype.
|
|
160
|
-
*/
|
|
161
|
-
function visitObject(leaves, value, tree) {
|
|
162
|
-
const kept = [];
|
|
163
|
-
for (const [key, field] of Object.entries(value)) {
|
|
164
|
-
const schemas = leaves.flatMap((leaf) => fieldSchemas(leaf, key));
|
|
165
|
-
const visited = schemas.length ? visit(schemas, field, tree) : REMOVED;
|
|
166
|
-
if (visited !== REMOVED) kept.push([key, visited]);
|
|
167
|
-
}
|
|
168
|
-
return Object.fromEntries(kept);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* A value as these schemas leave it: `REMOVED` when one marks it or none admits it, and otherwise a copy of
|
|
172
|
-
* what they describe. Every schema that may match applies, so a field one union member marks is removed
|
|
173
|
-
* whichever member the value belongs to.
|
|
174
|
-
*/
|
|
175
|
-
function visit(schemas, value, tree) {
|
|
176
|
-
const leaves = [];
|
|
177
|
-
const seen = /* @__PURE__ */ new Set();
|
|
178
|
-
if (!schemas.every((schema) => collectLeaves(schema, tree, leaves, seen))) return REMOVED;
|
|
179
|
-
const admitting = leaves.filter((leaf) => typeAdmits(leaf.type, value));
|
|
180
|
-
if (admitting.length === 0) return REMOVED;
|
|
181
|
-
if (admitting.every(describesNothing)) return value;
|
|
182
|
-
if (Array.isArray(value)) return visitArray(admitting, value, tree);
|
|
183
|
-
return isRecord(value) ? visitObject(admitting, value, tree) : value;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* A copy of a value without every field its schema marks with `secret: true`, at any depth: through objects,
|
|
187
|
-
* records, arrays, tuples, unions, intersections, optional fields and references. The input is never changed.
|
|
188
|
-
* A marked list element goes with its value, a marked tuple position becomes `null`, and a schema marked at
|
|
189
|
-
* its root answers `undefined`.
|
|
190
|
-
*
|
|
191
|
-
* Pass a value of the shape the schema describes: what it checked, before any codec ran. It fails closed where
|
|
192
|
-
* a schema cannot speak for a value: a name marked in any member of a union is removed from all of them, a
|
|
193
|
-
* key a strict object refuses is removed, as is a value of another type, or one under a `$ref` the schema does
|
|
194
|
-
* not define.
|
|
195
|
-
*/
|
|
196
|
-
function withoutSecrets(schema, value) {
|
|
197
|
-
const kept = visit([schema], value, treeOf(schema));
|
|
198
|
-
return kept === REMOVED ? void 0 : kept;
|
|
199
|
-
}
|
|
200
|
-
/** Keywords whose values are data, never schemas: `secret` inside one is the data's own. */
|
|
201
|
-
const DATA_KEYWORDS = /* @__PURE__ */ new Set([
|
|
202
|
-
"const",
|
|
203
|
-
"default",
|
|
204
|
-
"enum",
|
|
205
|
-
"example",
|
|
206
|
-
"examples"
|
|
207
|
-
]);
|
|
208
|
-
/** Keywords whose values are named schemas: `secret` directly inside one is a field's name. */
|
|
209
|
-
const NAMED_SCHEMAS = /* @__PURE__ */ new Set([
|
|
210
|
-
"properties",
|
|
211
|
-
"patternProperties",
|
|
212
|
-
"$defs"
|
|
213
|
-
]);
|
|
214
|
-
/**
|
|
215
|
-
* A copy of a published schema or document without the `secret` keyword. It tells capture what to leave out,
|
|
216
|
-
* and no client needs it: marking a field changes no OpenAPI document and no tool listing.
|
|
217
|
-
*/
|
|
218
|
-
function withoutSecretKeyword(node, named = false) {
|
|
219
|
-
if (Array.isArray(node)) return node.map((item) => withoutSecretKeyword(item));
|
|
220
|
-
if (!isRecord(node)) return node;
|
|
221
|
-
const kept = [];
|
|
222
|
-
for (const [key, child] of Object.entries(node)) if (named) kept.push([key, withoutSecretKeyword(child)]);
|
|
223
|
-
else if (DATA_KEYWORDS.has(key) || key.startsWith("x-")) kept.push([key, child]);
|
|
224
|
-
else if (!(key === "secret" && child === true)) kept.push([key, withoutSecretKeyword(child, NAMED_SCHEMAS.has(key))]);
|
|
225
|
-
return Object.fromEntries(kept);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// src/input-schemas.ts
|
|
229
|
-
/**
|
|
230
|
-
* Owner codecs on TypeBox schemas: defaults, normalizers, transforms, inspections, refinements, pipes and
|
|
231
|
-
* unknown-key stripping. `inputSchema()` compiles them into one input schema, so the wire is checked as
|
|
232
|
-
* declared and the handler receives the decoded value.
|
|
233
|
-
*/
|
|
234
|
-
/** Every issue of a refused value, as `readSchema` throws it and `checkSchema` returns it. */
|
|
235
|
-
var SchemaValidationError = class extends TypeError {
|
|
236
|
-
/** Every issue of the refused value; the error's message is the first one's. */
|
|
237
|
-
issues;
|
|
238
|
-
constructor(issues) {
|
|
239
|
-
super(issues[0]?.message ?? "The input does not match its schema.");
|
|
240
|
-
this.name = "SchemaValidationError";
|
|
241
|
-
this.issues = issues;
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
/** The ordered codec steps a schema runs inside `inputSchema()`. */
|
|
245
|
-
const STEPS = Symbol("maximus.schema.steps");
|
|
246
|
-
/** The transforms a schema applies after its source value is valid. */
|
|
247
|
-
const TRANSFORMERS = Symbol("maximus.schema.transformers");
|
|
248
|
-
/** The normalizers a schema applies before its own checks. */
|
|
249
|
-
const NORMALIZERS = Symbol("maximus.schema.normalizers");
|
|
250
|
-
/** The inspections and refinements a schema runs on a valid value. */
|
|
251
|
-
const INSPECTORS = Symbol("maximus.schema.inspectors");
|
|
252
|
-
/** A union of literals: `literalUnion(['open', 'done'])`. */
|
|
253
|
-
const literalUnion = (values) => Type.Union(values.map((value) => Type.Literal(value)));
|
|
254
|
-
/**
|
|
255
|
-
* The schema with a default that decoding applies when the field is absent, so the field is optional on the
|
|
256
|
-
* wire.
|
|
257
|
-
*/
|
|
258
|
-
const defaultSchema = (schema, value) => ({
|
|
259
|
-
...schema,
|
|
260
|
-
default: value
|
|
261
|
-
});
|
|
262
|
-
/**
|
|
263
|
-
* A strict object that, read with `stripUnknown` (as `readSchema` does), drops unknown keys instead of
|
|
264
|
-
* refusing them. Wrap an input that uses it in `inputSchema()`.
|
|
265
|
-
*/
|
|
266
|
-
const stripObject = (properties) => ({
|
|
267
|
-
...Type.Object(properties, { additionalProperties: false }),
|
|
268
|
-
[STRIP]: true
|
|
269
|
-
});
|
|
270
|
-
/** The object, refusing unknown keys even where a read strips them. */
|
|
271
|
-
const strictObject = (schema) => ({
|
|
272
|
-
...schema,
|
|
273
|
-
additionalProperties: false,
|
|
274
|
-
[STRIP]: false
|
|
275
|
-
});
|
|
276
|
-
/** The object, admitting and keeping unknown keys. */
|
|
277
|
-
const looseObject = (schema) => ({
|
|
278
|
-
...schema,
|
|
279
|
-
additionalProperties: true,
|
|
280
|
-
[STRIP]: false
|
|
281
|
-
});
|
|
282
|
-
/** The object with more properties, keeping how it treats unknown keys. */
|
|
283
|
-
const extendObject = (schema, properties) => ({
|
|
284
|
-
...Type.Object({
|
|
285
|
-
...schema.properties,
|
|
286
|
-
...properties
|
|
287
|
-
}, { additionalProperties: schema.additionalProperties }),
|
|
288
|
-
[STRIP]: schema[STRIP]
|
|
289
|
-
});
|
|
290
|
-
/**
|
|
291
|
-
* Reads a field through `normalize`. For a string, or a string that may be null, the pattern, format and
|
|
292
|
-
* lengths apply to the normalized value, so the wire may send a raw spelling (another case, surrounding
|
|
293
|
-
* spaces); any other schema checks the raw value first. The wire type stays the schema's own. Runs inside
|
|
294
|
-
* `inputSchema()`.
|
|
295
|
-
*/
|
|
296
|
-
const normalizeSchema = (schema, normalize) => Type.Unsafe({
|
|
297
|
-
...schema,
|
|
298
|
-
[NORMALIZERS]: [...schema[NORMALIZERS] ?? [], normalize],
|
|
299
|
-
[STEPS]: [...schema[STEPS] ?? [], {
|
|
300
|
-
kind: "normalize",
|
|
301
|
-
apply: normalize
|
|
302
|
-
}]
|
|
303
|
-
});
|
|
304
|
-
/**
|
|
305
|
-
* Decodes a field with `transform` once its value passes `schema`: the wire sends what `schema` checks, and
|
|
306
|
-
* the handler receives what `transform` returns. Runs inside `inputSchema()`.
|
|
307
|
-
*/
|
|
308
|
-
const transformSchema = (schema, transform) => Type.Unsafe({
|
|
309
|
-
...schema,
|
|
310
|
-
[TRANSFORMERS]: [...schema[TRANSFORMERS] ?? [], transform],
|
|
311
|
-
[STEPS]: [...schema[STEPS] ?? [], {
|
|
312
|
-
kind: "transform",
|
|
313
|
-
apply: transform
|
|
314
|
-
}]
|
|
315
|
-
});
|
|
316
|
-
/**
|
|
317
|
-
* Refuses a canonical schema that marks a field `secret: true` when the wire schema it is read from marks
|
|
318
|
-
* nothing: capture reads the value the wire schema checked, where that mark would protect nothing.
|
|
319
|
-
*/
|
|
320
|
-
function requireSecretsOnWire(wire, canonical) {
|
|
321
|
-
if (holdsSecrets(canonical) && !holdsSecrets(wire)) throw new TypeError("A canonical schema marks a field secret: true and the wire schema it is read from does not; mark the field where the wire carries it.");
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Admits a value as `source`, then checks and decodes the result as `target`: a wire shape read into its
|
|
325
|
-
* canonical one. Runs inside `inputSchema()`. A `target` that marks a field `secret: true` needs `source` to
|
|
326
|
-
* mark it too, or it throws a TypeError.
|
|
327
|
-
*/
|
|
328
|
-
const pipeSchema = (source, target) => {
|
|
329
|
-
requireSecretsOnWire(source, target);
|
|
330
|
-
return {
|
|
331
|
-
...target,
|
|
332
|
-
[SOURCE]: source
|
|
333
|
-
};
|
|
334
|
-
};
|
|
335
|
-
/**
|
|
336
|
-
* Runs `inspect` on a value the schema admits; every `addIssue` refuses the value with that issue. Runs
|
|
337
|
-
* inside `inputSchema()`.
|
|
338
|
-
*/
|
|
339
|
-
const inspectSchema = (schema, inspect) => ({
|
|
340
|
-
...schema,
|
|
341
|
-
[INSPECTORS]: [...schema[INSPECTORS] ?? [], inspect],
|
|
342
|
-
[STEPS]: [...schema[STEPS] ?? [], {
|
|
343
|
-
kind: "inspect",
|
|
344
|
-
apply: inspect
|
|
345
|
-
}]
|
|
346
|
-
});
|
|
347
|
-
/**
|
|
348
|
-
* Refuses a value the schema admits when `predicate` rejects it, with a `custom` issue whose message and path
|
|
349
|
-
* `detail` gives (`Invalid value` at the field by default). Runs inside `inputSchema()`.
|
|
350
|
-
*/
|
|
351
|
-
const refineSchema = (schema, predicate, detail) => inspectSchema(schema, (value, context) => {
|
|
352
|
-
if (!predicate(value)) context.addIssue({
|
|
353
|
-
code: "custom",
|
|
354
|
-
message: typeof detail === "string" ? detail : detail?.message ?? detail?.error ?? "Invalid value",
|
|
355
|
-
path: typeof detail === "object" ? detail.path ?? [] : []
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
/**
|
|
359
|
-
* A URL, of at most `maxLength` characters when given, whose protocol (without its colon, such as `https`)
|
|
360
|
-
* and, optionally, hostname match the given patterns. Runs inside `inputSchema()`.
|
|
361
|
-
*/
|
|
362
|
-
const urlSchema = (options) => refineSchema(Type.String({
|
|
363
|
-
format: "uri",
|
|
364
|
-
...options.maxLength ? { maxLength: options.maxLength } : {}
|
|
365
|
-
}), (value) => {
|
|
366
|
-
try {
|
|
367
|
-
const url = new URL(value);
|
|
368
|
-
return options.protocol.test(url.protocol.slice(0, -1)) && (!options.hostname || options.hostname.test(url.hostname));
|
|
369
|
-
} catch {
|
|
370
|
-
return false;
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
/** Compile owner semantics into real native TypeBox source codecs. Canonical output schemas stay strict. */
|
|
374
|
-
const projectOwnerInput = (schema, stripUnknown) => {
|
|
375
|
-
if (schema[SOURCE]) {
|
|
376
|
-
const source = projectOwnerInput(schema[SOURCE], stripUnknown);
|
|
377
|
-
const targetSchema = {
|
|
378
|
-
...schema,
|
|
379
|
-
[SOURCE]: void 0
|
|
380
|
-
};
|
|
381
|
-
const target = projectOwnerInput(targetSchema, stripUnknown);
|
|
382
|
-
return Type.Transform(source).Decode((value) => {
|
|
383
|
-
const targetInput = Value.Default(target, value);
|
|
384
|
-
Value.Assert(target, targetInput);
|
|
385
|
-
return TransformDecode(target, [], targetInput);
|
|
386
|
-
}).Encode((value) => value);
|
|
387
|
-
}
|
|
388
|
-
const projected = { ...schema };
|
|
389
|
-
if (schema.properties) {
|
|
390
|
-
projected.properties = Object.fromEntries(Object.entries(schema.properties).map(([key, value]) => [key, projectOwnerInput(value, stripUnknown)]));
|
|
391
|
-
projected.required = schema.required?.filter((key) => !("default" in schema.properties[key]));
|
|
392
|
-
if (stripUnknown && schema[STRIP]) projected.additionalProperties = true;
|
|
393
|
-
}
|
|
394
|
-
if (schema.items) projected.items = Array.isArray(schema.items) ? schema.items.map((value) => projectOwnerInput(value, stripUnknown)) : projectOwnerInput(schema.items, stripUnknown);
|
|
395
|
-
if (schema.anyOf) projected.anyOf = schema.anyOf.map((value) => {
|
|
396
|
-
if ((schema[NORMALIZERS]?.length ?? 0) > 0 && value.type === "string") {
|
|
397
|
-
const source = { ...value };
|
|
398
|
-
delete source.pattern;
|
|
399
|
-
delete source.format;
|
|
400
|
-
delete source.minLength;
|
|
401
|
-
delete source.maxLength;
|
|
402
|
-
return projectOwnerInput(source, stripUnknown);
|
|
403
|
-
}
|
|
404
|
-
return projectOwnerInput(value, stripUnknown);
|
|
405
|
-
});
|
|
406
|
-
if (schema.patternProperties) projected.patternProperties = Object.fromEntries(Object.entries(schema.patternProperties).map(([key, value]) => [key, projectOwnerInput(value, stripUnknown)]));
|
|
407
|
-
const normalizers = schema[NORMALIZERS] ?? [];
|
|
408
|
-
if (schema.type === "string" && normalizers.length) {
|
|
409
|
-
delete projected.pattern;
|
|
410
|
-
delete projected.format;
|
|
411
|
-
delete projected.minLength;
|
|
412
|
-
delete projected.maxLength;
|
|
413
|
-
}
|
|
414
|
-
const transforms = schema[TRANSFORMERS] ?? [];
|
|
415
|
-
const inspectors = schema[INSPECTORS] ?? [];
|
|
416
|
-
const strip = stripUnknown && schema[STRIP] && schema.properties;
|
|
417
|
-
if (!(normalizers.length || transforms.length || inspectors.length || strip)) return projected;
|
|
418
|
-
return Type.Transform(projected).Decode((input) => {
|
|
419
|
-
let value = input;
|
|
420
|
-
if (strip && value !== null && typeof value === "object" && !Array.isArray(value)) value = Object.fromEntries(Object.entries(value).filter(([key]) => key in schema.properties));
|
|
421
|
-
const issues = [];
|
|
422
|
-
const steps = schema[STEPS] ?? [];
|
|
423
|
-
for (const [index, step] of steps.entries()) if (step.kind === "inspect") {
|
|
424
|
-
step.apply(value, { addIssue: (issue) => issues.push(issue) });
|
|
425
|
-
if (issues.length) throw new SchemaValidationError(issues);
|
|
426
|
-
} else {
|
|
427
|
-
value = step.apply(value);
|
|
428
|
-
if (step.kind === "normalize" && steps[index + 1]?.kind !== "normalize" && (schema.type === "string" || schema.anyOf?.every((branch) => branch.type === "string" || branch.type === "null")) && !Value.Check(schema, value)) throw new SchemaValidationError([{
|
|
429
|
-
code: "invalid_input",
|
|
430
|
-
message: "The normalized value does not match its schema.",
|
|
431
|
-
path: []
|
|
432
|
-
}]);
|
|
433
|
-
}
|
|
434
|
-
return value;
|
|
435
|
-
}).Encode((value) => value);
|
|
436
|
-
};
|
|
437
|
-
/**
|
|
438
|
-
* An input schema as callers send it, for the documents that publish it: a field with a default is optional,
|
|
439
|
-
* since decoding fills it in. A schema `inputSchema()` compiled is its wire form already.
|
|
440
|
-
*/
|
|
441
|
-
function wireInput(schema) {
|
|
442
|
-
return TransformKind in schema ? schema : projectOwnerInput(schema, false);
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Whether a schema carries owner codecs that only `inputSchema()` compiles: a normalizer, transform,
|
|
446
|
-
* inspector or refinement, a `pipeSchema` source, or a `stripObject`. A plain decode ignores their
|
|
447
|
-
* markers, so such a schema used directly as an operation input would admit raw, unnormalized and
|
|
448
|
-
* unrefined values without a word. A compiled subtree (a TypeBox transform) is already honest.
|
|
449
|
-
*/
|
|
450
|
-
function hasUncompiledCodecs(schema) {
|
|
451
|
-
const seen = /* @__PURE__ */ new Set();
|
|
452
|
-
/**
|
|
453
|
-
* Whether this schema, or any schema below it that no transform already compiled, carries a codec marker.
|
|
454
|
-
*/
|
|
455
|
-
const visit = (node) => {
|
|
456
|
-
if (!node || typeof node !== "object" || seen.has(node) || TransformKind in node) return false;
|
|
457
|
-
seen.add(node);
|
|
458
|
-
const owner = node;
|
|
459
|
-
if ((owner[STEPS]?.length ?? 0) > 0 || owner[SOURCE] || owner[STRIP] === true) return true;
|
|
460
|
-
return [
|
|
461
|
-
...Object.values(owner.properties ?? {}),
|
|
462
|
-
...Object.values(owner.patternProperties ?? {}),
|
|
463
|
-
...[owner.items].flat(),
|
|
464
|
-
...owner.anyOf ?? [],
|
|
465
|
-
...owner.allOf ?? [],
|
|
466
|
-
...owner.oneOf ?? []
|
|
467
|
-
].some(visit);
|
|
468
|
-
};
|
|
469
|
-
return visit(schema);
|
|
470
|
-
}
|
|
471
|
-
function inputSchema(schema, source) {
|
|
472
|
-
const wire = source && Kind in source ? source : void 0;
|
|
473
|
-
if (wire) requireSecretsOnWire(wire, schema);
|
|
474
|
-
const stripUnknown = !wire && source && "stripUnknown" in source && source.stripUnknown === true;
|
|
475
|
-
const projected = projectOwnerInput(schema, Boolean(stripUnknown));
|
|
476
|
-
return Type.Transform(Type.Unsafe(wire ? projectOwnerInput(wire, false) : projected)).Decode((input) => {
|
|
477
|
-
if (!wire) return input;
|
|
478
|
-
const value = Value.Default(projected, input);
|
|
479
|
-
Value.Assert(projected, value);
|
|
480
|
-
return TransformDecode(projected, [], value);
|
|
481
|
-
}).Encode((value) => value);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// src/internal/decode-issues.ts
|
|
485
|
-
/**
|
|
486
|
-
* Turns a failed TypeBox decode into field issues whose paths start at the input's root, as the
|
|
487
|
-
* INVALID_INPUT failure carries them.
|
|
488
|
-
*/
|
|
489
|
-
/** The segments of a JSON Pointer, unescaped. */
|
|
490
|
-
function segments(path) {
|
|
491
|
-
return path.split("/").slice(1).filter(Boolean).map((part) => part.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* The issues of a refused decode, at most `limit`, each with its path from the input's root, or null when
|
|
495
|
-
* nothing refused the input: an owner codec threw something other than a `SchemaValidationError`, which is
|
|
496
|
-
* the codec's fault for the caller to report. A codec nested in another reports its issues below the path
|
|
497
|
-
* the outer one decodes. TypeBox finds issues one at a time, so an input with millions of them costs no
|
|
498
|
-
* more than `limit`.
|
|
499
|
-
*/
|
|
500
|
-
function decodeIssues(error, limit = Number.POSITIVE_INFINITY) {
|
|
501
|
-
let cause = error;
|
|
502
|
-
const path = [];
|
|
503
|
-
while (cause instanceof TransformDecodeError) {
|
|
504
|
-
path.push(...segments(cause.path));
|
|
505
|
-
cause = cause.error;
|
|
506
|
-
}
|
|
507
|
-
if (cause instanceof SchemaValidationError) return cause.issues.slice(0, limit).map((issue) => ({
|
|
508
|
-
...issue,
|
|
509
|
-
path: [...path, ...issue.path]
|
|
510
|
-
}));
|
|
511
|
-
if (cause instanceof AssertError) {
|
|
512
|
-
const issues = [];
|
|
513
|
-
for (const issue of cause.Errors()) {
|
|
514
|
-
issues.push({
|
|
515
|
-
code: "invalid_input",
|
|
516
|
-
message: issue.message,
|
|
517
|
-
path: [...path, ...segments(issue.path)]
|
|
518
|
-
});
|
|
519
|
-
if (issues.length >= limit) break;
|
|
520
|
-
}
|
|
521
|
-
return issues;
|
|
522
|
-
}
|
|
523
|
-
return null;
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* What an owner codec threw, without the TypeBox error that wraps it: that wrapper carries the value being
|
|
527
|
-
* decoded, which a fault must never take with it to a log.
|
|
528
|
-
*/
|
|
529
|
-
function codecFault(error) {
|
|
530
|
-
let cause = error;
|
|
531
|
-
while (cause instanceof TransformDecodeError) cause = cause.error;
|
|
532
|
-
return cause;
|
|
533
|
-
}
|
|
534
|
-
/** RFC 6901 JSON Pointer of an issue path; the empty string names the whole input. */
|
|
535
|
-
function jsonPointer(path) {
|
|
536
|
-
return path.map((part) => `/${String(part).replaceAll("~", "~0").replaceAll("/", "~1")}`).join("");
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
// src/validation.ts
|
|
540
|
-
/**
|
|
541
|
-
* Schema validation: the check-only and the decoding validators, the guard that admits only bounded plain
|
|
542
|
-
* JSON, and the RFC format validators registered once with TypeBox.
|
|
543
|
-
*/
|
|
544
|
-
for (const name of [
|
|
545
|
-
"date",
|
|
546
|
-
"time",
|
|
547
|
-
"date-time",
|
|
548
|
-
"email",
|
|
549
|
-
"uuid",
|
|
550
|
-
"uri"
|
|
551
|
-
]) {
|
|
552
|
-
const format = fullFormats[name];
|
|
553
|
-
const validator = typeof format === "object" && !(format instanceof RegExp) ? format.validate : format;
|
|
554
|
-
FormatRegistry.Set(name, (value) => {
|
|
555
|
-
if (value.length > 8192) return false;
|
|
556
|
-
if (validator instanceof RegExp) return validator.test(value);
|
|
557
|
-
return typeof validator === "function" && validator(value) === true;
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Checks a value against a schema without decoding it: the value when it matches, else an `INVALID_INPUT`
|
|
562
|
-
* failure without issues. Use `decodeInput` for input with defaults and codecs.
|
|
563
|
-
*/
|
|
564
|
-
function parseInput(schema, input) {
|
|
565
|
-
if (Value.Check(schema, input)) return {
|
|
566
|
-
ok: true,
|
|
567
|
-
value: input
|
|
568
|
-
};
|
|
569
|
-
return {
|
|
570
|
-
ok: false,
|
|
571
|
-
error: {
|
|
572
|
-
code: "INVALID_INPUT",
|
|
573
|
-
message: "The input does not match its schema.",
|
|
574
|
-
retryable: false,
|
|
575
|
-
data: {}
|
|
576
|
-
}
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
|
-
/** Checks a value against a schema and returns it typed, or throws a TypeError that names no value. */
|
|
580
|
-
function parseSchema(schema, input) {
|
|
581
|
-
if (!Value.Check(schema, input)) throw new TypeError("Value does not match its schema");
|
|
582
|
-
return input;
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* Whether a value is a bounded plain JSON tree, which TypeBox's traversals expect and every transport admits:
|
|
586
|
-
* no cycles, class instances, functions, non-finite numbers, nesting beyond 100 levels or own `__proto__` key.
|
|
587
|
-
*/
|
|
588
|
-
function boundedInput(value, depth = 0, parents = /* @__PURE__ */ new Set()) {
|
|
589
|
-
if (depth > 100) return false;
|
|
590
|
-
if (value === void 0 || value === null || typeof value === "string" || typeof value === "boolean") return true;
|
|
591
|
-
if (typeof value === "number") return Number.isFinite(value);
|
|
592
|
-
if (typeof value !== "object" || parents.has(value)) return false;
|
|
593
|
-
if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) return false;
|
|
594
|
-
if (Object.hasOwn(value, "__proto__")) return false;
|
|
595
|
-
parents.add(value);
|
|
596
|
-
const valid = Object.values(value).every((child) => boundedInput(child, depth + 1, parents));
|
|
597
|
-
parents.delete(value);
|
|
598
|
-
return valid;
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Decodes input against its schema: admits only a bounded plain JSON tree, applies the declared defaults to a
|
|
602
|
-
* copy, checks it and runs its codecs. Throws a `SchemaValidationError` (or TypeBox's assertion error) when the
|
|
603
|
-
* input is refused, and whatever an owner codec throws otherwise. Output is checked, never decoded: what a
|
|
604
|
-
* handler returns must already be canonical.
|
|
605
|
-
*/
|
|
606
|
-
function decodeSchema(schema, input) {
|
|
607
|
-
return admitSchema(schema, input).value;
|
|
608
|
-
}
|
|
609
|
-
/** Decodes input as `decodeSchema` does, and also answers what the schema checked, before any codec ran. */
|
|
610
|
-
function admitSchema(schema, input) {
|
|
611
|
-
if (!boundedInput(input)) throw new SchemaValidationError([{
|
|
612
|
-
code: "invalid_input",
|
|
613
|
-
message: "Input must be a bounded plain JSON tree.",
|
|
614
|
-
path: []
|
|
615
|
-
}]);
|
|
616
|
-
const checked = Value.Default(schema, Value.Clone(input));
|
|
617
|
-
Value.Assert(schema, checked);
|
|
618
|
-
return {
|
|
619
|
-
value: HasTransform(schema, []) ? TransformDecode(schema, [], checked) : checked,
|
|
620
|
-
checked
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
/** The most field issues one refusal carries. */
|
|
624
|
-
const MAX_ISSUES = 10;
|
|
625
|
-
/** The longest path or message one issue carries. */
|
|
626
|
-
const MAX_ISSUE_TEXT = 256;
|
|
627
|
-
/**
|
|
628
|
-
* Decodes input as `decodeSchema` does and answers a refusal as the `INVALID_INPUT` failure, with its first
|
|
629
|
-
* field issues: where the input failed and what was expected, never a value. A fault of an owner codec, an
|
|
630
|
-
* error that refuses nothing, is thrown for the caller to report: it is the server's failure, not the input's.
|
|
631
|
-
*/
|
|
632
|
-
function decodeInput(schema, input) {
|
|
633
|
-
const admitted = admitInput(schema, input);
|
|
634
|
-
return admitted.ok ? {
|
|
635
|
-
ok: true,
|
|
636
|
-
value: admitted.value
|
|
637
|
-
} : admitted;
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* `decodeInput`, also answering, for an admitted input, `checked`: the plain JSON its schema checked before any
|
|
641
|
-
* codec ran, the one shape the schema's own keywords describe.
|
|
642
|
-
*/
|
|
643
|
-
function admitInput(schema, input) {
|
|
644
|
-
let admitted;
|
|
645
|
-
try {
|
|
646
|
-
admitted = admitSchema(schema, input);
|
|
647
|
-
} catch (error) {
|
|
648
|
-
const refused = decodeIssues(error, MAX_ISSUES);
|
|
649
|
-
if (!refused) throw codecFault(error);
|
|
650
|
-
return {
|
|
651
|
-
ok: false,
|
|
652
|
-
error: {
|
|
653
|
-
code: "INVALID_INPUT",
|
|
654
|
-
message: "The input does not match its schema.",
|
|
655
|
-
retryable: false,
|
|
656
|
-
data: { issues: refused.map((issue) => ({
|
|
657
|
-
path: jsonPointer(issue.path).slice(0, MAX_ISSUE_TEXT),
|
|
658
|
-
message: issue.message.slice(0, MAX_ISSUE_TEXT)
|
|
659
|
-
})) }
|
|
660
|
-
}
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
return {
|
|
664
|
-
ok: true,
|
|
665
|
-
value: admitted.value,
|
|
666
|
-
checked: admitted.checked
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
export { wireInput as C, withoutSecrets as E, urlSchema as S, withoutSecretKeyword as T, pipeSchema as _, parseSchema as a, stripObject as b, SchemaValidationError as c, hasUncompiledCodecs as d, inputSchema as f, normalizeSchema as g, looseObject as h, parseInput as i, defaultSchema as l, literalUnion as m, decodeInput as n, codecFault as o, inspectSchema as p, decodeSchema as r, decodeIssues as s, admitInput as t, extendObject as u, refineSchema as v, holdsSecrets as w, transformSchema as x, strictObject as y };
|