From ac4bd0a558d95aaa155c68cc66e07d6c676e54b4 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Sun, 16 Jun 2024 19:50:47 +0000 Subject: [PATCH] Change: OpenApiValidator to only be created in dev --- server/Server.js | 50 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/server/Server.js b/server/Server.js index 6ac69692f..0ecdad328 100644 --- a/server/Server.js +++ b/server/Server.js @@ -205,30 +205,32 @@ class Server { }) // Install the OpenApiValidator middleware - const apiSpec = Path.join(__dirname, 'openapi.json') - app.use( - OpenApiValidator.middleware({ - apiSpec: apiSpec, - validateRequests: true, - validateResponses: { - onError: (error, body, req) => { - console.log(`Response body fails validation: `, error) - console.log(`Emitted from:`, req.originalUrl) - console.debug(body) - } - }, - formats: [ - // Define custom format types for OpenAPI spec - { - name: 'uuid', - type: 'string', - validate: (v) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(v.toString()) - } - ], - unknownFormats: ['li_[a-z0-9]{18}', '[0-9]*'], - ignoreUndocumented: true - }) - ) + if (Logger.isDev) { + const apiSpec = Path.join(__dirname, 'openapi.json') + app.use( + OpenApiValidator.middleware({ + apiSpec: apiSpec, + validateRequests: true, + validateResponses: { + onError: (error, body, req) => { + console.log(`Response body fails validation: `, error) + console.log(`Emitted from:`, req.originalUrl) + console.debug(body) + } + }, + formats: [ + // Define custom format types for OpenAPI spec + { + name: 'uuid', + type: 'string', + validate: (v) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(v.toString()) + } + ], + unknownFormats: ['li_[a-z0-9]{18}', '[0-9]*'], + ignoreUndocumented: true + }) + ) + } // parse cookies in requests app.use(cookieParser())