Optional Fields in a Record

An omitted field and a present field carrying an empty value can be different states of a record.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Define the edit rule
  2. Test the unchanged record
  3. Represent absence according to the actual operation
  4. Document constraints that depend on several fields

Define the edit rule

A profile patch may leave the website untouched when omitted and remove it when present with null. Treat the two samples as distinct inputs. Sentinel values help represent omission during parsing.

Test the unchanged record

Start with a stored website, apply an empty patch, and assert that the website remains. Apply a null website next and assert that it clears. The mutation contract should also identify whether the operation updates the supplied record or returns a new one.[1]

Represent absence according to the actual operation

An update payload can omit an apartment number to leave the existing value untouched. An explicit null might clear that number. An empty string may be invalid, while the string zero can be an apartment label. If a parser replaces all three falsey or missing outcomes with one default, it destroys those distinctions. Test each case against a record with a previous value. See missing, empty and zero.

Document constraints that depend on several fields

A pickup order might omit a delivery address, while a delivery order requires one. Marking address optional in storage captures the available shapes but does not enforce the relationship to order type. Use a validator that can see both fields. Test a pickup with no address and a delivery with the same omission. If more order modes appear, distinguish missing data from an intentionally inapplicable field. See decision tables.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩