From 2b70ca8f265f3b5ccebb512347a5106e3bba5ba2 Mon Sep 17 00:00:00 2001 From: Gabe Owen Date: Wed, 3 May 2023 21:29:54 -0500 Subject: [PATCH] fix for issue #2173 --- .../compile/compileURI/validateParams.js | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/dredd-transactions/compile/compileURI/validateParams.js b/packages/dredd-transactions/compile/compileURI/validateParams.js index b6a7c4315..08aae084d 100644 --- a/packages/dredd-transactions/compile/compileURI/validateParams.js +++ b/packages/dredd-transactions/compile/compileURI/validateParams.js @@ -5,32 +5,46 @@ module.exports = function validateParams(params) { let text; const param = params[paramName]; + if (param.required && !(typeof param.example !== 'undefined' && param.example !== '') && !(typeof param.default !== 'undefined' && param.default !== '')) { text = `Required URI parameter '${paramName}' has no example or default value.`; result.errors.push(text); } - switch (param.type) { - case 'number': - if (Number.isNaN(parseFloat(param.example))) { - text = `URI parameter '${paramName}' is declared as 'number' but it is a string.`; - result.errors.push(text); - } - break; - case 'boolean': - if ((param.example !== 'true') && (param.example !== 'false')) { - text = `URI parameter '${paramName}' is declared as 'boolean' but it is not.`; + if (param.schema && param.schema.example && typeof param.example === 'string'){ + const example = param.schema.example; + if(!example || example.length){ + text = `URI parameter '${paramName}' example value is an empty string.`; + result.warnings.push(text); + } + else if(param.example !== example){ + text = `URI parameter '${paramName}' example value does not match schema's example value` + result.warnings.push(text); + } + } + else{ + switch (param.type) { + case 'number': + if (Number.isNaN(parseFloat(param.example))) { + text = `URI parameter '${paramName}' is declared as 'number' but it is a string.`; + result.errors.push(text); + } + break; + case 'boolean': + if ((param.example !== 'true') && (param.example !== 'false')) { + text = `URI parameter '${paramName}' is declared as 'boolean' but it is not.`; + result.errors.push(text); + } + break; + default: + break; + } + + if (param.values.length > 0) { + if (!(param.values.indexOf(param.example) > -1)) { + text = `URI parameter '${paramName}' example value is not one of enum values.`; result.errors.push(text); } - break; - default: - break; - } - - if (param.values.length > 0) { - if (!(param.values.indexOf(param.example) > -1)) { - text = `URI parameter '${paramName}' example value is not one of enum values.`; - result.errors.push(text); } } });