Skip to content

Commit 1590dc9

Browse files
committed
test(validation): test readOnly + required behavior
1 parent 2a2a3a6 commit 1590dc9

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

test/unit/core/utils.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,108 @@ describe("utils", () => {
608608
assertValidateOas3Param(param, value, [])
609609
})
610610

611+
it("bypass required check if readOnly", () => {
612+
// valid any because readOnly true
613+
param = {
614+
schema: {
615+
type: "string",
616+
readOnly: true
617+
}
618+
}
619+
value = "test"
620+
assertValidateOas3Param(param, value, [])
621+
622+
// should validate if provided never the less
623+
param = {
624+
schema: {
625+
type: "string",
626+
minLength: 10,
627+
readOnly: true
628+
}
629+
}
630+
value = "123"
631+
assertValidateOas3Param(param, value, [`Value must be at least ${param.schema.minLength} characters`])
632+
633+
// valid undefined because of bypass required: true if readOnly true
634+
param = {
635+
schema: {
636+
required: true,
637+
type: "string",
638+
readOnly: true
639+
}
640+
}
641+
value = undefined
642+
assertValidateOas3Param(param, value, [])
643+
644+
// invalid undefined because required: true if readOnly false
645+
param = {
646+
schema: {
647+
required: true,
648+
type: "string",
649+
readOnly: false
650+
}
651+
}
652+
value = undefined
653+
assertValidateOas3Param(param, value, ["Required field is not provided"])
654+
655+
// invalid undefined because required: true if readOnly undefined
656+
param = {
657+
schema: {
658+
required: true,
659+
type: "string"
660+
}
661+
}
662+
value = undefined
663+
assertValidateOas3Param(param, value, ["Required field is not provided"])
664+
665+
// valid undefined property if required: [key] because properties.key.readOnly true
666+
param = {
667+
schema: {
668+
required: ["probe"],
669+
type: "object",
670+
properties: {
671+
probe: {
672+
type: "string",
673+
readOnly: true
674+
}
675+
}
676+
}
677+
}
678+
value = {}
679+
assertValidateOas3Param(param, value, [])
680+
681+
// invalid undefined property if required: [key] because properties.key.readOnly false
682+
param = {
683+
schema: {
684+
required: ["probe"],
685+
type: "object",
686+
properties: {
687+
probe: {
688+
type: "string",
689+
readOnly: false
690+
}
691+
}
692+
}
693+
}
694+
value = {}
695+
assertValidateOas3Param(param, value, [{ propKey: "probe", error: "Required property not found" }])
696+
697+
// invalid undefined property if required: [key] because properties.key.readOnly undefined
698+
param = {
699+
schema: {
700+
required: ["probe"],
701+
type: "object",
702+
properties: {
703+
probe: {
704+
type: "string"
705+
}
706+
}
707+
}
708+
}
709+
value = {}
710+
assertValidateOas3Param(param, value, [{ propKey: "probe", error: "Required property not found" }])
711+
})
712+
611713
it("validates required strings with min and max length", () => {
612714
// invalid string with max length
613715
param = {

0 commit comments

Comments
 (0)