UNPKG

fastify-type-provider-zod

Version:

Zod Type Provider for Fastify@5

119 lines (118 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const v4 = require("zod/v4"); const errors = require("./errors.cjs"); const zodToJson = require("./zod-to-json.cjs"); const defaultSkipList = [ "/documentation/", "/documentation/initOAuth", "/documentation/json", "/documentation/uiConfig", "/documentation/yaml", "/documentation/*", "/documentation/static/*" ]; const createJsonSchemaTransform = ({ skipList = defaultSkipList, schemaRegistry = v4.z.globalRegistry }) => { return ({ schema, url }) => { if (!schema) { return { schema, url }; } const { response, headers, querystring, body, params, hide, ...rest } = schema; const transformed = {}; if (skipList.includes(url) || hide) { transformed.hide = true; return { schema: transformed, url }; } const zodSchemas = { headers, querystring, body, params }; for (const prop in zodSchemas) { const zodSchema = zodSchemas[prop]; if (zodSchema) { transformed[prop] = zodToJson.zodSchemaToJson(zodSchema, schemaRegistry, "input"); } } if (response) { transformed.response = {}; for (const prop in response) { const zodSchema = resolveSchema(response[prop]); transformed.response[prop] = zodToJson.zodSchemaToJson(zodSchema, schemaRegistry, "output"); } } for (const prop in rest) { const meta = rest[prop]; if (meta) { transformed[prop] = meta; } } return { schema: transformed, url }; }; }; const jsonSchemaTransform = createJsonSchemaTransform({}); const createJsonSchemaTransformObject = ({ schemaRegistry = v4.z.globalRegistry }) => (input) => { var _a; if ("swaggerObject" in input) { console.warn("This package currently does not support component references for Swagger 2.0"); return input.swaggerObject; } const inputSchemas = zodToJson.zodRegistryToJson(schemaRegistry, "input"); const outputSchemas = zodToJson.zodRegistryToJson(schemaRegistry, "output"); for (const key in outputSchemas) { if (inputSchemas[key]) { throw new Error( `Collision detected for schema "${key}". The is already an input schema with the same name.` ); } } return { ...input.openapiObject, components: { ...input.openapiObject.components, schemas: { ...(_a = input.openapiObject.components) == null ? void 0 : _a.schemas, ...inputSchemas, ...outputSchemas } } }; }; const jsonSchemaTransformObject = createJsonSchemaTransformObject({}); const validatorCompiler = ({ schema }) => (data) => { const result = schema.safeParse(data); if (result.error) { return { error: errors.createValidationError(result.error) }; } return { value: result.data }; }; function resolveSchema(maybeSchema) { if ("safeParse" in maybeSchema) { return maybeSchema; } if ("properties" in maybeSchema) { return maybeSchema.properties; } throw new errors.InvalidSchemaError(JSON.stringify(maybeSchema)); } const createSerializerCompiler = (options) => ({ schema: maybeSchema, method, url }) => (data) => { const schema = resolveSchema(maybeSchema); const result = schema.safeParse(data); if (result.error) { throw new errors.ResponseSerializationError(method, url, { cause: result.error }); } return JSON.stringify(result.data, options == null ? void 0 : options.replacer); }; const serializerCompiler = createSerializerCompiler({}); exports.createJsonSchemaTransform = createJsonSchemaTransform; exports.createJsonSchemaTransformObject = createJsonSchemaTransformObject; exports.createSerializerCompiler = createSerializerCompiler; exports.jsonSchemaTransform = jsonSchemaTransform; exports.jsonSchemaTransformObject = jsonSchemaTransformObject; exports.serializerCompiler = serializerCompiler; exports.validatorCompiler = validatorCompiler; //# sourceMappingURL=core.cjs.map