@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.
@@ -1,16 +1,16 @@
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'> {
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;
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
- 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
- }
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 { isJsonFileSchema, isStandardSchemaOptional, JsonSchemaFormat, JsonSchemaXNativeType, } from '@orpc/json-schema';
2
- import { toJsonSchema } from '@valibot/to-json-schema';
3
- import { any, tuple } from 'valibot';
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
- * 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 })) };
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
- * 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
- */
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
- 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)];
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
- * Runs the user's override on top of oRPC's conversion, or on its own when oRPC has none.
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
- return jsonSchema
159
- ? (override?.({ ...context, jsonSchema, errors: undefined }) ?? jsonSchema)
160
- : override?.(context);
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
- // 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);
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
- export * from './converter.ts';
2
- export type { JsonSchema } from '@orpc/json-schema';
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 * from './converter.js';
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.49",
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.49",
39
+ "@orpc/json-schema": "2.0.0-beta.50",
40
40
  "@valibot/to-json-schema": "^1.8.0"
41
41
  },
42
42
  "devDependencies": {