@orpc/valibot 2.0.0-beta.40 → 2.0.0-beta.41

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
@@ -85,7 +85,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
85
85
 
86
86
  <table>
87
87
  <tr>
88
- <td width="2000"><a href="https://screenshotone.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener" title="The screenshot API for developers"><img src="https://avatars.githubusercontent.com/u/97035603?v=4" width="64" align="left" hspace="12" alt="ScreenshotOne.com"/><b>ScreenshotOne.com</b></a><br /><sub>The screenshot API for developers</sub></td>
88
+ <td width="2000"><a href="https://screenshotone.com/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="The screenshot API for developers"><img src="https://avatars.githubusercontent.com/u/97035603?v=4" width="64" align="left" hspace="12" alt="ScreenshotOne.com"/><b>ScreenshotOne.com</b></a><br /><sub>The screenshot API for developers</sub></td>
89
89
  </tr>
90
90
  <tr>
91
91
  <td width="2000"><a href="https://yuzu.health/careers?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="We&#39;re hiring NYC based engineers"><img src="https://avatars.githubusercontent.com/u/102488956?v=4" width="64" align="left" hspace="12" alt="Yuzu"/><b>Yuzu</b></a><br /><sub>We&#39;re hiring NYC based engineers</sub></td>
@@ -127,19 +127,18 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
127
127
  <td align="center"><a href="https://soymilk.party/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="あわわわとーにゅ"><img src="https://avatars.githubusercontent.com/u/17376330?u=de3353804be889f009f7e0a1582daf04d0ab292d&amp;v=4" width="139" alt="あわわわとーにゅ"/><br />あわわわとーにゅ</a></td>
128
128
  <td align="center"><a href="https://github.com/nicognaW?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="nk"><img src="https://avatars.githubusercontent.com/u/66731869?u=4699bda3a9092d3ec34fbd959450767bcc8b8b6d&amp;v=4" width="139" alt="nk"/><br />nk</a></td>
129
129
  <td align="center"><a href="https://supastarter.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="supastarter"><img src="https://avatars.githubusercontent.com/u/110960143?v=4" width="139" alt="supastarter"/><br />supastarter</a></td>
130
- <td align="center"><a href="https://github.com/divmgl?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Dexter Miguel"><img src="https://avatars.githubusercontent.com/u/5452298?u=645993204be8696c085ecf0d228c3062efe2ed65&amp;v=4" width="139" alt="Dexter Miguel"/><br />Dexter Miguel</a></td>
131
130
  <td align="center"><a href="https://github.com/herrfugbaum?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="herrfugbaum"><img src="https://avatars.githubusercontent.com/u/12859776?u=644dc1666d0220bc0468eb0de3c56b919f635b16&amp;v=4" width="139" alt="herrfugbaum"/><br />herrfugbaum</a></td>
131
+ <td align="center"><a href="https://laststance.io/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryota Murakami"><img src="https://avatars.githubusercontent.com/u/5501268?u=599389e03340734325726ca3f8f423c021d47d7f&amp;v=4" width="139" alt="Ryota Murakami"/><br />Ryota Murakami</a></td>
132
132
  </tr>
133
133
  <tr>
134
- <td align="center"><a href="https://laststance.io/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryota Murakami"><img src="https://avatars.githubusercontent.com/u/5501268?u=599389e03340734325726ca3f8f423c021d47d7f&amp;v=4" width="139" alt="Ryota Murakami"/><br />Ryota Murakami</a></td>
135
134
  <td align="center"><a href="https://cra.mr/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="David Cramer"><img src="https://avatars.githubusercontent.com/u/23610?v=4" width="139" alt="David Cramer"/><br />David Cramer</a></td>
136
135
  <td align="center"><a href="https://valerii15298.github.io/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Valerii Petryniak"><img src="https://avatars.githubusercontent.com/u/44531564?u=88ac74d9bacd20401518441907acad21063cd397&amp;v=4" width="139" alt="Valerii Petryniak"/><br />Valerii Petryniak</a></td>
137
136
  <td align="center"><a href="https://letstri.dev/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Valerii Strilets"><img src="https://avatars.githubusercontent.com/u/13253748?u=c7b10399ccc8f8081e24db94ec32cd9858e86ac3&amp;v=4" width="139" alt="Valerii Strilets"/><br />Valerii Strilets</a></td>
138
137
  <td align="center"><a href="https://blacklight.sh/?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kyle Mistele"><img src="https://avatars.githubusercontent.com/u/18430555?u=3afebeb81de666e35aaac3ed46f14159d7603ffb&amp;v=4" width="139" alt="Kyle Mistele"/><br />Kyle Mistele</a></td>
139
138
  <td align="center"><a href="https://github.com/christ12938?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td>
139
+ <td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
140
140
  </tr>
141
141
  <tr>
142
- <td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
143
142
  <td align="center"><a href="https://github.com/itigoore01?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&amp;v=4" width="139" alt="shota"/><br />shota</a></td>
144
143
  <td align="center"><a href="https://github.com/ellis-driscoll?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&amp;v=4" width="139" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
145
144
  <td align="center"><a href="https://github.com/hoangbn?ref=middleapi&amp;utm_source=middleapi&amp;utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&amp;v=4" width="139" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
@@ -173,12 +172,13 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
173
172
  <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="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
174
173
  <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="119" alt="Guy Ariely"/><br />Guy Ariely</a></td>
175
174
  <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="119" alt="PaulSenon"/><br />PaulSenon</a></td>
175
+ <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="119" alt="Shotaro Nakamura"/><br />Shotaro Nakamura</a></td>
176
176
  <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="119" alt="Alex"/><br />Alex</a></td>
177
177
  <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="119" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
178
178
  </tr>
179
179
  </table>
180
180
 
181
- With thanks to [36 past sponsors](https://htmlpreview.github.io/?https://github.com/middleapi/static/blob/main/sponsors.svg) who helped get oRPC here.
181
+ With thanks to [38 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
182
182
 
183
183
  ## References
184
184
 
package/dist/index.d.mts CHANGED
@@ -26,7 +26,10 @@ declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
26
26
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
27
27
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
28
28
  private convertUncached;
29
- private convertValibot;
29
+ private createConversionConfig;
30
+ private overrideSchema;
31
+ private convertNativeType;
32
+ private convertWithin;
30
33
  }
31
34
 
32
35
  export { ValibotToJsonSchemaConverter };
package/dist/index.d.ts CHANGED
@@ -26,7 +26,10 @@ declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
26
26
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
27
27
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
28
28
  private convertUncached;
29
- private convertValibot;
29
+ private createConversionConfig;
30
+ private overrideSchema;
31
+ private convertNativeType;
32
+ private convertWithin;
30
33
  }
31
34
 
32
35
  export { ValibotToJsonSchemaConverter };
package/dist/index.mjs CHANGED
@@ -1,6 +1,8 @@
1
- import { JsonSchemaXNativeType, JsonSchemaFormat } from '@orpc/json-schema';
1
+ import { isStandardSchemaOptional, JsonSchemaXNativeType, JsonSchemaFormat } from '@orpc/json-schema';
2
2
  import { toJsonSchema } from '@valibot/to-json-schema';
3
+ import { any, tuple } from 'valibot';
3
4
 
5
+ const NESTED_CONVERSION_SENTINEL = any();
4
6
  class ValibotToJsonSchemaConverter {
5
7
  conversionConfig;
6
8
  cache;
@@ -28,60 +30,68 @@ class ValibotToJsonSchemaConverter {
28
30
  return result;
29
31
  }
30
32
  convertUncached(valibotSchema, direction) {
31
- const jsonSchema = this.convertValibot(valibotSchema, direction);
32
- let optional = false;
33
- try {
34
- const result = valibotSchema["~standard"].validate(void 0);
35
- if (!(result instanceof Promise) && !result.issues) {
36
- optional = direction === "input" ? true : result.value === void 0;
37
- }
38
- } catch {
39
- }
40
- return [jsonSchema, optional];
33
+ const { $schema, ...jsonSchema } = toJsonSchema(valibotSchema, this.createConversionConfig(direction));
34
+ return [jsonSchema, isStandardSchemaOptional(valibotSchema, direction)];
41
35
  }
42
- convertValibot(schema, direction) {
43
- const jsonSchema = toJsonSchema(schema, {
36
+ createConversionConfig(direction) {
37
+ return {
44
38
  errorMode: "ignore",
45
39
  ...this.conversionConfig,
46
40
  target: "draft-2020-12",
47
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: {},
48
75
  overrideSchema: (context) => {
49
- if (context.valibotSchema.type === "bigint") {
50
- context.jsonSchema.type = "string";
51
- context.jsonSchema.pattern = "^-?[0-9]+$";
52
- context.jsonSchema["x-native-type"] = JsonSchemaXNativeType.BigInt;
53
- } else if (context.valibotSchema.type === "date") {
54
- context.jsonSchema.type = "string";
55
- context.jsonSchema.format = JsonSchemaFormat.DateTime;
56
- context.jsonSchema["x-native-type"] = JsonSchemaXNativeType.Date;
57
- } else if (context.valibotSchema.type === "set") {
58
- const setSchema = context.valibotSchema;
59
- context.jsonSchema.type = "array";
60
- context.jsonSchema.uniqueItems = true;
61
- context.jsonSchema.items = this.convertValibot(setSchema.value, direction);
62
- context.jsonSchema["x-native-type"] = JsonSchemaXNativeType.Set;
63
- } else if (context.valibotSchema.type === "map") {
64
- const mapSchema = context.valibotSchema;
65
- context.jsonSchema.type = "array";
66
- context.jsonSchema.items = {
67
- type: "array",
68
- prefixItems: [
69
- this.convertValibot(mapSchema.key, direction),
70
- this.convertValibot(mapSchema.value, direction)
71
- ],
72
- maxItems: 2,
73
- minItems: 2
74
- };
75
- context.jsonSchema["x-native-type"] = JsonSchemaXNativeType.Map;
76
- }
77
- if (this.conversionConfig.overrideSchema) {
78
- return this.conversionConfig.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);
79
81
  }
80
82
  }
81
83
  });
82
- const { $schema, ...rest } = jsonSchema;
83
- return rest;
84
+ mergeConversionContext(parent, child);
85
+ return prefixItems.slice(1);
86
+ }
87
+ }
88
+ function mergeConversionContext(target, source) {
89
+ if (!source.referenceMap.size) {
90
+ return;
84
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);
85
95
  }
86
96
 
87
97
  export { ValibotToJsonSchemaConverter };
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.40",
4
+ "version": "2.0.0-beta.41",
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.40",
42
+ "@orpc/json-schema": "2.0.0-beta.41",
43
43
  "@valibot/to-json-schema": "^1.8.0"
44
44
  },
45
45
  "devDependencies": {