34 lines
855 B
JavaScript
34 lines
855 B
JavaScript
|
|
/**
|
||
|
|
* Validation utilities for Fastify
|
||
|
|
*/
|
||
|
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||
|
|
/**
|
||
|
|
* Convert Zod schema to Fastify JSON schema
|
||
|
|
*/
|
||
|
|
export function zodToFastifySchema(zodSchema) {
|
||
|
|
const jsonSchema = zodToJsonSchema(zodSchema, {
|
||
|
|
target: 'openApi3',
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
body: jsonSchema,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Create Fastify schema from Zod schema for request body
|
||
|
|
*/
|
||
|
|
export function createBodySchema(schema) {
|
||
|
|
return zodToFastifySchema(schema);
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Create Fastify schema with body and response
|
||
|
|
*/
|
||
|
|
export function createSchema(bodySchema, responseSchema) {
|
||
|
|
const schema = zodToFastifySchema(bodySchema);
|
||
|
|
if (responseSchema) {
|
||
|
|
schema.response = {
|
||
|
|
200: zodToJsonSchema(responseSchema, { target: 'openApi3' }),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return schema;
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=validation.js.map
|