Parsing and Domain Validation

A parser can accept the spelling of a value while the surrounding application still rejects its meaning.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Test both stages
  2. Retain the domain invariant
  3. Follow accepted spelling into a business rule
  4. Check independent constraints at their own boundaries

Test both stages

Given "-4" in a seat count field, Python can parse an integer. A positive count rule then rejects it. The input "four" fails earlier because it has no accepted integer representation in that parser. The parsing boundary gives each failure an actionable description.

Retain the domain invariant

A valid seat count satisfying \(1\le n\le 12\) can be passed to the pricing routine. Tests for 0, 1, 12, and 13 verify both sides of both limits. This is more informative than checking only a typical value. See boundary test cases for building that set.[1][2]

Follow accepted spelling into a business rule

A room reservation form can accept a seat count from one through twelve inclusive. The input "9" parses as an integer and passes the range check. Text "nine" fails to parse. Text "0" parses successfully and then fails the allowed count. A clear diagnostic can identify whether readers should change a spelling or choose another quantity. See a small conversion boundary.

Check independent constraints at their own boundaries

An otherwise acceptable quantity can still exceed the available seats in the particular session. Checking that dynamic rule can require an up-to-date observation and a coordinated write. A function accepting a number between one and twelve can describe that restriction as a precondition while a separate booking operation owns the reservation. Test 0, 1, 12, and 13 for the static rule, plus one case where a valid requested quantity exceeds the currently available count.

In a larger input record, associate the failure with its field and source row. Keep a syntactically valid value long enough to explain which domain rule it violated. This helps callers display an error without needing to interpret a generic traceback.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩