@bram-dc/fastify-type-provider-zod 5.0.2 → 7.0.0
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 +83 -46
- package/dist/cjs/core.cjs +125 -0
- package/dist/cjs/core.cjs.map +1 -0
- package/dist/cjs/core.d.cts +68 -0
- package/dist/cjs/errors.cjs +57 -0
- package/dist/cjs/errors.cjs.map +1 -0
- package/dist/cjs/errors.d.cts +30 -0
- package/dist/cjs/index.cjs +16 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/registry.cjs +43 -0
- package/dist/cjs/registry.cjs.map +1 -0
- package/dist/cjs/registry.d.cts +9 -0
- package/dist/cjs/utils.cjs +21 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +12 -0
- package/dist/cjs/zod-to-json.cjs +93 -0
- package/dist/cjs/zod-to-json.cjs.map +1 -0
- package/dist/cjs/zod-to-json.d.cts +8 -0
- package/dist/esm/core.d.ts +68 -0
- package/dist/esm/core.js +125 -0
- package/dist/esm/core.js.map +1 -0
- package/dist/esm/errors.d.ts +30 -0
- package/dist/esm/errors.js +57 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/registry.d.ts +9 -0
- package/dist/esm/registry.js +43 -0
- package/dist/esm/registry.js.map +1 -0
- package/dist/esm/utils.d.ts +12 -0
- package/dist/esm/utils.js +21 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/zod-to-json.d.ts +8 -0
- package/dist/esm/zod-to-json.js +93 -0
- package/dist/esm/zod-to-json.js.map +1 -0
- package/package.json +75 -58
- package/src/core.ts +228 -0
- package/src/errors.ts +99 -0
- package/src/index.ts +21 -0
- package/src/registry.ts +64 -0
- package/src/utils.ts +33 -0
- package/src/zod-to-json.ts +160 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -16
- package/dist/src/core.d.ts +0 -59
- package/dist/src/core.js +0 -121
- package/dist/src/errors.d.ts +0 -35
- package/dist/src/errors.js +0 -43
package/src/utils.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
2
|
+
|
|
3
|
+
type SwaggerObject = {
|
|
4
|
+
swaggerObject: Partial<OpenAPIV2.Document>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
type OpenAPIObject = {
|
|
8
|
+
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const assertIsOpenAPIObject: (
|
|
12
|
+
obj: SwaggerObject | OpenAPIObject,
|
|
13
|
+
) => asserts obj is OpenAPIObject = (obj) => {
|
|
14
|
+
if ('swaggerObject' in obj) {
|
|
15
|
+
throw new Error('This package currently does not support component references for Swagger 2.0')
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type JSONSchemaTarget = 'draft-2020-12' | 'openapi-3.0'
|
|
20
|
+
|
|
21
|
+
export const getReferenceUri = (input: string): string => {
|
|
22
|
+
const id = input.replace(/^#\/(?:\$defs|definitions|components\/schemas)\//, '')
|
|
23
|
+
|
|
24
|
+
return `#/components/schemas/${id}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const getJSONSchemaTarget = (version = '3.0.0'): JSONSchemaTarget => {
|
|
28
|
+
if (version.startsWith('3.0')) {
|
|
29
|
+
return 'openapi-3.0'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return 'draft-2020-12'
|
|
33
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
$ZodDate,
|
|
3
|
+
$ZodUndefined,
|
|
4
|
+
$ZodUnion,
|
|
5
|
+
JSONSchema,
|
|
6
|
+
RegistryToJSONSchemaParams,
|
|
7
|
+
} from 'zod/v4/core'
|
|
8
|
+
import { $ZodRegistry, $ZodType, toJSONSchema } from 'zod/v4/core'
|
|
9
|
+
import type { SchemaRegistryMeta } from './registry'
|
|
10
|
+
import { getReferenceUri } from './utils'
|
|
11
|
+
|
|
12
|
+
const SCHEMA_REGISTRY_ID_PLACEHOLDER = '__SCHEMA__ID__PLACEHOLDER__'
|
|
13
|
+
const SCHEMA_URI_PLACEHOLDER = '__SCHEMA__PLACEHOLDER__'
|
|
14
|
+
|
|
15
|
+
function isZodDate(entity: unknown): entity is $ZodDate {
|
|
16
|
+
return entity instanceof $ZodType && entity._zod.def.type === 'date'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isZodUnion(entity: unknown): entity is $ZodUnion {
|
|
20
|
+
return entity instanceof $ZodType && entity._zod.def.type === 'union'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isZodUndefined(entity: unknown): entity is $ZodUndefined {
|
|
24
|
+
return entity instanceof $ZodType && entity._zod.def.type === 'undefined'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const getOverride = (
|
|
28
|
+
ctx: {
|
|
29
|
+
zodSchema: $ZodType
|
|
30
|
+
jsonSchema: JSONSchema.BaseSchema
|
|
31
|
+
},
|
|
32
|
+
io: 'input' | 'output',
|
|
33
|
+
) => {
|
|
34
|
+
if (isZodUnion(ctx.zodSchema)) {
|
|
35
|
+
// Filter unrepresentable types in unions
|
|
36
|
+
// TODO: Should be fixed upstream and not merged in this plugin.
|
|
37
|
+
// Remove when passed: https://github.com/colinhacks/zod/pull/5013
|
|
38
|
+
ctx.jsonSchema.anyOf = ctx.jsonSchema.anyOf?.filter((schema) => Object.keys(schema).length > 0)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isZodDate(ctx.zodSchema)) {
|
|
42
|
+
// Allow dates to be represented as strings in output schemas
|
|
43
|
+
if (io === 'output') {
|
|
44
|
+
ctx.jsonSchema.type = 'string'
|
|
45
|
+
ctx.jsonSchema.format = 'date-time'
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (isZodUndefined(ctx.zodSchema)) {
|
|
50
|
+
// Allow undefined to be represented as null in output schemas
|
|
51
|
+
if (io === 'output') {
|
|
52
|
+
ctx.jsonSchema.type = 'null'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ZodToJsonConfig = {} & Omit<
|
|
58
|
+
RegistryToJSONSchemaParams,
|
|
59
|
+
'io' | 'metadata' | 'cycles' | 'reused' | 'uri'
|
|
60
|
+
>
|
|
61
|
+
|
|
62
|
+
const deleteInvalidProperties: (
|
|
63
|
+
schema: JSONSchema.BaseSchema,
|
|
64
|
+
) => Omit<JSONSchema.BaseSchema, 'id' | '$schema'> = (schema) => {
|
|
65
|
+
const object = { ...schema }
|
|
66
|
+
|
|
67
|
+
delete object.id
|
|
68
|
+
delete object.$schema
|
|
69
|
+
|
|
70
|
+
// ToDo added in newer zod
|
|
71
|
+
delete object.$id
|
|
72
|
+
|
|
73
|
+
return object
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const zodSchemaToJson: (
|
|
77
|
+
zodSchema: $ZodType,
|
|
78
|
+
registry: $ZodRegistry<SchemaRegistryMeta>,
|
|
79
|
+
io: 'input' | 'output',
|
|
80
|
+
config: ZodToJsonConfig,
|
|
81
|
+
) => ReturnType<typeof deleteInvalidProperties> = (zodSchema, registry, io, config) => {
|
|
82
|
+
/**
|
|
83
|
+
* Checks whether the provided schema is registered in the given registry.
|
|
84
|
+
* If it is present and has an `id`, it can be referenced as component.
|
|
85
|
+
*
|
|
86
|
+
* @see https://github.com/turkerdev/fastify-type-provider-zod/issues/173
|
|
87
|
+
*/
|
|
88
|
+
const schemaRegistryEntry = registry.get(zodSchema)
|
|
89
|
+
if (schemaRegistryEntry?.id) {
|
|
90
|
+
return { $ref: getReferenceUri(schemaRegistryEntry.id) }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Unfortunately, at the time of writing, there is no way to generate a schema with `$ref`
|
|
95
|
+
* using `toJSONSchema` and a zod schema.
|
|
96
|
+
*
|
|
97
|
+
* As a workaround, we create a zod registry containing only the specific schema we want to convert.
|
|
98
|
+
*
|
|
99
|
+
* @see https://github.com/colinhacks/zod/issues/4281
|
|
100
|
+
*/
|
|
101
|
+
const tempRegistry = new $ZodRegistry<SchemaRegistryMeta>()
|
|
102
|
+
tempRegistry.add(zodSchema, { id: SCHEMA_REGISTRY_ID_PLACEHOLDER })
|
|
103
|
+
|
|
104
|
+
const {
|
|
105
|
+
schemas: { [SCHEMA_REGISTRY_ID_PLACEHOLDER]: result },
|
|
106
|
+
} = toJSONSchema(tempRegistry, {
|
|
107
|
+
...config,
|
|
108
|
+
io,
|
|
109
|
+
target: config.target,
|
|
110
|
+
metadata: registry,
|
|
111
|
+
unrepresentable: config.unrepresentable ?? 'any',
|
|
112
|
+
cycles: 'ref',
|
|
113
|
+
reused: 'inline',
|
|
114
|
+
/**
|
|
115
|
+
* The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.
|
|
116
|
+
* As a workaround, we set a placeholder that looks something like this.
|
|
117
|
+
* @see jsonSchemaReplaceRef
|
|
118
|
+
* @see https://github.com/colinhacks/zod/issues/4750
|
|
119
|
+
*/
|
|
120
|
+
uri: () => SCHEMA_URI_PLACEHOLDER,
|
|
121
|
+
override: config.override ?? ((ctx) => getOverride(ctx, io)),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const jsonSchema = deleteInvalidProperties(result)
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Replace the previous generated placeholders with the final `$ref` value
|
|
128
|
+
*/
|
|
129
|
+
return JSON.parse(JSON.stringify(jsonSchema), (__key, value) => {
|
|
130
|
+
if (typeof value === 'string' && value.startsWith(SCHEMA_URI_PLACEHOLDER)) {
|
|
131
|
+
return getReferenceUri(value.slice(SCHEMA_URI_PLACEHOLDER.length))
|
|
132
|
+
}
|
|
133
|
+
return value
|
|
134
|
+
}) as typeof result
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const zodRegistryToJson: (
|
|
138
|
+
registry: $ZodRegistry<SchemaRegistryMeta>,
|
|
139
|
+
io: 'input' | 'output',
|
|
140
|
+
config: ZodToJsonConfig,
|
|
141
|
+
) => Record<string, JSONSchema.BaseSchema> = (registry, io, config) => {
|
|
142
|
+
const result = toJSONSchema(registry, {
|
|
143
|
+
...config,
|
|
144
|
+
io,
|
|
145
|
+
target: config.target,
|
|
146
|
+
metadata: registry,
|
|
147
|
+
unrepresentable: config.unrepresentable ?? 'any',
|
|
148
|
+
cycles: 'ref',
|
|
149
|
+
reused: 'inline',
|
|
150
|
+
uri: (id) => getReferenceUri(id),
|
|
151
|
+
override: config.override ?? ((ctx) => getOverride(ctx, io)),
|
|
152
|
+
}).schemas
|
|
153
|
+
|
|
154
|
+
const jsonSchemas: Record<string, JSONSchema.BaseSchema> = {}
|
|
155
|
+
for (const id in result) {
|
|
156
|
+
jsonSchemas[id] = deleteInvalidProperties(result[id])
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return jsonSchemas
|
|
160
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export { type ZodTypeProvider, type FastifyPluginAsyncZod, type FastifyPluginCallbackZod, type ZodSerializerCompilerOptions, jsonSchemaTransform, createJsonSchemaTransform, jsonSchemaTransformObject, createJsonSchemaTransformObject, serializerCompiler, validatorCompiler, createSerializerCompiler, } from './src/core';
|
|
2
|
-
export { type ZodFastifySchemaValidationError, ResponseSerializationError, InvalidSchemaError, hasZodFastifySchemaValidationErrors, isResponseSerializationError, } from './src/errors';
|
package/dist/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isResponseSerializationError = exports.hasZodFastifySchemaValidationErrors = exports.InvalidSchemaError = exports.ResponseSerializationError = exports.createSerializerCompiler = exports.validatorCompiler = exports.serializerCompiler = exports.createJsonSchemaTransformObject = exports.jsonSchemaTransformObject = exports.createJsonSchemaTransform = exports.jsonSchemaTransform = void 0;
|
|
4
|
-
var core_1 = require("./src/core");
|
|
5
|
-
Object.defineProperty(exports, "jsonSchemaTransform", { enumerable: true, get: function () { return core_1.jsonSchemaTransform; } });
|
|
6
|
-
Object.defineProperty(exports, "createJsonSchemaTransform", { enumerable: true, get: function () { return core_1.createJsonSchemaTransform; } });
|
|
7
|
-
Object.defineProperty(exports, "jsonSchemaTransformObject", { enumerable: true, get: function () { return core_1.jsonSchemaTransformObject; } });
|
|
8
|
-
Object.defineProperty(exports, "createJsonSchemaTransformObject", { enumerable: true, get: function () { return core_1.createJsonSchemaTransformObject; } });
|
|
9
|
-
Object.defineProperty(exports, "serializerCompiler", { enumerable: true, get: function () { return core_1.serializerCompiler; } });
|
|
10
|
-
Object.defineProperty(exports, "validatorCompiler", { enumerable: true, get: function () { return core_1.validatorCompiler; } });
|
|
11
|
-
Object.defineProperty(exports, "createSerializerCompiler", { enumerable: true, get: function () { return core_1.createSerializerCompiler; } });
|
|
12
|
-
var errors_1 = require("./src/errors");
|
|
13
|
-
Object.defineProperty(exports, "ResponseSerializationError", { enumerable: true, get: function () { return errors_1.ResponseSerializationError; } });
|
|
14
|
-
Object.defineProperty(exports, "InvalidSchemaError", { enumerable: true, get: function () { return errors_1.InvalidSchemaError; } });
|
|
15
|
-
Object.defineProperty(exports, "hasZodFastifySchemaValidationErrors", { enumerable: true, get: function () { return errors_1.hasZodFastifySchemaValidationErrors; } });
|
|
16
|
-
Object.defineProperty(exports, "isResponseSerializationError", { enumerable: true, get: function () { return errors_1.isResponseSerializationError; } });
|
package/dist/src/core.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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 { z } from 'zod';
|
|
5
|
-
export interface ZodTypeProvider extends FastifyTypeProvider {
|
|
6
|
-
validator: this['schema'] extends z.ZodTypeAny ? z.output<this['schema']> : unknown;
|
|
7
|
-
serializer: this['schema'] extends z.ZodTypeAny ? z.input<this['schema']> : unknown;
|
|
8
|
-
}
|
|
9
|
-
interface Schema extends FastifySchema {
|
|
10
|
-
hide?: boolean;
|
|
11
|
-
}
|
|
12
|
-
type CreateJsonSchemaTransformOptions = {
|
|
13
|
-
skipList?: readonly string[];
|
|
14
|
-
schemaRegistry?: z.core.$ZodJSONSchemaRegistry;
|
|
15
|
-
};
|
|
16
|
-
export declare const createJsonSchemaTransform: ({ skipList, schemaRegistry, }: CreateJsonSchemaTransformOptions) => SwaggerTransform<Schema>;
|
|
17
|
-
export declare const jsonSchemaTransform: SwaggerTransform<Schema>;
|
|
18
|
-
type CreateJsonSchemaTransformObjectOptions = {
|
|
19
|
-
schemaRegistry?: z.core.$ZodJSONSchemaRegistry;
|
|
20
|
-
};
|
|
21
|
-
export declare const createJsonSchemaTransformObject: ({ schemaRegistry, }: CreateJsonSchemaTransformObjectOptions) => SwaggerTransformObject;
|
|
22
|
-
export declare const jsonSchemaTransformObject: SwaggerTransformObject;
|
|
23
|
-
export declare const validatorCompiler: FastifySchemaCompiler<z.ZodTypeAny>;
|
|
24
|
-
type ReplacerFunction = (this: any, key: string, value: any) => any;
|
|
25
|
-
export type ZodSerializerCompilerOptions = {
|
|
26
|
-
replacer?: ReplacerFunction;
|
|
27
|
-
};
|
|
28
|
-
export declare const createSerializerCompiler: (options?: ZodSerializerCompilerOptions) => FastifySerializerCompiler<z.ZodTypeAny | {
|
|
29
|
-
properties: z.ZodTypeAny;
|
|
30
|
-
}>;
|
|
31
|
-
export declare const serializerCompiler: FastifySerializerCompiler<z.ZodType<unknown, unknown> | {
|
|
32
|
-
properties: z.ZodTypeAny;
|
|
33
|
-
}>;
|
|
34
|
-
/**
|
|
35
|
-
* FastifyPluginCallbackZod with Zod automatic type inference
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* import { FastifyPluginCallbackZod } from "fastify-type-provider-zod"
|
|
40
|
-
*
|
|
41
|
-
* const plugin: FastifyPluginCallbackZod = (fastify, options, done) => {
|
|
42
|
-
* done()
|
|
43
|
-
* }
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export type FastifyPluginCallbackZod<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginCallback<Options, Server, ZodTypeProvider>;
|
|
47
|
-
/**
|
|
48
|
-
* FastifyPluginAsyncZod with Zod automatic type inference
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* import { FastifyPluginAsyncZod } from "fastify-type-provider-zod"
|
|
53
|
-
*
|
|
54
|
-
* const plugin: FastifyPluginAsyncZod = async (fastify, options) => {
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
export type FastifyPluginAsyncZod<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginAsync<Options, Server, ZodTypeProvider>;
|
|
59
|
-
export {};
|
package/dist/src/core.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.serializerCompiler = exports.createSerializerCompiler = exports.validatorCompiler = exports.jsonSchemaTransformObject = exports.createJsonSchemaTransformObject = exports.jsonSchemaTransform = exports.createJsonSchemaTransform = void 0;
|
|
4
|
-
const zod_1 = require("zod");
|
|
5
|
-
const errors_1 = require("./errors");
|
|
6
|
-
const defaultSkipList = [
|
|
7
|
-
'/documentation/',
|
|
8
|
-
'/documentation/initOAuth',
|
|
9
|
-
'/documentation/json',
|
|
10
|
-
'/documentation/uiConfig',
|
|
11
|
-
'/documentation/yaml',
|
|
12
|
-
'/documentation/*',
|
|
13
|
-
'/documentation/static/*',
|
|
14
|
-
];
|
|
15
|
-
const createJsonSchemaTransform = ({ skipList = defaultSkipList, schemaRegistry = zod_1.z.globalRegistry, }) => {
|
|
16
|
-
return ({ schema, url }) => {
|
|
17
|
-
if (!schema) {
|
|
18
|
-
return {
|
|
19
|
-
schema,
|
|
20
|
-
url,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
const { response, headers, querystring, body, params, hide, ...rest } = schema;
|
|
24
|
-
const transformed = {};
|
|
25
|
-
if (skipList.includes(url) || hide) {
|
|
26
|
-
transformed.hide = true;
|
|
27
|
-
return { schema: transformed, url };
|
|
28
|
-
}
|
|
29
|
-
const zodSchemas = { headers, querystring, body, params };
|
|
30
|
-
for (const prop in zodSchemas) {
|
|
31
|
-
const zodSchema = zodSchemas[prop];
|
|
32
|
-
if (zodSchema) {
|
|
33
|
-
transformed[prop] = zod_1.z.toJSONSchema(zodSchema, {
|
|
34
|
-
external: {
|
|
35
|
-
registry: schemaRegistry,
|
|
36
|
-
uri: (id) => `#/components/schemas/${id}`,
|
|
37
|
-
defs: {},
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (response) {
|
|
43
|
-
transformed.response = {};
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
-
for (const prop in response) {
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
-
const zodSchema = resolveSchema(response[prop]);
|
|
48
|
-
const transformedResponse = zod_1.z.toJSONSchema(zodSchema, {
|
|
49
|
-
external: {
|
|
50
|
-
registry: schemaRegistry,
|
|
51
|
-
uri: (id) => `#/components/schemas/${id}`,
|
|
52
|
-
defs: {},
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
transformed.response[prop] = transformedResponse;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
for (const prop in rest) {
|
|
59
|
-
const meta = rest[prop];
|
|
60
|
-
if (meta) {
|
|
61
|
-
transformed[prop] = meta;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return { schema: transformed, url };
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
exports.createJsonSchemaTransform = createJsonSchemaTransform;
|
|
68
|
-
exports.jsonSchemaTransform = (0, exports.createJsonSchemaTransform)({});
|
|
69
|
-
const createJsonSchemaTransformObject = ({ schemaRegistry = zod_1.z.globalRegistry, }) => (input) => {
|
|
70
|
-
if ('swaggerObject' in input) {
|
|
71
|
-
console.warn('This package currently does not support component references for Swagger 2.0');
|
|
72
|
-
return input.swaggerObject;
|
|
73
|
-
}
|
|
74
|
-
const { schemas } = zod_1.z.toJSONSchema(schemaRegistry, {
|
|
75
|
-
uri: (id) => `#/components/schemas/${id}`,
|
|
76
|
-
external: {
|
|
77
|
-
registry: schemaRegistry,
|
|
78
|
-
uri: (id) => `#/components/schemas/${id}`,
|
|
79
|
-
defs: {},
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
return {
|
|
83
|
-
...input.openapiObject,
|
|
84
|
-
components: {
|
|
85
|
-
...input.openapiObject.components,
|
|
86
|
-
schemas: {
|
|
87
|
-
...input.openapiObject.components?.schemas,
|
|
88
|
-
...schemas,
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
exports.createJsonSchemaTransformObject = createJsonSchemaTransformObject;
|
|
94
|
-
exports.jsonSchemaTransformObject = (0, exports.createJsonSchemaTransformObject)({});
|
|
95
|
-
const validatorCompiler = ({ schema }) => (data) => {
|
|
96
|
-
const result = schema.safeParse(data);
|
|
97
|
-
if (result.error) {
|
|
98
|
-
return { error: (0, errors_1.createValidationError)(result.error) };
|
|
99
|
-
}
|
|
100
|
-
return { value: result.data };
|
|
101
|
-
};
|
|
102
|
-
exports.validatorCompiler = validatorCompiler;
|
|
103
|
-
function resolveSchema(maybeSchema) {
|
|
104
|
-
if ('safeParse' in maybeSchema) {
|
|
105
|
-
return maybeSchema;
|
|
106
|
-
}
|
|
107
|
-
if ('properties' in maybeSchema) {
|
|
108
|
-
return maybeSchema.properties;
|
|
109
|
-
}
|
|
110
|
-
throw new errors_1.InvalidSchemaError(JSON.stringify(maybeSchema));
|
|
111
|
-
}
|
|
112
|
-
const createSerializerCompiler = (options) => ({ schema: maybeSchema, method, url }) => (data) => {
|
|
113
|
-
const schema = resolveSchema(maybeSchema);
|
|
114
|
-
const result = schema.safeParse(data);
|
|
115
|
-
if (result.error) {
|
|
116
|
-
throw new errors_1.ResponseSerializationError(method, url, { cause: result.error });
|
|
117
|
-
}
|
|
118
|
-
return JSON.stringify(result.data, options?.replacer);
|
|
119
|
-
};
|
|
120
|
-
exports.createSerializerCompiler = createSerializerCompiler;
|
|
121
|
-
exports.serializerCompiler = (0, exports.createSerializerCompiler)({});
|
package/dist/src/errors.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { FastifyError } from 'fastify';
|
|
2
|
-
import type { z } from 'zod';
|
|
3
|
-
declare const ResponseSerializationError_base: import("@fastify/error").FastifyErrorConstructor<{
|
|
4
|
-
code: string;
|
|
5
|
-
}, [{
|
|
6
|
-
cause: z.core.$ZodError;
|
|
7
|
-
}]>;
|
|
8
|
-
export declare class ResponseSerializationError extends ResponseSerializationError_base {
|
|
9
|
-
method: string;
|
|
10
|
-
url: string;
|
|
11
|
-
cause: z.core.$ZodError;
|
|
12
|
-
constructor(method: string, url: string, options: {
|
|
13
|
-
cause: z.core.$ZodError;
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
export declare function isResponseSerializationError(value: unknown): value is ResponseSerializationError;
|
|
17
|
-
export declare const InvalidSchemaError: import("@fastify/error").FastifyErrorConstructor<{
|
|
18
|
-
code: string;
|
|
19
|
-
}, [string]>;
|
|
20
|
-
declare const ZodFastifySchemaValidationErrorSymbol: unique symbol;
|
|
21
|
-
export type ZodFastifySchemaValidationError = {
|
|
22
|
-
[ZodFastifySchemaValidationErrorSymbol]: true;
|
|
23
|
-
keyword: string;
|
|
24
|
-
instancePath: string;
|
|
25
|
-
schemaPath: string;
|
|
26
|
-
params: {
|
|
27
|
-
issue: z.core.$ZodIssue;
|
|
28
|
-
};
|
|
29
|
-
message: string;
|
|
30
|
-
};
|
|
31
|
-
export declare const hasZodFastifySchemaValidationErrors: (error: unknown) => error is Omit<FastifyError, "validation"> & {
|
|
32
|
-
validation: ZodFastifySchemaValidationError[];
|
|
33
|
-
};
|
|
34
|
-
export declare const createValidationError: (error: z.core.$ZodError) => ZodFastifySchemaValidationError[];
|
|
35
|
-
export {};
|
package/dist/src/errors.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createValidationError = exports.hasZodFastifySchemaValidationErrors = exports.InvalidSchemaError = exports.ResponseSerializationError = void 0;
|
|
7
|
-
exports.isResponseSerializationError = isResponseSerializationError;
|
|
8
|
-
const error_1 = __importDefault(require("@fastify/error"));
|
|
9
|
-
class ResponseSerializationError extends (0, error_1.default)('FST_ERR_RESPONSE_SERIALIZATION', "Response doesn't match the schema", 500) {
|
|
10
|
-
constructor(method, url, options) {
|
|
11
|
-
super({ cause: options.cause });
|
|
12
|
-
this.method = method;
|
|
13
|
-
this.url = url;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.ResponseSerializationError = ResponseSerializationError;
|
|
17
|
-
function isResponseSerializationError(value) {
|
|
18
|
-
return 'method' in value;
|
|
19
|
-
}
|
|
20
|
-
exports.InvalidSchemaError = (0, error_1.default)('FST_ERR_INVALID_SCHEMA', 'Invalid schema passed: %s', 500);
|
|
21
|
-
const ZodFastifySchemaValidationErrorSymbol = Symbol.for('ZodFastifySchemaValidationError');
|
|
22
|
-
const isZodFastifySchemaValidationError = (error) => typeof error === 'object' &&
|
|
23
|
-
error !== null &&
|
|
24
|
-
ZodFastifySchemaValidationErrorSymbol in error &&
|
|
25
|
-
error[ZodFastifySchemaValidationErrorSymbol] === true;
|
|
26
|
-
const hasZodFastifySchemaValidationErrors = (error) => typeof error === 'object' &&
|
|
27
|
-
error !== null &&
|
|
28
|
-
'validation' in error &&
|
|
29
|
-
Array.isArray(error.validation) &&
|
|
30
|
-
error.validation.length > 0 &&
|
|
31
|
-
isZodFastifySchemaValidationError(error.validation[0]);
|
|
32
|
-
exports.hasZodFastifySchemaValidationErrors = hasZodFastifySchemaValidationErrors;
|
|
33
|
-
const createValidationError = (error) => error.issues.map((issue) => ({
|
|
34
|
-
[ZodFastifySchemaValidationErrorSymbol]: true,
|
|
35
|
-
keyword: issue.code ?? 'custom',
|
|
36
|
-
instancePath: `/${issue.path.join('/')}`,
|
|
37
|
-
schemaPath: `#/${issue.path.join('/')}/${issue.code}`,
|
|
38
|
-
params: {
|
|
39
|
-
issue,
|
|
40
|
-
},
|
|
41
|
-
message: issue.message,
|
|
42
|
-
}));
|
|
43
|
-
exports.createValidationError = createValidationError;
|