@bram-dc/fastify-type-provider-zod 5.0.3 → 7.0.1

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.
Files changed (41) hide show
  1. package/README.md +34 -4
  2. package/dist/cjs/core.cjs +33 -45
  3. package/dist/cjs/core.cjs.map +1 -1
  4. package/dist/cjs/core.d.cts +11 -12
  5. package/dist/cjs/errors.cjs +1 -1
  6. package/dist/cjs/errors.cjs.map +1 -1
  7. package/dist/cjs/registry.cjs +43 -0
  8. package/dist/cjs/registry.cjs.map +1 -0
  9. package/dist/cjs/registry.d.cts +9 -0
  10. package/dist/cjs/utils.cjs +21 -0
  11. package/dist/cjs/utils.cjs.map +1 -0
  12. package/dist/cjs/utils.d.cts +12 -0
  13. package/dist/cjs/zod-to-json.cjs +36 -41
  14. package/dist/cjs/zod-to-json.cjs.map +1 -1
  15. package/dist/cjs/zod-to-json.d.cts +7 -7
  16. package/dist/esm/core.d.ts +11 -12
  17. package/dist/esm/core.js +34 -46
  18. package/dist/esm/core.js.map +1 -1
  19. package/dist/esm/errors.js +1 -1
  20. package/dist/esm/errors.js.map +1 -1
  21. package/dist/esm/registry.d.ts +9 -0
  22. package/dist/esm/registry.js +43 -0
  23. package/dist/esm/registry.js.map +1 -0
  24. package/dist/esm/utils.d.ts +12 -0
  25. package/dist/esm/utils.js +21 -0
  26. package/dist/esm/utils.js.map +1 -0
  27. package/dist/esm/zod-to-json.d.ts +7 -7
  28. package/dist/esm/zod-to-json.js +36 -41
  29. package/dist/esm/zod-to-json.js.map +1 -1
  30. package/package.json +13 -11
  31. package/src/core.ts +44 -60
  32. package/src/registry.ts +64 -0
  33. package/src/utils.ts +33 -0
  34. package/src/zod-to-json.ts +62 -52
  35. package/dist/cjs/json-to-oas.cjs +0 -85
  36. package/dist/cjs/json-to-oas.cjs.map +0 -1
  37. package/dist/cjs/json-to-oas.d.cts +0 -9
  38. package/dist/esm/json-to-oas.d.ts +0 -9
  39. package/dist/esm/json-to-oas.js +0 -85
  40. package/dist/esm/json-to-oas.js.map +0 -1
  41. package/src/json-to-oas.ts +0 -109
@@ -1,29 +1,28 @@
1
1
  import type { SwaggerTransform, SwaggerTransformObject } from "@fastify/swagger";
2
- import type { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifySchema, FastifySchemaCompiler, FastifyTypeProvider, RawServerBase, RawServerDefault } from "fastify";
3
- import type { FastifySerializerCompiler } from "fastify/types/schema";
4
- import type { $ZodRegistry, input, output } from "zod/v4/core";
2
+ import type { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, FastifyTypeProvider, RawServerBase, RawServerDefault } from "fastify";
3
+ import type { $ZodRegistry, output } from "zod/v4/core";
5
4
  import { $ZodType } from "zod/v4/core";
5
+ import { type SchemaRegistryMeta } from "../esm/registry.js";
6
+ import { type ZodToJsonConfig } from "../esm/zod-to-json.js";
6
7
  export interface ZodTypeProvider extends FastifyTypeProvider {
7
8
  validator: this["schema"] extends $ZodType ? output<this["schema"]> : unknown;
8
- serializer: this["schema"] extends $ZodType ? input<this["schema"]> : unknown;
9
+ serializer: this["schema"] extends $ZodType ? output<this["schema"]> : unknown;
9
10
  }
10
11
  interface Schema extends FastifySchema {
11
12
  hide?: boolean;
12
13
  }
13
14
  type CreateJsonSchemaTransformOptions = {
14
15
  skipList?: readonly string[];
15
- schemaRegistry?: $ZodRegistry<{
16
- id?: string | undefined;
17
- }>;
16
+ schemaRegistry?: $ZodRegistry<SchemaRegistryMeta>;
17
+ zodToJsonConfig?: ZodToJsonConfig;
18
18
  };
19
- export declare const createJsonSchemaTransform: ({ skipList, schemaRegistry }: CreateJsonSchemaTransformOptions) => SwaggerTransform<Schema>;
19
+ export declare const createJsonSchemaTransform: ({ skipList, schemaRegistry, zodToJsonConfig }: CreateJsonSchemaTransformOptions) => SwaggerTransform<Schema>;
20
20
  export declare const jsonSchemaTransform: SwaggerTransform<Schema>;
21
21
  type CreateJsonSchemaTransformObjectOptions = {
22
- schemaRegistry?: $ZodRegistry<{
23
- id?: string | undefined;
24
- }>;
22
+ schemaRegistry?: $ZodRegistry<SchemaRegistryMeta>;
23
+ zodToJsonConfig?: ZodToJsonConfig;
25
24
  };
26
- export declare const createJsonSchemaTransformObject: ({ schemaRegistry }: CreateJsonSchemaTransformObjectOptions) => SwaggerTransformObject;
25
+ export declare const createJsonSchemaTransformObject: ({ schemaRegistry, zodToJsonConfig }: CreateJsonSchemaTransformObjectOptions) => SwaggerTransformObject;
27
26
  export declare const jsonSchemaTransformObject: SwaggerTransformObject;
28
27
  export declare const validatorCompiler: FastifySchemaCompiler<$ZodType>;
29
28
  type ReplacerFunction = (this: any, key: string, value: any) => any;
package/dist/esm/core.js CHANGED
@@ -1,6 +1,7 @@
1
- import { globalRegistry, safeParse, $ZodType } from "zod/v4/core";
1
+ import { globalRegistry, safeEncode, safeDecode, $ZodType } from "zod/v4/core";
2
2
  import { ResponseSerializationError, createValidationError, InvalidSchemaError } from "./errors.js";
3
- import { getOASVersion, jsonSchemaToOAS } from "./json-to-oas.js";
3
+ import { generateIORegistries } from "./registry.js";
4
+ import { assertIsOpenAPIObject, getJSONSchemaTarget } from "./utils.js";
4
5
  import { zodSchemaToJson, zodRegistryToJson } from "./zod-to-json.js";
5
6
  const defaultSkipList = [
6
7
  "/documentation/",
@@ -13,19 +14,24 @@ const defaultSkipList = [
13
14
  ];
14
15
  const createJsonSchemaTransform = ({
15
16
  skipList = defaultSkipList,
16
- schemaRegistry = globalRegistry
17
+ schemaRegistry = globalRegistry,
18
+ zodToJsonConfig = {}
17
19
  }) => {
18
- return (input) => {
19
- if ("swaggerObject" in input) {
20
- throw new Error("OpenAPI 2.0 is not supported");
21
- }
22
- const { schema, url } = input;
20
+ return (document) => {
21
+ assertIsOpenAPIObject(document);
22
+ const { schema, url } = document;
23
23
  if (!schema) {
24
24
  return {
25
25
  schema,
26
26
  url
27
27
  };
28
28
  }
29
+ const target = getJSONSchemaTarget(document.openapiObject.openapi);
30
+ const config = {
31
+ target,
32
+ ...zodToJsonConfig
33
+ };
34
+ const { inputRegistry, outputRegistry } = generateIORegistries(schemaRegistry);
29
35
  const { response, headers, querystring, body, params, hide, ...rest } = schema;
30
36
  const transformed = {};
31
37
  if (skipList.includes(url) || hide) {
@@ -33,26 +39,17 @@ const createJsonSchemaTransform = ({
33
39
  return { schema: transformed, url };
34
40
  }
35
41
  const zodSchemas = { headers, querystring, body, params };
36
- const oasVersion = getOASVersion(input);
37
42
  for (const prop in zodSchemas) {
38
43
  const zodSchema = zodSchemas[prop];
39
44
  if (zodSchema) {
40
- const jsonSchema = zodSchemaToJson(zodSchema, schemaRegistry, "input");
41
- const oasSchema = jsonSchemaToOAS(jsonSchema, oasVersion);
42
- transformed[prop] = oasSchema;
45
+ transformed[prop] = zodSchemaToJson(zodSchema, inputRegistry, "input", config);
43
46
  }
44
47
  }
45
48
  if (response) {
46
49
  transformed.response = {};
47
50
  for (const prop in response) {
48
51
  const zodSchema = resolveSchema(response[prop]);
49
- const jsonSchema = zodSchemaToJson(zodSchema, schemaRegistry, "output");
50
- if (jsonSchema.type === "null") {
51
- transformed.response[prop] = jsonSchema;
52
- continue;
53
- }
54
- const oasSchema = jsonSchemaToOAS(jsonSchema, oasVersion);
55
- transformed.response[prop] = oasSchema;
52
+ transformed.response[prop] = zodSchemaToJson(zodSchema, outputRegistry, "output", config);
56
53
  }
57
54
  }
58
55
  for (const prop in rest) {
@@ -66,42 +63,33 @@ const createJsonSchemaTransform = ({
66
63
  };
67
64
  const jsonSchemaTransform = createJsonSchemaTransform({});
68
65
  const createJsonSchemaTransformObject = ({
69
- schemaRegistry = globalRegistry
70
- }) => (input) => {
71
- if ("swaggerObject" in input) {
72
- throw new Error("OpenAPI 2.0 is not supported");
73
- }
74
- const oasVersion = getOASVersion(input);
75
- const inputSchemas = zodRegistryToJson(schemaRegistry, "input");
76
- const outputSchemas = zodRegistryToJson(schemaRegistry, "output");
77
- for (const key in outputSchemas) {
78
- if (inputSchemas[key]) {
79
- throw new Error(
80
- `Collision detected for schema "${key}". The is already an input schema with the same name.`
81
- );
82
- }
83
- }
84
- const jsonSchemas = {
85
- ...inputSchemas,
86
- ...outputSchemas
66
+ schemaRegistry = globalRegistry,
67
+ zodToJsonConfig = {}
68
+ }) => (document) => {
69
+ assertIsOpenAPIObject(document);
70
+ const target = getJSONSchemaTarget(document.openapiObject.openapi);
71
+ const config = {
72
+ target,
73
+ ...zodToJsonConfig
87
74
  };
88
- const oasSchemas = Object.fromEntries(
89
- Object.entries(jsonSchemas).map(([key, value]) => [key, jsonSchemaToOAS(value, oasVersion)])
90
- );
75
+ const { inputRegistry, outputRegistry } = generateIORegistries(schemaRegistry);
76
+ const inputSchemas = zodRegistryToJson(inputRegistry, "input", config);
77
+ const outputSchemas = zodRegistryToJson(outputRegistry, "output", config);
91
78
  return {
92
- ...input.openapiObject,
79
+ ...document.openapiObject,
93
80
  components: {
94
- ...input.openapiObject.components,
81
+ ...document.openapiObject.components,
95
82
  schemas: {
96
- ...input.openapiObject.components?.schemas,
97
- ...oasSchemas
83
+ ...document.openapiObject.components?.schemas,
84
+ ...inputSchemas,
85
+ ...outputSchemas
98
86
  }
99
87
  }
100
88
  };
101
89
  };
102
90
  const jsonSchemaTransformObject = createJsonSchemaTransformObject({});
103
91
  const validatorCompiler = ({ schema }) => (data) => {
104
- const result = safeParse(schema, data);
92
+ const result = safeDecode(schema, data);
105
93
  if (result.error) {
106
94
  return { error: createValidationError(result.error) };
107
95
  }
@@ -118,7 +106,7 @@ function resolveSchema(maybeSchema) {
118
106
  }
119
107
  const createSerializerCompiler = (options) => ({ schema: maybeSchema, method, url }) => (data) => {
120
108
  const schema = resolveSchema(maybeSchema);
121
- const result = safeParse(schema, data);
109
+ const result = safeEncode(schema, data);
122
110
  if (result.error) {
123
111
  throw new ResponseSerializationError(method, url, { cause: result.error });
124
112
  }
@@ -1 +1 @@
1
- {"version":3,"file":"core.js","sources":["../../src/core.ts"],"sourcesContent":["import type { SwaggerTransform, SwaggerTransformObject } from '@fastify/swagger'\nimport type {\n FastifyPluginAsync,\n FastifyPluginCallback,\n FastifyPluginOptions,\n FastifySchema,\n FastifySchemaCompiler,\n FastifyTypeProvider,\n RawServerBase,\n RawServerDefault,\n} from 'fastify'\nimport type { FastifySerializerCompiler } from 'fastify/types/schema'\nimport type { $ZodRegistry, input, output } from 'zod/v4/core'\nimport { $ZodType, globalRegistry, safeParse } from 'zod/v4/core'\nimport { createValidationError, InvalidSchemaError, ResponseSerializationError } from './errors'\nimport { getOASVersion, jsonSchemaToOAS } from './json-to-oas'\nimport { zodRegistryToJson, zodSchemaToJson } from './zod-to-json'\n\ntype FreeformRecord = Record<string, any>\n\nconst defaultSkipList = [\n '/documentation/',\n '/documentation/initOAuth',\n '/documentation/json',\n '/documentation/uiConfig',\n '/documentation/yaml',\n '/documentation/*',\n '/documentation/static/*',\n]\n\nexport interface ZodTypeProvider extends FastifyTypeProvider {\n validator: this['schema'] extends $ZodType ? output<this['schema']> : unknown\n serializer: this['schema'] extends $ZodType ? input<this['schema']> : unknown\n}\n\ninterface Schema extends FastifySchema {\n hide?: boolean\n}\n\ntype CreateJsonSchemaTransformOptions = {\n skipList?: readonly string[]\n schemaRegistry?: $ZodRegistry<{ id?: string | undefined }>\n}\n\nexport const createJsonSchemaTransform = ({\n skipList = defaultSkipList,\n schemaRegistry = globalRegistry,\n}: CreateJsonSchemaTransformOptions): SwaggerTransform<Schema> => {\n return (input) => {\n if ('swaggerObject' in input) {\n throw new Error('OpenAPI 2.0 is not supported')\n }\n\n const { schema, url } = input\n\n if (!schema) {\n return {\n schema,\n url,\n }\n }\n\n const { response, headers, querystring, body, params, hide, ...rest } = schema\n\n const transformed: FreeformRecord = {}\n\n if (skipList.includes(url) || hide) {\n transformed.hide = true\n return { schema: transformed, url }\n }\n\n const zodSchemas: FreeformRecord = { headers, querystring, body, params }\n\n const oasVersion = getOASVersion(input)\n\n for (const prop in zodSchemas) {\n const zodSchema = zodSchemas[prop]\n if (zodSchema) {\n const jsonSchema = zodSchemaToJson(zodSchema, schemaRegistry, 'input')\n const oasSchema = jsonSchemaToOAS(jsonSchema, oasVersion)\n\n transformed[prop] = oasSchema\n }\n }\n\n if (response) {\n transformed.response = {}\n\n for (const prop in response as any) {\n const zodSchema = resolveSchema((response as any)[prop])\n const jsonSchema = zodSchemaToJson(zodSchema, schemaRegistry, 'output')\n\n // Check is the JSON schema is null then return as it is since fastify-swagger will handle it\n if (jsonSchema.type === 'null') {\n transformed.response[prop] = jsonSchema\n continue\n }\n\n const oasSchema = jsonSchemaToOAS(jsonSchema, oasVersion)\n\n transformed.response[prop] = oasSchema\n }\n }\n\n for (const prop in rest) {\n const meta = rest[prop as keyof typeof rest]\n if (meta) {\n transformed[prop] = meta\n }\n }\n\n return { schema: transformed, url }\n }\n}\n\nexport const jsonSchemaTransform: SwaggerTransform<Schema> = createJsonSchemaTransform({})\n\ntype CreateJsonSchemaTransformObjectOptions = {\n schemaRegistry?: $ZodRegistry<{ id?: string | undefined }>\n}\n\nexport const createJsonSchemaTransformObject =\n ({\n schemaRegistry = globalRegistry,\n }: CreateJsonSchemaTransformObjectOptions): SwaggerTransformObject =>\n (input) => {\n if ('swaggerObject' in input) {\n throw new Error('OpenAPI 2.0 is not supported')\n }\n\n const oasVersion = getOASVersion(input)\n\n const inputSchemas = zodRegistryToJson(schemaRegistry, 'input')\n const outputSchemas = zodRegistryToJson(schemaRegistry, 'output')\n\n for (const key in outputSchemas) {\n if (inputSchemas[key]) {\n throw new Error(\n `Collision detected for schema \"${key}\". The is already an input schema with the same name.`,\n )\n }\n }\n\n const jsonSchemas = {\n ...inputSchemas,\n ...outputSchemas,\n }\n\n const oasSchemas = Object.fromEntries(\n Object.entries(jsonSchemas).map(([key, value]) => [key, jsonSchemaToOAS(value, oasVersion)]),\n )\n\n return {\n ...input.openapiObject,\n components: {\n ...input.openapiObject.components,\n schemas: {\n ...input.openapiObject.components?.schemas,\n ...oasSchemas,\n },\n },\n } as ReturnType<SwaggerTransformObject>\n }\n\nexport const jsonSchemaTransformObject: SwaggerTransformObject = createJsonSchemaTransformObject({})\n\nexport const validatorCompiler: FastifySchemaCompiler<$ZodType> =\n ({ schema }) =>\n (data) => {\n const result = safeParse(schema, data)\n if (result.error) {\n return { error: createValidationError(result.error) as unknown as Error }\n }\n\n return { value: result.data }\n }\n\nfunction resolveSchema(maybeSchema: $ZodType | { properties: $ZodType }): $ZodType {\n if (maybeSchema instanceof $ZodType) {\n return maybeSchema\n }\n if ('properties' in maybeSchema && maybeSchema.properties instanceof $ZodType) {\n return maybeSchema.properties\n }\n throw new InvalidSchemaError(JSON.stringify(maybeSchema))\n}\n\ntype ReplacerFunction = (this: any, key: string, value: any) => any\n\nexport type ZodSerializerCompilerOptions = {\n replacer?: ReplacerFunction\n}\n\nexport const createSerializerCompiler =\n (\n options?: ZodSerializerCompilerOptions,\n ): FastifySerializerCompiler<$ZodType | { properties: $ZodType }> =>\n ({ schema: maybeSchema, method, url }) =>\n (data) => {\n const schema = resolveSchema(maybeSchema)\n\n const result = safeParse(schema, data)\n if (result.error) {\n throw new ResponseSerializationError(method, url, { cause: result.error })\n }\n\n return JSON.stringify(result.data, options?.replacer)\n }\n\nexport const serializerCompiler: ReturnType<typeof createSerializerCompiler> =\n createSerializerCompiler({})\n\n/**\n * FastifyPluginCallbackZod with Zod automatic type inference\n *\n * @example\n * ```typescript\n * import { FastifyPluginCallbackZod } from \"fastify-type-provider-zod\"\n *\n * const plugin: FastifyPluginCallbackZod = (fastify, options, done) => {\n * done()\n * }\n * ```\n */\nexport type FastifyPluginCallbackZod<\n Options extends FastifyPluginOptions = Record<never, never>,\n Server extends RawServerBase = RawServerDefault,\n> = FastifyPluginCallback<Options, Server, ZodTypeProvider>\n\n/**\n * FastifyPluginAsyncZod with Zod automatic type inference\n *\n * @example\n * ```typescript\n * import { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\"\n *\n * const plugin: FastifyPluginAsyncZod = async (fastify, options) => {\n * }\n * ```\n */\nexport type FastifyPluginAsyncZod<\n Options extends FastifyPluginOptions = Record<never, never>,\n Server extends RawServerBase = RawServerDefault,\n> = FastifyPluginAsync<Options, Server, ZodTypeProvider>\n"],"names":[],"mappings":";;;;AAoBA,MAAM,kBAAkB;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAgBO,MAAM,4BAA4B,CAAC;AAAA,EACxC,WAAW;AAAA,EACX,iBAAiB;AACnB,MAAkE;AAChE,SAAO,CAAC,UAAU;AAChB,QAAI,mBAAmB,OAAO;AAC5B,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AAEA,UAAM,EAAE,QAAQ,IAAA,IAAQ;AAExB,QAAI,CAAC,QAAQ;AACX,aAAO;AAAA,QACL;AAAA,QACA;AAAA,MAAA;AAAA,IAEJ;AAEA,UAAM,EAAE,UAAU,SAAS,aAAa,MAAM,QAAQ,MAAM,GAAG,KAAA,IAAS;AAExE,UAAM,cAA8B,CAAA;AAEpC,QAAI,SAAS,SAAS,GAAG,KAAK,MAAM;AAClC,kBAAY,OAAO;AACnB,aAAO,EAAE,QAAQ,aAAa,IAAA;AAAA,IAChC;AAEA,UAAM,aAA6B,EAAE,SAAS,aAAa,MAAM,OAAA;AAEjE,UAAM,aAAa,cAAc,KAAK;AAEtC,eAAW,QAAQ,YAAY;AAC7B,YAAM,YAAY,WAAW,IAAI;AACjC,UAAI,WAAW;AACb,cAAM,aAAa,gBAAgB,WAAW,gBAAgB,OAAO;AACrE,cAAM,YAAY,gBAAgB,YAAY,UAAU;AAExD,oBAAY,IAAI,IAAI;AAAA,MACtB;AAAA,IACF;AAEA,QAAI,UAAU;AACZ,kBAAY,WAAW,CAAA;AAEvB,iBAAW,QAAQ,UAAiB;AAClC,cAAM,YAAY,cAAe,SAAiB,IAAI,CAAC;AACvD,cAAM,aAAa,gBAAgB,WAAW,gBAAgB,QAAQ;AAGt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AE;AAE5F,MAAM,oBACX,CAAC,EAAE,OAAA,MACH,CAAC,SAAS;AACR,QAAM,SAAS,UAAU,QAAQ,IAAI;AACrC,MAAI,OAAO,OAAO;AAChB,WAAO,EAAE,OAAO,sBAAsB,OAAO,KAAK,EAAA;AAAA,EACpD;AAEA,SAAO,EAAE,OAAO,OAAO,KAAA;AACzB;AAEF,SAAS,cAAc,aAA4D;AACjF,MAAI,uBAAuB,UAAU;AACnC,WAAO;AAAA,EACT;AACA,MAAI,gBAAgB,eAAe,YAAY,sBAAsB,UAAU;AAC7E,WAAO,YAAY;AAAA,EACrB;AACA,QAAM,IAAI,mBAAmB,KAAK,UAAU,WAAW,CAAC;AAC1D;AAQO,MAAM,2BACX,CACE,YAEF,CAAC,EAAE,QAAQ,aAAa,QAAQ,UAChC,CAAC,SAAS;AACR,QAAM,SAAS,cAAc,WAAW;AAExC,QAAM,SAAS,UAAU,QAAQ,IAAI;AACrC,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI,2BAA2B,QAAQ,KAAK,EAAE,OAAO,OAAO,OAAO;AAAA,EAC3E;AAEA,SAAO,KAAK,UAAU,OAAO,MAAM,SAAS,QAAQ;AACtD;AAEK,MAAM,qBACX,yBAAyB,CAAA,CAAE;"}
1
+ {"version":3,"file":"core.js","sources":["../../src/core.ts"],"sourcesContent":["import type { SwaggerTransform, SwaggerTransformObject } from '@fastify/swagger'\nimport type {\n FastifyPluginAsync,\n FastifyPluginCallback,\n FastifyPluginOptions,\n FastifySchema,\n FastifySchemaCompiler,\n FastifySerializerCompiler,\n FastifyTypeProvider,\n RawServerBase,\n RawServerDefault,\n} from 'fastify'\nimport type { $ZodRegistry, output } from 'zod/v4/core'\nimport { $ZodType, globalRegistry, safeDecode, safeEncode } from 'zod/v4/core'\nimport { createValidationError, InvalidSchemaError, ResponseSerializationError } from './errors'\nimport { generateIORegistries, type SchemaRegistryMeta } from './registry'\nimport { assertIsOpenAPIObject, getJSONSchemaTarget } from './utils'\nimport { type ZodToJsonConfig, zodRegistryToJson, zodSchemaToJson } from './zod-to-json'\n\ntype FreeformRecord = Record<string, any>\n\nconst defaultSkipList = [\n '/documentation/',\n '/documentation/initOAuth',\n '/documentation/json',\n '/documentation/uiConfig',\n '/documentation/yaml',\n '/documentation/*',\n '/documentation/static/*',\n]\n\nexport interface ZodTypeProvider extends FastifyTypeProvider {\n validator: this['schema'] extends $ZodType ? output<this['schema']> : unknown\n serializer: this['schema'] extends $ZodType ? output<this['schema']> : unknown\n}\n\ninterface Schema extends FastifySchema {\n hide?: boolean\n}\n\ntype CreateJsonSchemaTransformOptions = {\n skipList?: readonly string[]\n schemaRegistry?: $ZodRegistry<SchemaRegistryMeta>\n zodToJsonConfig?: ZodToJsonConfig\n}\n\nexport const createJsonSchemaTransform = ({\n skipList = defaultSkipList,\n schemaRegistry = globalRegistry,\n zodToJsonConfig = {},\n}: CreateJsonSchemaTransformOptions): SwaggerTransform<Schema> => {\n return (document) => {\n assertIsOpenAPIObject(document)\n\n const { schema, url } = document\n\n if (!schema) {\n return {\n schema,\n url,\n }\n }\n\n const target = getJSONSchemaTarget(document.openapiObject.openapi)\n const config = {\n target,\n ...zodToJsonConfig,\n }\n\n const { inputRegistry, outputRegistry } = generateIORegistries(schemaRegistry)\n\n const { response, headers, querystring, body, params, hide, ...rest } = schema\n\n const transformed: FreeformRecord = {}\n\n if (skipList.includes(url) || hide) {\n transformed.hide = true\n return { schema: transformed, url }\n }\n\n const zodSchemas: FreeformRecord = { headers, querystring, body, params }\n\n for (const prop in zodSchemas) {\n const zodSchema = zodSchemas[prop]\n if (zodSchema) {\n transformed[prop] = zodSchemaToJson(zodSchema, inputRegistry, 'input', config)\n }\n }\n\n if (response) {\n transformed.response = {}\n\n for (const prop in response as any) {\n const zodSchema = resolveSchema((response as any)[prop])\n\n transformed.response[prop] = zodSchemaToJson(zodSchema, outputRegistry, 'output', config)\n }\n }\n\n for (const prop in rest) {\n const meta = rest[prop as keyof typeof rest]\n if (meta) {\n transformed[prop] = meta\n }\n }\n\n return { schema: transformed, url }\n }\n}\n\nexport const jsonSchemaTransform: SwaggerTransform<Schema> = createJsonSchemaTransform({})\n\ntype CreateJsonSchemaTransformObjectOptions = {\n schemaRegistry?: $ZodRegistry<SchemaRegistryMeta>\n zodToJsonConfig?: ZodToJsonConfig\n}\n\nexport const createJsonSchemaTransformObject =\n ({\n schemaRegistry = globalRegistry,\n zodToJsonConfig = {},\n }: CreateJsonSchemaTransformObjectOptions): SwaggerTransformObject =>\n (document) => {\n assertIsOpenAPIObject(document)\n\n const target = getJSONSchemaTarget(document.openapiObject.openapi)\n const config = {\n target,\n ...zodToJsonConfig,\n }\n\n const { inputRegistry, outputRegistry } = generateIORegistries(schemaRegistry)\n const inputSchemas = zodRegistryToJson(inputRegistry, 'input', config)\n const outputSchemas = zodRegistryToJson(outputRegistry, 'output', config)\n\n return {\n ...document.openapiObject,\n components: {\n ...document.openapiObject.components,\n schemas: {\n ...document.openapiObject.components?.schemas,\n ...inputSchemas,\n ...outputSchemas,\n },\n },\n } as ReturnType<SwaggerTransformObject>\n }\n\nexport const jsonSchemaTransformObject: SwaggerTransformObject = createJsonSchemaTransformObject({})\n\nexport const validatorCompiler: FastifySchemaCompiler<$ZodType> =\n ({ schema }) =>\n (data) => {\n const result = safeDecode(schema, data)\n if (result.error) {\n return { error: createValidationError(result.error) as unknown as Error }\n }\n\n return { value: result.data }\n }\n\nfunction resolveSchema(maybeSchema: $ZodType | { properties: $ZodType }): $ZodType {\n if (maybeSchema instanceof $ZodType) {\n return maybeSchema as $ZodType\n }\n if ('properties' in maybeSchema && maybeSchema.properties instanceof $ZodType) {\n return maybeSchema.properties as $ZodType\n }\n throw new InvalidSchemaError(JSON.stringify(maybeSchema))\n}\n\ntype ReplacerFunction = (this: any, key: string, value: any) => any\n\nexport type ZodSerializerCompilerOptions = {\n replacer?: ReplacerFunction\n}\n\nexport const createSerializerCompiler =\n (\n options?: ZodSerializerCompilerOptions,\n ): FastifySerializerCompiler<$ZodType | { properties: $ZodType }> =>\n ({ schema: maybeSchema, method, url }) =>\n (data) => {\n const schema = resolveSchema(maybeSchema)\n\n const result = safeEncode(schema, data)\n if (result.error) {\n throw new ResponseSerializationError(method, url, { cause: result.error })\n }\n\n return JSON.stringify(result.data, options?.replacer)\n }\n\nexport const serializerCompiler: ReturnType<typeof createSerializerCompiler> =\n createSerializerCompiler({})\n\n/**\n * FastifyPluginCallbackZod with Zod automatic type inference\n *\n * @example\n * ```typescript\n * import { FastifyPluginCallbackZod } from \"fastify-type-provider-zod\"\n *\n * const plugin: FastifyPluginCallbackZod = (fastify, options, done) => {\n * done()\n * }\n * ```\n */\nexport type FastifyPluginCallbackZod<\n Options extends FastifyPluginOptions = Record<never, never>,\n Server extends RawServerBase = RawServerDefault,\n> = FastifyPluginCallback<Options, Server, ZodTypeProvider>\n\n/**\n * FastifyPluginAsyncZod with Zod automatic type inference\n *\n * @example\n * ```typescript\n * import { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\"\n *\n * const plugin: FastifyPluginAsyncZod = async (fastify, options) => {\n * }\n * ```\n */\nexport type FastifyPluginAsyncZod<\n Options extends FastifyPluginOptions = Record<never, never>,\n Server extends RawServerBase = RawServerDefault,\n> = FastifyPluginAsync<Options, Server, ZodTypeProvider>\n"],"names":[],"mappings":";;;;;AAqBA,MAAM,kBAAkB;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAiBO,MAAM,4BAA4B,CAAC;AAAA,EACxC,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,kBAAkB,CAAA;AACpB,MAAkE;AAChE,SAAO,CAAC,aAAa;AACnB,0BAAsB,QAAQ;AAE9B,UAAM,EAAE,QAAQ,IAAA,IAAQ;AAExB,QAAI,CAAC,QAAQ;AACX,aAAO;AAAA,QACL;AAAA,QACA;AAAA,MAAA;AAAA,IAEJ;AAEA,UAAM,SAAS,oBAAoB,SAAS,cAAc,OAAO;AACjE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kB,eAAe,SAAS,MAAM;AACrE,QAAM,gBAAgB,kBAAkB,gBAAgB,UAAU,MAAM;AAExE,SAAO;AAAA,IACL,GAAG,SAAS;AAAA,IACZ,YAAY;AAAA,MACV,GAAG,SAAS,cAAc;AAAA,MAC1B,SAAS;AAAA,QACP,GAAG,SAAS,cAAc,YAAY;AAAA,QACtC,GAAG;AAAA,QACH,GAAG;AAAA,MAAA;AAAA,IACL;AAAA,EACF;AAEJ;AAEK,MAAM,4BAAoD,gCAAgC,CAAA,CAAE;AAE5F,MAAM,oBACX,CAAC,EAAE,OAAA,MACH,CAAC,SAAS;AACR,QAAM,SAAS,WAAW,QAAQ,IAAI;AACtC,MAAI,OAAO,OAAO;AAChB,WAAO,EAAE,OAAO,sBAAsB,OAAO,KAAK,EAAA;AAAA,EACpD;AAEA,SAAO,EAAE,OAAO,OAAO,KAAA;AACzB;AAEF,SAAS,cAAc,aAA4D;AACjF,MAAI,uBAAuB,UAAU;AACnC,WAAO;AAAA,EACT;AACA,MAAI,gBAAgB,eAAe,YAAY,sBAAsB,UAAU;AAC7E,WAAO,YAAY;AAAA,EACrB;AACA,QAAM,IAAI,mBAAmB,KAAK,UAAU,WAAW,CAAC;AAC1D;AAQO,MAAM,2BACX,CACE,YAEF,CAAC,EAAE,QAAQ,aAAa,QAAQ,UAChC,CAAC,SAAS;AACR,QAAM,SAAS,cAAc,WAAW;AAExC,QAAM,SAAS,WAAW,QAAQ,IAAI;AACtC,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI,2BAA2B,QAAQ,KAAK,EAAE,OAAO,OAAO,OAAO;AAAA,EAC3E;AAEA,SAAO,KAAK,UAAU,OAAO,MAAM,SAAS,QAAQ;AACtD;AAEK,MAAM,qBACX,yBAAyB,CAAA,CAAE;"}
@@ -1,6 +1,6 @@
1
1
  import createError from "@fastify/error";
2
2
  const InvalidSchemaError = createError("FST_ERR_INVALID_SCHEMA", "Invalid schema passed: %s", 500);
3
- const ZodFastifySchemaValidationErrorSymbol = Symbol.for("ZodFastifySchemaValidationError");
3
+ const ZodFastifySchemaValidationErrorSymbol = /* @__PURE__ */ Symbol.for("ZodFastifySchemaValidationError");
4
4
  const ResponseSerializationBase = createError(
5
5
  "FST_ERR_RESPONSE_SERIALIZATION",
6
6
  "Response doesn't match the schema",
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sources":["../../src/errors.ts"],"sourcesContent":["import createError, { type FastifyErrorConstructor } from '@fastify/error'\nimport type { FastifyError } from 'fastify'\nimport type { FastifySchemaValidationError } from 'fastify/types/schema'\nimport type { $ZodError } from 'zod/v4/core'\n\nexport const InvalidSchemaError: FastifyErrorConstructor<\n {\n code: string\n },\n [string]\n> = createError<[string]>('FST_ERR_INVALID_SCHEMA', 'Invalid schema passed: %s', 500)\n\nconst ZodFastifySchemaValidationErrorSymbol: symbol = Symbol.for('ZodFastifySchemaValidationError')\n\nexport type ZodFastifySchemaValidationError = FastifySchemaValidationError & {\n [ZodFastifySchemaValidationErrorSymbol]: true\n}\n\nconst ResponseSerializationBase: FastifyErrorConstructor<\n {\n code: string\n },\n [\n {\n cause: $ZodError\n },\n ]\n> = createError<[{ cause: $ZodError }]>(\n 'FST_ERR_RESPONSE_SERIALIZATION',\n \"Response doesn't match the schema\",\n 500,\n)\n\nexport class ResponseSerializationError extends ResponseSerializationBase {\n cause!: $ZodError\n\n constructor(\n public method: string,\n public url: string,\n options: { cause: $ZodError },\n ) {\n super({ cause: options.cause })\n\n this.cause = options.cause\n }\n}\n\nexport function isResponseSerializationError(value: unknown): value is ResponseSerializationError {\n return 'method' in (value as ResponseSerializationError)\n}\n\nfunction isZodFastifySchemaValidationError(\n error: unknown,\n): error is ZodFastifySchemaValidationError {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as ZodFastifySchemaValidationError)[ZodFastifySchemaValidationErrorSymbol] === true\n )\n}\n\nexport function hasZodFastifySchemaValidationErrors(\n error: unknown,\n): error is Omit<FastifyError, 'validation'> & { validation: ZodFastifySchemaValidationError[] } {\n return (\n typeof error === 'object' &&\n error !== null &&\n 'validation' in error &&\n Array.isArray(error.validation) &&\n error.validation.length > 0 &&\n isZodFastifySchemaValidationError(error.validation[0])\n )\n}\n\nfunction omit<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K> {\n const result = {} as Omit<T, K>\n for (const key of Object.keys(obj) as Array<keyof T>) {\n if (!keys.includes(key as K)) {\n // @ts-expect-error\n result[key] = obj[key]\n }\n }\n return result\n}\n\nexport function createValidationError(error: $ZodError): ZodFastifySchemaValidationError[] {\n return error.issues.map((issue) => {\n return {\n [ZodFastifySchemaValidationErrorSymbol]: true,\n keyword: issue.code,\n instancePath: `/${issue.path.join('/')}`,\n schemaPath: `#/${issue.path.join('/')}/${issue.code}`,\n message: issue.message,\n params: {\n ...omit(issue, ['path', 'code', 'message']),\n },\n }\n })\n}\n"],"names":[],"mappings":";AAKO,MAAM,qBAKT,YAAsB,0BAA0B,6BAA6B,GAAG;AAEpF,MAAM,wCAAgD,OAAO,IAAI,iCAAiC;AAMlG,MAAM,4BASF;AAAA,EACF;AAAA,EACA;AAAA,EACA;AACF;AAEO,MAAM,mCAAmC,0BAA0B;AAAA,EAGxE,YACS,QACA,KACP,SACA;AACA,UAAM,EAAE,OAAO,QAAQ,MAAA,CAAO;AAJvB,SAAA,SAAA;AACA,SAAA,MAAA;AAKP,SAAK,QAAQ,QAAQ;AAAA,EACvB;AAAA,EAVA;AAWF;AAEO,SAAS,6BAA6B,OAAqD;AAChG,SAAO,YAAa;AACtB;AAEA,SAAS,kCACP,OAC0C;AAC1C,SACE,OAAO,UAAU,YACjB,UAAU,QACT,MAA0C,qCAAqC,MAAM;AAE1F;AAEO,SAAS,oCACd,OAC+F;AAC/F,SACE,OAAO,UAAU,YACjB,UAAU,QACV,gBAAgB,SAChB,MAAM,QAAQ,MAAM,UAAU,KAC9B,MAAM,WAAW,SAAS,KAC1B,kCAAkC,MAAM,WAAW,CAAC,CAAC;AAEzD;AAEA,SAAS,KAA0C,KAAQ,MAAgC;AACzF,QAAM,SAAS,CAAA;AACf,aAAW,OAAO,OAAO,KAAK,GAAG,GAAqB;AACpD,QAAI,CAAC,KAAK,SAAS,GAAQ,GAAG;AAE5B,aAAO,GAAG,IAAI,IAAI,GAAG;AAAA,IACvB;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,sBAAsB,OAAqD;AACzF,SAAO,MAAM,OAAO,IAAI,CAAC,UAAU;AACjC,WAAO;AAAA,MACL,CAAC,qCAAqC,GAAG;AAAA,MACzC,SAAS,MAAM;AAAA,MACf,cAAc,IAAI,MAAM,KAAK,KAAK,GAAG,CAAC;AAAA,MACtC,YAAY,KAAK,MAAM,KAAK,KAAK,GAAG,CAAC,IAAI,MAAM,IAAI;AAAA,MACnD,SAAS,MAAM;AAAA,MACf,QAAQ;AAAA,QACN,GAAG,KAAK,OAAO,CAAC,QAAQ,QAAQ,SAAS,CAAC;AAAA,MAAA;AAAA,IAC5C;AAAA,EAEJ,CAAC;AACH;"}
1
+ {"version":3,"file":"errors.js","sources":["../../src/errors.ts"],"sourcesContent":["import createError, { type FastifyErrorConstructor } from '@fastify/error'\nimport type { FastifyError } from 'fastify'\nimport type { FastifySchemaValidationError } from 'fastify/types/schema'\nimport type { $ZodError } from 'zod/v4/core'\n\nexport const InvalidSchemaError: FastifyErrorConstructor<\n {\n code: string\n },\n [string]\n> = createError<[string]>('FST_ERR_INVALID_SCHEMA', 'Invalid schema passed: %s', 500)\n\nconst ZodFastifySchemaValidationErrorSymbol: symbol = Symbol.for('ZodFastifySchemaValidationError')\n\nexport type ZodFastifySchemaValidationError = FastifySchemaValidationError & {\n [ZodFastifySchemaValidationErrorSymbol]: true\n}\n\nconst ResponseSerializationBase: FastifyErrorConstructor<\n {\n code: string\n },\n [\n {\n cause: $ZodError\n },\n ]\n> = createError<[{ cause: $ZodError }]>(\n 'FST_ERR_RESPONSE_SERIALIZATION',\n \"Response doesn't match the schema\",\n 500,\n)\n\nexport class ResponseSerializationError extends ResponseSerializationBase {\n cause!: $ZodError\n\n constructor(\n public method: string,\n public url: string,\n options: { cause: $ZodError },\n ) {\n super({ cause: options.cause })\n\n this.cause = options.cause\n }\n}\n\nexport function isResponseSerializationError(value: unknown): value is ResponseSerializationError {\n return 'method' in (value as ResponseSerializationError)\n}\n\nfunction isZodFastifySchemaValidationError(\n error: unknown,\n): error is ZodFastifySchemaValidationError {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as ZodFastifySchemaValidationError)[ZodFastifySchemaValidationErrorSymbol] === true\n )\n}\n\nexport function hasZodFastifySchemaValidationErrors(\n error: unknown,\n): error is Omit<FastifyError, 'validation'> & { validation: ZodFastifySchemaValidationError[] } {\n return (\n typeof error === 'object' &&\n error !== null &&\n 'validation' in error &&\n Array.isArray(error.validation) &&\n error.validation.length > 0 &&\n isZodFastifySchemaValidationError(error.validation[0])\n )\n}\n\nfunction omit<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K> {\n const result = {} as Omit<T, K>\n for (const key of Object.keys(obj) as Array<keyof T>) {\n if (!keys.includes(key as K)) {\n // @ts-expect-error\n result[key] = obj[key]\n }\n }\n return result\n}\n\nexport function createValidationError(error: $ZodError): ZodFastifySchemaValidationError[] {\n return error.issues.map((issue) => {\n return {\n [ZodFastifySchemaValidationErrorSymbol]: true,\n keyword: issue.code,\n instancePath: `/${issue.path.join('/')}`,\n schemaPath: `#/${issue.path.join('/')}/${issue.code}`,\n message: issue.message,\n params: {\n ...omit(issue, ['path', 'code', 'message']),\n },\n }\n })\n}\n"],"names":[],"mappings":";AAKO,MAAM,qBAKT,YAAsB,0BAA0B,6BAA6B,GAAG;AAEpF,MAAM,wCAAgD,uBAAO,IAAI,iCAAiC;AAMlG,MAAM,4BASF;AAAA,EACF;AAAA,EACA;AAAA,EACA;AACF;AAEO,MAAM,mCAAmC,0BAA0B;AAAA,EAGxE,YACS,QACA,KACP,SACA;AACA,UAAM,EAAE,OAAO,QAAQ,MAAA,CAAO;AAJvB,SAAA,SAAA;AACA,SAAA,MAAA;AAKP,SAAK,QAAQ,QAAQ;AAAA,EACvB;AAAA,EAVA;AAWF;AAEO,SAAS,6BAA6B,OAAqD;AAChG,SAAO,YAAa;AACtB;AAEA,SAAS,kCACP,OAC0C;AAC1C,SACE,OAAO,UAAU,YACjB,UAAU,QACT,MAA0C,qCAAqC,MAAM;AAE1F;AAEO,SAAS,oCACd,OAC+F;AAC/F,SACE,OAAO,UAAU,YACjB,UAAU,QACV,gBAAgB,SAChB,MAAM,QAAQ,MAAM,UAAU,KAC9B,MAAM,WAAW,SAAS,KAC1B,kCAAkC,MAAM,WAAW,CAAC,CAAC;AAEzD;AAEA,SAAS,KAA0C,KAAQ,MAAgC;AACzF,QAAM,SAAS,CAAA;AACf,aAAW,OAAO,OAAO,KAAK,GAAG,GAAqB;AACpD,QAAI,CAAC,KAAK,SAAS,GAAQ,GAAG;AAE5B,aAAO,GAAG,IAAI,IAAI,GAAG;AAAA,IACvB;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,sBAAsB,OAAqD;AACzF,SAAO,MAAM,OAAO,IAAI,CAAC,UAAU;AACjC,WAAO;AAAA,MACL,CAAC,qCAAqC,GAAG;AAAA,MACzC,SAAS,MAAM;AAAA,MACf,cAAc,IAAI,MAAM,KAAK,KAAK,GAAG,CAAC;AAAA,MACtC,YAAY,KAAK,MAAM,KAAK,KAAK,GAAG,CAAC,IAAI,MAAM,IAAI;AAAA,MACnD,SAAS,MAAM;AAAA,MACf,QAAQ;AAAA,QACN,GAAG,KAAK,OAAO,CAAC,QAAQ,QAAQ,SAAS,CAAC;AAAA,MAAA;AAAA,IAC5C;AAAA,EAEJ,CAAC;AACH;"}
@@ -0,0 +1,9 @@
1
+ import { $ZodRegistry } from "zod/v4/core";
2
+ export type SchemaRegistryMeta = {
3
+ id?: string | undefined;
4
+ [key: string]: unknown;
5
+ };
6
+ export declare const generateIORegistries: (baseRegistry: $ZodRegistry<SchemaRegistryMeta>) => {
7
+ inputRegistry: $ZodRegistry<SchemaRegistryMeta>;
8
+ outputRegistry: $ZodRegistry<SchemaRegistryMeta>;
9
+ };
@@ -0,0 +1,43 @@
1
+ import { $ZodRegistry } from "zod/v4/core";
2
+ const getSchemaId = (id, io) => {
3
+ return io === "input" ? `${id}Input` : id;
4
+ };
5
+ class WeakMapWithFallback extends WeakMap {
6
+ constructor(fallback) {
7
+ super();
8
+ this.fallback = fallback;
9
+ }
10
+ get(key) {
11
+ return super.get(key) ?? this.fallback.get(key);
12
+ }
13
+ has(key) {
14
+ return super.has(key) || this.fallback.has(key);
15
+ }
16
+ }
17
+ const copyRegistry = (inputRegistry, idReplaceFn) => {
18
+ const outputRegistry = new $ZodRegistry();
19
+ outputRegistry._map = new WeakMapWithFallback(inputRegistry._map);
20
+ inputRegistry._idmap.forEach((schema, id) => {
21
+ outputRegistry.add(schema, {
22
+ ...inputRegistry._map.get(schema),
23
+ id: idReplaceFn(id)
24
+ });
25
+ });
26
+ return outputRegistry;
27
+ };
28
+ const generateIORegistries = (baseRegistry) => {
29
+ const inputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, "input"));
30
+ const outputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, "output"));
31
+ inputRegistry._idmap.forEach((_, id) => {
32
+ if (outputRegistry._idmap.has(id)) {
33
+ throw new Error(
34
+ `Collision detected for schema "${id}". There is already an input schema with the same name.`
35
+ );
36
+ }
37
+ });
38
+ return { inputRegistry, outputRegistry };
39
+ };
40
+ export {
41
+ generateIORegistries
42
+ };
43
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sources":["../../src/registry.ts"],"sourcesContent":["import { $ZodRegistry, type $ZodType } from 'zod/v4/core'\n\nexport type SchemaRegistryMeta = {\n id?: string | undefined\n [key: string]: unknown\n}\n\nconst getSchemaId = (id: string, io: 'input' | 'output'): string => {\n return io === 'input' ? `${id}Input` : id\n}\n\n// A WeakMap that falls back to another WeakMap when a key is not found, this is to ensure nested metadata is properly resolved\nclass WeakMapWithFallback extends WeakMap<$ZodType, SchemaRegistryMeta> {\n constructor(private fallback: WeakMap<$ZodType, SchemaRegistryMeta>) {\n super()\n }\n\n get(key: $ZodType): SchemaRegistryMeta | undefined {\n return super.get(key) ?? this.fallback.get(key)\n }\n\n has(key: $ZodType): boolean {\n return super.has(key) || this.fallback.has(key)\n }\n}\n\nconst copyRegistry = (\n inputRegistry: $ZodRegistry<SchemaRegistryMeta>,\n idReplaceFn: (id: string) => string,\n): $ZodRegistry<SchemaRegistryMeta> => {\n const outputRegistry = new $ZodRegistry<SchemaRegistryMeta>()\n\n outputRegistry._map = new WeakMapWithFallback(inputRegistry._map)\n\n inputRegistry._idmap.forEach((schema, id) => {\n outputRegistry.add(schema, {\n ...inputRegistry._map.get(schema),\n id: idReplaceFn(id),\n })\n })\n\n return outputRegistry\n}\n\nexport const generateIORegistries = (\n baseRegistry: $ZodRegistry<SchemaRegistryMeta>,\n): {\n inputRegistry: $ZodRegistry<SchemaRegistryMeta>\n outputRegistry: $ZodRegistry<SchemaRegistryMeta>\n} => {\n const inputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, 'input'))\n const outputRegistry = copyRegistry(baseRegistry, (id) => getSchemaId(id, 'output'))\n\n // Detect colliding schemas\n inputRegistry._idmap.forEach((_, id) => {\n if (outputRegistry._idmap.has(id)) {\n throw new Error(\n `Collision detected for schema \"${id}\". There is already an input schema with the same name.`,\n )\n }\n })\n\n return { inputRegistry, outputRegistry }\n}\n"],"names":[],"mappings":";AAOA,MAAM,cAAc,CAAC,IAAY,OAAmC;AAClE,SAAO,OAAO,UAAU,GAAG,EAAE,UAAU;AACzC;AAGA,MAAM,4BAA4B,QAAsC;AAAA,EACtE,YAAoB,UAAiD;AACnE,UAAA;AADkB,SAAA,WAAA;AAAA,EAEpB;AAAA,EAEA,IAAI,KAA+C;AACjD,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,SAAS,IAAI,GAAG;AAAA,EAChD;AAAA,EAEA,IAAI,KAAwB;AAC1B,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,SAAS,IAAI,GAAG;AAAA,EAChD;AACF;AAEA,MAAM,eAAe,CACnB,eACA,gBACqC;AACrC,QAAM,iBAAiB,IAAI,aAAA;AAE3B,iBAAe,OAAO,IAAI,oBAAoB,cAAc,IAAI;AAEhE,gBAAc,OAAO,QAAQ,CAAC,QAAQ,OAAO;AAC3C,mBAAe,IAAI,QAAQ;AAAA,MACzB,GAAG,cAAc,KAAK,IAAI,MAAM;AAAA,MAChC,IAAI,YAAY,EAAE;AAAA,IAAA,CACnB;AAAA,EACH,CAAC;AAED,SAAO;AACT;AAEO,MAAM,uBAAuB,CAClC,iBAIG;AACH,QAAM,gBAAgB,aAAa,cAAc,CAAC,OAAO,YAAY,IAAI,OAAO,CAAC;AACjF,QAAM,iBAAiB,aAAa,cAAc,CAAC,OAAO,YAAY,IAAI,QAAQ,CAAC;AAGnF,gBAAc,OAAO,QAAQ,CAAC,GAAG,OAAO;AACtC,QAAI,eAAe,OAAO,IAAI,EAAE,GAAG;AACjC,YAAM,IAAI;AAAA,QACR,kCAAkC,EAAE;AAAA,MAAA;AAAA,IAExC;AAAA,EACF,CAAC;AAED,SAAO,EAAE,eAAe,eAAA;AAC1B;"}
@@ -0,0 +1,12 @@
1
+ import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from "openapi-types";
2
+ type SwaggerObject = {
3
+ swaggerObject: Partial<OpenAPIV2.Document>;
4
+ };
5
+ type OpenAPIObject = {
6
+ openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;
7
+ };
8
+ export declare const assertIsOpenAPIObject: (obj: SwaggerObject | OpenAPIObject) => asserts obj is OpenAPIObject;
9
+ export type JSONSchemaTarget = "draft-2020-12" | "openapi-3.0";
10
+ export declare const getReferenceUri: (input: string) => string;
11
+ export declare const getJSONSchemaTarget: (version?: string) => JSONSchemaTarget;
12
+ export {};
@@ -0,0 +1,21 @@
1
+ const assertIsOpenAPIObject = (obj) => {
2
+ if ("swaggerObject" in obj) {
3
+ throw new Error("This package currently does not support component references for Swagger 2.0");
4
+ }
5
+ };
6
+ const getReferenceUri = (input) => {
7
+ const id = input.replace(/^#\/(?:\$defs|definitions|components\/schemas)\//, "");
8
+ return `#/components/schemas/${id}`;
9
+ };
10
+ const getJSONSchemaTarget = (version = "3.0.0") => {
11
+ if (version.startsWith("3.0")) {
12
+ return "openapi-3.0";
13
+ }
14
+ return "draft-2020-12";
15
+ };
16
+ export {
17
+ assertIsOpenAPIObject,
18
+ getJSONSchemaTarget,
19
+ getReferenceUri
20
+ };
21
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\ntype SwaggerObject = {\n swaggerObject: Partial<OpenAPIV2.Document>\n}\n\ntype OpenAPIObject = {\n openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>\n}\n\nexport const assertIsOpenAPIObject: (\n obj: SwaggerObject | OpenAPIObject,\n) => asserts obj is OpenAPIObject = (obj) => {\n if ('swaggerObject' in obj) {\n throw new Error('This package currently does not support component references for Swagger 2.0')\n }\n}\n\nexport type JSONSchemaTarget = 'draft-2020-12' | 'openapi-3.0'\n\nexport const getReferenceUri = (input: string): string => {\n const id = input.replace(/^#\\/(?:\\$defs|definitions|components\\/schemas)\\//, '')\n\n return `#/components/schemas/${id}`\n}\n\nexport const getJSONSchemaTarget = (version = '3.0.0'): JSONSchemaTarget => {\n if (version.startsWith('3.0')) {\n return 'openapi-3.0'\n }\n\n return 'draft-2020-12'\n}\n"],"names":[],"mappings":"AAUO,MAAM,wBAEuB,CAAC,QAAQ;AAC3C,MAAI,mBAAmB,KAAK;AAC1B,UAAM,IAAI,MAAM,8EAA8E;AAAA,EAChG;AACF;AAIO,MAAM,kBAAkB,CAAC,UAA0B;AACxD,QAAM,KAAK,MAAM,QAAQ,oDAAoD,EAAE;AAE/E,SAAO,wBAAwB,EAAE;AACnC;AAEO,MAAM,sBAAsB,CAAC,UAAU,YAA8B;AAC1E,MAAI,QAAQ,WAAW,KAAK,GAAG;AAC7B,WAAO;AAAA,EACT;AAEA,SAAO;AACT;"}
@@ -1,8 +1,8 @@
1
- import type { JSONSchema } from "zod/v4/core";
1
+ import type { JSONSchema, RegistryToJSONSchemaParams } from "zod/v4/core";
2
2
  import { $ZodRegistry, $ZodType } from "zod/v4/core";
3
- export declare const zodSchemaToJson: (zodSchema: $ZodType, registry: $ZodRegistry<{
4
- id?: string;
5
- }>, io: "input" | "output") => JSONSchema.BaseSchema;
6
- export declare const zodRegistryToJson: (registry: $ZodRegistry<{
7
- id?: string;
8
- }>, io: "input" | "output") => Record<string, JSONSchema.BaseSchema>;
3
+ import type { SchemaRegistryMeta } from "../esm/registry.js";
4
+ export type ZodToJsonConfig = {} & Omit<RegistryToJSONSchemaParams, "io" | "metadata" | "cycles" | "reused" | "uri">;
5
+ declare const deleteInvalidProperties: (schema: JSONSchema.BaseSchema) => Omit<JSONSchema.BaseSchema, "id" | "$schema">;
6
+ export declare const zodSchemaToJson: (zodSchema: $ZodType, registry: $ZodRegistry<SchemaRegistryMeta>, io: "input" | "output", config: ZodToJsonConfig) => ReturnType<typeof deleteInvalidProperties>;
7
+ export declare const zodRegistryToJson: (registry: $ZodRegistry<SchemaRegistryMeta>, io: "input" | "output", config: ZodToJsonConfig) => Record<string, JSONSchema.BaseSchema>;
8
+ export {};
@@ -1,10 +1,7 @@
1
1
  import { $ZodRegistry, toJSONSchema, $ZodType } from "zod/v4/core";
2
- const getSchemaId = (id, io) => {
3
- return io === "input" ? `${id}Input` : id;
4
- };
5
- const getReferenceUri = (id, io) => {
6
- return `#/components/schemas/${getSchemaId(id, io)}`;
7
- };
2
+ import { getReferenceUri } from "./utils.js";
3
+ const SCHEMA_REGISTRY_ID_PLACEHOLDER = "__SCHEMA__ID__PLACEHOLDER__";
4
+ const SCHEMA_URI_PLACEHOLDER = "__SCHEMA__PLACEHOLDER__";
8
5
  function isZodDate(entity) {
9
6
  return entity instanceof $ZodType && entity._zod.def.type === "date";
10
7
  }
@@ -30,64 +27,62 @@ const getOverride = (ctx, io) => {
30
27
  }
31
28
  }
32
29
  };
33
- const zodSchemaToJson = (zodSchema, registry, io) => {
30
+ const deleteInvalidProperties = (schema) => {
31
+ const object = { ...schema };
32
+ delete object.id;
33
+ delete object.$schema;
34
+ delete object.$id;
35
+ return object;
36
+ };
37
+ const zodSchemaToJson = (zodSchema, registry, io, config) => {
34
38
  const schemaRegistryEntry = registry.get(zodSchema);
35
39
  if (schemaRegistryEntry?.id) {
36
- return {
37
- $ref: getReferenceUri(schemaRegistryEntry.id, io)
38
- };
40
+ return { $ref: getReferenceUri(schemaRegistryEntry.id) };
39
41
  }
40
- const tempID = "GEN";
41
42
  const tempRegistry = new $ZodRegistry();
42
- tempRegistry.add(zodSchema, { id: tempID });
43
+ tempRegistry.add(zodSchema, { id: SCHEMA_REGISTRY_ID_PLACEHOLDER });
43
44
  const {
44
- schemas: { [tempID]: result }
45
+ schemas: { [SCHEMA_REGISTRY_ID_PLACEHOLDER]: result }
45
46
  } = toJSONSchema(tempRegistry, {
46
- target: "draft-2020-12",
47
- metadata: registry,
47
+ ...config,
48
48
  io,
49
- unrepresentable: "any",
49
+ target: config.target,
50
+ metadata: registry,
51
+ unrepresentable: config.unrepresentable ?? "any",
50
52
  cycles: "ref",
51
53
  reused: "inline",
52
54
  /**
53
55
  * The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.
54
- * As a workaround, we set a placeholder that looks something like this:
55
- *
56
- * | marker | always added by zod | meta.id |
57
- * |__SCHEMA__PLACEHOLDER__| #/$defs/ | User |
58
- *
59
- * @example `__SCHEMA__PLACEHOLDER__#/$defs/User"`
60
- * @example `__SCHEMA__PLACEHOLDER__#/$defs/Group"`
61
- *
56
+ * As a workaround, we set a placeholder that looks something like this.
62
57
  * @see jsonSchemaReplaceRef
63
58
  * @see https://github.com/colinhacks/zod/issues/4750
64
59
  */
65
- uri: () => `__SCHEMA__PLACEHOLDER__`,
66
- override: (ctx) => getOverride(ctx, io)
60
+ uri: () => SCHEMA_URI_PLACEHOLDER,
61
+ override: config.override ?? ((ctx) => getOverride(ctx, io))
62
+ });
63
+ const jsonSchema = deleteInvalidProperties(result);
64
+ return JSON.parse(JSON.stringify(jsonSchema), (__key, value) => {
65
+ if (typeof value === "string" && value.startsWith(SCHEMA_URI_PLACEHOLDER)) {
66
+ return getReferenceUri(value.slice(SCHEMA_URI_PLACEHOLDER.length));
67
+ }
68
+ return value;
67
69
  });
68
- const jsonSchema = { ...result };
69
- delete jsonSchema.id;
70
- const jsonSchemaReplaceRef = JSON.stringify(jsonSchema).replaceAll(
71
- /"__SCHEMA__PLACEHOLDER__#\/\$defs\/(.+?)"/g,
72
- (_, id) => `"${getReferenceUri(id, io)}"`
73
- );
74
- return JSON.parse(jsonSchemaReplaceRef);
75
70
  };
76
- const zodRegistryToJson = (registry, io) => {
71
+ const zodRegistryToJson = (registry, io, config) => {
77
72
  const result = toJSONSchema(registry, {
78
- target: "draft-2020-12",
73
+ ...config,
79
74
  io,
80
- unrepresentable: "any",
75
+ target: config.target,
76
+ metadata: registry,
77
+ unrepresentable: config.unrepresentable ?? "any",
81
78
  cycles: "ref",
82
79
  reused: "inline",
83
- uri: (id) => getReferenceUri(id, io),
84
- override: (ctx) => getOverride(ctx, io)
80
+ uri: (id) => getReferenceUri(id),
81
+ override: config.override ?? ((ctx) => getOverride(ctx, io))
85
82
  }).schemas;
86
83
  const jsonSchemas = {};
87
84
  for (const id in result) {
88
- const jsonSchema = { ...result[id] };
89
- delete jsonSchema.id;
90
- jsonSchemas[getSchemaId(id, io)] = jsonSchema;
85
+ jsonSchemas[id] = deleteInvalidProperties(result[id]);
91
86
  }
92
87
  return jsonSchemas;
93
88
  };
@@ -1 +1 @@
1
- {"version":3,"file":"zod-to-json.js","sources":["../../src/zod-to-json.ts"],"sourcesContent":["import type { $ZodDate, $ZodUndefined, $ZodUnion, JSONSchema } from 'zod/v4/core'\nimport { $ZodRegistry, $ZodType, toJSONSchema } from 'zod/v4/core'\n\nconst getSchemaId = (id: string, io: 'input' | 'output') => {\n return io === 'input' ? `${id}Input` : id\n}\n\nconst getReferenceUri = (id: string, io: 'input' | 'output') => {\n return `#/components/schemas/${getSchemaId(id, io)}`\n}\n\nfunction isZodDate(entity: unknown): entity is $ZodDate {\n return entity instanceof $ZodType && entity._zod.def.type === 'date'\n}\n\nfunction isZodUnion(entity: unknown): entity is $ZodUnion {\n return entity instanceof $ZodType && entity._zod.def.type === 'union'\n}\n\nfunction isZodUndefined(entity: unknown): entity is $ZodUndefined {\n return entity instanceof $ZodType && entity._zod.def.type === 'undefined'\n}\n\nconst getOverride = (\n ctx: {\n zodSchema: $ZodType\n jsonSchema: JSONSchema.BaseSchema\n },\n io: 'input' | 'output',\n) => {\n if (isZodUnion(ctx.zodSchema)) {\n // Filter unrepresentable types in unions\n // TODO: Should be fixed upstream and not merged in this plugin.\n // Remove when passed: https://github.com/colinhacks/zod/pull/5013\n ctx.jsonSchema.anyOf = ctx.jsonSchema.anyOf?.filter((schema) => Object.keys(schema).length > 0)\n }\n\n if (isZodDate(ctx.zodSchema)) {\n // Allow dates to be represented as strings in output schemas\n if (io === 'output') {\n ctx.jsonSchema.type = 'string'\n ctx.jsonSchema.format = 'date-time'\n }\n }\n\n if (isZodUndefined(ctx.zodSchema)) {\n // Allow undefined to be represented as null in output schemas\n if (io === 'output') {\n ctx.jsonSchema.type = 'null'\n }\n }\n}\n\nexport const zodSchemaToJson: (\n zodSchema: $ZodType,\n registry: $ZodRegistry<{ id?: string }>,\n io: 'input' | 'output',\n) => JSONSchema.BaseSchema = (zodSchema, registry, io) => {\n const schemaRegistryEntry = registry.get(zodSchema)\n\n /**\n * Checks whether the provided schema is registered in the given registry.\n * If it is present and has an `id`, it can be referenced as component.\n *\n * @see https://github.com/turkerdev/fastify-type-provider-zod/issues/173\n */\n if (schemaRegistryEntry?.id) {\n return {\n $ref: getReferenceUri(schemaRegistryEntry.id, io),\n }\n }\n\n /**\n * Unfortunately, at the time of writing, there is no way to generate a schema with `$ref`\n * using `toJSONSchema` and a zod schema.\n *\n * As a workaround, we create a zod registry containing only the specific schema we want to convert.\n *\n * @see https://github.com/colinhacks/zod/issues/4281\n */\n const tempID = 'GEN'\n const tempRegistry = new $ZodRegistry<{ id?: string }>()\n tempRegistry.add(zodSchema, { id: tempID })\n\n const {\n schemas: { [tempID]: result },\n } = toJSONSchema(tempRegistry, {\n target: 'draft-2020-12',\n metadata: registry,\n io,\n unrepresentable: 'any',\n cycles: 'ref',\n reused: 'inline',\n\n /**\n * The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.\n * As a workaround, we set a placeholder that looks something like this:\n *\n * | marker | always added by zod | meta.id |\n * |__SCHEMA__PLACEHOLDER__| #/$defs/ | User |\n *\n * @example `__SCHEMA__PLACEHOLDER__#/$defs/User\"`\n * @example `__SCHEMA__PLACEHOLDER__#/$defs/Group\"`\n *\n * @see jsonSchemaReplaceRef\n * @see https://github.com/colinhacks/zod/issues/4750\n */\n uri: () => `__SCHEMA__PLACEHOLDER__`,\n override: (ctx) => getOverride(ctx, io),\n })\n\n const jsonSchema = { ...result }\n delete jsonSchema.id\n\n /**\n * Replace the previous generated placeholders with the final `$ref` value\n */\n const jsonSchemaReplaceRef = JSON.stringify(jsonSchema).replaceAll(\n /\"__SCHEMA__PLACEHOLDER__#\\/\\$defs\\/(.+?)\"/g,\n (_, id) => `\"${getReferenceUri(id, io)}\"`,\n )\n\n return JSON.parse(jsonSchemaReplaceRef) as typeof result\n}\n\nexport const zodRegistryToJson: (\n registry: $ZodRegistry<{ id?: string }>,\n io: 'input' | 'output',\n) => Record<string, JSONSchema.BaseSchema> = (registry, io) => {\n const result = toJSONSchema(registry, {\n target: 'draft-2020-12',\n io,\n unrepresentable: 'any',\n cycles: 'ref',\n reused: 'inline',\n uri: (id) => getReferenceUri(id, io),\n override: (ctx) => getOverride(ctx, io),\n }).schemas\n\n const jsonSchemas: Record<string, JSONSchema.BaseSchema> = {}\n for (const id in result) {\n const jsonSchema = { ...result[id] }\n\n delete jsonSchema.id\n\n jsonSchemas[getSchemaId(id, io)] = jsonSchema\n }\n\n return jsonSchemas\n}\n"],"names":[],"mappings":";AAGA,MAAM,cAAc,CAAC,IAAY,OAA2B;AAC1D,SAAO,OAAO,UAAU,GAAG,EAAE,UAAU;AACzC;AAEA,MAAM,kBAAkB,CAAC,IAAY,OAA2B;AAC9D,SAAO,wBAAwB,YAAY,IAAI,EAAE,CAAC;AACpD;AAEA,SAAS,UAAU,QAAqC;AACtD,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,WAAW,QAAsC;AACxD,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,eAAe,QAA0C;AAChE,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,MAAM,cAAc,CAClB,KAIA,OACG;AACH,MAAI,WAAW,IAAI,SAAS,GAAG;AAI7B,QAAI,WAAW,QAAQ,IAAI,WAAW,OAAO,OAAO,CAAC,WAAW,OAAO,KAAK,MAAM,EAAE,SAAS,CAAC;AAAA,EAChG;AAEA,MAAI,UAAU,IAAI,SAAS,GAAG;AAE5B,QAAI,OAAO,UAAU;AACnB,UAAI,WAAW,OAAO;AACtB,UAAI,WAAW,SAAS;AAAA,IAC1B;AAAA,EACF;AAEA,MAAI,eAAe,IAAI,SAAS,GAAG;AAEjC,QAAI,OAAO,UAAU;AACnB,UAAI,WAAW,OAAO;AAAA,IACxB;AAAA,EACF;AACF;AAEO,MAAM,kBAIgB,CAAC,WAAW,UAAU,OAAO;AACxD,QAAM,sBAAsB,SAAS,IAAI,SAAS;AAQlD,MAAI,qBAAqB,IAAI;AAC3B,WAAO;AAAA,MACL,MAA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-json.js","sources":["../../src/zod-to-json.ts"],"sourcesContent":["import type {\n $ZodDate,\n $ZodUndefined,\n $ZodUnion,\n JSONSchema,\n RegistryToJSONSchemaParams,\n} from 'zod/v4/core'\nimport { $ZodRegistry, $ZodType, toJSONSchema } from 'zod/v4/core'\nimport type { SchemaRegistryMeta } from './registry'\nimport { getReferenceUri } from './utils'\n\nconst SCHEMA_REGISTRY_ID_PLACEHOLDER = '__SCHEMA__ID__PLACEHOLDER__'\nconst SCHEMA_URI_PLACEHOLDER = '__SCHEMA__PLACEHOLDER__'\n\nfunction isZodDate(entity: unknown): entity is $ZodDate {\n return entity instanceof $ZodType && entity._zod.def.type === 'date'\n}\n\nfunction isZodUnion(entity: unknown): entity is $ZodUnion {\n return entity instanceof $ZodType && entity._zod.def.type === 'union'\n}\n\nfunction isZodUndefined(entity: unknown): entity is $ZodUndefined {\n return entity instanceof $ZodType && entity._zod.def.type === 'undefined'\n}\n\nconst getOverride = (\n ctx: {\n zodSchema: $ZodType\n jsonSchema: JSONSchema.BaseSchema\n },\n io: 'input' | 'output',\n) => {\n if (isZodUnion(ctx.zodSchema)) {\n // Filter unrepresentable types in unions\n // TODO: Should be fixed upstream and not merged in this plugin.\n // Remove when passed: https://github.com/colinhacks/zod/pull/5013\n ctx.jsonSchema.anyOf = ctx.jsonSchema.anyOf?.filter((schema) => Object.keys(schema).length > 0)\n }\n\n if (isZodDate(ctx.zodSchema)) {\n // Allow dates to be represented as strings in output schemas\n if (io === 'output') {\n ctx.jsonSchema.type = 'string'\n ctx.jsonSchema.format = 'date-time'\n }\n }\n\n if (isZodUndefined(ctx.zodSchema)) {\n // Allow undefined to be represented as null in output schemas\n if (io === 'output') {\n ctx.jsonSchema.type = 'null'\n }\n }\n}\n\nexport type ZodToJsonConfig = {} & Omit<\n RegistryToJSONSchemaParams,\n 'io' | 'metadata' | 'cycles' | 'reused' | 'uri'\n>\n\nconst deleteInvalidProperties: (\n schema: JSONSchema.BaseSchema,\n) => Omit<JSONSchema.BaseSchema, 'id' | '$schema'> = (schema) => {\n const object = { ...schema }\n\n delete object.id\n delete object.$schema\n\n // ToDo added in newer zod\n delete object.$id\n\n return object\n}\n\nexport const zodSchemaToJson: (\n zodSchema: $ZodType,\n registry: $ZodRegistry<SchemaRegistryMeta>,\n io: 'input' | 'output',\n config: ZodToJsonConfig,\n) => ReturnType<typeof deleteInvalidProperties> = (zodSchema, registry, io, config) => {\n /**\n * Checks whether the provided schema is registered in the given registry.\n * If it is present and has an `id`, it can be referenced as component.\n *\n * @see https://github.com/turkerdev/fastify-type-provider-zod/issues/173\n */\n const schemaRegistryEntry = registry.get(zodSchema)\n if (schemaRegistryEntry?.id) {\n return { $ref: getReferenceUri(schemaRegistryEntry.id) }\n }\n\n /**\n * Unfortunately, at the time of writing, there is no way to generate a schema with `$ref`\n * using `toJSONSchema` and a zod schema.\n *\n * As a workaround, we create a zod registry containing only the specific schema we want to convert.\n *\n * @see https://github.com/colinhacks/zod/issues/4281\n */\n const tempRegistry = new $ZodRegistry<SchemaRegistryMeta>()\n tempRegistry.add(zodSchema, { id: SCHEMA_REGISTRY_ID_PLACEHOLDER })\n\n const {\n schemas: { [SCHEMA_REGISTRY_ID_PLACEHOLDER]: result },\n } = toJSONSchema(tempRegistry, {\n ...config,\n io,\n target: config.target,\n metadata: registry,\n unrepresentable: config.unrepresentable ?? 'any',\n cycles: 'ref',\n reused: 'inline',\n /**\n * The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.\n * As a workaround, we set a placeholder that looks something like this.\n * @see jsonSchemaReplaceRef\n * @see https://github.com/colinhacks/zod/issues/4750\n */\n uri: () => SCHEMA_URI_PLACEHOLDER,\n override: config.override ?? ((ctx) => getOverride(ctx, io)),\n })\n\n const jsonSchema = deleteInvalidProperties(result)\n\n /**\n * Replace the previous generated placeholders with the final `$ref` value\n */\n return JSON.parse(JSON.stringify(jsonSchema), (__key, value) => {\n if (typeof value === 'string' && value.startsWith(SCHEMA_URI_PLACEHOLDER)) {\n return getReferenceUri(value.slice(SCHEMA_URI_PLACEHOLDER.length))\n }\n return value\n }) as typeof result\n}\n\nexport const zodRegistryToJson: (\n registry: $ZodRegistry<SchemaRegistryMeta>,\n io: 'input' | 'output',\n config: ZodToJsonConfig,\n) => Record<string, JSONSchema.BaseSchema> = (registry, io, config) => {\n const result = toJSONSchema(registry, {\n ...config,\n io,\n target: config.target,\n metadata: registry,\n unrepresentable: config.unrepresentable ?? 'any',\n cycles: 'ref',\n reused: 'inline',\n uri: (id) => getReferenceUri(id),\n override: config.override ?? ((ctx) => getOverride(ctx, io)),\n }).schemas\n\n const jsonSchemas: Record<string, JSONSchema.BaseSchema> = {}\n for (const id in result) {\n jsonSchemas[id] = deleteInvalidProperties(result[id])\n }\n\n return jsonSchemas\n}\n"],"names":[],"mappings":";;AAWA,MAAM,iCAAiC;AACvC,MAAM,yBAAyB;AAE/B,SAAS,UAAU,QAAqC;AACtD,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,WAAW,QAAsC;AACxD,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,eAAe,QAA0C;AAChE,SAAO,kBAAkB,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,MAAM,cAAc,CAClB,KAIA,OACG;AACH,MAAI,WAAW,IAAI,SAAS,GAAG;AAI7B,QAAI,WAAW,QAAQ,IAAI,WAAW,OAAO,OAAO,CAAC,WAAW,OAAO,KAAK,MAAM,EAAE,SAAS,CAAC;AAAA,EAChG;AAEA,MAAI,UAAU,IAAI,SAAS,GAAG;AAE5B,QAAI,OAAO,UAAU;AACnB,UAAI,WAAW,OAAO;AACtB,UAAI,WAAW,SAAS;AAAA,IAC1B;AAAA,EACF;AAEA,MAAI,eAAe,IAAI,SAAS,GAAG;AAEjC,QAAI,OAAO,UAAU;AACnB,UAAI,WAAW,OAAO;AAAA,IACxB;AAAA,EACF;AACF;AAOA,MAAM,0BAE+C,CA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KAAK,CAAC,OAAO,gBAAgB,EAAE;AAAA,IAC/B,UAAU,OAAO,aAAa,CAAC,QAAQ,YAAY,KAAK,EAAE;AAAA,EAAA,CAC3D,EAAE;AAEH,QAAM,cAAqD,CAAA;AAC3D,aAAW,MAAM,QAAQ;AACvB,gBAAY,EAAE,IAAI,wBAAwB,OAAO,EAAE,CAAC;AAAA,EACtD;AAEA,SAAO;AACT;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bram-dc/fastify-type-provider-zod",
3
- "version": "5.0.3",
3
+ "version": "7.0.1",
4
4
  "description": "Zod Type Provider for Fastify@5",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.cjs",
@@ -23,6 +23,7 @@
23
23
  "scripts": {
24
24
  "build": "vite build",
25
25
  "prepare": "npm run build",
26
+ "test": "vitest",
26
27
  "test:coverage": "vitest --coverage",
27
28
  "test:ci": "npm run build && npm run typescript && npm run test:coverage",
28
29
  "lint": "biome check . && tsc --noEmit",
@@ -50,25 +51,26 @@
50
51
  },
51
52
  "peerDependencies": {
52
53
  "@fastify/swagger": ">=9.5.1",
53
- "fastify": "^5.0.0",
54
+ "fastify": "^5.5.0",
54
55
  "openapi-types": "^12.1.3",
55
- "zod": ">=3.25.67"
56
+ "zod": ">=4.1.5"
56
57
  },
57
58
  "devDependencies": {
58
- "@biomejs/biome": "^2.0.6",
59
- "@fastify/swagger": "^9.5.1",
59
+ "@biomejs/biome": "^2.3.0",
60
+ "@fastify/swagger": "^9.5.2",
60
61
  "@fastify/swagger-ui": "^5.2.3",
61
62
  "@kibertoad/biome-config": "^2.0.0",
62
- "@types/node": "^22.16.0",
63
+ "@readme/openapi-parser": "^5.0.2",
64
+ "@types/node": "^24.9.1",
63
65
  "@vitest/coverage-v8": "^3.2.4",
64
- "fastify": "^5.4.0",
66
+ "fastify": "^5.6.1",
65
67
  "fastify-plugin": "^5.0.1",
66
68
  "oas-validator": "^5.0.8",
67
- "tsd": "^0.32.0",
68
- "typescript": "^5.8.3",
69
- "unplugin-isolated-decl": "^0.14.5",
69
+ "tsd": "^0.33.0",
70
+ "typescript": "^5.9.3",
71
+ "unplugin-isolated-decl": "^0.15.2",
70
72
  "vitest": "^3.2.4",
71
- "zod": "^4.0.5"
73
+ "zod": "^4.2.0"
72
74
  },
73
75
  "tsd": {
74
76
  "directory": "types"