@orpc/json-schema 2.0.0-beta.47 → 2.0.0-beta.49
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/README.md +6 -7
- package/dist/coercer.d.ts +29 -0
- package/dist/coercer.js +498 -0
- package/dist/composition-utils.d.ts +41 -0
- package/dist/composition-utils.js +380 -0
- package/dist/constants.d.ts +8 -0
- package/dist/constants.js +66 -0
- package/dist/convert.d.ts +29 -0
- package/dist/convert.js +14 -0
- package/dist/index.d.ts +10 -297
- package/dist/index.js +9 -0
- package/dist/ref-utils.d.ts +59 -0
- package/dist/ref-utils.js +253 -0
- package/dist/smart-coercion-handler-plugin.d.ts +22 -0
- package/dist/smart-coercion-handler-plugin.js +50 -0
- package/dist/smart-coercion-link-plugin.d.ts +28 -0
- package/dist/smart-coercion-link-plugin.js +75 -0
- package/dist/standard-json-schema-converter.d.ts +16 -0
- package/dist/standard-json-schema-converter.js +45 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.js +40 -0
- package/dist/utils.d.ts +45 -0
- package/dist/utils.js +106 -0
- package/package.json +25 -27
- package/dist/index.d.mts +0 -297
- package/dist/index.mjs +0 -1117
package/dist/index.d.ts
CHANGED
|
@@ -1,297 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* A JSON Schema (draft 2020-12) representation used across oRPC's JSON schema tooling.
|
|
12
|
-
*
|
|
13
|
-
* @remarks
|
|
14
|
-
* It is also the OpenAPI 3.2 Schema Object, so converted schemas embed into OpenAPI documents as-is.
|
|
15
|
-
*
|
|
16
|
-
* @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
|
|
17
|
-
*/
|
|
18
|
-
type JsonSchema<Value = any> = OpenAPIV3_2.SchemaObject<Value>;
|
|
19
|
-
/**
|
|
20
|
-
* The declared JSON Schema keywords, the index signature is filtered out.
|
|
21
|
-
*/
|
|
22
|
-
type JsonSchemaKeywords = keyof {
|
|
23
|
-
[K in keyof OpenAPIV3_2.SchemaObjectFields as string extends K ? never : K]: unknown;
|
|
24
|
-
};
|
|
25
|
-
declare enum JsonSchemaType {
|
|
26
|
-
Array = "array",
|
|
27
|
-
Boolean = "boolean",
|
|
28
|
-
Integer = "integer",
|
|
29
|
-
Null = "null",
|
|
30
|
-
Number = "number",
|
|
31
|
-
Object = "object",
|
|
32
|
-
String = "string"
|
|
33
|
-
}
|
|
34
|
-
declare enum JsonSchemaFormat {
|
|
35
|
-
Date = "date",
|
|
36
|
-
DateTime = "date-time",
|
|
37
|
-
Duration = "duration",
|
|
38
|
-
Email = "email",
|
|
39
|
-
Hostname = "hostname",
|
|
40
|
-
IDNEmail = "idn-email",
|
|
41
|
-
IDNHostname = "idn-hostname",
|
|
42
|
-
IPv4 = "ipv4",
|
|
43
|
-
IPv6 = "ipv6",
|
|
44
|
-
IRI = "iri",
|
|
45
|
-
IRIReference = "iri-reference",
|
|
46
|
-
JSONPointer = "json-pointer",
|
|
47
|
-
RegEx = "regex",
|
|
48
|
-
RelativeJSONPointer = "relative-json-pointer",
|
|
49
|
-
Time = "time",
|
|
50
|
-
URI = "uri",
|
|
51
|
-
URIReference = "uri-reference",
|
|
52
|
-
URITemplate = "uri-template",
|
|
53
|
-
UUID = "uuid"
|
|
54
|
-
}
|
|
55
|
-
declare enum JsonSchemaXNativeType {
|
|
56
|
-
BigInt = "bigint",
|
|
57
|
-
Date = "date",
|
|
58
|
-
Url = "url",
|
|
59
|
-
Set = "set",
|
|
60
|
-
Map = "map"
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
interface JsonSchemaCoercerOptions {
|
|
64
|
-
/**
|
|
65
|
-
* Maximum digits, excluding a leading `-`, of a string coerced to `bigint`. Longer strings are left unchanged.
|
|
66
|
-
* `Number.POSITIVE_INFINITY` disables it.
|
|
67
|
-
*
|
|
68
|
-
* @default 4300
|
|
69
|
-
*/
|
|
70
|
-
maxBigIntDigits?: number | undefined;
|
|
71
|
-
/**
|
|
72
|
-
* Maximum coercion steps per input value, where a step applies one schema to one value.
|
|
73
|
-
* Inputs with fewer than 100 values are budgeted as 100. Past the budget the whole input is left uncoerced,
|
|
74
|
-
* so validation still runs and reports any mismatch.
|
|
75
|
-
*
|
|
76
|
-
* @default 1000
|
|
77
|
-
*/
|
|
78
|
-
maxStepsPerValue?: number | undefined;
|
|
79
|
-
}
|
|
80
|
-
declare class JsonSchemaCoercer {
|
|
81
|
-
private readonly nativeCoerceOptions;
|
|
82
|
-
private readonly maxStepsPerValue;
|
|
83
|
-
/** Reused between calls, since building a context is slow until V8 warms up. */
|
|
84
|
-
private idleContext;
|
|
85
|
-
constructor(options?: JsonSchemaCoercerOptions);
|
|
86
|
-
coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
|
|
87
|
-
private coerceInternal;
|
|
88
|
-
private coerceRef;
|
|
89
|
-
private extendBudget;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* These utilities assume the schema has only one root-level `$defs` object
|
|
94
|
-
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
|
|
98
|
-
type JsonFileSchema = JsonSchema & object & {
|
|
99
|
-
type: 'string';
|
|
100
|
-
contentMediaType?: string;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Returns true when the schema is a file-like string schema.
|
|
104
|
-
*/
|
|
105
|
-
declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
|
|
106
|
-
/**
|
|
107
|
-
* Returns true when a file schema appears anywhere in the schema, such as in a property, a composition, or a def.
|
|
108
|
-
*/
|
|
109
|
-
declare function containsJsonFileSchema(schema: JsonSchema): boolean;
|
|
110
|
-
type JsonObjectSchema = JsonSchema & object & {
|
|
111
|
-
type: 'object';
|
|
112
|
-
};
|
|
113
|
-
/**
|
|
114
|
-
* Returns true when the schema is an object schema.
|
|
115
|
-
*/
|
|
116
|
-
declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
|
|
117
|
-
type JsonArraySchema = JsonSchema & object & {
|
|
118
|
-
type: 'array';
|
|
119
|
-
};
|
|
120
|
-
/**
|
|
121
|
-
* Returns true when the schema is an array schema.
|
|
122
|
-
*/
|
|
123
|
-
declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
|
|
124
|
-
/**
|
|
125
|
-
* Returns true when the schema does not apply any recognized constraints.
|
|
126
|
-
*/
|
|
127
|
-
declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
|
|
128
|
-
/**
|
|
129
|
-
* Ensures a JSON Schema is represented as an object schema document.
|
|
130
|
-
*/
|
|
131
|
-
declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* These utilities assume the schema has only one root-level `$defs` object
|
|
135
|
-
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
|
|
140
|
-
*/
|
|
141
|
-
declare function combineJsonSchemasWithComposition(keyword: 'allOf' | 'anyOf' | 'oneOf', schemas: JsonSchema[]): JsonSchema;
|
|
142
|
-
type JsonObjectSchemaEntry = [name: string, schema: JsonSchema, optional: boolean];
|
|
143
|
-
/**
|
|
144
|
-
* Combines object property entries back into a single object schema.
|
|
145
|
-
*/
|
|
146
|
-
declare function combineJsonObjectSchemaEntries(entries: JsonObjectSchemaEntry[]): JsonObjectSchema;
|
|
147
|
-
/**
|
|
148
|
-
* Parses an object schema, or a composition of object schemas, into property entries.
|
|
149
|
-
*/
|
|
150
|
-
declare function extractJsonObjectSchemaEntries(schema: JsonSchema): JsonObjectSchemaEntry[] | undefined;
|
|
151
|
-
/**
|
|
152
|
-
* Returns true when an object can stand for every value the schema describes: the schema constrains objects,
|
|
153
|
-
* by `type` or object keywords, composes only such schemas, or is unconstrained, since an object satisfies it.
|
|
154
|
-
* Unlike {@link extractJsonObjectSchemaEntries}, an unconstrained schema counts as object-like.
|
|
155
|
-
*/
|
|
156
|
-
declare function isJsonObjectLikeSchema(schema: JsonSchema): boolean;
|
|
157
|
-
/**
|
|
158
|
-
* Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
|
|
159
|
-
*/
|
|
160
|
-
declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
|
|
161
|
-
/**
|
|
162
|
-
* Matches a union made of a single item schema and its array form.
|
|
163
|
-
*/
|
|
164
|
-
declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
|
|
165
|
-
declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* The conversion direction: `input` targets the schema's input type, `output` targets its output type.
|
|
169
|
-
*
|
|
170
|
-
* @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
|
|
171
|
-
*/
|
|
172
|
-
type JsonSchemaConverterDirection = 'input' | 'output';
|
|
173
|
-
/**
|
|
174
|
-
* Interface for converting validation schemas into JSON Schema representations,
|
|
175
|
-
* used by tools such as the OpenAPI Generator and Smart Coercion plugins.
|
|
176
|
-
*
|
|
177
|
-
* @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
|
|
178
|
-
*/
|
|
179
|
-
interface JsonSchemaConverter {
|
|
180
|
-
/**
|
|
181
|
-
* Determines whether this converter can handle the given schema.
|
|
182
|
-
*/
|
|
183
|
-
condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): boolean;
|
|
184
|
-
/**
|
|
185
|
-
* Converts an ORPC schema to a JSON Schema representation.
|
|
186
|
-
*/
|
|
187
|
-
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
188
|
-
}
|
|
189
|
-
declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
|
|
190
|
-
private readonly converters;
|
|
191
|
-
constructor(converters?: JsonSchemaConverter[]);
|
|
192
|
-
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* These utilities assume the schema has only one root-level `$defs` object
|
|
197
|
-
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
198
|
-
*/
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Encodes a JSON Pointer segment according to RFC 6901.
|
|
202
|
-
*
|
|
203
|
-
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
204
|
-
*/
|
|
205
|
-
declare function encodeJsonPointerSegment(segment: string): string;
|
|
206
|
-
/**
|
|
207
|
-
* Decodes a JSON Pointer segment according to RFC 6901.
|
|
208
|
-
*
|
|
209
|
-
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
210
|
-
*/
|
|
211
|
-
declare function decodeJsonPointerSegment(segment: string): string;
|
|
212
|
-
/**
|
|
213
|
-
* Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
|
|
214
|
-
* Shared or cyclic object instances are visited once.
|
|
215
|
-
*/
|
|
216
|
-
declare function visitJsonSchemaRefs(value: JsonSchema, visit: (ref: string) => void, schemaLevel?: boolean, seen?: Set<object>): void;
|
|
217
|
-
declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
|
|
218
|
-
/**
|
|
219
|
-
* Rewrites recursive root `#` refs by moving the schema body into `$defs`.
|
|
220
|
-
*/
|
|
221
|
-
declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
|
|
222
|
-
/**
|
|
223
|
-
* Resolves a local `$ref` at the **root level** of the given schema, if present.
|
|
224
|
-
*
|
|
225
|
-
* Only handles refs of the form `#/$defs/<name>` pointing into the provided
|
|
226
|
-
* (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
|
|
227
|
-
* intentionally left untouched.
|
|
228
|
-
*
|
|
229
|
-
* If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
|
|
230
|
-
* schema is returned as-is. Chained refs are followed until one repeats,
|
|
231
|
-
* which is kept.
|
|
232
|
-
*
|
|
233
|
-
* @param schema - The schema whose root-level `$ref` should be resolved.
|
|
234
|
-
* @param $defs - Definition map to resolve against. If omitted, falls back to
|
|
235
|
-
* `schema.$defs`. When provided, takes precedence over any `$defs` embedded
|
|
236
|
-
* in the schema.
|
|
237
|
-
*/
|
|
238
|
-
declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
|
|
239
|
-
|
|
240
|
-
interface SmartCoercionHandlerPluginOptions extends JsonSchemaCoercerOptions {
|
|
241
|
-
converters?: JsonSchemaConverter[] | undefined;
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Coerces incoming request data to match the expected `.input` schema types before validation,
|
|
245
|
-
* based on JSON schemas produced by the configured converters.
|
|
246
|
-
*
|
|
247
|
-
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
248
|
-
*/
|
|
249
|
-
declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
250
|
-
name: string;
|
|
251
|
-
private readonly converter;
|
|
252
|
-
private readonly coercer;
|
|
253
|
-
private readonly cache;
|
|
254
|
-
constructor(options?: SmartCoercionHandlerPluginOptions);
|
|
255
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
256
|
-
private coerceValue;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
interface SmartCoercionLinkPluginOptions extends JsonSchemaCoercerOptions {
|
|
260
|
-
converters?: JsonSchemaConverter[] | undefined;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Coerces server responses to match the expected `.output` or `.errors` schema types before validation,
|
|
264
|
-
* based on JSON schemas produced by the configured converters.
|
|
265
|
-
*
|
|
266
|
-
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
267
|
-
*/
|
|
268
|
-
declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
269
|
-
private readonly contract;
|
|
270
|
-
name: string;
|
|
271
|
-
/**
|
|
272
|
-
* Output and error values should be coerced before validation.
|
|
273
|
-
*/
|
|
274
|
-
after: string[];
|
|
275
|
-
private readonly converter;
|
|
276
|
-
private readonly coercer;
|
|
277
|
-
private readonly cache;
|
|
278
|
-
constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
|
|
279
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
280
|
-
private coerceValue;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Returns true when the schema accepts `undefined` in the given direction,
|
|
285
|
-
* found by validating `undefined` against it. For `output`, the validated value must also be `undefined`.
|
|
286
|
-
*
|
|
287
|
-
* Returns false when validation throws or is async. An async result is never left as an unhandled rejection.
|
|
288
|
-
*/
|
|
289
|
-
declare function isStandardSchemaOptional(schema: AnySchema, direction: JsonSchemaConverterDirection): boolean;
|
|
290
|
-
declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
|
|
291
|
-
condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
|
|
292
|
-
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
293
|
-
convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaFormat, JsonSchemaType, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, containsJsonFileSchema, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectLikeSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isStandardSchemaOptional, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef, visitJsonSchemaRefs };
|
|
297
|
-
export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaCoercerOptions, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };
|
|
1
|
+
export * from './coercer.ts';
|
|
2
|
+
export * from './composition-utils.ts';
|
|
3
|
+
export * from './convert.ts';
|
|
4
|
+
export * from './ref-utils.ts';
|
|
5
|
+
export * from './smart-coercion-handler-plugin.ts';
|
|
6
|
+
export * from './smart-coercion-link-plugin.ts';
|
|
7
|
+
export * from './standard-json-schema-converter.ts';
|
|
8
|
+
export * from './types.ts';
|
|
9
|
+
export * from './utils.ts';
|
|
10
|
+
export type { AnySchema, Schema } from '@orpc/contract';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './coercer.js';
|
|
2
|
+
export * from './composition-utils.js';
|
|
3
|
+
export * from './convert.js';
|
|
4
|
+
export * from './ref-utils.js';
|
|
5
|
+
export * from './smart-coercion-handler-plugin.js';
|
|
6
|
+
export * from './smart-coercion-link-plugin.js';
|
|
7
|
+
export * from './standard-json-schema-converter.js';
|
|
8
|
+
export * from './types.js';
|
|
9
|
+
export * from './utils.js';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These utilities assume the schema has only one root-level `$defs` object
|
|
3
|
+
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
4
|
+
*/
|
|
5
|
+
import type { JsonSchema } from './types.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Encodes a JSON Pointer segment according to RFC 6901.
|
|
8
|
+
*
|
|
9
|
+
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
10
|
+
*/
|
|
11
|
+
export declare function encodeJsonPointerSegment(segment: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Decodes a JSON Pointer segment according to RFC 6901.
|
|
14
|
+
*
|
|
15
|
+
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
16
|
+
*/
|
|
17
|
+
export declare function decodeJsonPointerSegment(segment: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves a local JSON pointer ref (`#` or `#/...`) against the root schema.
|
|
20
|
+
* Returns `undefined` for non-local refs and missing targets.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveJsonSchemaLocalPointer(root: JsonSchema, ref: string): JsonSchema | undefined;
|
|
23
|
+
export type JsonSchemaKeywordKind = 'ref' | 'discriminator' | 'schema' | 'schema-record' | 'data';
|
|
24
|
+
/**
|
|
25
|
+
* Classifies `value` under `key` in a schema object, as traversed by {@link visitJsonSchemaRefs}
|
|
26
|
+
* and {@link mapJsonSchemaRefs}: a `$ref` string, an OpenAPI `discriminator`, a subschema (or list
|
|
27
|
+
* of subschemas), a record of subschemas, or data that holds no refs (including `const`/`enum`).
|
|
28
|
+
*
|
|
29
|
+
* @param schemaLevel `false` when the object is itself a record of subschemas, e.g. `properties`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function getJsonSchemaKeywordKind(key: string, value: unknown, schemaLevel?: boolean): JsonSchemaKeywordKind;
|
|
32
|
+
/**
|
|
33
|
+
* Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
|
|
34
|
+
* Local pointers in OpenAPI `discriminator.mapping`/`defaultMapping` values are visited too.
|
|
35
|
+
* Shared or cyclic object instances are visited once.
|
|
36
|
+
*/
|
|
37
|
+
export declare function visitJsonSchemaRefs(value: JsonSchema, visit: (ref: string) => void, schemaLevel?: boolean, seen?: Set<object>): void;
|
|
38
|
+
export declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
|
|
39
|
+
/**
|
|
40
|
+
* Rewrites recursive root `#` refs by moving the schema body into `$defs`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
|
|
43
|
+
/**
|
|
44
|
+
* Resolves a local `$ref` at the **root level** of the given schema, if present.
|
|
45
|
+
*
|
|
46
|
+
* Only handles refs of the form `#/$defs/<name>` pointing into the provided
|
|
47
|
+
* (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
|
|
48
|
+
* intentionally left untouched.
|
|
49
|
+
*
|
|
50
|
+
* If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
|
|
51
|
+
* schema is returned as-is. Chained refs are followed until one repeats,
|
|
52
|
+
* which is kept.
|
|
53
|
+
*
|
|
54
|
+
* @param schema - The schema whose root-level `$ref` should be resolved.
|
|
55
|
+
* @param $defs - Definition map to resolve against. If omitted, falls back to
|
|
56
|
+
* `schema.$defs`. When provided, takes precedence over any `$defs` embedded
|
|
57
|
+
* in the schema.
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These utilities assume the schema has only one root-level `$defs` object
|
|
3
|
+
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
4
|
+
*/
|
|
5
|
+
import { get, isPlainObject, setOwn } from '@orpc/shared';
|
|
6
|
+
import { JSON_SCHEMA_LITERAL_KEYWORDS, JSON_SCHEMA_LOGIC_KEYWORDS, JSON_SCHEMA_RECORD_KEYWORDS, } from './constants.js';
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a JSON Pointer segment according to RFC 6901.
|
|
9
|
+
*
|
|
10
|
+
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
11
|
+
*/
|
|
12
|
+
export function encodeJsonPointerSegment(segment) {
|
|
13
|
+
return segment.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Decodes a JSON Pointer segment according to RFC 6901.
|
|
17
|
+
*
|
|
18
|
+
* https://datatracker.ietf.org/doc/html/rfc6901
|
|
19
|
+
*/
|
|
20
|
+
export function decodeJsonPointerSegment(segment) {
|
|
21
|
+
return segment.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Resolves a local JSON pointer ref (`#` or `#/...`) against the root schema.
|
|
25
|
+
* Returns `undefined` for non-local refs and missing targets.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveJsonSchemaLocalPointer(root, ref) {
|
|
28
|
+
if (ref === '#') {
|
|
29
|
+
return root;
|
|
30
|
+
}
|
|
31
|
+
if (!ref.startsWith('#/')) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return get(root, ref.slice(2).split('/').map(decodeJsonPointerSegment));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Classifies `value` under `key` in a schema object, as traversed by {@link visitJsonSchemaRefs}
|
|
38
|
+
* and {@link mapJsonSchemaRefs}: a `$ref` string, an OpenAPI `discriminator`, a subschema (or list
|
|
39
|
+
* of subschemas), a record of subschemas, or data that holds no refs (including `const`/`enum`).
|
|
40
|
+
*
|
|
41
|
+
* @param schemaLevel `false` when the object is itself a record of subschemas, e.g. `properties`.
|
|
42
|
+
*/
|
|
43
|
+
export function getJsonSchemaKeywordKind(key, value, schemaLevel = true) {
|
|
44
|
+
if (key === '$ref' && typeof value === 'string') {
|
|
45
|
+
return 'ref';
|
|
46
|
+
}
|
|
47
|
+
if (!schemaLevel) {
|
|
48
|
+
return 'schema';
|
|
49
|
+
}
|
|
50
|
+
if (key === 'discriminator') {
|
|
51
|
+
return 'discriminator';
|
|
52
|
+
}
|
|
53
|
+
if (JSON_SCHEMA_RECORD_KEYWORDS.has(key)) {
|
|
54
|
+
return 'schema-record';
|
|
55
|
+
}
|
|
56
|
+
if (JSON_SCHEMA_LOGIC_KEYWORDS.has(key) && !JSON_SCHEMA_LITERAL_KEYWORDS.has(key)) {
|
|
57
|
+
return 'schema';
|
|
58
|
+
}
|
|
59
|
+
return 'data';
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* OpenAPI `discriminator.mapping` and `discriminator.defaultMapping` values are refs when they
|
|
63
|
+
* are local JSON pointers; other values (bare schema names, external refs) are left alone.
|
|
64
|
+
*/
|
|
65
|
+
function isDiscriminatorRef(value) {
|
|
66
|
+
return typeof value === 'string' && value.startsWith('#');
|
|
67
|
+
}
|
|
68
|
+
function visitDiscriminatorRefs(discriminator, visit) {
|
|
69
|
+
if (!isPlainObject(discriminator)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const { mapping, defaultMapping } = discriminator;
|
|
73
|
+
const values = isPlainObject(mapping)
|
|
74
|
+
? [...Object.values(mapping), defaultMapping]
|
|
75
|
+
: [defaultMapping];
|
|
76
|
+
for (const value of values) {
|
|
77
|
+
if (isDiscriminatorRef(value)) {
|
|
78
|
+
visit(value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
|
|
84
|
+
* Local pointers in OpenAPI `discriminator.mapping`/`defaultMapping` values are visited too.
|
|
85
|
+
* Shared or cyclic object instances are visited once.
|
|
86
|
+
*/
|
|
87
|
+
export function visitJsonSchemaRefs(value, visit, schemaLevel = true, seen = new Set()) {
|
|
88
|
+
if (!value || typeof value !== 'object' || seen.has(value)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
seen.add(value);
|
|
92
|
+
if (Array.isArray(value)) {
|
|
93
|
+
for (const item of value) {
|
|
94
|
+
visitJsonSchemaRefs(item, visit, schemaLevel, seen);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
for (const key of Object.keys(value)) {
|
|
99
|
+
const val = value[key];
|
|
100
|
+
switch (getJsonSchemaKeywordKind(key, val, schemaLevel)) {
|
|
101
|
+
case 'ref':
|
|
102
|
+
visit(val);
|
|
103
|
+
break;
|
|
104
|
+
case 'discriminator':
|
|
105
|
+
visitDiscriminatorRefs(val, visit);
|
|
106
|
+
break;
|
|
107
|
+
case 'schema':
|
|
108
|
+
visitJsonSchemaRefs(val, visit, true, seen);
|
|
109
|
+
break;
|
|
110
|
+
case 'schema-record':
|
|
111
|
+
visitJsonSchemaRefs(val, visit, false, seen);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function mapJsonSchemaRefs(value, map, schemaLevel = true, path = []) {
|
|
117
|
+
if (!value || typeof value !== 'object') {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
return value.map((item, index) => mapJsonSchemaRefs(item, map, schemaLevel, [...path, index]));
|
|
122
|
+
}
|
|
123
|
+
const result = {};
|
|
124
|
+
for (const key of Object.keys(value)) {
|
|
125
|
+
const val = value[key];
|
|
126
|
+
switch (getJsonSchemaKeywordKind(key, val, schemaLevel)) {
|
|
127
|
+
case 'ref':
|
|
128
|
+
setOwn(result, key, map(val, [...path, key]));
|
|
129
|
+
break;
|
|
130
|
+
case 'discriminator':
|
|
131
|
+
setOwn(result, key, mapDiscriminatorRefs(val, map, path));
|
|
132
|
+
break;
|
|
133
|
+
case 'schema':
|
|
134
|
+
setOwn(result, key, mapJsonSchemaRefs(val, map, true, [...path, key]));
|
|
135
|
+
break;
|
|
136
|
+
case 'schema-record':
|
|
137
|
+
setOwn(result, key, mapJsonSchemaRefs(val, map, false, [...path, key]));
|
|
138
|
+
break;
|
|
139
|
+
default:
|
|
140
|
+
setOwn(result, key, val);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
function mapDiscriminatorRefs(discriminator, map, path) {
|
|
146
|
+
if (!isPlainObject(discriminator)) {
|
|
147
|
+
return discriminator;
|
|
148
|
+
}
|
|
149
|
+
const { mapping, defaultMapping } = discriminator;
|
|
150
|
+
let mappedMapping;
|
|
151
|
+
if (isPlainObject(mapping)) {
|
|
152
|
+
for (const key of Object.keys(mapping)) {
|
|
153
|
+
const ref = mapping[key];
|
|
154
|
+
if (isDiscriminatorRef(ref)) {
|
|
155
|
+
mappedMapping ??= { ...mapping };
|
|
156
|
+
setOwn(mappedMapping, key, map(ref, [...path, 'discriminator', 'mapping', key]));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const hasDefaultMappingRef = isDiscriminatorRef(defaultMapping);
|
|
161
|
+
if (!mappedMapping && !hasDefaultMappingRef) {
|
|
162
|
+
return discriminator;
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
...discriminator,
|
|
166
|
+
...(mappedMapping && { mapping: mappedMapping }),
|
|
167
|
+
...(hasDefaultMappingRef && {
|
|
168
|
+
defaultMapping: map(defaultMapping, [...path, 'discriminator', 'defaultMapping']),
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Rewrites recursive root `#` refs by moving the schema body into `$defs`.
|
|
174
|
+
*/
|
|
175
|
+
export function hoistRecursiveRefToDef(schema) {
|
|
176
|
+
if (typeof schema !== 'object') {
|
|
177
|
+
return schema;
|
|
178
|
+
}
|
|
179
|
+
const isRootRef = (ref) => ref === '#' ||
|
|
180
|
+
(!ref.startsWith('#/$defs/') && resolveJsonSchemaLocalPointer(schema, ref) !== undefined);
|
|
181
|
+
// most schemas have no root ref, so detect one without copying the schema
|
|
182
|
+
let hasRootRef = false;
|
|
183
|
+
visitJsonSchemaRefs(schema, (ref) => {
|
|
184
|
+
hasRootRef ||= isRootRef(ref);
|
|
185
|
+
});
|
|
186
|
+
if (!hasRootRef) {
|
|
187
|
+
return schema;
|
|
188
|
+
}
|
|
189
|
+
const defName = findRecursiveJsonSchemaDefName(schema.$defs);
|
|
190
|
+
const defRef = `#/$defs/${encodeJsonPointerSegment(defName)}`;
|
|
191
|
+
const { $defs, ...rest } = mapJsonSchemaRefs(schema, (ref) => isRootRef(ref) ? `${defRef}${ref.slice(1)}` : ref);
|
|
192
|
+
return {
|
|
193
|
+
$ref: defRef,
|
|
194
|
+
$defs: {
|
|
195
|
+
...$defs,
|
|
196
|
+
[defName]: rest,
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Resolves a local `$ref` at the **root level** of the given schema, if present.
|
|
202
|
+
*
|
|
203
|
+
* Only handles refs of the form `#/$defs/<name>` pointing into the provided
|
|
204
|
+
* (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
|
|
205
|
+
* intentionally left untouched.
|
|
206
|
+
*
|
|
207
|
+
* If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
|
|
208
|
+
* schema is returned as-is. Chained refs are followed until one repeats,
|
|
209
|
+
* which is kept.
|
|
210
|
+
*
|
|
211
|
+
* @param schema - The schema whose root-level `$ref` should be resolved.
|
|
212
|
+
* @param $defs - Definition map to resolve against. If omitted, falls back to
|
|
213
|
+
* `schema.$defs`. When provided, takes precedence over any `$defs` embedded
|
|
214
|
+
* in the schema.
|
|
215
|
+
*/
|
|
216
|
+
export function resolveJsonSchemaRootLocalRef(schema, $defs) {
|
|
217
|
+
if (typeof schema === 'boolean') {
|
|
218
|
+
return schema;
|
|
219
|
+
}
|
|
220
|
+
if (arguments.length === 1) {
|
|
221
|
+
$defs = schema.$defs;
|
|
222
|
+
}
|
|
223
|
+
if (!$defs) {
|
|
224
|
+
return schema;
|
|
225
|
+
}
|
|
226
|
+
const followedRefs = new Set();
|
|
227
|
+
let current = schema;
|
|
228
|
+
while (typeof current.$ref === 'string' &&
|
|
229
|
+
current.$ref.startsWith('#/$defs/') &&
|
|
230
|
+
!followedRefs.has(current.$ref)) {
|
|
231
|
+
followedRefs.add(current.$ref);
|
|
232
|
+
const resolved = get($defs, current.$ref.slice('#/$defs/'.length).split('/').map(decodeJsonPointerSegment));
|
|
233
|
+
if (resolved === undefined) {
|
|
234
|
+
return current;
|
|
235
|
+
}
|
|
236
|
+
if (typeof resolved !== 'object') {
|
|
237
|
+
return resolved;
|
|
238
|
+
}
|
|
239
|
+
const { $ref: _ref, ...rest } = current;
|
|
240
|
+
current = {
|
|
241
|
+
...rest,
|
|
242
|
+
...resolved,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
return current;
|
|
246
|
+
}
|
|
247
|
+
function findRecursiveJsonSchemaDefName(defs) {
|
|
248
|
+
let index = 0;
|
|
249
|
+
while (defs?.[`__schema${index}`] !== undefined) {
|
|
250
|
+
index++;
|
|
251
|
+
}
|
|
252
|
+
return `__schema${index}`;
|
|
253
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context } from '@orpc/server';
|
|
2
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '@orpc/server/standard';
|
|
3
|
+
import type { JsonSchemaCoercerOptions } from './coercer.ts';
|
|
4
|
+
import type { JsonSchemaConverter } from './convert.ts';
|
|
5
|
+
export interface SmartCoercionHandlerPluginOptions extends JsonSchemaCoercerOptions {
|
|
6
|
+
converters?: JsonSchemaConverter[] | undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Coerces incoming request data to match the expected `.input` schema types before validation,
|
|
10
|
+
* based on JSON schemas produced by the configured converters.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
13
|
+
*/
|
|
14
|
+
export declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
15
|
+
name: string;
|
|
16
|
+
private readonly converter;
|
|
17
|
+
private readonly coercer;
|
|
18
|
+
private readonly cache;
|
|
19
|
+
constructor(options?: SmartCoercionHandlerPluginOptions);
|
|
20
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
21
|
+
private coerceValue;
|
|
22
|
+
}
|