@orpc/valibot 2.0.0-beta.49 → 2.0.0-beta.50
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/converter.d.ts +25 -25
- package/dist/converter.js +142 -159
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/converter.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
export interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from "@orpc/json-schema";
|
|
2
|
+
import { ConversionConfig } from "@valibot/to-json-schema";
|
|
3
|
+
export interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, "target" | "typeMode" | "overrideRef"> {
|
|
4
|
+
/**
|
|
5
|
+
* Caches conversion results in a WeakMap keyed by the Valibot schema instance,
|
|
6
|
+
* so converting the same schema again is free.
|
|
7
|
+
*
|
|
8
|
+
* When enabled, repeated conversions return the same JSON schema object,
|
|
9
|
+
* so treat returned schemas as immutable.
|
|
10
|
+
*
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
cache?: boolean | undefined;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
|
|
@@ -19,15 +19,15 @@ export interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConf
|
|
|
19
19
|
* @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
|
|
20
20
|
*/
|
|
21
21
|
export declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
22
|
+
private readonly conversionConfig;
|
|
23
|
+
private readonly cache;
|
|
24
|
+
constructor({ cache, ...conversionConfig }?: ValibotToJsonSchemaConverterOptions);
|
|
25
|
+
condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
|
|
26
|
+
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
27
|
+
private convertUncached;
|
|
28
|
+
private createConversionConfig;
|
|
29
|
+
private overrideAction;
|
|
30
|
+
private overrideSchema;
|
|
31
|
+
private convertNativeType;
|
|
32
|
+
private convertWithin;
|
|
33
|
+
}
|
package/dist/converter.js
CHANGED
|
@@ -1,170 +1,153 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toJsonSchema } from
|
|
3
|
-
import { any, tuple } from
|
|
1
|
+
import { JsonSchemaFormat, JsonSchemaXNativeType, isJsonFileSchema, isStandardSchemaOptional } from "@orpc/json-schema";
|
|
2
|
+
import { toJsonSchema } from "@valibot/to-json-schema";
|
|
3
|
+
import { any, tuple } from "valibot";
|
|
4
|
+
// src/converter.ts
|
|
4
5
|
const NESTED_CONVERSION_SENTINEL = /* @__PURE__ */ any();
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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 })) };
|
|
7
|
+
* Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
|
|
8
|
+
* with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, `v.map()`, `v.file()`, and `v.blob()`.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
|
|
11
|
+
*/
|
|
12
|
+
var ValibotToJsonSchemaConverter = class {
|
|
13
|
+
conversionConfig;
|
|
14
|
+
cache;
|
|
15
|
+
constructor({ cache, ...conversionConfig } = {}) {
|
|
16
|
+
this.conversionConfig = conversionConfig;
|
|
17
|
+
if (cache) this.cache = {
|
|
18
|
+
input: /* @__PURE__ */ new WeakMap(),
|
|
19
|
+
output: /* @__PURE__ */ new WeakMap()
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
condition(schema, _direction) {
|
|
23
|
+
return schema?.["~standard"].vendor === "valibot";
|
|
24
|
+
}
|
|
25
|
+
convert(schema, direction) {
|
|
26
|
+
const valibotSchema = schema;
|
|
27
|
+
if (this.cache) {
|
|
28
|
+
const cached = this.cache[direction].get(valibotSchema);
|
|
29
|
+
if (cached) return cached;
|
|
30
|
+
}
|
|
31
|
+
const result = this.convertUncached(valibotSchema, direction);
|
|
32
|
+
if (this.cache) this.cache[direction].set(valibotSchema, result);
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
convertUncached(valibotSchema, direction) {
|
|
36
|
+
const { $schema: _schema, ...jsonSchema } = toJsonSchema(valibotSchema, this.createConversionConfig(direction));
|
|
37
|
+
return [jsonSchema, isStandardSchemaOptional(valibotSchema, direction)];
|
|
38
|
+
}
|
|
39
|
+
createConversionConfig(direction) {
|
|
40
|
+
return {
|
|
41
|
+
errorMode: "ignore",
|
|
42
|
+
...this.conversionConfig,
|
|
43
|
+
target: "draft-2020-12",
|
|
44
|
+
typeMode: direction,
|
|
45
|
+
overrideSchema: (context) => this.overrideSchema(context, direction),
|
|
46
|
+
overrideAction: (context) => this.overrideAction(context)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
overrideAction(context) {
|
|
50
|
+
return applyOverride(context, convertAction(context), this.conversionConfig.overrideAction);
|
|
51
|
+
}
|
|
52
|
+
overrideSchema(context, direction) {
|
|
53
|
+
return applyOverride(context, this.convertNativeType(context, direction), this.conversionConfig.overrideSchema);
|
|
54
|
+
}
|
|
55
|
+
convertNativeType(context, direction) {
|
|
56
|
+
switch (context.valibotSchema.type) {
|
|
57
|
+
case "bigint": return {
|
|
58
|
+
"type": "string",
|
|
59
|
+
"pattern": "^-?[0-9]+$",
|
|
60
|
+
"x-native-type": JsonSchemaXNativeType.BigInt
|
|
61
|
+
};
|
|
62
|
+
case "date": return {
|
|
63
|
+
"type": "string",
|
|
64
|
+
"format": JsonSchemaFormat.DateTime,
|
|
65
|
+
"x-native-type": JsonSchemaXNativeType.Date
|
|
66
|
+
};
|
|
67
|
+
case "set": {
|
|
68
|
+
const { value } = context.valibotSchema;
|
|
69
|
+
return {
|
|
70
|
+
"type": "array",
|
|
71
|
+
"uniqueItems": true,
|
|
72
|
+
"items": this.convertWithin(context, [value], direction)[0],
|
|
73
|
+
"x-native-type": JsonSchemaXNativeType.Set
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
case "map": {
|
|
77
|
+
const { key, value } = context.valibotSchema;
|
|
78
|
+
return {
|
|
79
|
+
"type": "array",
|
|
80
|
+
"items": {
|
|
81
|
+
type: "array",
|
|
82
|
+
prefixItems: this.convertWithin(context, [key, value], direction),
|
|
83
|
+
maxItems: 2,
|
|
84
|
+
minItems: 2
|
|
85
|
+
},
|
|
86
|
+
"x-native-type": JsonSchemaXNativeType.Map
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
case "blob":
|
|
90
|
+
case "file": return {
|
|
91
|
+
type: "string",
|
|
92
|
+
contentMediaType: "*/*"
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
convertWithin(parent, schemas, direction) {
|
|
97
|
+
const wrapper = tuple([NESTED_CONVERSION_SENTINEL, ...schemas]);
|
|
98
|
+
let child;
|
|
99
|
+
const { prefixItems } = toJsonSchema(wrapper, {
|
|
100
|
+
...this.createConversionConfig(direction),
|
|
101
|
+
definitions: {},
|
|
102
|
+
overrideSchema: (context) => {
|
|
103
|
+
if (context.valibotSchema === NESTED_CONVERSION_SENTINEL) {
|
|
104
|
+
child = context;
|
|
105
|
+
mergeConversionContext(child, parent);
|
|
106
|
+
} else if (context.valibotSchema !== wrapper) return this.overrideSchema(context, direction);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
mergeConversionContext(parent, child);
|
|
110
|
+
return prefixItems.slice(1);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
function convertAction({ valibotAction, jsonSchema }) {
|
|
114
|
+
if (valibotAction.type !== "mime_type" || !isJsonFileSchema(jsonSchema)) return;
|
|
115
|
+
const mimeTypes = toMimeTypes(valibotAction.requirement);
|
|
116
|
+
if (!mimeTypes) return;
|
|
117
|
+
if (mimeTypes.length === 1) return {
|
|
118
|
+
...jsonSchema,
|
|
119
|
+
contentMediaType: mimeTypes[0]
|
|
120
|
+
};
|
|
121
|
+
return { anyOf: mimeTypes.map((contentMediaType) => ({
|
|
122
|
+
...jsonSchema,
|
|
123
|
+
contentMediaType
|
|
124
|
+
})) };
|
|
138
125
|
}
|
|
139
126
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
127
|
+
* Reads the `v.mimeType()` requirement defensively, since it is not part of
|
|
128
|
+
* `@valibot/to-json-schema`'s contract: Valibot 1.x requires an array, and a single
|
|
129
|
+
* string is accepted in case a future version allows one. Anything else is left to
|
|
130
|
+
* the errorMode option instead of throwing a TypeError.
|
|
131
|
+
*/
|
|
145
132
|
function toMimeTypes(requirement) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
!values.every((value) => typeof value === 'string')) {
|
|
150
|
-
return undefined;
|
|
151
|
-
}
|
|
152
|
-
return [...new Set(values)];
|
|
133
|
+
const values = typeof requirement === "string" ? [requirement] : requirement;
|
|
134
|
+
if (!Array.isArray(values) || values.length === 0 || !values.every((value) => typeof value === "string")) return;
|
|
135
|
+
return [...new Set(values)];
|
|
153
136
|
}
|
|
154
137
|
/**
|
|
155
|
-
|
|
156
|
-
|
|
138
|
+
* Runs the user's override on top of oRPC's conversion, or on its own when oRPC has none.
|
|
139
|
+
*/
|
|
157
140
|
function applyOverride(context, jsonSchema, override) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
141
|
+
return jsonSchema ? override?.({
|
|
142
|
+
...context,
|
|
143
|
+
jsonSchema,
|
|
144
|
+
errors: void 0
|
|
145
|
+
}) ?? jsonSchema : override?.(context);
|
|
161
146
|
}
|
|
162
147
|
function mergeConversionContext(target, source) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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);
|
|
148
|
+
if (!source.referenceMap.size) return;
|
|
149
|
+
source.referenceMap.forEach((referenceId, schema) => target.referenceMap.set(schema, referenceId));
|
|
150
|
+
source.getterMap.forEach((schema, getter) => target.getterMap.set(getter, schema));
|
|
151
|
+
Object.assign(target.definitions, source.definitions);
|
|
170
152
|
}
|
|
153
|
+
export { ValibotToJsonSchemaConverter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { ValibotToJsonSchemaConverter, ValibotToJsonSchemaConverterOptions } from "./converter.js";
|
|
2
|
+
import { JsonSchema } from "@orpc/json-schema";
|
|
3
|
+
export { type JsonSchema, ValibotToJsonSchemaConverter, ValibotToJsonSchemaConverterOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { ValibotToJsonSchemaConverter } from "./converter.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/valibot",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
4
4
|
"description": "Valibot integration for oRPC: convert Valibot schemas into JSON Schema for OpenAPI generation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json-schema",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@orpc/json-schema": "2.0.0-beta.
|
|
39
|
+
"@orpc/json-schema": "2.0.0-beta.50",
|
|
40
40
|
"@valibot/to-json-schema": "^1.8.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|