Skip to content

Commit 00c8e96

Browse files
authored
fix: accept string-represented values in required array runtime validation (#5609)
* rename `listCheck` -> `arrayListCheck` * allow non-empty strings to quality a required array value
1 parent 85f2bf3 commit 00c8e96

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/core/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
523523
// These checks should evaluate to true if there is a parameter
524524
let stringCheck = type === "string" && value
525525
let arrayCheck = type === "array" && Array.isArray(value) && value.length
526-
let listCheck = type === "array" && Im.List.isList(value) && value.count()
526+
let arrayListCheck = type === "array" && Im.List.isList(value) && value.count()
527+
let arrayStringCheck = type === "array" && typeof value === "string" && value
527528
let fileCheck = type === "file" && value instanceof win.File
528529
let booleanCheck = type === "boolean" && (value || value === false)
529530
let numberCheck = type === "number" && (value || value === 0)
@@ -543,8 +544,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
543544
// }
544545

545546
const allChecks = [
546-
stringCheck, arrayCheck, listCheck, fileCheck, booleanCheck,
547-
numberCheck, integerCheck, objectCheck, objectStringCheck,
547+
stringCheck, arrayCheck, arrayListCheck, arrayStringCheck, fileCheck,
548+
booleanCheck, numberCheck, integerCheck, objectCheck, objectStringCheck,
548549
]
549550

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

608-
if ( !listCheck || !value.count() ) { return errors }
609+
if ( !arrayListCheck || !value.count() ) { return errors }
609610

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

test/mocha/core/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ describe("utils", function() {
602602
}
603603
value = []
604604
assertValidateParam(param, value, ["Required field is not provided"])
605+
606+
// invalid (empty) array, represented as a string
607+
param = {
608+
required: true,
609+
type: "array"
610+
}
611+
value = ""
612+
assertValidateParam(param, value, ["Required field is not provided"])
605613

606614
// invalid (not an array)
607615
param = {
@@ -629,6 +637,14 @@ describe("utils", function() {
629637
}
630638
value = [1]
631639
assertValidateParam(param, value, [])
640+
641+
// valid array, with no 'type' for items, represented as a string
642+
param = {
643+
required: true,
644+
type: "array"
645+
}
646+
value = "[1]"
647+
assertValidateParam(param, value, [])
632648

633649
// valid array, items match type
634650
param = {

0 commit comments

Comments
 (0)