Skip to content

Commit

Permalink
fix: accept string-represented values in required array runtime valid…
Browse files Browse the repository at this point in the history
…ation (#5609)

* rename `listCheck` -> `arrayListCheck`

* allow non-empty strings to quality a required array value
  • Loading branch information
shockey authored Sep 15, 2019
1 parent 85f2bf3 commit 00c8e96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
// These checks should evaluate to true if there is a parameter
let stringCheck = type === "string" && value
let arrayCheck = type === "array" && Array.isArray(value) && value.length
let listCheck = type === "array" && Im.List.isList(value) && value.count()
let arrayListCheck = type === "array" && Im.List.isList(value) && value.count()
let arrayStringCheck = type === "array" && typeof value === "string" && value
let fileCheck = type === "file" && value instanceof win.File
let booleanCheck = type === "boolean" && (value || value === false)
let numberCheck = type === "number" && (value || value === 0)
Expand All @@ -543,8 +544,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
// }

const allChecks = [
stringCheck, arrayCheck, listCheck, fileCheck, booleanCheck,
numberCheck, integerCheck, objectCheck, objectStringCheck,
stringCheck, arrayCheck, arrayListCheck, arrayStringCheck, fileCheck,
booleanCheck, numberCheck, integerCheck, objectCheck, objectStringCheck,
]

const passedAnyCheck = allChecks.some(v => !!v)
Expand Down Expand Up @@ -605,7 +606,7 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
} else if ( type === "array" ) {
let itemType

if ( !listCheck || !value.count() ) { return errors }
if ( !arrayListCheck || !value.count() ) { return errors }

itemType = paramDetails.getIn(["items", "type"])

Expand Down
16 changes: 16 additions & 0 deletions test/mocha/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ describe("utils", function() {
}
value = []
assertValidateParam(param, value, ["Required field is not provided"])

// invalid (empty) array, represented as a string
param = {
required: true,
type: "array"
}
value = ""
assertValidateParam(param, value, ["Required field is not provided"])

// invalid (not an array)
param = {
Expand Down Expand Up @@ -629,6 +637,14 @@ describe("utils", function() {
}
value = [1]
assertValidateParam(param, value, [])

// valid array, with no 'type' for items, represented as a string
param = {
required: true,
type: "array"
}
value = "[1]"
assertValidateParam(param, value, [])

// valid array, items match type
param = {
Expand Down

0 comments on commit 00c8e96

Please sign in to comment.