Master Data Gatekeeper
A legacy purchasing system exports vendor applications as one long text: records separated by |, and each record a list of KEY=VALUE fields separated by ;. Before the import job creates any vendors, the gatekeeper must answer one question: how many of these applications are actually importable? A record counts only when every mandatory field is present and passes its rule — everything else is quietly rejected.
Requirements
Create a codeunit named "Batch Validator" with one public procedure:
procedure CountValid(Batch: Text): IntegerCountValid returns the number of valid records in the batch. It never raises an error — an unreadable record is simply an invalid one. An empty or all-spaces Batch yields 0.
Reading the batch
- Records are separated by
|. A record that is empty or only spaces is skipped entirely — it is neither valid nor invalid. - Fields within a record are separated by
;. Field segments that are empty or only spaces are skipped. - Each field segment is cut at its first
=: the part before it is the key, and everything after it is the value. Both are trimmed of leading and trailing spaces; inner spaces are preserved. - A field segment that has no
=, or whose key is empty after trimming, makes the whole record invalid. - When the same key appears more than once in a record, the last occurrence is the one that gets validated.
- Keys are case-sensitive. Keys other than the four mandatory ones below are ignored — extra fields never invalidate a record.
The rules
A record is valid exactly when all four mandatory fields are present and each value passes its rule:
VAT— exactly 2 uppercase lettersA–Zfollowed by exactly 9 digits, for exampleDE123456789. Lowercase letters, a wrong length, or any other character fail.POSTCODE— exactly 5 digits. Leading zeros are fine:00123passes;123,123456, and12A45fail.CREDIT— digits only, and its numeric value is between 500 and 20000 inclusive. A sign, a thousands separator, or decimals fail (-500,1,500,1500.00); leading zeros are fine (00500is 500).CURRENCY— exactlyEUR,USD, orGBP, case-sensitive (eurandEUROfail).
What the tests check
The tests call CountValid and check every rule above: a fully valid record; each mandatory field missing in turn; extra unknown fields being ignored; a catalogue of bad VAT, POSTCODE, and CURRENCY values; the CREDIT boundaries 499/500/20000/20001; leading zeros in POSTCODE and CREDIT; spaces around keys and values; blank records and blank field segments; last-occurrence-wins for duplicate keys; a record invalidated by a segment without = and by an empty key; and a randomized batch where only the generated valid records may be counted. One record carries a 30-digit CREDIT — it is merely out of range, and your code must reject it without crashing.