@orpc/json-schema 1.14.6 → 2.0.0-beta.10

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,11 +1,16 @@
1
- import * as Draft07 from 'json-schema-typed/draft-07';
2
- import * as Draft2019 from 'json-schema-typed/draft-2019-09';
3
1
  import * as Draft2020 from 'json-schema-typed/draft-2020-12';
4
- import { ConditionalSchemaConverter } from '@orpc/openapi';
2
+ export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
3
+ import { AnySchema, RouterContract } from '@orpc/contract';
4
+ export { AnySchema, Schema } from '@orpc/contract';
5
+ import { Promisable } from '@orpc/shared';
5
6
  import { Context } from '@orpc/server';
6
7
  import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
8
+ import { ClientContext } from '@orpc/client';
9
+ import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
10
+ import { StandardJSONSchemaV1 } from '@standard-schema/spec';
7
11
 
8
- type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
12
+ type JsonSchema = Draft2020.JSONSchema;
13
+ type JsonSchemaKeywords = typeof Draft2020.keywords[number];
9
14
  declare enum JsonSchemaXNativeType {
10
15
  BigInt = "bigint",
11
16
  RegExp = "regexp",
@@ -15,25 +20,168 @@ declare enum JsonSchemaXNativeType {
15
20
  Map = "map"
16
21
  }
17
22
 
18
- interface JsonSchemaCoerceOptions {
19
- components?: Record<string, JsonSchema>;
20
- }
21
23
  declare class JsonSchemaCoercer {
22
- #private;
23
- coerce(schema: JsonSchema, value: unknown, options?: JsonSchemaCoerceOptions): unknown;
24
+ coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
25
+ private coerceInternal;
26
+ }
27
+
28
+ /**
29
+ * These utilities assume the schema has only one root-level `$defs` object
30
+ * and exclusively use absolute JSON pointers for `$ref` values.
31
+ */
32
+
33
+ declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
34
+ type JsonFileSchema = JsonSchema & object & {
35
+ type: 'string';
36
+ contentMediaType?: string;
37
+ };
38
+ /**
39
+ * Returns true when the schema is a file-like string schema.
40
+ */
41
+ declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
42
+ type JsonObjectSchema = JsonSchema & object & {
43
+ type: 'object';
44
+ };
45
+ /**
46
+ * Returns true when the schema is an object schema.
47
+ */
48
+ declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
49
+ type JsonArraySchema = JsonSchema & object & {
50
+ type: 'array';
51
+ };
52
+ /**
53
+ * Returns true when the schema is an array schema.
54
+ */
55
+ declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
56
+ /**
57
+ * Returns true when the schema does not apply any recognized constraints.
58
+ */
59
+ declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
60
+ /**
61
+ * Ensures a JSON Schema is represented as an object schema document.
62
+ */
63
+ declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
64
+
65
+ /**
66
+ * These utilities assume the schema has only one root-level `$defs` object
67
+ * and exclusively use absolute JSON pointers for `$ref` values.
68
+ */
69
+
70
+ /**
71
+ * Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
72
+ */
73
+ declare function combineJsonSchemasWithComposition(keyword: 'allOf' | 'anyOf' | 'oneOf', schemas: JsonSchema[]): JsonSchema;
74
+ type JsonObjectSchemaEntry = [name: string, schema: JsonSchema, optional: boolean];
75
+ /**
76
+ * Combines object property entries back into a single object schema.
77
+ */
78
+ declare function combineJsonObjectSchemaEntries(entries: JsonObjectSchemaEntry[]): JsonObjectSchema;
79
+ /**
80
+ * Parses an object schema, or a composition of object schemas, into property entries.
81
+ */
82
+ declare function extractJsonObjectSchemaEntries(schema: JsonSchema): JsonObjectSchemaEntry[] | undefined;
83
+ /**
84
+ * Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
85
+ */
86
+ declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
87
+ /**
88
+ * Matches a union made of a single item schema and its array form.
89
+ */
90
+ declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
91
+ declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
92
+
93
+ type JsonSchemaConverterDirection = 'input' | 'output';
94
+ interface JsonSchemaConverter {
95
+ /**
96
+ * Determines whether this converter can handle the given schema.
97
+ */
98
+ condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<boolean>;
99
+ /**
100
+ * Converts an ORPC schema to a JSON Schema representation.
101
+ */
102
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<[jsonSchema: JsonSchema, optional: boolean]>;
103
+ }
104
+ declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
105
+ private readonly converters;
106
+ constructor(converters?: JsonSchemaConverter[]);
107
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promise<[jsonSchema: JsonSchema, optional: boolean]>;
108
+ }
109
+
110
+ /**
111
+ * These utilities assume the schema has only one root-level `$defs` object
112
+ * and exclusively use absolute JSON pointers for `$ref` values.
113
+ */
114
+
115
+ /**
116
+ * Encodes a JSON Pointer segment according to RFC 6901.
117
+ *
118
+ * https://datatracker.ietf.org/doc/html/rfc6901
119
+ */
120
+ declare function encodeJsonPointerSegment(segment: string): string;
121
+ /**
122
+ * Decodes a JSON Pointer segment according to RFC 6901.
123
+ *
124
+ * https://datatracker.ietf.org/doc/html/rfc6901
125
+ */
126
+ declare function decodeJsonPointerSegment(segment: string): string;
127
+ declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
128
+ /**
129
+ * Rewrites recursive root `#` refs by moving the schema body into `$defs`.
130
+ */
131
+ declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
132
+ /**
133
+ * Resolves a local `$ref` at the **root level** of the given schema, if present.
134
+ *
135
+ * Only handles refs of the form `#/$defs/<name>` pointing into the provided
136
+ * (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
137
+ * intentionally left untouched.
138
+ *
139
+ * If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
140
+ * schema is returned as-is.
141
+ *
142
+ * @param schema - The schema whose root-level `$ref` should be resolved.
143
+ * @param $defs - Definition map to resolve against. If omitted, falls back to
144
+ * `schema.$defs`. When provided, takes precedence over any `$defs` embedded
145
+ * in the schema.
146
+ */
147
+ declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
148
+
149
+ interface SmartCoercionHandlerPluginOptions {
150
+ converters?: undefined | JsonSchemaConverter[];
151
+ }
152
+ declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
153
+ name: string;
154
+ private readonly converter;
155
+ private readonly coercer;
156
+ private readonly cache;
157
+ constructor(options?: SmartCoercionHandlerPluginOptions);
158
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
159
+ private coerceValue;
24
160
  }
25
161
 
26
- interface SmartCoercionPluginOptions {
27
- schemaConverters?: readonly ConditionalSchemaConverter[];
162
+ interface SmartCoercionLinkPluginOptions {
163
+ converters?: undefined | JsonSchemaConverter[];
28
164
  }
29
- declare class SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
30
- #private;
165
+ declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
166
+ private readonly contract;
167
+ name: string;
168
+ /**
169
+ * Output and error values should be coerced before validation.
170
+ */
171
+ after: string[];
31
172
  private readonly converter;
32
173
  private readonly coercer;
33
174
  private readonly cache;
34
- constructor(options?: SmartCoercionPluginOptions);
35
- init(options: StandardHandlerOptions<T>): void;
175
+ constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
176
+ init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
177
+ private coerceValue;
178
+ }
179
+
180
+ declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
181
+ condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
182
+ convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
183
+ convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
36
184
  }
37
185
 
38
- export { JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionPlugin };
39
- export type { JsonSchema, JsonSchemaCoerceOptions, SmartCoercionPluginOptions };
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 };
187
+ export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };