Converting Input Records to Typed Values
Translate incoming fields into domain values at one clear boundary so downstream code can use a consistent representation.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Keep distinct fields distinct
A row with account ID "007" and purchase count "007" can preserve the account as text and parse the count as integer seven. Representing both fields with the same type because their spellings agree can erase leading zeros from the account. Types and supported operations guide what each caller should expect.
Report the field that failed
If the count is "n/a", describe the failing row and field and state the expected value. Report invalid counts separately from missing rows. Parsing and domain validation distinguishes accepted spellings from business rules such as a maximum purchase size.[1][2]
Assign meaning before storing converted fields
Suppose a row contains "007,007,0.25". Treat the first field as a registration code preserving three characters. Parse the second as a count of seven and the third as a rate of one quarter if that is the file contract. Converting all fields to numbers changes the code. Leaving every field as text leaves a calculator able to join strings accidentally where it meant to multiply quantities. A record with named fields lets each representation have its own role.
Keep the rejected record traceable
A source row can structurally hold three fields and still contain a bad count or an out-of-range rate. Report the field and the rule violated without silently moving the values into another field or changing their meanings. A staged parser can attach the beginning line number before deciding how to handle errors.
Test a valid padded code, a malformed shape, a malformed number, and a numeric value outside the domain. When a value is missing, decide whether to preserve absence or request correction. The reader should be able to rely on those outcomes independently of the storage or output formatting used next.