@orpc/valibot 2.0.0-beta.22 → 2.0.0-beta.23

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/index.d.mts CHANGED
@@ -2,12 +2,24 @@ import { JsonSchemaConverter, AnySchema, JsonSchemaConverterDirection, JsonSchem
2
2
  import { ConversionConfig } from '@valibot/to-json-schema';
3
3
 
4
4
  interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'target' | 'typeMode' | 'overrideRef'> {
5
+ /**
6
+ * Caches conversion results in a WeakMap keyed by the Valibot schema instance,
7
+ * so converting the same schema again is free.
8
+ *
9
+ * When enabled, repeated conversions return the same JSON schema object,
10
+ * so treat returned schemas as immutable.
11
+ *
12
+ * @default false
13
+ */
14
+ cache?: boolean;
5
15
  }
6
16
  declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
7
- private readonly options;
8
- constructor(options?: ValibotToJsonSchemaConverterOptions);
17
+ private readonly conversionConfig;
18
+ private readonly cache;
19
+ constructor({ cache, ...conversionConfig }?: ValibotToJsonSchemaConverterOptions);
9
20
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
10
21
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
22
+ private convertUncached;
11
23
  private convertValibot;
12
24
  }
13
25
 
package/dist/index.d.ts CHANGED
@@ -2,12 +2,24 @@ import { JsonSchemaConverter, AnySchema, JsonSchemaConverterDirection, JsonSchem
2
2
  import { ConversionConfig } from '@valibot/to-json-schema';
3
3
 
4
4
  interface ValibotToJsonSchemaConverterOptions extends Omit<ConversionConfig, 'target' | 'typeMode' | 'overrideRef'> {
5
+ /**
6
+ * Caches conversion results in a WeakMap keyed by the Valibot schema instance,
7
+ * so converting the same schema again is free.
8
+ *
9
+ * When enabled, repeated conversions return the same JSON schema object,
10
+ * so treat returned schemas as immutable.
11
+ *
12
+ * @default false
13
+ */
14
+ cache?: boolean;
5
15
  }
6
16
  declare class ValibotToJsonSchemaConverter implements JsonSchemaConverter {
7
- private readonly options;
8
- constructor(options?: ValibotToJsonSchemaConverterOptions);
17
+ private readonly conversionConfig;
18
+ private readonly cache;
19
+ constructor({ cache, ...conversionConfig }?: ValibotToJsonSchemaConverterOptions);
9
20
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
10
21
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
22
+ private convertUncached;
11
23
  private convertValibot;
12
24
  }
13
25
 
package/dist/index.mjs CHANGED
@@ -2,14 +2,32 @@ import { JsonSchemaXNativeType, JsonSchemaFormat } from '@orpc/json-schema';
2
2
  import { toJsonSchema } from '@valibot/to-json-schema';
3
3
 
4
4
  class ValibotToJsonSchemaConverter {
5
- constructor(options = {}) {
6
- this.options = options;
5
+ conversionConfig;
6
+ cache;
7
+ constructor({ cache, ...conversionConfig } = {}) {
8
+ this.conversionConfig = conversionConfig;
9
+ if (cache) {
10
+ this.cache = { input: /* @__PURE__ */ new WeakMap(), output: /* @__PURE__ */ new WeakMap() };
11
+ }
7
12
  }
8
13
  condition(schema, _direction) {
9
14
  return schema?.["~standard"].vendor === "valibot";
10
15
  }
11
16
  convert(schema, direction) {
12
17
  const valibotSchema = schema;
18
+ if (this.cache) {
19
+ const cached = this.cache[direction].get(valibotSchema);
20
+ if (cached) {
21
+ return cached;
22
+ }
23
+ }
24
+ const result = this.convertUncached(valibotSchema, direction);
25
+ if (this.cache) {
26
+ this.cache[direction].set(valibotSchema, result);
27
+ }
28
+ return result;
29
+ }
30
+ convertUncached(valibotSchema, direction) {
13
31
  const jsonSchema = this.convertValibot(valibotSchema, direction);
14
32
  let optional = false;
15
33
  try {
@@ -24,7 +42,7 @@ class ValibotToJsonSchemaConverter {
24
42
  convertValibot(schema, direction) {
25
43
  const jsonSchema = toJsonSchema(schema, {
26
44
  errorMode: "ignore",
27
- ...this.options,
45
+ ...this.conversionConfig,
28
46
  target: "draft-2020-12",
29
47
  typeMode: direction,
30
48
  overrideSchema: (context) => {
@@ -56,8 +74,8 @@ class ValibotToJsonSchemaConverter {
56
74
  };
57
75
  context.jsonSchema["x-native-type"] = JsonSchemaXNativeType.Map;
58
76
  }
59
- if (this.options.overrideSchema) {
60
- return this.options.overrideSchema(context);
77
+ if (this.conversionConfig.overrideSchema) {
78
+ return this.conversionConfig.overrideSchema(context);
61
79
  }
62
80
  }
63
81
  });
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.22",
4
+ "version": "2.0.0-beta.23",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.dev",
7
7
  "repository": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@valibot/to-json-schema": "^1.5.0",
33
- "@orpc/json-schema": "2.0.0-beta.22"
33
+ "@orpc/json-schema": "2.0.0-beta.23"
34
34
  },
35
35
  "devDependencies": {
36
36
  "valibot": "^1.4.1",