@orpc/json-schema 2.0.0-beta.4 → 2.0.0-beta.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,19 +1,59 @@
1
- import * as Draft2020 from 'json-schema-typed/draft-2020-12';
2
- export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
1
+ import { OpenAPIV3_2 } from '@openapi-spec/types';
3
2
  import { AnySchema, RouterContract } from '@orpc/contract';
4
3
  export { AnySchema, Schema } from '@orpc/contract';
5
- import { Promisable } from '@orpc/shared';
6
4
  import { Context } from '@orpc/server';
7
5
  import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
8
6
  import { ClientContext } from '@orpc/client';
9
7
  import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
10
8
  import { StandardJSONSchemaV1 } from '@standard-schema/spec';
11
9
 
12
- type JsonSchema = Draft2020.JSONSchema;
13
- type JsonSchemaKeywords = typeof Draft2020.keywords[number];
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
+ }
14
55
  declare enum JsonSchemaXNativeType {
15
56
  BigInt = "bigint",
16
- RegExp = "regexp",
17
57
  Date = "date",
18
58
  Url = "url",
19
59
  Set = "set",
@@ -90,21 +130,32 @@ declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
90
130
  declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
91
131
  declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
92
132
 
133
+ /**
134
+ * The conversion direction: `input` targets the schema's input type, `output` targets its output type.
135
+ *
136
+ * @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
137
+ */
93
138
  type JsonSchemaConverterDirection = 'input' | 'output';
139
+ /**
140
+ * Interface for converting validation schemas into JSON Schema representations,
141
+ * used by tools such as the OpenAPI Generator and Smart Coercion plugins.
142
+ *
143
+ * @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
144
+ */
94
145
  interface JsonSchemaConverter {
95
146
  /**
96
147
  * Determines whether this converter can handle the given schema.
97
148
  */
98
- condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<boolean>;
149
+ condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): boolean;
99
150
  /**
100
151
  * Converts an ORPC schema to a JSON Schema representation.
101
152
  */
102
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<[jsonSchema: JsonSchema, optional: boolean]>;
153
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
103
154
  }
104
155
  declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
105
156
  private readonly converters;
106
157
  constructor(converters?: JsonSchemaConverter[]);
107
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promise<[jsonSchema: JsonSchema, optional: boolean]>;
158
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
108
159
  }
109
160
 
110
161
  /**
@@ -124,6 +175,11 @@ declare function encodeJsonPointerSegment(segment: string): string;
124
175
  * https://datatracker.ietf.org/doc/html/rfc6901
125
176
  */
126
177
  declare function decodeJsonPointerSegment(segment: string): string;
178
+ /**
179
+ * Visits every `$ref` in a schema, traversing the same keywords as {@link mapJsonSchemaRefs}.
180
+ * Shared or cyclic object instances are visited once.
181
+ */
182
+ declare function visitJsonSchemaRefs(value: JsonSchema, visit: (ref: string) => void, schemaLevel?: boolean, seen?: Set<object>): void;
127
183
  declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
128
184
  /**
129
185
  * Rewrites recursive root `#` refs by moving the schema body into `$defs`.
@@ -137,7 +193,8 @@ declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
137
193
  * intentionally left untouched.
138
194
  *
139
195
  * If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
140
- * schema is returned as-is.
196
+ * schema is returned as-is. Chained refs are followed until one repeats,
197
+ * which is kept.
141
198
  *
142
199
  * @param schema - The schema whose root-level `$ref` should be resolved.
143
200
  * @param $defs - Definition map to resolve against. If omitted, falls back to
@@ -149,6 +206,12 @@ declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclu
149
206
  interface SmartCoercionHandlerPluginOptions {
150
207
  converters?: undefined | JsonSchemaConverter[];
151
208
  }
209
+ /**
210
+ * Coerces incoming request data to match the expected `.input` schema types before validation,
211
+ * based on JSON schemas produced by the configured converters.
212
+ *
213
+ * @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
214
+ */
152
215
  declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
153
216
  name: string;
154
217
  private readonly converter;
@@ -162,6 +225,12 @@ declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardH
162
225
  interface SmartCoercionLinkPluginOptions {
163
226
  converters?: undefined | JsonSchemaConverter[];
164
227
  }
228
+ /**
229
+ * Coerces server responses to match the expected `.output` or `.errors` schema types before validation,
230
+ * based on JSON schemas produced by the configured converters.
231
+ *
232
+ * @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
233
+ */
165
234
  declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
166
235
  private readonly contract;
167
236
  name: string;
@@ -177,11 +246,18 @@ declare class SmartCoercionLinkPlugin<T extends ClientContext> implements Standa
177
246
  private coerceValue;
178
247
  }
179
248
 
249
+ /**
250
+ * Returns true when the schema accepts `undefined` in the given direction,
251
+ * found by validating `undefined` against it. For `output`, the validated value must also be `undefined`.
252
+ *
253
+ * Returns false when validation throws or is async. An async result is never left as an unhandled rejection.
254
+ */
255
+ declare function isStandardSchemaOptional(schema: AnySchema, direction: JsonSchemaConverterDirection): boolean;
180
256
  declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
181
257
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
182
258
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
183
259
  convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
184
260
  }
185
261
 
186
- export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef };
262
+ export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaFormat, JsonSchemaType, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isStandardSchemaOptional, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef, visitJsonSchemaRefs };
187
263
  export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };