Skip to content

Commit

Permalink
feat: validate object deep
Browse files Browse the repository at this point in the history
Signed-off-by: mathis-m <mathis.michel@outlook.de>
  • Loading branch information
mathis-m committed Jan 31, 2021
1 parent 815d133 commit 5265a22
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
31 changes: 28 additions & 3 deletions src/core/json-schema-components.jsx
Expand Up @@ -345,6 +345,31 @@ export class JsonSchema_boolean extends Component {
}
}

const stringifyObjectErrors = (errors) => {
return errors.map(err => {
const meta = err.propKey !== undefined ? err.propKey : err.index
let stringError = typeof err === "string" ? err : typeof err.error === "string" ? err.error : null

if(!meta && stringError) {
return stringError
}
let currentError = err.error
let path = `/${err.propKey}`
while(typeof currentError === "object") {
const part = currentError.propKey !== undefined ? currentError.propKey : currentError.index
if(part === undefined) {
break
}
path += `/${part}`
if (!currentError.error) {
break
}
currentError = currentError.error
}
return `${path}: ${currentError}`
})
}

export class JsonSchema_object extends PureComponent {
constructor() {
super()
Expand Down Expand Up @@ -372,18 +397,18 @@ export class JsonSchema_object extends PureComponent {
} = this.props

const TextArea = getComponent("TextArea")
errors = errors.toJS ? errors.toJS() : Array.isArray(errors) ? errors : []

return (
<div>
<TextArea
className={cx({ invalid: errors.size })}
title={ errors.size ? errors.join(", ") : ""}
className={cx({ invalid: errors.length })}
title={ errors.length ? stringifyObjectErrors(errors).join(", ") : ""}
value={stringify(value)}
disabled={disabled}
onChange={ this.handleOnChange }/>
</div>
)

}
}

Expand Down
31 changes: 23 additions & 8 deletions src/core/utils.js
Expand Up @@ -453,7 +453,7 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
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 || type === "array" && !value)) {
if (type && (isParamRequired || required || value !== undefined || type === "array")) {
// 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
Expand All @@ -477,18 +477,33 @@ function validateValueBySchema(value, schema, isParamRequired, bypassRequiredChe
errors.push("Required field is not provided")
return errors
}

if (
type === "object" &&
typeof value === "string" &&
(parameterContentMediaType === null ||
parameterContentMediaType === "application/json")
) {
try {
JSON.parse(value)
} catch (e) {
errors.push("Parameter string value must be valid JSON")
return errors
let objectVal = value
if(typeof value === "string") {
try {
objectVal = JSON.parse(value)
} catch (e) {
errors.push("Parameter string value must be valid JSON")
return errors
}
}
if(schema && schema.has("required") && isFunc(required.isList) && required.isList()) {
required.forEach(key => {
if(objectVal[key] === undefined) {
errors.push({ propKey: key, error: "Required property not found" })
}
})
}
if(schema && schema.has("properties")) {
schema.get("properties").forEach((val, key) => {
const errs = validateValueBySchema(objectVal[key], val, false, bypassRequiredCheck, parameterContentMediaType)
errors.push(...errs
.map((error) => ({ propKey: key, error })))
})
}
}

Expand Down

0 comments on commit 5265a22

Please sign in to comment.