@webpieces/core-util 0.4.1025 → 0.4.1027
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/package.json +1 -1
- package/src/mcp/DtoSchema.d.ts +30 -0
- package/src/mcp/DtoSchema.js +92 -0
- package/src/mcp/DtoSchema.js.map +1 -1
package/package.json
CHANGED
package/src/mcp/DtoSchema.d.ts
CHANGED
|
@@ -121,6 +121,30 @@ export declare class ApiJsonSchema {
|
|
|
121
121
|
* server checks.
|
|
122
122
|
*/
|
|
123
123
|
static openForClients(schema: ApiJsonSchema): ApiJsonSchema;
|
|
124
|
+
/**
|
|
125
|
+
* A copy of `value` with every OPTIONAL property that holds `null` REMOVED, at every depth the
|
|
126
|
+
* schema describes — the form an MCP tool's result is validated and sent in (#1496).
|
|
127
|
+
*
|
|
128
|
+
* An optional DTO field (`organizationId?: string`) publishes as ABSENT-or-a-string, never `null`.
|
|
129
|
+
* A row mapper that copies a SQL `NULL` into it compiles (a loosely typed row satisfies `?:`), and
|
|
130
|
+
* every HTTP caller accepts the reply, so the only place it used to surface was an MCP tool call
|
|
131
|
+
* failing whole at runtime, on whichever row happened to hold the NULL. Omitting the key gives the
|
|
132
|
+
* agent the same document it gets when the source omitted the field, and the PUBLISHED schema stays
|
|
133
|
+
* "absent, not null" — nothing is advertised as nullable that the contract does not declare so.
|
|
134
|
+
*
|
|
135
|
+
* Only what the schema says is optional is touched:
|
|
136
|
+
* - a property in `required` that holds `null` is KEPT, so the validator still refuses it;
|
|
137
|
+
* - a property whose schema allows `null` (`T | null`) is KEPT — null is its documented value;
|
|
138
|
+
* - a typed-map entry, an array element and an undeclared key are not "optional fields" and are
|
|
139
|
+
* KEPT as they are (their nested objects are still walked);
|
|
140
|
+
* - a union is walked through the branch its discriminator selects, and left alone without one.
|
|
141
|
+
*
|
|
142
|
+
* The input is not modified: an implementation may return an object it also holds elsewhere.
|
|
143
|
+
*/
|
|
144
|
+
static omitOptionalNulls(schema: ApiJsonSchema, value: DtoValue): DtoValue;
|
|
145
|
+
private static omitOptionalNullsOfObject;
|
|
146
|
+
/** The `oneOf` branch whose discriminator `enum` carries the value's tag, if the union has one. */
|
|
147
|
+
private static discriminatedBranchOf;
|
|
124
148
|
}
|
|
125
149
|
/**
|
|
126
150
|
* Builds the CLOSED object schema a hand-written runtime boundary validates against — `@WpStream`'s
|
|
@@ -196,6 +220,12 @@ export declare class ApiJsonSchemaValidator {
|
|
|
196
220
|
/** The string literals ONE branch declares for the discriminator property. */
|
|
197
221
|
private static branchValuesOf;
|
|
198
222
|
private validateObject;
|
|
223
|
+
/**
|
|
224
|
+
* `null` in a DECLARED property that does not allow it, worded so the message names the fix
|
|
225
|
+
* (#1496). The bare `must not be null` left the reader to work out whether the DTO or the value was
|
|
226
|
+
* wrong; the two cases have different cures, so they get different text.
|
|
227
|
+
*/
|
|
228
|
+
private static nullPropertyFailure;
|
|
199
229
|
private validateArray;
|
|
200
230
|
private validateString;
|
|
201
231
|
private validateNumber;
|
package/src/mcp/DtoSchema.js
CHANGED
|
@@ -155,6 +155,83 @@ class ApiJsonSchema {
|
|
|
155
155
|
}
|
|
156
156
|
return open;
|
|
157
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* A copy of `value` with every OPTIONAL property that holds `null` REMOVED, at every depth the
|
|
160
|
+
* schema describes — the form an MCP tool's result is validated and sent in (#1496).
|
|
161
|
+
*
|
|
162
|
+
* An optional DTO field (`organizationId?: string`) publishes as ABSENT-or-a-string, never `null`.
|
|
163
|
+
* A row mapper that copies a SQL `NULL` into it compiles (a loosely typed row satisfies `?:`), and
|
|
164
|
+
* every HTTP caller accepts the reply, so the only place it used to surface was an MCP tool call
|
|
165
|
+
* failing whole at runtime, on whichever row happened to hold the NULL. Omitting the key gives the
|
|
166
|
+
* agent the same document it gets when the source omitted the field, and the PUBLISHED schema stays
|
|
167
|
+
* "absent, not null" — nothing is advertised as nullable that the contract does not declare so.
|
|
168
|
+
*
|
|
169
|
+
* Only what the schema says is optional is touched:
|
|
170
|
+
* - a property in `required` that holds `null` is KEPT, so the validator still refuses it;
|
|
171
|
+
* - a property whose schema allows `null` (`T | null`) is KEPT — null is its documented value;
|
|
172
|
+
* - a typed-map entry, an array element and an undeclared key are not "optional fields" and are
|
|
173
|
+
* KEPT as they are (their nested objects are still walked);
|
|
174
|
+
* - a union is walked through the branch its discriminator selects, and left alone without one.
|
|
175
|
+
*
|
|
176
|
+
* The input is not modified: an implementation may return an object it also holds elsewhere.
|
|
177
|
+
*/
|
|
178
|
+
// webpieces-disable no-function-outside-class -- static transform of this class (see baseTypeOf)
|
|
179
|
+
static omitOptionalNulls(schema, value) {
|
|
180
|
+
if (value === null || typeof value !== 'object')
|
|
181
|
+
return value;
|
|
182
|
+
if (Array.isArray(value)) {
|
|
183
|
+
const items = schema.items;
|
|
184
|
+
if (items === undefined)
|
|
185
|
+
return value;
|
|
186
|
+
return value.map((element) => ApiJsonSchema.omitOptionalNulls(items, element));
|
|
187
|
+
}
|
|
188
|
+
if (schema.oneOf !== undefined) {
|
|
189
|
+
const branch = ApiJsonSchema.discriminatedBranchOf(schema, value);
|
|
190
|
+
return branch === undefined ? value : ApiJsonSchema.omitOptionalNulls(branch, value);
|
|
191
|
+
}
|
|
192
|
+
if (schema.properties === undefined && typeof schema.additionalProperties !== 'object') {
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
return ApiJsonSchema.omitOptionalNullsOfObject(schema, value);
|
|
196
|
+
}
|
|
197
|
+
// webpieces-disable no-function-outside-class -- private static helper of omitOptionalNulls
|
|
198
|
+
static omitOptionalNullsOfObject(schema, value) {
|
|
199
|
+
const properties = schema.properties ?? {};
|
|
200
|
+
const required = schema.required ?? [];
|
|
201
|
+
const extra = schema.additionalProperties;
|
|
202
|
+
const copy = {};
|
|
203
|
+
for (const key of Object.keys(value)) {
|
|
204
|
+
const entry = Object.getOwnPropertyDescriptor(value, key)?.value;
|
|
205
|
+
const declared = Object.getOwnPropertyDescriptor(properties, key)?.value;
|
|
206
|
+
if (declared !== undefined) {
|
|
207
|
+
const omit = entry === null &&
|
|
208
|
+
!required.includes(key) &&
|
|
209
|
+
!ApiJsonSchema.allowsNull(declared);
|
|
210
|
+
if (!omit)
|
|
211
|
+
copy[key] = ApiJsonSchema.omitOptionalNulls(declared, entry);
|
|
212
|
+
}
|
|
213
|
+
else if (typeof extra === 'object' && extra !== null) {
|
|
214
|
+
copy[key] = ApiJsonSchema.omitOptionalNulls(extra, entry);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
copy[key] = entry;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return copy;
|
|
221
|
+
}
|
|
222
|
+
/** The `oneOf` branch whose discriminator `enum` carries the value's tag, if the union has one. */
|
|
223
|
+
// webpieces-disable no-function-outside-class -- private static helper of omitOptionalNulls
|
|
224
|
+
static discriminatedBranchOf(schema, value) {
|
|
225
|
+
const property = schema.discriminator?.propertyName;
|
|
226
|
+
if (property === undefined)
|
|
227
|
+
return undefined;
|
|
228
|
+
const held = Object.getOwnPropertyDescriptor(value, property)?.value;
|
|
229
|
+
return (schema.oneOf ?? []).find((branch) => {
|
|
230
|
+
const declared = Object.getOwnPropertyDescriptor(branch.properties ?? {}, property)
|
|
231
|
+
?.value;
|
|
232
|
+
return (declared?.enum ?? []).includes(held);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
158
235
|
}
|
|
159
236
|
exports.ApiJsonSchema = ApiJsonSchema;
|
|
160
237
|
/**
|
|
@@ -379,6 +456,9 @@ class ApiJsonSchemaValidator {
|
|
|
379
456
|
return failure;
|
|
380
457
|
continue;
|
|
381
458
|
}
|
|
459
|
+
if (entry === null && !ApiJsonSchema.allowsNull(declared)) {
|
|
460
|
+
return ApiJsonSchemaValidator.nullPropertyFailure(schema, key, `${path}.${key}`);
|
|
461
|
+
}
|
|
382
462
|
const failure = this.validateAt(declared, entry, `${path}.${key}`);
|
|
383
463
|
if (failure)
|
|
384
464
|
return failure;
|
|
@@ -390,6 +470,18 @@ class ApiJsonSchemaValidator {
|
|
|
390
470
|
}
|
|
391
471
|
return undefined;
|
|
392
472
|
}
|
|
473
|
+
/**
|
|
474
|
+
* `null` in a DECLARED property that does not allow it, worded so the message names the fix
|
|
475
|
+
* (#1496). The bare `must not be null` left the reader to work out whether the DTO or the value was
|
|
476
|
+
* wrong; the two cases have different cures, so they get different text.
|
|
477
|
+
*/
|
|
478
|
+
// webpieces-disable no-function-outside-class -- private static message builder of this validator
|
|
479
|
+
static nullPropertyFailure(schema, key, at) {
|
|
480
|
+
if ((schema.required ?? []).includes(key)) {
|
|
481
|
+
return new DtoValidationFailure(at, `${at} is required in the DTO but holds null — supply a value, or declare the field optional (or \`| null\`) in the DTO`);
|
|
482
|
+
}
|
|
483
|
+
return new DtoValidationFailure(at, `${at} is optional in the DTO; omit the field instead of setting it to null`);
|
|
484
|
+
}
|
|
393
485
|
validateArray(schema, value, path) {
|
|
394
486
|
if (!Array.isArray(value)) {
|
|
395
487
|
return new DtoValidationFailure(path, `${path} must be an array`);
|
package/src/mcp/DtoSchema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DtoSchema.js","sourceRoot":"","sources":["../../../../../../packages/core/core-util/src/mcp/DtoSchema.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAmBH;;;;;;;;;;;;;GAaG;AACH,MAAa,0BAA0B;IAGxB;IAEA;IAJX;IACI,qFAAqF;IAC9E,YAAoB;IAC3B,yFAAyF;IAClF,OAA+B;QAF/B,iBAAY,GAAZ,YAAY,CAAQ;QAEpB,YAAO,GAAP,OAAO,CAAwB;IACvC,CAAC;CACP;AAPD,gEAOC;AAED,6DAA6D;AAC7D,MAAa,aAAa;IACtB,uDAAuD;IACvD,IAAI,CAAoD;IACxD,WAAW,CAAU;IACrB,UAAU,CAAiC;IAC3C,QAAQ,CAAY;IACpB,kGAAkG;IAClG,oBAAoB,CAA2B;IAC/C,KAAK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,oBAAoB,CAAC,IAAI,EAAE,GAAG,IAAI,eAAe,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;QACD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,KAAK,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YACzD,OAAO,IAAI,oBAAoB,CAAC,IAAI,EAAE,GAAG,IAAI,eAAe,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ;AAxRD,wDAwRC","sourcesContent":["/**\n * The JSON Schema subset webpieces publishes on its API boundaries, and the ONE runtime validator of\n * it.\n *\n * ## Nothing here reads reflect-metadata any more\n *\n * Until #984 this file also carried `@WpDto`, `@WpDtoField`, `WpDtoFieldOptions`,\n * `WpDtoMapFieldOptions`, `WpMcpHeader`, `@WpResponseDto` and `DtoSchemaBuilder` — a second, runtime\n * spelling of facts the TypeScript compiler already holds. #983 measured the two against each other\n * and found the compiler reproduces every one of those arguments byte for byte, so they are deleted\n * rather than deprecated (`.claude/rules/no-backwards-compat.md`): a schema is now BUILT at build\n * time from the source by `@webpieces/api-doc-model`, and the runtime is HANDED the result.\n *\n * What survives is the schema itself, a builder for the boundaries that construct one by hand, and a\n * validator that reads the schema instead of a decorator.\n */\n\n/** Runtime values accepted at the DTO validation boundary. */\nexport type DtoValue = object | string | number | boolean | null;\n\n/**\n * One JSON Schema `type`. `null` is a member because NULLABLE is expressed as `[\"string\", \"null\"]`,\n * which is how JSON Schema says \"present, and may hold null\" — a different wire document from an\n * ABSENT property, and one the reflect-metadata runtime could neither see nor write down.\n */\nexport type ApiJsonSchemaType =\n | 'object'\n | 'string'\n | 'number'\n | 'integer'\n | 'boolean'\n | 'array'\n | 'null';\n\n/**\n * The DISCRIMINATOR of a {@link ApiJsonSchema.oneOf} — which property narrows the union, and what\n * each of its values names.\n *\n * The shape MIRRORS what `OpenApiGenerator`'s `SchemaRenderer` already emits for the same union\n * (`propertyName` + `mapping`), because a union has exactly ONE published spelling in this repo and\n * the OpenAPI renderer is the reference implementation of it. The one difference is what a mapping\n * VALUE holds: OpenAPI maps a value to `#/components/schemas/ScheduledWindow`, and an MCP tool\n * schema is INLINE and has no `$ref` to point at, so it maps to the branch's type NAME.\n *\n * `mapping` is DOCUMENTATION for the reader. {@link ApiJsonSchemaValidator} never narrows with it:\n * it selects the branch whose own `enum` carries the value, so the accepted request and the\n * published document cannot disagree about which branch a value means.\n */\nexport class ApiJsonSchemaDiscriminator {\n constructor(\n /** The property every branch declares as exactly one string literal, e.g. `kind`. */\n public propertyName: string,\n /** discriminator value -> the branch's TYPE NAME (MCP is inline: there is no `$ref`). */\n public mapping: Record<string, string>,\n ) {}\n}\n\n/** The JSON Schema subset emitted for Webpieces API DTOs. */\nexport class ApiJsonSchema {\n /** One type, or `[T, \"null\"]` for a nullable field. */\n type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[];\n description?: string;\n properties?: Record<string, ApiJsonSchema>;\n required?: string[];\n /** `false` closes an object; a schema makes it a typed map whose values all match that schema. */\n additionalProperties?: boolean | ApiJsonSchema;\n items?: ApiJsonSchema;\n /**\n * The allowed values. A NULLABLE enum carries `null` here as well as in {@link type}: JSON Schema\n * checks `enum` independently of `type`, so `{type: [\"string\",\"null\"], enum: [\"a\"]}` still rejects\n * `null` (#1197).\n */\n enum?: (string | null)[];\n minimum?: number;\n maximum?: number;\n /**\n * The branches of a union, INLINE (MCP tool schemas have no `$ref`), in declaration order.\n *\n * A union is legal NESTED, inside a property. It is NOT legal at the ROOT of a tool's parameter\n * schema — both the OpenAI and the Anthropic function-calling APIs reject a top-level\n * `oneOf`/`anyOf`/`allOf`, and because a server sends its whole tool list on every request, one\n * such tool makes EVERY request 400. That is refused at build time by the `no-root-union-api-type`\n * rule, before a contract can be published at all.\n */\n oneOf?: readonly ApiJsonSchema[];\n /**\n * Set ONLY when every branch carries the same property typed as one string literal — DERIVED,\n * never invented. A union TypeScript itself cannot narrow is published as a bare {@link oneOf},\n * because claiming a narrowing the source does not have is worse than admitting there is none.\n */\n discriminator?: ApiJsonSchemaDiscriminator;\n /** MCP 2026 SEP-2243 schema extension consumed by the official SDK. */\n declare 'x-mcp-header'?: string;\n\n constructor(type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[]) {\n this.type = type;\n }\n\n /**\n * The non-`null` member of the type, which is the one that decides how a value is checked.\n *\n * These three are STATIC and not instance methods on purpose: an instance method would put a\n * function on the structural type, and this class is handed straight to the MCP SDK, whose\n * parameter types are index-signature JSON objects that a class carrying methods no longer\n * satisfies. It stays a data-only structure (`CLAUDE.md` §1) and the helpers live beside it.\n */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static baseTypeOf(schema: ApiJsonSchema): ApiJsonSchemaType | undefined {\n if (schema.type === undefined) return undefined;\n if (typeof schema.type === 'string') return schema.type;\n return schema.type.find((one: ApiJsonSchemaType) => one !== 'null');\n }\n\n /** True when the schema was widened with `\"null\"`, i.e. the property may hold `null`. */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static allowsNull(schema: ApiJsonSchema): boolean {\n return typeof schema.type !== 'string' && (schema.type?.includes('null') ?? false);\n }\n\n /**\n * The one closure gate for an object schema. Closed means no key can carry an unspecified value:\n * either extra keys are forbidden (`additionalProperties: false`) or every extra key's value is\n * typed (`additionalProperties: <schema>`, a typed map). A missing or `true` value is open.\n * Use this instead of `schema.additionalProperties === false`, which rejects typed maps.\n */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static isClosed(schema: ApiJsonSchema): boolean {\n const extra = schema.additionalProperties;\n return extra === false || (typeof extra === 'object' && extra !== null);\n }\n\n /**\n * A copy of `schema` with every `additionalProperties: false` REMOVED, at every depth — the form\n * an MCP tool's `outputSchema` is PUBLISHED in (#1441). Strict in, tolerant out:\n *\n * - An MCP client (Claude Code, claude.ai, a ChatGPT connector) caches a tool's `outputSchema`\n * when it connects and validates every `structuredContent` against that cached copy. A CLOSED\n * output schema therefore turns ADDING a response field into a break for every client already\n * connected: the server sends the new key and the cached schema refuses it. Open, adding a\n * field is additive and safe; removing, renaming or narrowing one is still a break.\n * - An `inputSchema` is NEVER opened. Refusing an unknown request key catches a caller's typo.\n * - A typed map (`additionalProperties: <schema>`) stays a typed map — only its value schema is\n * opened, like every other nested object.\n *\n * The input is not modified. The server keeps validating its OWN result against the schema it\n * was handed, and {@link ApiJsonSchemaValidator} refuses an undeclared key whether or not the\n * schema says `additionalProperties: false`, so opening what is published loosens nothing the\n * server checks.\n */\n // webpieces-disable no-function-outside-class -- static transform of this class (see baseTypeOf)\n static openForClients(schema: ApiJsonSchema): ApiJsonSchema {\n const open = Object.assign(new ApiJsonSchema(schema.type), schema);\n const extra = schema.additionalProperties;\n if (extra === false) {\n delete open.additionalProperties;\n } else if (typeof extra === 'object' && extra !== null) {\n open.additionalProperties = ApiJsonSchema.openForClients(extra);\n }\n if (schema.properties !== undefined) {\n const properties: Record<string, ApiJsonSchema> = {};\n for (const [name, child] of Object.entries(schema.properties)) {\n properties[name] = ApiJsonSchema.openForClients(child);\n }\n open.properties = properties;\n }\n if (schema.items !== undefined) open.items = ApiJsonSchema.openForClients(schema.items);\n if (schema.oneOf !== undefined) {\n open.oneOf = schema.oneOf.map((branch: ApiJsonSchema) =>\n ApiJsonSchema.openForClients(branch),\n );\n }\n return open;\n }\n}\n\n/**\n * Builds the CLOSED object schema a hand-written runtime boundary validates against — `@WpStream`'s\n * two event schemas are the whole of its client list today.\n *\n * It exists because the alternative spellings are both worse: a decorator that re-states the field\n * types the compiler already knows is exactly what #984 deleted, and a bare object literal is what\n * `CLAUDE.md` §3 forbids. A generated schema never comes through here — it is read from the build's\n * own artifact.\n */\nexport class ObjectSchemaBuilder {\n private readonly schema = new ApiJsonSchema('object');\n private readonly requiredNames: string[] = [];\n\n constructor() {\n this.schema.properties = {};\n this.schema.additionalProperties = false;\n }\n\n required(name: string, type: ApiJsonSchema): this {\n this.requiredNames.push(name);\n return this.optional(name, type);\n }\n\n optional(name: string, type: ApiJsonSchema): this {\n this.schema.properties![name] = type;\n return this;\n }\n\n build(): ApiJsonSchema {\n if (this.requiredNames.length > 0) this.schema.required = [...this.requiredNames];\n return this.schema;\n }\n}\n\n/** A validation result safe to show to an API caller/model. */\nexport class DtoValidationFailure {\n constructor(\n /** JSON path of the offending value, e.g. `$.address.city`. */\n public readonly field: string,\n public readonly message: string,\n ) {}\n}\n\n/**\n * Validates a value against an {@link ApiJsonSchema} — the ONE runtime narrowing boundary for MCP\n * tool arguments, MCP structured output and typed stream events.\n *\n * It reads the SCHEMA, which is the same object the build published to agents, so a document that\n * says a field is required and a server that accepts it missing cannot disagree. The predecessor\n * walked reflect-metadata instead, which meant the published document and the accepted request were\n * derived from two different declarations.\n */\nexport class ApiJsonSchemaValidator {\n validate(schema: ApiJsonSchema, value: DtoValue): DtoValidationFailure | undefined {\n return this.validateAt(schema, value, '$');\n }\n\n private validateAt(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (value === null) {\n if (!ApiJsonSchema.allowsNull(schema)) {\n return new DtoValidationFailure(path, `${path} must not be null`);\n }\n // `enum` is checked independently of `type`, exactly as every JSON Schema client checks\n // it: a nullable enum whose `enum` omits null is refused HERE rather than by the agent's\n // client after the call already ran (#1197).\n if (schema.enum && !schema.enum.includes(null)) {\n return new DtoValidationFailure(\n path,\n `${path} must be one of: ${schema.enum.map(String).join(', ')}`,\n );\n }\n return undefined;\n }\n if (schema.oneOf !== undefined) {\n return this.validateUnion(schema, value, path);\n }\n const types = ApiJsonSchemaValidator.nonNullTypesOf(schema);\n if (types.length > 1) {\n return this.validateMultiType(schema, types, value, path);\n }\n return this.validateAs(types[0], schema, value, path);\n }\n\n /**\n * A `type` ARRAY naming two or more non-null types — `[\"string\",\"number\",\"object\"]`, the open\n * JSON schema `JsonValueDto` publishes (#1447). JSON Schema accepts the value when ANY listed type\n * matches it, so the value's own JSON type selects the one to validate against; `integer` matches\n * a whole number only. A value matching none is refused naming every accepted type.\n */\n private validateMultiType(\n schema: ApiJsonSchema,\n types: readonly ApiJsonSchemaType[],\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n const matched = types.find((type: ApiJsonSchemaType) =>\n ApiJsonSchemaValidator.isOfType(type, value),\n );\n if (matched === undefined) {\n return new DtoValidationFailure(path, `${path} must be one of: ${types.join(', ')}`);\n }\n return this.validateAs(matched, schema, value, path);\n }\n\n /** True when `value`'s JSON type is `type`; `integer` is a whole `number`. */\n // webpieces-disable no-function-outside-class -- private static predicate of this validator\n private static isOfType(type: ApiJsonSchemaType, value: DtoValue): boolean {\n if (type === 'array') return Array.isArray(value);\n if (type === 'object') return typeof value === 'object' && value !== null && !Array.isArray(value);\n if (type === 'integer') return typeof value === 'number' && Number.isInteger(value);\n if (type === 'number') return typeof value === 'number' && Number.isFinite(value);\n if (type === 'null') return value === null;\n return typeof value === type;\n }\n\n /** Every member of `type` except `null`, in declaration order — empty when `type` is absent. */\n // webpieces-disable no-function-outside-class -- private static reader of a schema\n private static nonNullTypesOf(schema: ApiJsonSchema): readonly ApiJsonSchemaType[] {\n if (schema.type === undefined) return [];\n if (typeof schema.type === 'string') return [schema.type];\n return schema.type.filter((one: ApiJsonSchemaType) => one !== 'null');\n }\n\n /** Validate `value` as ONE JSON type of `schema` — `undefined` (no type) accepts anything. */\n private validateAs(\n type: ApiJsonSchemaType | undefined,\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n switch (type) {\n case 'object':\n return this.validateObject(schema, value, path);\n case 'array':\n return this.validateArray(schema, value, path);\n case 'string':\n return this.validateString(schema, value, path);\n case 'number':\n case 'integer':\n return this.validateNumber(type, schema, value, path);\n case 'boolean':\n return typeof value === 'boolean'\n ? undefined\n : new DtoValidationFailure(path, `${path} must be a boolean`);\n default:\n return undefined;\n }\n }\n\n /**\n * A union, validated BY DISCRIMINATOR.\n *\n * The discriminator property is read FIRST and the branch chosen from it, so a value naming no\n * branch is reported as what it is — `$.window.kind must be one of: scheduled | asap`, naming the\n * field that is wrong and the values it may hold. Trying every branch and reporting \"no branch\n * matched\" tells a caller nothing they can act on: it names neither the offending property nor\n * the legal values, and it hides a branch that matched on `kind` and failed three levels down.\n *\n * The branch is selected by the branches' own `enum`s, never by\n * {@link ApiJsonSchemaDiscriminator.mapping} — the branches ARE the schema, so selection and\n * validation read one source and cannot disagree.\n */\n private validateUnion(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n const branches = schema.oneOf ?? [];\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an object`);\n }\n const discriminator = schema.discriminator;\n if (discriminator === undefined) {\n return this.validateAnyBranch(branches, value, path);\n }\n const property = discriminator.propertyName;\n const held: DtoValue = Object.getOwnPropertyDescriptor(value, property)?.value;\n const branch = branches.find((one: ApiJsonSchema) =>\n ApiJsonSchemaValidator.branchValuesOf(one, property).includes(held as string),\n );\n if (branch === undefined) {\n const legal = branches\n .flatMap((one: ApiJsonSchema) =>\n ApiJsonSchemaValidator.branchValuesOf(one, property),\n )\n .join(' | ');\n return new DtoValidationFailure(\n `${path}.${property}`,\n `${path}.${property} must be one of: ${legal}`,\n );\n }\n return this.validateAt(branch, value, path);\n }\n\n /**\n * A union with NO discriminator — one TypeScript itself cannot narrow. Every branch is tried and\n * the value accepted when one matches. There is no better report when none does: the source does\n * not narrow, so no property can honestly be named as the wrong one.\n */\n private validateAnyBranch(\n branches: readonly ApiJsonSchema[],\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n for (const branch of branches) {\n if (this.validateAt(branch, value, path) === undefined) {\n return undefined;\n }\n }\n return new DtoValidationFailure(\n path,\n `${path} matches none of the ${branches.length} accepted shapes`,\n );\n }\n\n /** The string literals ONE branch declares for the discriminator property. */\n // webpieces-disable no-function-outside-class -- private static reader of a branch schema\n private static branchValuesOf(branch: ApiJsonSchema, property: string): readonly string[] {\n const declared = Object.getOwnPropertyDescriptor(branch.properties ?? {}, property)\n ?.value as ApiJsonSchema | undefined;\n return (declared?.enum ?? []).filter((value: string | null): value is string => value !== null);\n }\n\n private validateObject(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an object`);\n }\n const properties = schema.properties ?? {};\n const extra = schema.additionalProperties;\n for (const key of Object.keys(value)) {\n const entry: DtoValue = Object.getOwnPropertyDescriptor(value, key)?.value;\n // A key HOLDING `undefined` is ABSENT: `JSON.stringify` drops it, so the document the\n // caller receives never carries it, and the `required` loop below already reads\n // `=== undefined` as missing. Validating it here made the two halves of this one walk\n // disagree — `{ next: hasMore ? cursor : undefined }` was refused as \"must be a string\"\n // although its JSON is valid (#1055). `null` is a VALUE and is still checked.\n if ((entry as DtoValue | undefined) === undefined) continue;\n const declared = Object.getOwnPropertyDescriptor(properties, key)?.value as\n | ApiJsonSchema\n | undefined;\n if (declared === undefined) {\n // `additionalProperties: true` is an EXPLICITLY open object — the JSON payload\n // `JsonValueDto` publishes (#1447) — and accepts any key holding any JSON value. It is\n // not the same as an ABSENT `additionalProperties`, which stays strict below.\n if (extra === true) continue;\n // An undeclared key is refused unless the schema is a TYPED MAP — also when the\n // schema is OPEN (no `additionalProperties`). That is deliberate (#1441): a tool's\n // published outputSchema is opened for clients by `ApiJsonSchema.openForClients`,\n // and the server still checks its OWN result exactly against what it declared.\n if (typeof extra !== 'object' || extra === null) {\n return new DtoValidationFailure(\n `${path}.${key}`,\n `${path}.${key} is not allowed`,\n );\n }\n const failure = this.validateAt(extra, entry, `${path}.${key}`);\n if (failure) return failure;\n continue;\n }\n const failure = this.validateAt(declared, entry, `${path}.${key}`);\n if (failure) return failure;\n }\n for (const name of schema.required ?? []) {\n if (Object.getOwnPropertyDescriptor(value, name)?.value === undefined) {\n return new DtoValidationFailure(`${path}.${name}`, `${path}.${name} is required`);\n }\n }\n return undefined;\n }\n\n private validateArray(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (!Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an array`);\n }\n const items = schema.items;\n if (items === undefined) return undefined;\n for (let index = 0; index < value.length; index += 1) {\n const failure = this.validateAt(items, value[index], `${path}[${index}]`);\n if (failure) return failure;\n }\n return undefined;\n }\n\n private validateString(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'string') {\n return new DtoValidationFailure(path, `${path} must be a string`);\n }\n if (schema.enum && !schema.enum.includes(value)) {\n return new DtoValidationFailure(\n path,\n `${path} must be one of: ${schema.enum.map(String).join(', ')}`,\n );\n }\n return undefined;\n }\n\n private validateNumber(\n type: 'number' | 'integer',\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n return new DtoValidationFailure(path, `${path} must be a number`);\n }\n if (type === 'integer' && !Number.isInteger(value)) {\n return new DtoValidationFailure(path, `${path} must be an integer`);\n }\n if (schema.minimum !== undefined && value < schema.minimum) {\n return new DtoValidationFailure(path, `${path} must be >= ${schema.minimum}`);\n }\n if (schema.maximum !== undefined && value > schema.maximum) {\n return new DtoValidationFailure(path, `${path} must be <= ${schema.maximum}`);\n }\n return undefined;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"DtoSchema.js","sourceRoot":"","sources":["../../../../../../packages/core/core-util/src/mcp/DtoSchema.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAmBH;;;;;;;;;;;;;GAaG;AACH,MAAa,0BAA0B;IAGxB;IAEA;IAJX;IACI,qFAAqF;IAC9E,YAAoB;IAC3B,yFAAyF;IAClF,OAA+B;QAF/B,iBAAY,GAAZ,YAAY,CAAQ;QAEpB,YAAO,GAAP,OAAO,CAAwB;IACvC,CAAC;CACP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AK,SAAS,IAAI,KAAK,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YACzD,OAAO,IAAI,oBAAoB,CAAC,IAAI,EAAE,GAAG,IAAI,eAAe,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ;AAlTD,wDAkTC","sourcesContent":["/**\n * The JSON Schema subset webpieces publishes on its API boundaries, and the ONE runtime validator of\n * it.\n *\n * ## Nothing here reads reflect-metadata any more\n *\n * Until #984 this file also carried `@WpDto`, `@WpDtoField`, `WpDtoFieldOptions`,\n * `WpDtoMapFieldOptions`, `WpMcpHeader`, `@WpResponseDto` and `DtoSchemaBuilder` — a second, runtime\n * spelling of facts the TypeScript compiler already holds. #983 measured the two against each other\n * and found the compiler reproduces every one of those arguments byte for byte, so they are deleted\n * rather than deprecated (`.claude/rules/no-backwards-compat.md`): a schema is now BUILT at build\n * time from the source by `@webpieces/api-doc-model`, and the runtime is HANDED the result.\n *\n * What survives is the schema itself, a builder for the boundaries that construct one by hand, and a\n * validator that reads the schema instead of a decorator.\n */\n\n/** Runtime values accepted at the DTO validation boundary. */\nexport type DtoValue = object | string | number | boolean | null;\n\n/**\n * One JSON Schema `type`. `null` is a member because NULLABLE is expressed as `[\"string\", \"null\"]`,\n * which is how JSON Schema says \"present, and may hold null\" — a different wire document from an\n * ABSENT property, and one the reflect-metadata runtime could neither see nor write down.\n */\nexport type ApiJsonSchemaType =\n | 'object'\n | 'string'\n | 'number'\n | 'integer'\n | 'boolean'\n | 'array'\n | 'null';\n\n/**\n * The DISCRIMINATOR of a {@link ApiJsonSchema.oneOf} — which property narrows the union, and what\n * each of its values names.\n *\n * The shape MIRRORS what `OpenApiGenerator`'s `SchemaRenderer` already emits for the same union\n * (`propertyName` + `mapping`), because a union has exactly ONE published spelling in this repo and\n * the OpenAPI renderer is the reference implementation of it. The one difference is what a mapping\n * VALUE holds: OpenAPI maps a value to `#/components/schemas/ScheduledWindow`, and an MCP tool\n * schema is INLINE and has no `$ref` to point at, so it maps to the branch's type NAME.\n *\n * `mapping` is DOCUMENTATION for the reader. {@link ApiJsonSchemaValidator} never narrows with it:\n * it selects the branch whose own `enum` carries the value, so the accepted request and the\n * published document cannot disagree about which branch a value means.\n */\nexport class ApiJsonSchemaDiscriminator {\n constructor(\n /** The property every branch declares as exactly one string literal, e.g. `kind`. */\n public propertyName: string,\n /** discriminator value -> the branch's TYPE NAME (MCP is inline: there is no `$ref`). */\n public mapping: Record<string, string>,\n ) {}\n}\n\n/** The JSON Schema subset emitted for Webpieces API DTOs. */\nexport class ApiJsonSchema {\n /** One type, or `[T, \"null\"]` for a nullable field. */\n type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[];\n description?: string;\n properties?: Record<string, ApiJsonSchema>;\n required?: string[];\n /** `false` closes an object; a schema makes it a typed map whose values all match that schema. */\n additionalProperties?: boolean | ApiJsonSchema;\n items?: ApiJsonSchema;\n /**\n * The allowed values. A NULLABLE enum carries `null` here as well as in {@link type}: JSON Schema\n * checks `enum` independently of `type`, so `{type: [\"string\",\"null\"], enum: [\"a\"]}` still rejects\n * `null` (#1197).\n */\n enum?: (string | null)[];\n minimum?: number;\n maximum?: number;\n /**\n * The branches of a union, INLINE (MCP tool schemas have no `$ref`), in declaration order.\n *\n * A union is legal NESTED, inside a property. It is NOT legal at the ROOT of a tool's parameter\n * schema — both the OpenAI and the Anthropic function-calling APIs reject a top-level\n * `oneOf`/`anyOf`/`allOf`, and because a server sends its whole tool list on every request, one\n * such tool makes EVERY request 400. That is refused at build time by the `no-root-union-api-type`\n * rule, before a contract can be published at all.\n */\n oneOf?: readonly ApiJsonSchema[];\n /**\n * Set ONLY when every branch carries the same property typed as one string literal — DERIVED,\n * never invented. A union TypeScript itself cannot narrow is published as a bare {@link oneOf},\n * because claiming a narrowing the source does not have is worse than admitting there is none.\n */\n discriminator?: ApiJsonSchemaDiscriminator;\n /** MCP 2026 SEP-2243 schema extension consumed by the official SDK. */\n declare 'x-mcp-header'?: string;\n\n constructor(type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[]) {\n this.type = type;\n }\n\n /**\n * The non-`null` member of the type, which is the one that decides how a value is checked.\n *\n * These three are STATIC and not instance methods on purpose: an instance method would put a\n * function on the structural type, and this class is handed straight to the MCP SDK, whose\n * parameter types are index-signature JSON objects that a class carrying methods no longer\n * satisfies. It stays a data-only structure (`CLAUDE.md` §1) and the helpers live beside it.\n */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static baseTypeOf(schema: ApiJsonSchema): ApiJsonSchemaType | undefined {\n if (schema.type === undefined) return undefined;\n if (typeof schema.type === 'string') return schema.type;\n return schema.type.find((one: ApiJsonSchemaType) => one !== 'null');\n }\n\n /** True when the schema was widened with `\"null\"`, i.e. the property may hold `null`. */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static allowsNull(schema: ApiJsonSchema): boolean {\n return typeof schema.type !== 'string' && (schema.type?.includes('null') ?? false);\n }\n\n /**\n * The one closure gate for an object schema. Closed means no key can carry an unspecified value:\n * either extra keys are forbidden (`additionalProperties: false`) or every extra key's value is\n * typed (`additionalProperties: <schema>`, a typed map). A missing or `true` value is open.\n * Use this instead of `schema.additionalProperties === false`, which rejects typed maps.\n */\n // webpieces-disable no-function-outside-class -- static reader of this class\n static isClosed(schema: ApiJsonSchema): boolean {\n const extra = schema.additionalProperties;\n return extra === false || (typeof extra === 'object' && extra !== null);\n }\n\n /**\n * A copy of `schema` with every `additionalProperties: false` REMOVED, at every depth — the form\n * an MCP tool's `outputSchema` is PUBLISHED in (#1441). Strict in, tolerant out:\n *\n * - An MCP client (Claude Code, claude.ai, a ChatGPT connector) caches a tool's `outputSchema`\n * when it connects and validates every `structuredContent` against that cached copy. A CLOSED\n * output schema therefore turns ADDING a response field into a break for every client already\n * connected: the server sends the new key and the cached schema refuses it. Open, adding a\n * field is additive and safe; removing, renaming or narrowing one is still a break.\n * - An `inputSchema` is NEVER opened. Refusing an unknown request key catches a caller's typo.\n * - A typed map (`additionalProperties: <schema>`) stays a typed map — only its value schema is\n * opened, like every other nested object.\n *\n * The input is not modified. The server keeps validating its OWN result against the schema it\n * was handed, and {@link ApiJsonSchemaValidator} refuses an undeclared key whether or not the\n * schema says `additionalProperties: false`, so opening what is published loosens nothing the\n * server checks.\n */\n // webpieces-disable no-function-outside-class -- static transform of this class (see baseTypeOf)\n static openForClients(schema: ApiJsonSchema): ApiJsonSchema {\n const open = Object.assign(new ApiJsonSchema(schema.type), schema);\n const extra = schema.additionalProperties;\n if (extra === false) {\n delete open.additionalProperties;\n } else if (typeof extra === 'object' && extra !== null) {\n open.additionalProperties = ApiJsonSchema.openForClients(extra);\n }\n if (schema.properties !== undefined) {\n const properties: Record<string, ApiJsonSchema> = {};\n for (const [name, child] of Object.entries(schema.properties)) {\n properties[name] = ApiJsonSchema.openForClients(child);\n }\n open.properties = properties;\n }\n if (schema.items !== undefined) open.items = ApiJsonSchema.openForClients(schema.items);\n if (schema.oneOf !== undefined) {\n open.oneOf = schema.oneOf.map((branch: ApiJsonSchema) =>\n ApiJsonSchema.openForClients(branch),\n );\n }\n return open;\n }\n\n /**\n * A copy of `value` with every OPTIONAL property that holds `null` REMOVED, at every depth the\n * schema describes — the form an MCP tool's result is validated and sent in (#1496).\n *\n * An optional DTO field (`organizationId?: string`) publishes as ABSENT-or-a-string, never `null`.\n * A row mapper that copies a SQL `NULL` into it compiles (a loosely typed row satisfies `?:`), and\n * every HTTP caller accepts the reply, so the only place it used to surface was an MCP tool call\n * failing whole at runtime, on whichever row happened to hold the NULL. Omitting the key gives the\n * agent the same document it gets when the source omitted the field, and the PUBLISHED schema stays\n * \"absent, not null\" — nothing is advertised as nullable that the contract does not declare so.\n *\n * Only what the schema says is optional is touched:\n * - a property in `required` that holds `null` is KEPT, so the validator still refuses it;\n * - a property whose schema allows `null` (`T | null`) is KEPT — null is its documented value;\n * - a typed-map entry, an array element and an undeclared key are not \"optional fields\" and are\n * KEPT as they are (their nested objects are still walked);\n * - a union is walked through the branch its discriminator selects, and left alone without one.\n *\n * The input is not modified: an implementation may return an object it also holds elsewhere.\n */\n // webpieces-disable no-function-outside-class -- static transform of this class (see baseTypeOf)\n static omitOptionalNulls(schema: ApiJsonSchema, value: DtoValue): DtoValue {\n if (value === null || typeof value !== 'object') return value;\n if (Array.isArray(value)) {\n const items = schema.items;\n if (items === undefined) return value;\n return value.map((element: DtoValue) => ApiJsonSchema.omitOptionalNulls(items, element));\n }\n if (schema.oneOf !== undefined) {\n const branch = ApiJsonSchema.discriminatedBranchOf(schema, value);\n return branch === undefined ? value : ApiJsonSchema.omitOptionalNulls(branch, value);\n }\n if (schema.properties === undefined && typeof schema.additionalProperties !== 'object') {\n return value;\n }\n return ApiJsonSchema.omitOptionalNullsOfObject(schema, value);\n }\n\n // webpieces-disable no-function-outside-class -- private static helper of omitOptionalNulls\n private static omitOptionalNullsOfObject(\n schema: ApiJsonSchema,\n value: object,\n ): Record<string, DtoValue> {\n const properties = schema.properties ?? {};\n const required = schema.required ?? [];\n const extra = schema.additionalProperties;\n const copy: Record<string, DtoValue> = {};\n for (const key of Object.keys(value)) {\n const entry: DtoValue = Object.getOwnPropertyDescriptor(value, key)?.value;\n const declared = Object.getOwnPropertyDescriptor(properties, key)?.value as\n | ApiJsonSchema\n | undefined;\n if (declared !== undefined) {\n const omit =\n entry === null &&\n !required.includes(key) &&\n !ApiJsonSchema.allowsNull(declared);\n if (!omit) copy[key] = ApiJsonSchema.omitOptionalNulls(declared, entry);\n } else if (typeof extra === 'object' && extra !== null) {\n copy[key] = ApiJsonSchema.omitOptionalNulls(extra, entry);\n } else {\n copy[key] = entry;\n }\n }\n return copy;\n }\n\n /** The `oneOf` branch whose discriminator `enum` carries the value's tag, if the union has one. */\n // webpieces-disable no-function-outside-class -- private static helper of omitOptionalNulls\n private static discriminatedBranchOf(\n schema: ApiJsonSchema,\n value: object,\n ): ApiJsonSchema | undefined {\n const property = schema.discriminator?.propertyName;\n if (property === undefined) return undefined;\n const held: DtoValue = Object.getOwnPropertyDescriptor(value, property)?.value;\n return (schema.oneOf ?? []).find((branch: ApiJsonSchema) => {\n const declared = Object.getOwnPropertyDescriptor(branch.properties ?? {}, property)\n ?.value as ApiJsonSchema | undefined;\n return (declared?.enum ?? []).includes(held as string);\n });\n }\n}\n\n/**\n * Builds the CLOSED object schema a hand-written runtime boundary validates against — `@WpStream`'s\n * two event schemas are the whole of its client list today.\n *\n * It exists because the alternative spellings are both worse: a decorator that re-states the field\n * types the compiler already knows is exactly what #984 deleted, and a bare object literal is what\n * `CLAUDE.md` §3 forbids. A generated schema never comes through here — it is read from the build's\n * own artifact.\n */\nexport class ObjectSchemaBuilder {\n private readonly schema = new ApiJsonSchema('object');\n private readonly requiredNames: string[] = [];\n\n constructor() {\n this.schema.properties = {};\n this.schema.additionalProperties = false;\n }\n\n required(name: string, type: ApiJsonSchema): this {\n this.requiredNames.push(name);\n return this.optional(name, type);\n }\n\n optional(name: string, type: ApiJsonSchema): this {\n this.schema.properties![name] = type;\n return this;\n }\n\n build(): ApiJsonSchema {\n if (this.requiredNames.length > 0) this.schema.required = [...this.requiredNames];\n return this.schema;\n }\n}\n\n/** A validation result safe to show to an API caller/model. */\nexport class DtoValidationFailure {\n constructor(\n /** JSON path of the offending value, e.g. `$.address.city`. */\n public readonly field: string,\n public readonly message: string,\n ) {}\n}\n\n/**\n * Validates a value against an {@link ApiJsonSchema} — the ONE runtime narrowing boundary for MCP\n * tool arguments, MCP structured output and typed stream events.\n *\n * It reads the SCHEMA, which is the same object the build published to agents, so a document that\n * says a field is required and a server that accepts it missing cannot disagree. The predecessor\n * walked reflect-metadata instead, which meant the published document and the accepted request were\n * derived from two different declarations.\n */\nexport class ApiJsonSchemaValidator {\n validate(schema: ApiJsonSchema, value: DtoValue): DtoValidationFailure | undefined {\n return this.validateAt(schema, value, '$');\n }\n\n private validateAt(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (value === null) {\n if (!ApiJsonSchema.allowsNull(schema)) {\n return new DtoValidationFailure(path, `${path} must not be null`);\n }\n // `enum` is checked independently of `type`, exactly as every JSON Schema client checks\n // it: a nullable enum whose `enum` omits null is refused HERE rather than by the agent's\n // client after the call already ran (#1197).\n if (schema.enum && !schema.enum.includes(null)) {\n return new DtoValidationFailure(\n path,\n `${path} must be one of: ${schema.enum.map(String).join(', ')}`,\n );\n }\n return undefined;\n }\n if (schema.oneOf !== undefined) {\n return this.validateUnion(schema, value, path);\n }\n const types = ApiJsonSchemaValidator.nonNullTypesOf(schema);\n if (types.length > 1) {\n return this.validateMultiType(schema, types, value, path);\n }\n return this.validateAs(types[0], schema, value, path);\n }\n\n /**\n * A `type` ARRAY naming two or more non-null types — `[\"string\",\"number\",\"object\"]`, the open\n * JSON schema `JsonValueDto` publishes (#1447). JSON Schema accepts the value when ANY listed type\n * matches it, so the value's own JSON type selects the one to validate against; `integer` matches\n * a whole number only. A value matching none is refused naming every accepted type.\n */\n private validateMultiType(\n schema: ApiJsonSchema,\n types: readonly ApiJsonSchemaType[],\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n const matched = types.find((type: ApiJsonSchemaType) =>\n ApiJsonSchemaValidator.isOfType(type, value),\n );\n if (matched === undefined) {\n return new DtoValidationFailure(path, `${path} must be one of: ${types.join(', ')}`);\n }\n return this.validateAs(matched, schema, value, path);\n }\n\n /** True when `value`'s JSON type is `type`; `integer` is a whole `number`. */\n // webpieces-disable no-function-outside-class -- private static predicate of this validator\n private static isOfType(type: ApiJsonSchemaType, value: DtoValue): boolean {\n if (type === 'array') return Array.isArray(value);\n if (type === 'object') return typeof value === 'object' && value !== null && !Array.isArray(value);\n if (type === 'integer') return typeof value === 'number' && Number.isInteger(value);\n if (type === 'number') return typeof value === 'number' && Number.isFinite(value);\n if (type === 'null') return value === null;\n return typeof value === type;\n }\n\n /** Every member of `type` except `null`, in declaration order — empty when `type` is absent. */\n // webpieces-disable no-function-outside-class -- private static reader of a schema\n private static nonNullTypesOf(schema: ApiJsonSchema): readonly ApiJsonSchemaType[] {\n if (schema.type === undefined) return [];\n if (typeof schema.type === 'string') return [schema.type];\n return schema.type.filter((one: ApiJsonSchemaType) => one !== 'null');\n }\n\n /** Validate `value` as ONE JSON type of `schema` — `undefined` (no type) accepts anything. */\n private validateAs(\n type: ApiJsonSchemaType | undefined,\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n switch (type) {\n case 'object':\n return this.validateObject(schema, value, path);\n case 'array':\n return this.validateArray(schema, value, path);\n case 'string':\n return this.validateString(schema, value, path);\n case 'number':\n case 'integer':\n return this.validateNumber(type, schema, value, path);\n case 'boolean':\n return typeof value === 'boolean'\n ? undefined\n : new DtoValidationFailure(path, `${path} must be a boolean`);\n default:\n return undefined;\n }\n }\n\n /**\n * A union, validated BY DISCRIMINATOR.\n *\n * The discriminator property is read FIRST and the branch chosen from it, so a value naming no\n * branch is reported as what it is — `$.window.kind must be one of: scheduled | asap`, naming the\n * field that is wrong and the values it may hold. Trying every branch and reporting \"no branch\n * matched\" tells a caller nothing they can act on: it names neither the offending property nor\n * the legal values, and it hides a branch that matched on `kind` and failed three levels down.\n *\n * The branch is selected by the branches' own `enum`s, never by\n * {@link ApiJsonSchemaDiscriminator.mapping} — the branches ARE the schema, so selection and\n * validation read one source and cannot disagree.\n */\n private validateUnion(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n const branches = schema.oneOf ?? [];\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an object`);\n }\n const discriminator = schema.discriminator;\n if (discriminator === undefined) {\n return this.validateAnyBranch(branches, value, path);\n }\n const property = discriminator.propertyName;\n const held: DtoValue = Object.getOwnPropertyDescriptor(value, property)?.value;\n const branch = branches.find((one: ApiJsonSchema) =>\n ApiJsonSchemaValidator.branchValuesOf(one, property).includes(held as string),\n );\n if (branch === undefined) {\n const legal = branches\n .flatMap((one: ApiJsonSchema) =>\n ApiJsonSchemaValidator.branchValuesOf(one, property),\n )\n .join(' | ');\n return new DtoValidationFailure(\n `${path}.${property}`,\n `${path}.${property} must be one of: ${legal}`,\n );\n }\n return this.validateAt(branch, value, path);\n }\n\n /**\n * A union with NO discriminator — one TypeScript itself cannot narrow. Every branch is tried and\n * the value accepted when one matches. There is no better report when none does: the source does\n * not narrow, so no property can honestly be named as the wrong one.\n */\n private validateAnyBranch(\n branches: readonly ApiJsonSchema[],\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n for (const branch of branches) {\n if (this.validateAt(branch, value, path) === undefined) {\n return undefined;\n }\n }\n return new DtoValidationFailure(\n path,\n `${path} matches none of the ${branches.length} accepted shapes`,\n );\n }\n\n /** The string literals ONE branch declares for the discriminator property. */\n // webpieces-disable no-function-outside-class -- private static reader of a branch schema\n private static branchValuesOf(branch: ApiJsonSchema, property: string): readonly string[] {\n const declared = Object.getOwnPropertyDescriptor(branch.properties ?? {}, property)\n ?.value as ApiJsonSchema | undefined;\n return (declared?.enum ?? []).filter((value: string | null): value is string => value !== null);\n }\n\n private validateObject(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an object`);\n }\n const properties = schema.properties ?? {};\n const extra = schema.additionalProperties;\n for (const key of Object.keys(value)) {\n const entry: DtoValue = Object.getOwnPropertyDescriptor(value, key)?.value;\n // A key HOLDING `undefined` is ABSENT: `JSON.stringify` drops it, so the document the\n // caller receives never carries it, and the `required` loop below already reads\n // `=== undefined` as missing. Validating it here made the two halves of this one walk\n // disagree — `{ next: hasMore ? cursor : undefined }` was refused as \"must be a string\"\n // although its JSON is valid (#1055). `null` is a VALUE and is still checked.\n if ((entry as DtoValue | undefined) === undefined) continue;\n const declared = Object.getOwnPropertyDescriptor(properties, key)?.value as\n | ApiJsonSchema\n | undefined;\n if (declared === undefined) {\n // `additionalProperties: true` is an EXPLICITLY open object — the JSON payload\n // `JsonValueDto` publishes (#1447) — and accepts any key holding any JSON value. It is\n // not the same as an ABSENT `additionalProperties`, which stays strict below.\n if (extra === true) continue;\n // An undeclared key is refused unless the schema is a TYPED MAP — also when the\n // schema is OPEN (no `additionalProperties`). That is deliberate (#1441): a tool's\n // published outputSchema is opened for clients by `ApiJsonSchema.openForClients`,\n // and the server still checks its OWN result exactly against what it declared.\n if (typeof extra !== 'object' || extra === null) {\n return new DtoValidationFailure(\n `${path}.${key}`,\n `${path}.${key} is not allowed`,\n );\n }\n const failure = this.validateAt(extra, entry, `${path}.${key}`);\n if (failure) return failure;\n continue;\n }\n if (entry === null && !ApiJsonSchema.allowsNull(declared)) {\n return ApiJsonSchemaValidator.nullPropertyFailure(schema, key, `${path}.${key}`);\n }\n const failure = this.validateAt(declared, entry, `${path}.${key}`);\n if (failure) return failure;\n }\n for (const name of schema.required ?? []) {\n if (Object.getOwnPropertyDescriptor(value, name)?.value === undefined) {\n return new DtoValidationFailure(`${path}.${name}`, `${path}.${name} is required`);\n }\n }\n return undefined;\n }\n\n /**\n * `null` in a DECLARED property that does not allow it, worded so the message names the fix\n * (#1496). The bare `must not be null` left the reader to work out whether the DTO or the value was\n * wrong; the two cases have different cures, so they get different text.\n */\n // webpieces-disable no-function-outside-class -- private static message builder of this validator\n private static nullPropertyFailure(\n schema: ApiJsonSchema,\n key: string,\n at: string,\n ): DtoValidationFailure {\n if ((schema.required ?? []).includes(key)) {\n return new DtoValidationFailure(\n at,\n `${at} is required in the DTO but holds null — supply a value, or declare the field optional (or \\`| null\\`) in the DTO`,\n );\n }\n return new DtoValidationFailure(\n at,\n `${at} is optional in the DTO; omit the field instead of setting it to null`,\n );\n }\n\n private validateArray(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (!Array.isArray(value)) {\n return new DtoValidationFailure(path, `${path} must be an array`);\n }\n const items = schema.items;\n if (items === undefined) return undefined;\n for (let index = 0; index < value.length; index += 1) {\n const failure = this.validateAt(items, value[index], `${path}[${index}]`);\n if (failure) return failure;\n }\n return undefined;\n }\n\n private validateString(\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'string') {\n return new DtoValidationFailure(path, `${path} must be a string`);\n }\n if (schema.enum && !schema.enum.includes(value)) {\n return new DtoValidationFailure(\n path,\n `${path} must be one of: ${schema.enum.map(String).join(', ')}`,\n );\n }\n return undefined;\n }\n\n private validateNumber(\n type: 'number' | 'integer',\n schema: ApiJsonSchema,\n value: DtoValue,\n path: string,\n ): DtoValidationFailure | undefined {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n return new DtoValidationFailure(path, `${path} must be a number`);\n }\n if (type === 'integer' && !Number.isInteger(value)) {\n return new DtoValidationFailure(path, `${path} must be an integer`);\n }\n if (schema.minimum !== undefined && value < schema.minimum) {\n return new DtoValidationFailure(path, `${path} must be >= ${schema.minimum}`);\n }\n if (schema.maximum !== undefined && value > schema.maximum) {\n return new DtoValidationFailure(path, `${path} must be <= ${schema.maximum}`);\n }\n return undefined;\n }\n}\n"]}
|