From 8e7f3c6afce0c395d0adbf7515e325da3b42b36c Mon Sep 17 00:00:00 2001 From: mathis-m Date: Wed, 24 Mar 2021 02:57:28 +0100 Subject: [PATCH 1/2] test(validation): optional array with validation constraints empty value should be valid Signed-off-by: mathis-m --- test/unit/core/utils.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index e567f93a33c..a22891e3dcf 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -772,6 +772,39 @@ describe("utils", () => { value = [] assertValidateParam(param, value, []) + // valid, empty array, with validation constraint + param = { + required: false, + schema: { + type: "array", + minItems: 1 + } + } + value = undefined + assertValidateOas3Param(param, value, []) + + // invalid, empty array, with minItems validation constraint + param = { + required: false, + schema: { + type: "array", + minItems: 2 + } + } + value = ["12"] + assertValidateOas3Param(param, value, ["Array must contain at least 2 items"]) + + // valid, valid array with satisfied minItems validation constraint + param = { + required: false, + schema: { + type: "array", + minItems: 1 + } + } + value = ["probe"] + assertValidateOas3Param(param, value, []) + // invalid, items do not match correct type param = { required: false, From dc4d13403e8f2cd9f63a6f775924b6ae3603bfd8 Mon Sep 17 00:00:00 2001 From: mathis-m Date: Wed, 24 Mar 2021 03:05:40 +0100 Subject: [PATCH 2/2] fix(validation): constraint validation fix only force validate of constraints if not null Signed-off-by: mathis-m --- src/core/utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index fce58316f91..814737cb5b6 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -429,11 +429,12 @@ function validateValueBySchema(value, schema, requiredByParam, bypassRequiredChe let minItems = schema.get("minItems") let pattern = schema.get("pattern") - const needsExplicitConstraintValidation = type === "array" const schemaRequiresValue = requiredByParam || requiredBySchema const hasValue = value !== undefined && value !== null const isValidEmpty = !schemaRequiresValue && !hasValue + const needsExplicitConstraintValidation = hasValue && type === "array" + const requiresFurtherValidation = schemaRequiresValue || needsExplicitConstraintValidation