@orpc/valibot 2.0.0-beta.48 → 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 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="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fvalibot?logo=npm" />
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)
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
208
209
  <tr>
209
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>
210
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&amp;v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
211
- <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&amp;v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
212
212
  </tr>
213
213
  </table>
214
214
 
@@ -0,0 +1,33 @@
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()`, `v.map()`, `v.file()`, and `v.blob()`.
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 overrideAction;
30
+ private overrideSchema;
31
+ private convertNativeType;
32
+ private convertWithin;
33
+ }
@@ -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
+ }
@@ -0,0 +1,2 @@
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
- "type": "module",
4
- "version": "2.0.0-beta.48",
3
+ "version": "2.0.0-beta.49",
5
4
  "description": "Valibot integration for oRPC: convert Valibot schemas into JSON Schema for OpenAPI generation",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
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
- "keywords": [
18
- "orpc",
19
- "valibot",
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.mts",
31
- "import": "./dist/index.mjs",
32
- "default": "./dist/index.mjs"
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.48",
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
- "scripts": {
50
- "type:check": "tsc -b"
51
- }
46
+ "peerDependencies": {
47
+ "valibot": ">=1.5.0"
48
+ },
49
+ "scripts": {}
52
50
  }
@@ -1,32 +0,0 @@
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
- }
@@ -1,123 +0,0 @@
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 DELETED
@@ -1,2 +0,0 @@
1
- export * from "./converter.mjs";
2
- export type { JsonSchema } from "@orpc/json-schema";
package/dist/index.mjs DELETED
@@ -1 +0,0 @@
1
- export * from "./converter.mjs";