Required and Optional Data Fields
A schema can state which named fields must be present and which optional ones can use a defined default or absent state.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Compare two records
If a reading requires timestamp and value and optionally has a comment, a row without comment may be valid. A row without a value needs rejection or a different declared schema. Review fixed record fields.
Distinguish absent and present-null
A required member can be present with null and still fail its value-type rule. A presence test alone cannot verify the full schema. JSON null and absent members provide a concrete representation for both states.[1]
Test omission separately from a present null value
A contact schema may require an address key while allowing its value to be null. Another schema may allow the key to be omitted but require a string whenever present. These policies accept different objects. A presence check cannot stand in for a value-type check. See optional fields in records.
Payload Required nullable Optional nonnull
{} reject accept
{"address":null} accept reject
{"address":"A"} accept accept
Document what defaults affect
A schema may specify what a missing field means in application logic. Filling a default does not prove that the incoming object had the member. Test serialization and validation separately where consumers need to know the difference.