Description
I will recognize at the outset this is change would be a significant backwards incompatibility. I want to at least bring it for discussion.
I think the keyword "required" has a poor name for its purpose. All of the validation keywords express requirements of one sort or another. required
should be named for what it is actually requiring - the presence of the indicated properties. I think "present" is the best keyword to express this. some variant of "properties present" might be an option too.
The current name is okay for positively validating - saying a property is required does imply that its presence is the requirement. That case is natural enough to read and make intuitive sense. But we have all seen the confusion that results when using it in conditional or negated schemas.
if:
required:
- foo
then:
...
"If property foo
is required, then ..." is how that reads, but what it means is not very intuitive. "If property foo
is present" is much more applicable, and reads more naturally from if: {present: ["foo"]}
.
Likewise, not: {required: ["foo"]}
does not mean "property foo
is not required". Multiple properties are worse: not: {required: ["foo", "bar"]}
reads like "neither foo
nor bar
is required". Much improved, not: {present: ["foo", "bar"]}
does assert "properties foo
and bar
are not present" or "must not be present".
I think required
made more sense back when it was a boolean keyword of a property schema, but would have been better renamed when it was put in its right place as a sibling of properties
. I think the name present
is a significant improvement - I'm interested to hear people's opinions.
Of course, there are countless schemas which use required
. This would be a widely impactful change, though not the biggest (e.g. smaller than id
/ $id
, I think). There are tools to convert schemas between spec versions, and this would be a trivial conversion to implement. Maybe required
could live on for a while as a deprecated alias with the same function as present
(though I don't think the spec has ever done that before and that may be a whole other issue to address).
I think in the long run, though this change would have some pain in its transition, it is worth the benefit of more clearly expressing the intent and meaning of the keyword in all the contexts where it is used.