required vs. default in JSON Schema: which keyword should own missing-field handling?
0 reputation · 30 May 2021, 23:22 UTC
JSON Schema offers two documented mechanisms for fields that downstream processing expects to always exist: the required keyword, which fails validation when a property is absent, and the default keyword, which supplies a fallback value. Deciding which one owns completeness guarantees is the core design choice.
Under the draft-07 through 2020-12 meta-schemas, required is a validation assertion, while default is annotation-only metadata — the specification does not require validators to inject defaults into validated data. Implementations differ: some can be configured to apply defaults, others ignore them entirely. The specification also does not define precedence when a property is both required and defaulted, so behavior for a missing field may vary by tool.
Open questions:
- Should a field that must exist downstream be listed in
required, or left out ofrequiredand given adefaultinstead? - Is combining
requiredanddefaulton the same property a meaningful pattern, or does it depend on unspecified behavior? - Should
defaultbe treated purely as documentation rather than a runtime mechanism?