@webpieces/core-util 0.4.806 → 0.4.808

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.
@@ -1,64 +1,56 @@
1
- import 'reflect-metadata';
2
- export type DtoClass = abstract new (...args: any[]) => unknown;
3
- /** The element type of an array or the value type of a map; TypeScript erases both at runtime. */
4
- export type DtoElementType = 'string' | 'number' | 'integer' | 'boolean' | DtoClass;
1
+ /**
2
+ * The JSON Schema subset webpieces publishes on its API boundaries, and the ONE runtime validator of
3
+ * it.
4
+ *
5
+ * ## Nothing here reads reflect-metadata any more
6
+ *
7
+ * Until #984 this file also carried `@WpDto`, `@WpDtoField`, `WpDtoFieldOptions`,
8
+ * `WpDtoMapFieldOptions`, `WpMcpHeader`, `@WpResponseDto` and `DtoSchemaBuilder` — a second, runtime
9
+ * spelling of facts the TypeScript compiler already holds. #983 measured the two against each other
10
+ * and found the compiler reproduces every one of those arguments byte for byte, so they are deleted
11
+ * rather than deprecated (`.claude/rules/no-backwards-compat.md`): a schema is now BUILT at build
12
+ * time from the source by `@webpieces/api-doc-model`, and the runtime is HANDED the result.
13
+ *
14
+ * What survives is the schema itself, a builder for the boundaries that construct one by hand, and a
15
+ * validator that reads the schema instead of a decorator.
16
+ */
5
17
  /** Runtime values accepted at the DTO validation boundary. */
6
18
  export type DtoValue = object | string | number | boolean | null;
7
- /** Additive field metadata; the scalar/object type itself comes from `design:type`. */
8
- export declare class WpDtoFieldOptions {
9
- readonly description: string;
10
- /** Required/optional is explicit because TypeScript erases `?` at runtime. */
11
- readonly required: boolean;
12
- /** Required for arrays because TypeScript erases their element type. */
13
- readonly arrayItems?: DtoElementType | undefined;
14
- /** A reflected Number is otherwise emitted as JSON Schema `number`. */
15
- readonly integer: boolean;
16
- readonly minimum?: number | undefined;
17
- readonly maximum?: number | undefined;
18
- readonly enumValues?: readonly [string, ...string[]] | undefined;
19
- /** MCP 2026 SEP-2243 header mirrored as `Mcp-Param-{name}`. */
20
- readonly mcpHeader?: WpMcpHeader | undefined;
21
- constructor(description: string,
22
- /** Required/optional is explicit because TypeScript erases `?` at runtime. */
23
- required: boolean,
24
- /** Required for arrays because TypeScript erases their element type. */
25
- arrayItems?: DtoElementType | undefined,
26
- /** A reflected Number is otherwise emitted as JSON Schema `number`. */
27
- integer?: boolean, minimum?: number | undefined, maximum?: number | undefined, enumValues?: readonly [string, ...string[]] | undefined,
28
- /** MCP 2026 SEP-2243 header mirrored as `Mcp-Param-{name}`. */
29
- mcpHeader?: WpMcpHeader | undefined);
30
- }
31
19
  /**
32
- * Names the `x-mcp-header` a primitive tool parameter is mirrored into (`Mcp-Param-{name}`). A class,
33
- * not a bare string, so the 8th `WpDtoFieldOptions` argument can never be mistaken for a map value
34
- * type (maps use `WpDtoMapFieldOptions`).
20
+ * One JSON Schema `type`. `null` is a member because NULLABLE is expressed as `["string", "null"]`,
21
+ * which is how JSON Schema says "present, and may hold null" — a different wire document from an
22
+ * ABSENT property, and one the reflect-metadata runtime could neither see nor write down.
35
23
  */
36
- export declare class WpMcpHeader {
37
- readonly name: string;
38
- constructor(name: string);
39
- }
24
+ export type ApiJsonSchemaType = 'object' | 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'null';
40
25
  /**
41
- * Field metadata for a typed map such as `Record<string, string>` or `Record<string, SomeDto>`.
42
- * Keys are open strings; every VALUE must match `mapValues`. The value type is explicit for the same
43
- * reason `arrayItems` is: TypeScript erases it, and a `Record` reflects as plain `Object`.
26
+ * The DISCRIMINATOR of a {@link ApiJsonSchema.oneOf} — which property narrows the union, and what
27
+ * each of its values names.
28
+ *
29
+ * The shape MIRRORS what `OpenApiGenerator`'s `SchemaRenderer` already emits for the same union
30
+ * (`propertyName` + `mapping`), because a union has exactly ONE published spelling in this repo and
31
+ * the OpenAPI renderer is the reference implementation of it. The one difference is what a mapping
32
+ * VALUE holds: OpenAPI maps a value to `#/components/schemas/ScheduledWindow`, and an MCP tool
33
+ * schema is INLINE and has no `$ref` to point at, so it maps to the branch's type NAME.
34
+ *
35
+ * `mapping` is DOCUMENTATION for the reader. {@link ApiJsonSchemaValidator} never narrows with it:
36
+ * it selects the branch whose own `enum` carries the value, so the accepted request and the
37
+ * published document cannot disagree about which branch a value means.
44
38
  */
45
- export declare class WpDtoMapFieldOptions {
46
- readonly description: string;
47
- /** Required/optional is explicit because TypeScript erases `?` at runtime. */
48
- readonly required: boolean;
49
- readonly mapValues: DtoElementType;
50
- constructor(description: string,
51
- /** Required/optional is explicit because TypeScript erases `?` at runtime. */
52
- required: boolean, mapValues: DtoElementType);
53
- }
54
- export declare class WpDtoFieldMetadata {
55
- readonly propertyKey: string;
56
- readonly options: WpDtoFieldOptions | WpDtoMapFieldOptions;
57
- constructor(propertyKey: string, options: WpDtoFieldOptions | WpDtoMapFieldOptions);
39
+ export declare class ApiJsonSchemaDiscriminator {
40
+ /** The property every branch declares as exactly one string literal, e.g. `kind`. */
41
+ propertyName: string;
42
+ /** discriminator value -> the branch's TYPE NAME (MCP is inline: there is no `$ref`). */
43
+ mapping: Record<string, string>;
44
+ constructor(
45
+ /** The property every branch declares as exactly one string literal, e.g. `kind`. */
46
+ propertyName: string,
47
+ /** discriminator value -> the branch's TYPE NAME (MCP is inline: there is no `$ref`). */
48
+ mapping: Record<string, string>);
58
49
  }
59
50
  /** The JSON Schema subset emitted for Webpieces API DTOs. */
60
51
  export declare class ApiJsonSchema {
61
- type?: 'object' | 'string' | 'number' | 'integer' | 'boolean' | 'array';
52
+ /** One type, or `[T, "null"]` for a nullable field. */
53
+ type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[];
62
54
  description?: string;
63
55
  properties?: Record<string, ApiJsonSchema>;
64
56
  required?: string[];
@@ -68,9 +60,60 @@ export declare class ApiJsonSchema {
68
60
  enum?: string[];
69
61
  minimum?: number;
70
62
  maximum?: number;
63
+ /**
64
+ * The branches of a union, INLINE (MCP tool schemas have no `$ref`), in declaration order.
65
+ *
66
+ * A union is legal NESTED, inside a property. It is NOT legal at the ROOT of a tool's parameter
67
+ * schema — both the OpenAI and the Anthropic function-calling APIs reject a top-level
68
+ * `oneOf`/`anyOf`/`allOf`, and because a server sends its whole tool list on every request, one
69
+ * such tool makes EVERY request 400. That is refused at build time by the `no-root-union-api-type`
70
+ * rule, before a contract can be published at all.
71
+ */
72
+ oneOf?: readonly ApiJsonSchema[];
73
+ /**
74
+ * Set ONLY when every branch carries the same property typed as one string literal — DERIVED,
75
+ * never invented. A union TypeScript itself cannot narrow is published as a bare {@link oneOf},
76
+ * because claiming a narrowing the source does not have is worse than admitting there is none.
77
+ */
78
+ discriminator?: ApiJsonSchemaDiscriminator;
71
79
  /** MCP 2026 SEP-2243 schema extension consumed by the official SDK. */
72
80
  'x-mcp-header'?: string;
73
- constructor(type?: 'object' | 'string' | 'number' | 'integer' | 'boolean' | 'array');
81
+ constructor(type?: ApiJsonSchemaType | readonly ApiJsonSchemaType[]);
82
+ /**
83
+ * The non-`null` member of the type, which is the one that decides how a value is checked.
84
+ *
85
+ * These three are STATIC and not instance methods on purpose: an instance method would put a
86
+ * function on the structural type, and this class is handed straight to the MCP SDK, whose
87
+ * parameter types are index-signature JSON objects that a class carrying methods no longer
88
+ * satisfies. It stays a data-only structure (`CLAUDE.md` §1) and the helpers live beside it.
89
+ */
90
+ static baseTypeOf(schema: ApiJsonSchema): ApiJsonSchemaType | undefined;
91
+ /** True when the schema was widened with `"null"`, i.e. the property may hold `null`. */
92
+ static allowsNull(schema: ApiJsonSchema): boolean;
93
+ /**
94
+ * The one closure gate for an object schema. Closed means no key can carry an unspecified value:
95
+ * either extra keys are forbidden (`additionalProperties: false`) or every extra key's value is
96
+ * typed (`additionalProperties: <schema>`, a typed map). A missing or `true` value is open.
97
+ * Use this instead of `schema.additionalProperties === false`, which rejects typed maps.
98
+ */
99
+ static isClosed(schema: ApiJsonSchema): boolean;
100
+ }
101
+ /**
102
+ * Builds the CLOSED object schema a hand-written runtime boundary validates against — `@WpStream`'s
103
+ * two event schemas are the whole of its client list today.
104
+ *
105
+ * It exists because the alternative spellings are both worse: a decorator that re-states the field
106
+ * types the compiler already knows is exactly what #984 deleted, and a bare object literal is what
107
+ * `CLAUDE.md` §3 forbids. A generated schema never comes through here — it is read from the build's
108
+ * own artifact.
109
+ */
110
+ export declare class ObjectSchemaBuilder {
111
+ private readonly schema;
112
+ private readonly requiredNames;
113
+ constructor();
114
+ required(name: string, type: ApiJsonSchema): this;
115
+ optional(name: string, type: ApiJsonSchema): this;
116
+ build(): ApiJsonSchema;
74
117
  }
75
118
  /** A validation result safe to show to an API caller/model. */
76
119
  export declare class DtoValidationFailure {
@@ -81,35 +124,42 @@ export declare class DtoValidationFailure {
81
124
  /** JSON path of the offending value, e.g. `$.address.city`. */
82
125
  field: string, message: string);
83
126
  }
84
- /** Marks a request/response class as a schema-bearing DTO, including intentionally empty DTOs. */
85
- export declare function WpDto(): ClassDecorator;
86
- /** Adds documentation and runtime-only shape facts to a DTO property. */
87
- export declare function WpDtoField(options: WpDtoFieldOptions | WpDtoMapFieldOptions): PropertyDecorator;
88
- /** Declares an async API method's response DTO because reflection sees Promise, not its generic. */
89
- export declare function WpResponseDto(responseDto: () => DtoClass): MethodDecorator;
90
- /** Builds and validates the same DTO schema used for MCP input and structured output. */
91
- export declare class DtoSchemaBuilder {
92
- requestClassOf(apiClass: Function, methodName: string): DtoClass;
93
- responseClassOf(apiClass: Function, methodName: string): DtoClass;
127
+ /**
128
+ * Validates a value against an {@link ApiJsonSchema} — the ONE runtime narrowing boundary for MCP
129
+ * tool arguments, MCP structured output and typed stream events.
130
+ *
131
+ * It reads the SCHEMA, which is the same object the build published to agents, so a document that
132
+ * says a field is required and a server that accepts it missing cannot disagree. The predecessor
133
+ * walked reflect-metadata instead, which meant the published document and the accepted request were
134
+ * derived from two different declarations.
135
+ */
136
+ export declare class ApiJsonSchemaValidator {
137
+ validate(schema: ApiJsonSchema, value: DtoValue): DtoValidationFailure | undefined;
138
+ private validateAt;
94
139
  /**
95
- * The one closure gate for an object schema. Closed means no key can carry an unspecified value:
96
- * either extra keys are forbidden (`additionalProperties: false`) or every extra key's value is
97
- * typed (`additionalProperties: <schema>`, a typed map). A missing or `true` value is open.
98
- * Use this instead of `schema.additionalProperties === false`, which rejects typed maps.
140
+ * A union, validated BY DISCRIMINATOR.
141
+ *
142
+ * The discriminator property is read FIRST and the branch chosen from it, so a value naming no
143
+ * branch is reported as what it is — `$.window.kind must be one of: scheduled | asap`, naming the
144
+ * field that is wrong and the values it may hold. Trying every branch and reporting "no branch
145
+ * matched" tells a caller nothing they can act on: it names neither the offending property nor
146
+ * the legal values, and it hides a branch that matched on `kind` and failed three levels down.
147
+ *
148
+ * The branch is selected by the branches' own `enum`s, never by
149
+ * {@link ApiJsonSchemaDiscriminator.mapping} — the branches ARE the schema, so selection and
150
+ * validation read one source and cannot disagree.
99
151
  */
100
- isClosedSchema(schema: ApiJsonSchema): boolean;
101
- build(dtoClass: DtoClass): ApiJsonSchema;
102
- validate(dtoClass: DtoClass, value: DtoValue): DtoValidationFailure | undefined;
103
- private buildAt;
104
- private fieldSchema;
105
- private validateOptions;
106
- private itemSchema;
107
- private validateMcpHeaders;
108
- private validateAt;
109
- private validateField;
152
+ private validateUnion;
153
+ /**
154
+ * A union with NO discriminator — one TypeScript itself cannot narrow. Every branch is tried and
155
+ * the value accepted when one matches. There is no better report when none does: the source does
156
+ * not narrow, so no property can honestly be named as the wrong one.
157
+ */
158
+ private validateAnyBranch;
159
+ /** The string literals ONE branch declares for the discriminator property. */
160
+ private static branchValuesOf;
161
+ private validateObject;
110
162
  private validateArray;
111
- private validateMap;
112
- private validateElement;
113
- private fields;
114
- private requireDto;
163
+ private validateString;
164
+ private validateNumber;
115
165
  }