From 28bdc7621e13a96965a2a24caee873cf15b8aa31 Mon Sep 17 00:00:00 2001 From: NickOvt Date: Thu, 30 Nov 2023 14:29:12 +0200 Subject: [PATCH] fix(docs): Added support for enums ZMS-104 (#565) * query params add support for enums and example * add general enum and example support for requestBody as well --- lib/tools.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/tools.js b/lib/tools.js index fb96ce57..f71aafcc 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -767,6 +767,24 @@ function parseJoiObject(path, joiObject, requestBodyProperties) { data.format = format; } + // enum check + if (joiObject._valids) { + const enumValues = []; + for (const validEnumValue of joiObject._valids._values) { + enumValues.push(validEnumValue); + } + if (enumValues.length > 0) { + data.enum = enumValues; + } + } + + // example check + if (joiObject.$_terms && joiObject.$_terms.examples && joiObject.$_terms.examples.length > 0) { + const example = joiObject.$_terms.examples[0]; + + data.example = example; + } + if (path) { requestBodyProperties[path] = data; } else if (Array.isArray(requestBodyProperties)) { @@ -997,6 +1015,25 @@ module.exports = { obj.description = paramKeyData._flags.description; obj.required = paramKeyData._flags.presence === 'required'; obj.schema = { type: paramKeyData.type }; + + // enum check + if (paramKeyData._valids) { + const enumValues = []; + for (const validEnumValue of paramKeyData._valids._values) { + enumValues.push(validEnumValue); + } + if (enumValues.length > 0) { + obj.schema.enum = enumValues; + } + } + + // example check + if (paramKeyData.$_terms && paramKeyData.$_terms.examples && paramKeyData.$_terms.examples.length > 0) { + const example = paramKeyData.$_terms.examples[0]; + + obj.schema.example = example; + } + operationObj.parameters.push(obj); }