@orpc/valibot 2.0.0-beta.47 → 2.0.0-beta.48

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 CHANGED
@@ -191,15 +191,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
191
191
  <td align="center"><a href="https://kellychan.im/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&amp;v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
192
192
  <td align="center"><a href="https://guyariely.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&amp;v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
193
193
  <td align="center"><a href="https://paulsenon.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&amp;v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
194
- <td align="center"><a href="https://nakasyou.how/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Shotaro Nakamura"><img src="https://avatars.githubusercontent.com/u/79000684?u=f644df3f29f0e8677a90967115774564f1d9d6ab&amp;v=4" width="139" alt="Shotaro Nakamura"/><br />Shotaro Nakamura</a></td>
194
+ <td align="center"><a href="https://piscis.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&amp;v=4" width="139" alt="Alex"/><br />Alex</a></td>
195
195
  </tr>
196
196
  <tr>
197
- <td align="center"><a href="https://piscis.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&amp;v=4" width="139" alt="Alex"/><br />Alex</a></td>
198
197
  <td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&amp;v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
199
198
  </tr>
200
199
  </table>
201
200
 
202
- With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
201
+ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
203
202
 
204
203
  ## Open Source Programs
205
204
 
@@ -0,0 +1,32 @@
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;
14
+ }
15
+ /**
16
+ * Converts Valibot schemas into JSON Schema using Valibot's built-in `toJsonSchema`,
17
+ * with additional support for types such as `v.bigint()`, `v.date()`, `v.set()`, and `v.map()`.
18
+ *
19
+ * @see {@link https://orpc.dev/docs/integrations/valibot | Valibot Integration}
20
+ */
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 overrideSchema;
30
+ private convertNativeType;
31
+ private convertWithin;
32
+ }
@@ -0,0 +1,123 @@
1
+ import { 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
+ export class ValibotToJsonSchemaConverter {
7
+ conversionConfig;
8
+ cache;
9
+ constructor({ cache, ...conversionConfig } = {}) {
10
+ this.conversionConfig = conversionConfig;
11
+ if (cache) {
12
+ this.cache = {
13
+ input: new WeakMap(),
14
+ output: new WeakMap()
15
+ };
16
+ }
17
+ }
18
+ condition(schema, _direction) {
19
+ return schema?.["~standard"].vendor === "valibot";
20
+ }
21
+ convert(schema, direction) {
22
+ const valibotSchema = schema;
23
+ if (this.cache) {
24
+ const cached = this.cache[direction].get(valibotSchema);
25
+ if (cached) {
26
+ return cached;
27
+ }
28
+ }
29
+ const result = this.convertUncached(valibotSchema, direction);
30
+ if (this.cache) {
31
+ this.cache[direction].set(valibotSchema, result);
32
+ }
33
+ return result;
34
+ }
35
+ convertUncached(valibotSchema, direction) {
36
+
37
+
38
+ const { $schema, ...jsonSchema } = toJsonSchema(valibotSchema, this.createConversionConfig(direction));
39
+ return [jsonSchema, isStandardSchemaOptional(valibotSchema, direction)];
40
+ }
41
+ createConversionConfig(direction) {
42
+ return {
43
+ errorMode: "ignore",
44
+ ...this.conversionConfig,
45
+ target: "draft-2020-12",
46
+ typeMode: direction,
47
+ overrideSchema: (context) => this.overrideSchema(context, direction)
48
+ };
49
+ }
50
+ overrideSchema(context, direction) {
51
+ const jsonSchema = this.convertNativeType(context, direction);
52
+ return jsonSchema ? this.conversionConfig.overrideSchema?.({
53
+ ...context,
54
+ jsonSchema,
55
+ errors: undefined
56
+ }) ?? jsonSchema : this.conversionConfig.overrideSchema?.(context);
57
+ }
58
+ convertNativeType(context, direction) {
59
+ switch (context.valibotSchema.type) {
60
+ case "bigint": return {
61
+ "type": "string",
62
+ "pattern": "^-?[0-9]+$",
63
+ "x-native-type": JsonSchemaXNativeType.BigInt
64
+ };
65
+ case "date": return {
66
+ "type": "string",
67
+ "format": JsonSchemaFormat.DateTime,
68
+ "x-native-type": JsonSchemaXNativeType.Date
69
+ };
70
+ case "set": {
71
+ const { value } = context.valibotSchema;
72
+ return {
73
+ "type": "array",
74
+ "uniqueItems": true,
75
+ "items": this.convertWithin(context, [value], direction)[0],
76
+ "x-native-type": JsonSchemaXNativeType.Set
77
+ };
78
+ }
79
+ case "map": {
80
+ const { key, value } = context.valibotSchema;
81
+ return {
82
+ "type": "array",
83
+ "items": {
84
+ type: "array",
85
+ prefixItems: this.convertWithin(context, [key, value], direction),
86
+ maxItems: 2,
87
+ minItems: 2
88
+ },
89
+ "x-native-type": JsonSchemaXNativeType.Map
90
+ };
91
+ }
92
+ }
93
+ }
94
+ convertWithin(parent, schemas, direction) {
95
+
96
+
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) {
107
+ return this.overrideSchema(context, direction);
108
+ }
109
+ }
110
+ });
111
+ mergeConversionContext(parent, child);
112
+ return prefixItems.slice(1);
113
+ }
114
+ }
115
+ function mergeConversionContext(target, source) {
116
+
117
+ if (!source.referenceMap.size) {
118
+ return;
119
+ }
120
+ source.referenceMap.forEach((referenceId, schema) => target.referenceMap.set(schema, referenceId));
121
+ source.getterMap.forEach((schema, getter) => target.getterMap.set(getter, schema));
122
+ Object.assign(target.definitions, source.definitions);
123
+ }
package/dist/index.d.mts CHANGED
@@ -1,37 +1,2 @@
1
- import { JsonSchemaConverter, AnySchema, JsonSchemaConverterDirection, JsonSchema } from '@orpc/json-schema';
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.mjs";
2
+ export type { JsonSchema } from "@orpc/json-schema";
package/dist/index.mjs CHANGED
@@ -1,97 +1 @@
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 };
1
+ export * from "./converter.mjs";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/valibot",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.47",
4
+ "version": "2.0.0-beta.48",
5
5
  "description": "Valibot integration for oRPC: convert Valibot schemas into JSON Schema for OpenAPI generation",
6
6
  "license": "MIT",
7
7
  "funding": [
@@ -39,7 +39,7 @@
39
39
  "valibot": ">=1.5.0"
40
40
  },
41
41
  "dependencies": {
42
- "@orpc/json-schema": "2.0.0-beta.47",
42
+ "@orpc/json-schema": "2.0.0-beta.48",
43
43
  "@valibot/to-json-schema": "^1.8.0"
44
44
  },
45
45
  "devDependencies": {
package/dist/index.d.ts DELETED
@@ -1,37 +0,0 @@
1
- import { JsonSchemaConverter, AnySchema, JsonSchemaConverterDirection, JsonSchema } from '@orpc/json-schema';
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 };