Skip to content

Commit 2a2a3a6

Browse files
committed
feat(try-it-out): validation should consider readOnly
1 parent 9f62154 commit 2a2a3a6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/core/utils.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,16 @@ function validateValueBySchema(value, schema, requiredByParam, bypassRequiredChe
428428
let maxItems = schema.get("maxItems")
429429
let minItems = schema.get("minItems")
430430
let pattern = schema.get("pattern")
431+
const isReadOnly = schema.get("readOnly", false)
431432

432-
const schemaRequiresValue = requiredByParam || requiredBySchema === true
433+
const isRequired = !isReadOnly && (requiredByParam || requiredBySchema === true)
433434
const hasValue = value !== undefined && value !== null
434-
const isValidEmpty = !schemaRequiresValue && !hasValue
435+
const isValidEmpty = !isRequired && !hasValue
435436

436437
const needsExplicitConstraintValidation = hasValue && type === "array"
437438

438439
const requiresFurtherValidation =
439-
schemaRequiresValue
440+
isRequired
440441
|| needsExplicitConstraintValidation
441442
|| !isValidEmpty
442443

@@ -471,7 +472,7 @@ function validateValueBySchema(value, schema, requiredByParam, bypassRequiredChe
471472

472473
const passedAnyCheck = allChecks.some(v => !!v)
473474

474-
if (schemaRequiresValue && !passedAnyCheck && !bypassRequiredCheck) {
475+
if (isRequired && !passedAnyCheck && !bypassRequiredCheck) {
475476
errors.push("Required field is not provided")
476477
return errors
477478
}
@@ -491,7 +492,8 @@ function validateValueBySchema(value, schema, requiredByParam, bypassRequiredChe
491492
}
492493
if(schema && schema.has("required") && isFunc(requiredBySchema.isList) && requiredBySchema.isList()) {
493494
requiredBySchema.forEach(key => {
494-
if(objectVal[key] === undefined) {
495+
const propIsReadOnly = schema.getIn(["properties", key, "readOnly"], false)
496+
if(objectVal[key] === undefined && !propIsReadOnly) {
495497
errors.push({ propKey: key, error: "Required property not found" })
496498
}
497499
})

0 commit comments

Comments
 (0)