Skip to content

Commit

Permalink
fix: array constraint validation only if value was provided (#7112)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathis-m authored Mar 30, 2021
1 parent cabba62 commit 4103e0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/unit/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4103e0f

Please sign in to comment.