fix: optional empty validation (#7003)
internal logic does send null to validation in case of empty parameter input. * refactor: early return to clarify cases
This commit is contained in:
@@ -432,11 +432,11 @@ export const validatePattern = (val, rxPattern) => {
|
||||
}
|
||||
}
|
||||
|
||||
function validateValueBySchema(value, schema, isParamRequired, bypassRequiredCheck, parameterContentMediaType) {
|
||||
function validateValueBySchema(value, schema, requiredByParam, bypassRequiredCheck, parameterContentMediaType) {
|
||||
if(!schema) return []
|
||||
let errors = []
|
||||
let nullable = schema.get("nullable")
|
||||
let required = schema.get("required")
|
||||
let requiredBySchema = schema.get("required")
|
||||
let maximum = schema.get("maximum")
|
||||
let minimum = schema.get("minimum")
|
||||
let type = schema.get("type")
|
||||
@@ -448,18 +448,29 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
|
||||
let minItems = schema.get("minItems")
|
||||
let pattern = schema.get("pattern")
|
||||
|
||||
if(nullable && value === null) {
|
||||
const needsExplicitConstraintValidation = type === "array"
|
||||
const schemaRequiresValue = requiredByParam || requiredBySchema
|
||||
const hasValue = value !== undefined && value !== null
|
||||
const isValidEmpty = !schemaRequiresValue && !hasValue
|
||||
|
||||
const requiresFurtherValidation =
|
||||
schemaRequiresValue
|
||||
|| needsExplicitConstraintValidation
|
||||
|| !isValidEmpty
|
||||
|
||||
const isValidNullable = nullable && value === null
|
||||
|
||||
// will not be included in the request or [schema / value] does not [allow / require] further analysis.
|
||||
const noFurtherValidationNeeded =
|
||||
isValidNullable
|
||||
|| !type
|
||||
|| !requiresFurtherValidation
|
||||
|
||||
if(noFurtherValidationNeeded) {
|
||||
return []
|
||||
}
|
||||
|
||||
/*
|
||||
If the parameter is required OR the parameter has a value (meaning optional, but filled in)
|
||||
then we should do our validation routine.
|
||||
Only bother validating the parameter if the type was specified.
|
||||
in case of array an empty value needs validation too because constrains can be set to require minItems
|
||||
*/
|
||||
if (type && (isParamRequired || required || value !== undefined || type === "array")) {
|
||||
// These checks should evaluate to true if there is a parameter
|
||||
// Further this point the parameter is considered worth to validate
|
||||
let stringCheck = type === "string" && value
|
||||
let arrayCheck = type === "array" && Array.isArray(value) && value.length
|
||||
let arrayListCheck = type === "array" && Im.List.isList(value) && value.count()
|
||||
@@ -478,7 +489,7 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
|
||||
|
||||
const passedAnyCheck = allChecks.some(v => !!v)
|
||||
|
||||
if ((isParamRequired || required) && !passedAnyCheck && !bypassRequiredCheck) {
|
||||
if (schemaRequiresValue && !passedAnyCheck && !bypassRequiredCheck) {
|
||||
errors.push("Required field is not provided")
|
||||
return errors
|
||||
}
|
||||
@@ -496,8 +507,8 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
|
||||
return errors
|
||||
}
|
||||
}
|
||||
if(schema && schema.has("required") && isFunc(required.isList) && required.isList()) {
|
||||
required.forEach(key => {
|
||||
if(schema && schema.has("required") && isFunc(requiredBySchema.isList) && requiredBySchema.isList()) {
|
||||
requiredBySchema.forEach(key => {
|
||||
if(objectVal[key] === undefined) {
|
||||
errors.push({ propKey: key, error: "Required property not found" })
|
||||
}
|
||||
@@ -597,7 +608,6 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
|
||||
if (!err) return errors
|
||||
errors.push(err)
|
||||
}
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user