@orpc/json-schema 0.0.0-next.c465e55
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/LICENSE +21 -0
- package/README.md +76 -0
- package/dist/index.d.mts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.mjs +359 -0
- package/package.json +41 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 oRPC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
3
|
+
</div>
|
4
|
+
|
5
|
+
<h1></h1>
|
6
|
+
|
7
|
+
<div align="center">
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
10
|
+
</a>
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/json-schema">
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fjson-schema?logo=npm" />
|
13
|
+
</a>
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
16
|
+
</a>
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
19
|
+
</a>
|
20
|
+
</div>
|
21
|
+
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
23
|
+
|
24
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## Highlights
|
29
|
+
|
30
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
31
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
32
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
33
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
|
34
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
35
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
36
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
37
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
38
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
39
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
40
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
41
|
+
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
42
|
+
|
43
|
+
## Documentation
|
44
|
+
|
45
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
46
|
+
|
47
|
+
## Packages
|
48
|
+
|
49
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
50
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
51
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
52
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
53
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
54
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
55
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
56
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
57
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
58
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
59
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
60
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
61
|
+
|
62
|
+
## `@orpc/json-schema`
|
63
|
+
|
64
|
+
Json Schema related utilities for oRPC.
|
65
|
+
|
66
|
+
## Sponsors
|
67
|
+
|
68
|
+
<p align="center">
|
69
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
70
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
71
|
+
</a>
|
72
|
+
</p>
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
import * as Draft07 from 'json-schema-typed/draft-07';
|
2
|
+
import * as Draft2019 from 'json-schema-typed/draft-2019-09';
|
3
|
+
import * as Draft2020 from 'json-schema-typed/draft-2020-12';
|
4
|
+
import { ConditionalSchemaConverter } from '@orpc/openapi';
|
5
|
+
import { Context } from '@orpc/server';
|
6
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
7
|
+
|
8
|
+
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
9
|
+
declare enum JsonSchemaXNativeType {
|
10
|
+
BigInt = "bigint",
|
11
|
+
RegExp = "regexp",
|
12
|
+
Date = "date",
|
13
|
+
Url = "url",
|
14
|
+
Set = "set",
|
15
|
+
Map = "map"
|
16
|
+
}
|
17
|
+
|
18
|
+
interface experimental_JsonSchemaCoerceOptions {
|
19
|
+
components?: Record<string, JsonSchema>;
|
20
|
+
}
|
21
|
+
declare class experimental_JsonSchemaCoercer {
|
22
|
+
#private;
|
23
|
+
coerce(schema: JsonSchema, value: unknown, options?: experimental_JsonSchemaCoerceOptions): unknown;
|
24
|
+
}
|
25
|
+
|
26
|
+
interface experimental_SmartCoercionPluginOptions {
|
27
|
+
schemaConverters?: readonly ConditionalSchemaConverter[];
|
28
|
+
}
|
29
|
+
declare class experimental_SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
30
|
+
#private;
|
31
|
+
private readonly converter;
|
32
|
+
private readonly coercer;
|
33
|
+
private readonly cache;
|
34
|
+
constructor(options?: experimental_SmartCoercionPluginOptions);
|
35
|
+
init(options: StandardHandlerOptions<T>): void;
|
36
|
+
}
|
37
|
+
|
38
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
39
|
+
export type { JsonSchema, experimental_JsonSchemaCoerceOptions, experimental_SmartCoercionPluginOptions };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
import * as Draft07 from 'json-schema-typed/draft-07';
|
2
|
+
import * as Draft2019 from 'json-schema-typed/draft-2019-09';
|
3
|
+
import * as Draft2020 from 'json-schema-typed/draft-2020-12';
|
4
|
+
import { ConditionalSchemaConverter } from '@orpc/openapi';
|
5
|
+
import { Context } from '@orpc/server';
|
6
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
7
|
+
|
8
|
+
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
9
|
+
declare enum JsonSchemaXNativeType {
|
10
|
+
BigInt = "bigint",
|
11
|
+
RegExp = "regexp",
|
12
|
+
Date = "date",
|
13
|
+
Url = "url",
|
14
|
+
Set = "set",
|
15
|
+
Map = "map"
|
16
|
+
}
|
17
|
+
|
18
|
+
interface experimental_JsonSchemaCoerceOptions {
|
19
|
+
components?: Record<string, JsonSchema>;
|
20
|
+
}
|
21
|
+
declare class experimental_JsonSchemaCoercer {
|
22
|
+
#private;
|
23
|
+
coerce(schema: JsonSchema, value: unknown, options?: experimental_JsonSchemaCoerceOptions): unknown;
|
24
|
+
}
|
25
|
+
|
26
|
+
interface experimental_SmartCoercionPluginOptions {
|
27
|
+
schemaConverters?: readonly ConditionalSchemaConverter[];
|
28
|
+
}
|
29
|
+
declare class experimental_SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
30
|
+
#private;
|
31
|
+
private readonly converter;
|
32
|
+
private readonly coercer;
|
33
|
+
private readonly cache;
|
34
|
+
constructor(options?: experimental_SmartCoercionPluginOptions);
|
35
|
+
init(options: StandardHandlerOptions<T>): void;
|
36
|
+
}
|
37
|
+
|
38
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
39
|
+
export type { JsonSchema, experimental_JsonSchemaCoerceOptions, experimental_SmartCoercionPluginOptions };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,359 @@
|
|
1
|
+
import { isObject, toArray, guard } from '@orpc/shared';
|
2
|
+
import { CompositeSchemaConverter } from '@orpc/openapi';
|
3
|
+
|
4
|
+
var JsonSchemaXNativeType = /* @__PURE__ */ ((JsonSchemaXNativeType2) => {
|
5
|
+
JsonSchemaXNativeType2["BigInt"] = "bigint";
|
6
|
+
JsonSchemaXNativeType2["RegExp"] = "regexp";
|
7
|
+
JsonSchemaXNativeType2["Date"] = "date";
|
8
|
+
JsonSchemaXNativeType2["Url"] = "url";
|
9
|
+
JsonSchemaXNativeType2["Set"] = "set";
|
10
|
+
JsonSchemaXNativeType2["Map"] = "map";
|
11
|
+
return JsonSchemaXNativeType2;
|
12
|
+
})(JsonSchemaXNativeType || {});
|
13
|
+
|
14
|
+
const FLEXIBLE_DATE_FORMAT_REGEX = /^[^-]+-[^-]+-[^-]+$/;
|
15
|
+
class experimental_JsonSchemaCoercer {
|
16
|
+
coerce(schema, value, options = {}) {
|
17
|
+
const [, coerced] = this.#coerce(schema, value, options);
|
18
|
+
return coerced;
|
19
|
+
}
|
20
|
+
#coerce(schema, originalValue, options) {
|
21
|
+
if (typeof schema === "boolean") {
|
22
|
+
return [schema, originalValue];
|
23
|
+
}
|
24
|
+
if (Array.isArray(schema.type)) {
|
25
|
+
return this.#coerce({
|
26
|
+
anyOf: schema.type.map((type) => ({ ...schema, type }))
|
27
|
+
}, originalValue, options);
|
28
|
+
}
|
29
|
+
let coerced = originalValue;
|
30
|
+
let satisfied = true;
|
31
|
+
if (typeof schema.$ref === "string") {
|
32
|
+
const refSchema = options?.components?.[schema.$ref];
|
33
|
+
if (refSchema !== void 0) {
|
34
|
+
const [subSatisfied, subCoerced] = this.#coerce(refSchema, coerced, options);
|
35
|
+
coerced = subCoerced;
|
36
|
+
satisfied = subSatisfied;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
const enumValues = schema.const !== void 0 ? [schema.const] : schema.enum;
|
40
|
+
if (enumValues !== void 0 && !enumValues.includes(coerced)) {
|
41
|
+
if (typeof coerced === "string") {
|
42
|
+
const numberValue = this.#stringToNumber(coerced);
|
43
|
+
if (enumValues.includes(numberValue)) {
|
44
|
+
coerced = numberValue;
|
45
|
+
} else {
|
46
|
+
const booleanValue = this.#stringToBoolean(coerced);
|
47
|
+
if (enumValues.includes(booleanValue)) {
|
48
|
+
coerced = booleanValue;
|
49
|
+
} else {
|
50
|
+
satisfied = false;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
} else {
|
54
|
+
satisfied = false;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
if (typeof schema.type === "string") {
|
58
|
+
switch (schema.type) {
|
59
|
+
case "null": {
|
60
|
+
if (coerced !== null) {
|
61
|
+
satisfied = false;
|
62
|
+
}
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
case "string": {
|
66
|
+
if (typeof coerced !== "string") {
|
67
|
+
satisfied = false;
|
68
|
+
}
|
69
|
+
break;
|
70
|
+
}
|
71
|
+
case "number": {
|
72
|
+
if (typeof coerced === "string") {
|
73
|
+
coerced = this.#stringToNumber(coerced);
|
74
|
+
}
|
75
|
+
if (typeof coerced !== "number") {
|
76
|
+
satisfied = false;
|
77
|
+
}
|
78
|
+
break;
|
79
|
+
}
|
80
|
+
case "integer": {
|
81
|
+
if (typeof coerced === "string") {
|
82
|
+
coerced = this.#stringToInteger(coerced);
|
83
|
+
}
|
84
|
+
if (typeof coerced !== "number" || !Number.isInteger(coerced)) {
|
85
|
+
satisfied = false;
|
86
|
+
}
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
case "boolean": {
|
90
|
+
if (typeof coerced === "string") {
|
91
|
+
coerced = this.#stringToBoolean(coerced);
|
92
|
+
}
|
93
|
+
if (typeof coerced !== "boolean") {
|
94
|
+
satisfied = false;
|
95
|
+
}
|
96
|
+
break;
|
97
|
+
}
|
98
|
+
case "array": {
|
99
|
+
if (Array.isArray(coerced)) {
|
100
|
+
const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
|
101
|
+
const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
102
|
+
let shouldUseCoercedItems = false;
|
103
|
+
const coercedItems = coerced.map((item, i) => {
|
104
|
+
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
105
|
+
if (subSchema === void 0) {
|
106
|
+
satisfied = false;
|
107
|
+
return item;
|
108
|
+
}
|
109
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, item, options);
|
110
|
+
if (!subSatisfied) {
|
111
|
+
satisfied = false;
|
112
|
+
}
|
113
|
+
if (subCoerced !== item) {
|
114
|
+
shouldUseCoercedItems = true;
|
115
|
+
}
|
116
|
+
return subCoerced;
|
117
|
+
});
|
118
|
+
if (coercedItems.length < prefixItemSchemas.length) {
|
119
|
+
satisfied = false;
|
120
|
+
}
|
121
|
+
if (shouldUseCoercedItems) {
|
122
|
+
coerced = coercedItems;
|
123
|
+
}
|
124
|
+
} else {
|
125
|
+
satisfied = false;
|
126
|
+
}
|
127
|
+
break;
|
128
|
+
}
|
129
|
+
case "object": {
|
130
|
+
if (Array.isArray(coerced)) {
|
131
|
+
coerced = { ...coerced };
|
132
|
+
}
|
133
|
+
if (isObject(coerced)) {
|
134
|
+
let shouldUseCoercedItems = false;
|
135
|
+
const coercedItems = {};
|
136
|
+
const patternProperties = Object.entries(schema.patternProperties ?? {}).map(([key, value]) => [new RegExp(key), value]);
|
137
|
+
for (const key in coerced) {
|
138
|
+
const value = coerced[key];
|
139
|
+
const subSchema = schema.properties?.[key] ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
|
140
|
+
if (value === void 0 && !schema.required?.includes(key)) {
|
141
|
+
coercedItems[key] = value;
|
142
|
+
} else if (subSchema === void 0) {
|
143
|
+
coercedItems[key] = value;
|
144
|
+
satisfied = false;
|
145
|
+
} else {
|
146
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, value, options);
|
147
|
+
coercedItems[key] = subCoerced;
|
148
|
+
if (!subSatisfied) {
|
149
|
+
satisfied = false;
|
150
|
+
}
|
151
|
+
if (subCoerced !== value) {
|
152
|
+
shouldUseCoercedItems = true;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}
|
156
|
+
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
|
157
|
+
satisfied = false;
|
158
|
+
}
|
159
|
+
if (shouldUseCoercedItems) {
|
160
|
+
coerced = coercedItems;
|
161
|
+
}
|
162
|
+
} else {
|
163
|
+
satisfied = false;
|
164
|
+
}
|
165
|
+
break;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}
|
169
|
+
if ("x-native-type" in schema && typeof schema["x-native-type"] === "string") {
|
170
|
+
switch (schema["x-native-type"]) {
|
171
|
+
case JsonSchemaXNativeType.Date: {
|
172
|
+
if (typeof coerced === "string") {
|
173
|
+
coerced = this.#stringToDate(coerced);
|
174
|
+
}
|
175
|
+
if (!(coerced instanceof Date)) {
|
176
|
+
satisfied = false;
|
177
|
+
}
|
178
|
+
break;
|
179
|
+
}
|
180
|
+
case JsonSchemaXNativeType.BigInt: {
|
181
|
+
switch (typeof coerced) {
|
182
|
+
case "string":
|
183
|
+
coerced = this.#stringToBigInt(coerced);
|
184
|
+
break;
|
185
|
+
case "number":
|
186
|
+
coerced = this.#numberToBigInt(coerced);
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
if (typeof coerced !== "bigint") {
|
190
|
+
satisfied = false;
|
191
|
+
}
|
192
|
+
break;
|
193
|
+
}
|
194
|
+
case JsonSchemaXNativeType.RegExp: {
|
195
|
+
if (typeof coerced === "string") {
|
196
|
+
coerced = this.#stringToRegExp(coerced);
|
197
|
+
}
|
198
|
+
if (!(coerced instanceof RegExp)) {
|
199
|
+
satisfied = false;
|
200
|
+
}
|
201
|
+
break;
|
202
|
+
}
|
203
|
+
case JsonSchemaXNativeType.Url: {
|
204
|
+
if (typeof coerced === "string") {
|
205
|
+
coerced = this.#stringToURL(coerced);
|
206
|
+
}
|
207
|
+
if (!(coerced instanceof URL)) {
|
208
|
+
satisfied = false;
|
209
|
+
}
|
210
|
+
break;
|
211
|
+
}
|
212
|
+
case JsonSchemaXNativeType.Set: {
|
213
|
+
if (Array.isArray(coerced)) {
|
214
|
+
coerced = this.#arrayToSet(coerced);
|
215
|
+
}
|
216
|
+
if (!(coerced instanceof Set)) {
|
217
|
+
satisfied = false;
|
218
|
+
}
|
219
|
+
break;
|
220
|
+
}
|
221
|
+
case JsonSchemaXNativeType.Map: {
|
222
|
+
if (Array.isArray(coerced)) {
|
223
|
+
coerced = this.#arrayToMap(coerced);
|
224
|
+
}
|
225
|
+
if (!(coerced instanceof Map)) {
|
226
|
+
satisfied = false;
|
227
|
+
}
|
228
|
+
break;
|
229
|
+
}
|
230
|
+
}
|
231
|
+
}
|
232
|
+
if (schema.allOf) {
|
233
|
+
for (const subSchema of schema.allOf) {
|
234
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
235
|
+
coerced = subCoerced;
|
236
|
+
if (!subSatisfied) {
|
237
|
+
satisfied = false;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
}
|
241
|
+
for (const key of ["anyOf", "oneOf"]) {
|
242
|
+
if (schema[key]) {
|
243
|
+
let bestOptions;
|
244
|
+
for (const subSchema of schema[key]) {
|
245
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
246
|
+
if (subSatisfied) {
|
247
|
+
if (!bestOptions || subCoerced === coerced) {
|
248
|
+
bestOptions = { coerced: subCoerced, satisfied: subSatisfied };
|
249
|
+
}
|
250
|
+
if (subCoerced === coerced) {
|
251
|
+
break;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
}
|
255
|
+
coerced = bestOptions ? bestOptions.coerced : coerced;
|
256
|
+
satisfied = bestOptions ? bestOptions.satisfied : false;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
if (typeof schema.not !== "undefined") {
|
260
|
+
const [notSatisfied] = this.#coerce(schema.not, coerced, options);
|
261
|
+
if (notSatisfied) {
|
262
|
+
satisfied = false;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
return [satisfied, coerced];
|
266
|
+
}
|
267
|
+
#stringToNumber(value) {
|
268
|
+
const num = Number.parseFloat(value);
|
269
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
270
|
+
return value;
|
271
|
+
}
|
272
|
+
return num;
|
273
|
+
}
|
274
|
+
#stringToInteger(value) {
|
275
|
+
const num = Number.parseInt(value);
|
276
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
277
|
+
return value;
|
278
|
+
}
|
279
|
+
return num;
|
280
|
+
}
|
281
|
+
#stringToBoolean(value) {
|
282
|
+
const lower = value.toLowerCase();
|
283
|
+
if (lower === "false" || lower === "off") {
|
284
|
+
return false;
|
285
|
+
}
|
286
|
+
if (lower === "true" || lower === "on") {
|
287
|
+
return true;
|
288
|
+
}
|
289
|
+
return value;
|
290
|
+
}
|
291
|
+
#stringToBigInt(value) {
|
292
|
+
return guard(() => BigInt(value)) ?? value;
|
293
|
+
}
|
294
|
+
#numberToBigInt(value) {
|
295
|
+
return guard(() => BigInt(value)) ?? value;
|
296
|
+
}
|
297
|
+
#stringToDate(value) {
|
298
|
+
const date = new Date(value);
|
299
|
+
if (Number.isNaN(date.getTime()) || !FLEXIBLE_DATE_FORMAT_REGEX.test(value)) {
|
300
|
+
return value;
|
301
|
+
}
|
302
|
+
return date;
|
303
|
+
}
|
304
|
+
#stringToRegExp(value) {
|
305
|
+
const match = value.match(/^\/(.*)\/([a-z]*)$/);
|
306
|
+
if (match) {
|
307
|
+
const [, pattern, flags] = match;
|
308
|
+
return guard(() => new RegExp(pattern, flags)) ?? value;
|
309
|
+
}
|
310
|
+
return value;
|
311
|
+
}
|
312
|
+
#stringToURL(value) {
|
313
|
+
return guard(() => new URL(value)) ?? value;
|
314
|
+
}
|
315
|
+
#arrayToSet(value) {
|
316
|
+
const set = new Set(value);
|
317
|
+
if (set.size !== value.length) {
|
318
|
+
return value;
|
319
|
+
}
|
320
|
+
return set;
|
321
|
+
}
|
322
|
+
#arrayToMap(value) {
|
323
|
+
if (value.some((item) => !Array.isArray(item) || item.length !== 2)) {
|
324
|
+
return value;
|
325
|
+
}
|
326
|
+
return new Map(value);
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
class experimental_SmartCoercionPlugin {
|
331
|
+
converter;
|
332
|
+
coercer;
|
333
|
+
cache = /* @__PURE__ */ new WeakMap();
|
334
|
+
constructor(options = {}) {
|
335
|
+
this.converter = new CompositeSchemaConverter(toArray(options.schemaConverters));
|
336
|
+
this.coercer = new experimental_JsonSchemaCoercer();
|
337
|
+
}
|
338
|
+
init(options) {
|
339
|
+
options.clientInterceptors ??= [];
|
340
|
+
options.clientInterceptors.unshift(async (options2) => {
|
341
|
+
const inputSchema = options2.procedure["~orpc"].inputSchema;
|
342
|
+
if (!inputSchema) {
|
343
|
+
return options2.next();
|
344
|
+
}
|
345
|
+
const coercedInput = await this.#coerce(inputSchema, options2.input);
|
346
|
+
return options2.next({ ...options2, input: coercedInput });
|
347
|
+
});
|
348
|
+
}
|
349
|
+
async #coerce(schema, value) {
|
350
|
+
let jsonSchema = this.cache.get(schema);
|
351
|
+
if (!jsonSchema) {
|
352
|
+
jsonSchema = (await this.converter.convert(schema, { strategy: "input" }))[1];
|
353
|
+
this.cache.set(schema, jsonSchema);
|
354
|
+
}
|
355
|
+
return this.coercer.coerce(jsonSchema, value);
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
package/package.json
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
{
|
2
|
+
"name": "@orpc/json-schema",
|
3
|
+
"type": "module",
|
4
|
+
"version": "0.0.0-next.c465e55",
|
5
|
+
"license": "MIT",
|
6
|
+
"homepage": "https://orpc.unnoq.com",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "git+https://github.com/unnoq/orpc.git",
|
10
|
+
"directory": "packages/json-schema"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"unnoq",
|
14
|
+
"orpc"
|
15
|
+
],
|
16
|
+
"exports": {
|
17
|
+
".": {
|
18
|
+
"types": "./dist/index.d.mts",
|
19
|
+
"import": "./dist/index.mjs",
|
20
|
+
"default": "./dist/index.mjs"
|
21
|
+
}
|
22
|
+
},
|
23
|
+
"files": [
|
24
|
+
"dist"
|
25
|
+
],
|
26
|
+
"dependencies": {
|
27
|
+
"json-schema-typed": "^8.0.1",
|
28
|
+
"@orpc/contract": "0.0.0-next.c465e55",
|
29
|
+
"@orpc/openapi": "0.0.0-next.c465e55",
|
30
|
+
"@orpc/server": "0.0.0-next.c465e55",
|
31
|
+
"@orpc/shared": "0.0.0-next.c465e55"
|
32
|
+
},
|
33
|
+
"devDependencies": {
|
34
|
+
"zod": "^3.25.74"
|
35
|
+
},
|
36
|
+
"scripts": {
|
37
|
+
"build": "unbuild",
|
38
|
+
"build:watch": "pnpm run build --watch",
|
39
|
+
"type:check": "tsc -b"
|
40
|
+
}
|
41
|
+
}
|