UNPKG

fastify-openapi-connector

Version:

Fastify plugin that will set-up routes with security & json validation based on OpenAPI specification

36 lines (35 loc) 1.43 kB
/** * Helper function to create route schema from the OpenAPI specification * @param params OAS parameters object * @param contentTypes Priority list of content types we try to set for validation of bodySchema * @param requestBody OAS requestBody object * @param responses OAS responses object * @returns Fastify schema object */ export const createRouteSchema = (params, contentTypes, requestBody, responses, validateResponse) => { let bodySchema = undefined; // https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#validation-and-serialization // By default we set 'application/json', but can be overriden by user. For example 'application/scim+json' might be needed. for (const contentType of contentTypes) { // biome-ignore lint/suspicious/noExplicitAny: We not sure what we have bodySchema ?? (bodySchema = requestBody?.content?.[contentType]?.schema); } // Fastify have stupid validation where if we add the property as undefine it will show warning const schema = {}; if (bodySchema) { schema.body = bodySchema; } if (params.query) { schema.querystring = params.query; } if (params.path) { schema.params = params.path; } if (params.header) { schema.headers = params.header; } if (validateResponse === true && responses) { schema.response = responses; } return schema; };