fix: accept string-represented values in required array runtime validation (#5609)

* rename `listCheck` -> `arrayListCheck`

* allow non-empty strings to quality a required array value
This commit is contained in:
kyle
2019-09-15 12:17:42 -07:00
committed by GitHub
parent 85f2bf3688
commit 00c8e964e7
2 changed files with 21 additions and 4 deletions

View File

@@ -523,7 +523,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
// These checks should evaluate to true if there is a parameter // These checks should evaluate to true if there is a parameter
let stringCheck = type === "string" && value let stringCheck = type === "string" && value
let arrayCheck = type === "array" && Array.isArray(value) && value.length 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 fileCheck = type === "file" && value instanceof win.File
let booleanCheck = type === "boolean" && (value || value === false) let booleanCheck = type === "boolean" && (value || value === false)
let numberCheck = type === "number" && (value || value === 0) let numberCheck = type === "number" && (value || value === 0)
@@ -543,8 +544,8 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
// } // }
const allChecks = [ const allChecks = [
stringCheck, arrayCheck, listCheck, fileCheck, booleanCheck, stringCheck, arrayCheck, arrayListCheck, arrayStringCheck, fileCheck,
numberCheck, integerCheck, objectCheck, objectStringCheck, booleanCheck, numberCheck, integerCheck, objectCheck, objectStringCheck,
] ]
const passedAnyCheck = allChecks.some(v => !!v) const passedAnyCheck = allChecks.some(v => !!v)
@@ -605,7 +606,7 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
} else if ( type === "array" ) { } else if ( type === "array" ) {
let itemType let itemType
if ( !listCheck || !value.count() ) { return errors } if ( !arrayListCheck || !value.count() ) { return errors }
itemType = paramDetails.getIn(["items", "type"]) itemType = paramDetails.getIn(["items", "type"])

View File

@@ -603,6 +603,14 @@ describe("utils", function() {
value = [] value = []
assertValidateParam(param, value, ["Required field is not provided"]) 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) // invalid (not an array)
param = { param = {
required: true, required: true,
@@ -630,6 +638,14 @@ describe("utils", function() {
value = [1] value = [1]
assertValidateParam(param, value, []) 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 // valid array, items match type
param = { param = {
required: true, required: true,