@orpc/valibot 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/{index.d.mts → converter.d.ts} +6 -10
- package/dist/converter.js +170 -0
- package/dist/index.d.ts +2 -37
- package/dist/index.js +1 -0
- package/package.json +25 -27
- package/dist/index.mjs +0 -97
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/valibot">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fvalibot?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -191,15 +192,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
192
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
192
193
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
193
194
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
194
|
-
<td align="center"><a href="https://
|
|
195
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
195
196
|
</tr>
|
|
196
197
|
<tr>
|
|
197
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
198
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
199
|
</tr>
|
|
200
200
|
</table>
|
|
201
201
|
|
|
202
|
-
With thanks to [
|
|
202
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
203
|
|
|
204
204
|
## Open Source Programs
|
|
205
205
|
|
|
@@ -209,7 +209,6 @@ With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
209
209
|
<tr>
|
|
210
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
211
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
212
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
213
212
|
</tr>
|
|
214
213
|
</table>
|
|
215
214
|
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'target' | 'typeMode' | 'overrideRef'> {
|
|
1
|
+
import type { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from '@orpc/json-schema';
|
|
2
|
+
import type { ConversionConfig } from '@valibot/to-json-schema';
|
|
3
|
+
export interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'target' | 'typeMode' | 'overrideRef'> {
|
|
6
4
|
/**
|
|
7
5
|
* Caches conversion results in a WeakMap keyed by the Valibot schema instance,
|
|
8
6
|
* so converting the same schema again is free.
|
|
@@ -16,11 +14,11 @@ interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'ta
|
|
|
16
14
|
}
|
|
17
15
|
/**
|
|
18
16
|
* Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
|
|
19
|
-
* with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, and `v.
|
|
17
|
+
* with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, `v.map()`, `v.file()`, and `v.blob()`.
|
|
20
18
|
*
|
|
21
19
|
* @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
|
|
22
20
|
*/
|
|
23
|
-
declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
|
|
21
|
+
export declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
|
|
24
22
|
private readonly conversionConfig;
|
|
25
23
|
private readonly cache;
|
|
26
24
|
constructor({ cache, ...conversionConfig }?: ValibotToJsonSchemaConverterOptions);
|
|
@@ -28,10 +26,8 @@ declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
|
|
|
28
26
|
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
29
27
|
private convertUncached;
|
|
30
28
|
private createConversionConfig;
|
|
29
|
+
private overrideAction;
|
|
31
30
|
private overrideSchema;
|
|
32
31
|
private convertNativeType;
|
|
33
32
|
private convertWithin;
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
export { ValibotToJsonSchemaConverter };
|
|
37
|
-
export type { ValibotToJsonSchemaConverterOptions };
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { isJsonFileSchema, isStandardSchemaOptional, JsonSchemaFormat, JsonSchemaXNativeType, } from '@orpc/json-schema';
|
|
2
|
+
import { toJsonSchema } from '@valibot/to-json-schema';
|
|
3
|
+
import { any, tuple } from 'valibot';
|
|
4
|
+
const NESTED_CONVERSION_SENTINEL = /* @__PURE__ */ any();
|
|
5
|
+
/**
|
|
6
|
+
* Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
|
|
7
|
+
* with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, `v.map()`, `v.file()`, and `v.blob()`.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
|
|
10
|
+
*/
|
|
11
|
+
export class ValibotToJsonSchemaConverter {
|
|
12
|
+
conversionConfig;
|
|
13
|
+
cache;
|
|
14
|
+
constructor({ cache, ...conversionConfig } = {}) {
|
|
15
|
+
this.conversionConfig = conversionConfig;
|
|
16
|
+
if (cache) {
|
|
17
|
+
this.cache = { input: new WeakMap(), output: new WeakMap() };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
condition(schema, _direction) {
|
|
21
|
+
return schema?.['~standard'].vendor === 'valibot';
|
|
22
|
+
}
|
|
23
|
+
convert(schema, direction) {
|
|
24
|
+
const valibotSchema = schema;
|
|
25
|
+
if (this.cache) {
|
|
26
|
+
const cached = this.cache[direction].get(valibotSchema);
|
|
27
|
+
if (cached) {
|
|
28
|
+
return cached;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const result = this.convertUncached(valibotSchema, direction);
|
|
32
|
+
if (this.cache) {
|
|
33
|
+
this.cache[direction].set(valibotSchema, result);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
convertUncached(valibotSchema, direction) {
|
|
38
|
+
// Since the default oRPC format is always draft/2020-12,
|
|
39
|
+
// `$schema` can be safely omitted here.
|
|
40
|
+
const { $schema: _schema, ...jsonSchema } = toJsonSchema(valibotSchema, this.createConversionConfig(direction));
|
|
41
|
+
return [jsonSchema, isStandardSchemaOptional(valibotSchema, direction)];
|
|
42
|
+
}
|
|
43
|
+
createConversionConfig(direction) {
|
|
44
|
+
return {
|
|
45
|
+
errorMode: 'ignore',
|
|
46
|
+
...this.conversionConfig,
|
|
47
|
+
target: 'draft-2020-12',
|
|
48
|
+
typeMode: direction,
|
|
49
|
+
overrideSchema: (context) => this.overrideSchema(context, direction),
|
|
50
|
+
overrideAction: (context) => this.overrideAction(context),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
overrideAction(context) {
|
|
54
|
+
return applyOverride(context, convertAction(context), this.conversionConfig.overrideAction);
|
|
55
|
+
}
|
|
56
|
+
overrideSchema(context, direction) {
|
|
57
|
+
return applyOverride(context, this.convertNativeType(context, direction), this.conversionConfig.overrideSchema);
|
|
58
|
+
}
|
|
59
|
+
convertNativeType(context, direction) {
|
|
60
|
+
switch (context.valibotSchema.type) {
|
|
61
|
+
case 'bigint':
|
|
62
|
+
return {
|
|
63
|
+
'type': 'string',
|
|
64
|
+
'pattern': '^-?[0-9]+$',
|
|
65
|
+
'x-native-type': JsonSchemaXNativeType.BigInt,
|
|
66
|
+
};
|
|
67
|
+
case 'date':
|
|
68
|
+
return {
|
|
69
|
+
'type': 'string',
|
|
70
|
+
'format': JsonSchemaFormat.DateTime,
|
|
71
|
+
'x-native-type': JsonSchemaXNativeType.Date,
|
|
72
|
+
};
|
|
73
|
+
case 'set': {
|
|
74
|
+
const { value } = context.valibotSchema;
|
|
75
|
+
return {
|
|
76
|
+
'type': 'array',
|
|
77
|
+
'uniqueItems': true,
|
|
78
|
+
'items': this.convertWithin(context, [value], direction)[0],
|
|
79
|
+
'x-native-type': JsonSchemaXNativeType.Set,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
case 'map': {
|
|
83
|
+
const { key, value } = context.valibotSchema;
|
|
84
|
+
return {
|
|
85
|
+
'type': 'array',
|
|
86
|
+
'items': {
|
|
87
|
+
type: 'array',
|
|
88
|
+
prefixItems: this.convertWithin(context, [key, value], direction),
|
|
89
|
+
maxItems: 2,
|
|
90
|
+
minItems: 2,
|
|
91
|
+
},
|
|
92
|
+
'x-native-type': JsonSchemaXNativeType.Map,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
case 'blob':
|
|
96
|
+
case 'file':
|
|
97
|
+
// `v.mimeType()` narrows `contentMediaType` in `convertAction`.
|
|
98
|
+
return { type: 'string', contentMediaType: '*/*' };
|
|
99
|
+
}
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
convertWithin(parent, schemas, direction) {
|
|
103
|
+
// Relies on `@valibot/to-json-schema` converting tuple items in order,
|
|
104
|
+
// so the sentinel adopts the parent state before any of `schemas` converts.
|
|
105
|
+
const wrapper = tuple([NESTED_CONVERSION_SENTINEL, ...schemas]);
|
|
106
|
+
let child;
|
|
107
|
+
const { prefixItems } = toJsonSchema(wrapper, {
|
|
108
|
+
...this.createConversionConfig(direction),
|
|
109
|
+
definitions: {},
|
|
110
|
+
overrideSchema: (context) => {
|
|
111
|
+
if (context.valibotSchema === NESTED_CONVERSION_SENTINEL) {
|
|
112
|
+
child = context;
|
|
113
|
+
mergeConversionContext(child, parent);
|
|
114
|
+
}
|
|
115
|
+
else if (context.valibotSchema !== wrapper) {
|
|
116
|
+
return this.overrideSchema(context, direction);
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
mergeConversionContext(parent, child);
|
|
122
|
+
return prefixItems.slice(1);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function convertAction({ valibotAction, jsonSchema, }) {
|
|
126
|
+
if (valibotAction.type !== 'mime_type' || !isJsonFileSchema(jsonSchema)) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const mimeTypes = toMimeTypes(valibotAction.requirement);
|
|
130
|
+
if (!mimeTypes) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
if (mimeTypes.length === 1) {
|
|
134
|
+
return { ...jsonSchema, contentMediaType: mimeTypes[0] };
|
|
135
|
+
}
|
|
136
|
+
// One branch per media type, so the OpenAPI generator documents each one.
|
|
137
|
+
return { anyOf: mimeTypes.map((contentMediaType) => ({ ...jsonSchema, contentMediaType })) };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Reads the `v.mimeType()` requirement defensively, since it is not part of
|
|
141
|
+
* `@valibot/to-json-schema`'s contract: Valibot 1.x requires an array, and a single
|
|
142
|
+
* string is accepted in case a future version allows one. Anything else is left to
|
|
143
|
+
* the errorMode option instead of throwing a TypeError.
|
|
144
|
+
*/
|
|
145
|
+
function toMimeTypes(requirement) {
|
|
146
|
+
const values = typeof requirement === 'string' ? [requirement] : requirement;
|
|
147
|
+
if (!Array.isArray(values) ||
|
|
148
|
+
values.length === 0 ||
|
|
149
|
+
!values.every((value) => typeof value === 'string')) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
return [...new Set(values)];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Runs the user's override on top of oRPC's conversion, or on its own when oRPC has none.
|
|
156
|
+
*/
|
|
157
|
+
function applyOverride(context, jsonSchema, override) {
|
|
158
|
+
return jsonSchema
|
|
159
|
+
? (override?.({ ...context, jsonSchema, errors: undefined }) ?? jsonSchema)
|
|
160
|
+
: override?.(context);
|
|
161
|
+
}
|
|
162
|
+
function mergeConversionContext(target, source) {
|
|
163
|
+
// Assumes `@valibot/to-json-schema` only records lazy getters and definitions along with a reference.
|
|
164
|
+
if (!source.referenceMap.size) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
source.referenceMap.forEach((referenceId, schema) => target.referenceMap.set(schema, referenceId));
|
|
168
|
+
source.getterMap.forEach((schema, getter) => target.getterMap.set(getter, schema));
|
|
169
|
+
Object.assign(target.definitions, source.definitions);
|
|
170
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export { JsonSchema } from '@orpc/json-schema';
|
|
3
|
-
import { ConversionConfig } from '@valibot/to-json-schema';
|
|
4
|
-
|
|
5
|
-
interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'target' | 'typeMode' | 'overrideRef'> {
|
|
6
|
-
/**
|
|
7
|
-
* Caches conversion results in a WeakMap keyed by the Valibot schema instance,
|
|
8
|
-
* so converting the same schema again is free.
|
|
9
|
-
*
|
|
10
|
-
* When enabled, repeated conversions return the same JSON schema object,
|
|
11
|
-
* so treat returned schemas as immutable.
|
|
12
|
-
*
|
|
13
|
-
* @default false
|
|
14
|
-
*/
|
|
15
|
-
cache?: boolean | undefined;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
|
|
19
|
-
* with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, and `v.map()`.
|
|
20
|
-
*
|
|
21
|
-
* @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
|
|
22
|
-
*/
|
|
23
|
-
declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
|
|
24
|
-
private readonly conversionConfig;
|
|
25
|
-
private readonly cache;
|
|
26
|
-
constructor({ cache, ...conversionConfig }?: ValibotToJsonSchemaConverterOptions);
|
|
27
|
-
condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
|
|
28
|
-
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
29
|
-
private convertUncached;
|
|
30
|
-
private createConversionConfig;
|
|
31
|
-
private overrideSchema;
|
|
32
|
-
private convertNativeType;
|
|
33
|
-
private convertWithin;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export { ValibotToJsonSchemaConverter };
|
|
37
|
-
export type { ValibotToJsonSchemaConverterOptions };
|
|
1
|
+
export * from './converter.ts';
|
|
2
|
+
export type { JsonSchema } from '@orpc/json-schema';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './converter.js';
|
package/package.json
CHANGED
|
@@ -1,52 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/valibot",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
5
4
|
"description": "Valibot integration for oRPC: convert Valibot schemas into JSON Schema for OpenAPI generation",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"json-schema",
|
|
7
|
+
"openapi",
|
|
8
|
+
"orpc",
|
|
9
|
+
"typesafe",
|
|
10
|
+
"typescript",
|
|
11
|
+
"valibot",
|
|
12
|
+
"validation"
|
|
10
13
|
],
|
|
11
14
|
"homepage": "https://orpc.dev",
|
|
15
|
+
"license": "MIT",
|
|
12
16
|
"repository": {
|
|
13
17
|
"type": "git",
|
|
14
18
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
19
|
"directory": "packages/valibot"
|
|
16
20
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"json-schema",
|
|
21
|
-
"openapi",
|
|
22
|
-
"validation",
|
|
23
|
-
"typescript",
|
|
24
|
-
"typesafe"
|
|
21
|
+
"funding": [
|
|
22
|
+
"https://github.com/sponsors/dinwwwh",
|
|
23
|
+
"https://opencollective.com/middleapi"
|
|
25
24
|
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
26
29
|
"sideEffects": false,
|
|
27
30
|
"exports": {
|
|
28
31
|
"./package.json": "./package.json",
|
|
29
32
|
".": {
|
|
30
|
-
"types": "./dist/index.d.
|
|
31
|
-
"import": "./dist/index.
|
|
32
|
-
"default": "./dist/index.
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"default": "./dist/index.js"
|
|
33
36
|
}
|
|
34
37
|
},
|
|
35
|
-
"files": [
|
|
36
|
-
"dist"
|
|
37
|
-
],
|
|
38
|
-
"peerDependencies": {
|
|
39
|
-
"valibot": ">=1.5.0"
|
|
40
|
-
},
|
|
41
38
|
"dependencies": {
|
|
42
|
-
"@orpc/json-schema": "2.0.0-beta.
|
|
39
|
+
"@orpc/json-schema": "2.0.0-beta.49",
|
|
43
40
|
"@valibot/to-json-schema": "^1.8.0"
|
|
44
41
|
},
|
|
45
42
|
"devDependencies": {
|
|
46
43
|
"valibot": "^1.5.0",
|
|
47
44
|
"zod": "^4.6.7"
|
|
48
45
|
},
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
}
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"valibot": ">=1.5.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {}
|
|
52
50
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { isStandardSchemaOptional, JsonSchemaXNativeType, JsonSchemaFormat } from '@orpc/json-schema';
|
|
2
|
-
import { toJsonSchema } from '@valibot/to-json-schema';
|
|
3
|
-
import { any, tuple } from 'valibot';
|
|
4
|
-
|
|
5
|
-
const NESTED_CONVERSION_SENTINEL = any();
|
|
6
|
-
class ValibotToJsonSchemaConverter {
|
|
7
|
-
conversionConfig;
|
|
8
|
-
cache;
|
|
9
|
-
constructor({ cache, ...conversionConfig } = {}) {
|
|
10
|
-
this.conversionConfig = conversionConfig;
|
|
11
|
-
if (cache) {
|
|
12
|
-
this.cache = { input: /* @__PURE__ */ new WeakMap(), output: /* @__PURE__ */ new WeakMap() };
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
condition(schema, _direction) {
|
|
16
|
-
return schema?.["~standard"].vendor === "valibot";
|
|
17
|
-
}
|
|
18
|
-
convert(schema, direction) {
|
|
19
|
-
const valibotSchema = schema;
|
|
20
|
-
if (this.cache) {
|
|
21
|
-
const cached = this.cache[direction].get(valibotSchema);
|
|
22
|
-
if (cached) {
|
|
23
|
-
return cached;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
const result = this.convertUncached(valibotSchema, direction);
|
|
27
|
-
if (this.cache) {
|
|
28
|
-
this.cache[direction].set(valibotSchema, result);
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
convertUncached(valibotSchema, direction) {
|
|
33
|
-
const { $schema, ...jsonSchema } = toJsonSchema(valibotSchema, this.createConversionConfig(direction));
|
|
34
|
-
return [jsonSchema, isStandardSchemaOptional(valibotSchema, direction)];
|
|
35
|
-
}
|
|
36
|
-
createConversionConfig(direction) {
|
|
37
|
-
return {
|
|
38
|
-
errorMode: "ignore",
|
|
39
|
-
...this.conversionConfig,
|
|
40
|
-
target: "draft-2020-12",
|
|
41
|
-
typeMode: direction,
|
|
42
|
-
overrideSchema: (context) => this.overrideSchema(context, direction)
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
overrideSchema(context, direction) {
|
|
46
|
-
const jsonSchema = this.convertNativeType(context, direction);
|
|
47
|
-
return jsonSchema ? this.conversionConfig.overrideSchema?.({ ...context, jsonSchema, errors: void 0 }) ?? jsonSchema : this.conversionConfig.overrideSchema?.(context);
|
|
48
|
-
}
|
|
49
|
-
convertNativeType(context, direction) {
|
|
50
|
-
switch (context.valibotSchema.type) {
|
|
51
|
-
case "bigint":
|
|
52
|
-
return { "type": "string", "pattern": "^-?[0-9]+$", "x-native-type": JsonSchemaXNativeType.BigInt };
|
|
53
|
-
case "date":
|
|
54
|
-
return { "type": "string", "format": JsonSchemaFormat.DateTime, "x-native-type": JsonSchemaXNativeType.Date };
|
|
55
|
-
case "set": {
|
|
56
|
-
const { value } = context.valibotSchema;
|
|
57
|
-
return { "type": "array", "uniqueItems": true, "items": this.convertWithin(context, [value], direction)[0], "x-native-type": JsonSchemaXNativeType.Set };
|
|
58
|
-
}
|
|
59
|
-
case "map": {
|
|
60
|
-
const { key, value } = context.valibotSchema;
|
|
61
|
-
return {
|
|
62
|
-
"type": "array",
|
|
63
|
-
"items": { type: "array", prefixItems: this.convertWithin(context, [key, value], direction), maxItems: 2, minItems: 2 },
|
|
64
|
-
"x-native-type": JsonSchemaXNativeType.Map
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
convertWithin(parent, schemas, direction) {
|
|
70
|
-
const wrapper = tuple([NESTED_CONVERSION_SENTINEL, ...schemas]);
|
|
71
|
-
let child;
|
|
72
|
-
const { prefixItems } = toJsonSchema(wrapper, {
|
|
73
|
-
...this.createConversionConfig(direction),
|
|
74
|
-
definitions: {},
|
|
75
|
-
overrideSchema: (context) => {
|
|
76
|
-
if (context.valibotSchema === NESTED_CONVERSION_SENTINEL) {
|
|
77
|
-
child = context;
|
|
78
|
-
mergeConversionContext(child, parent);
|
|
79
|
-
} else if (context.valibotSchema !== wrapper) {
|
|
80
|
-
return this.overrideSchema(context, direction);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
mergeConversionContext(parent, child);
|
|
85
|
-
return prefixItems.slice(1);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function mergeConversionContext(target, source) {
|
|
89
|
-
if (!source.referenceMap.size) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
source.referenceMap.forEach((referenceId, schema) => target.referenceMap.set(schema, referenceId));
|
|
93
|
-
source.getterMap.forEach((schema, getter) => target.getterMap.set(getter, schema));
|
|
94
|
-
Object.assign(target.definitions, source.definitions);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export { ValibotToJsonSchemaConverter };
|