Module 2: Describe the file in a config
Goal
By the end of this module you can describe a fixed-width file and its duplicate rules in YAML, and get clear errors when the config is wrong.
The concept
Layout in config, not in code. A fixed-width file has no separators. Each field is a slice of the line, such as positions 13 to 29. If those numbers live in the code, every new file format means new code. If they live in a config, a new format is just a new YAML file.
A config has three parts:
fields: where each field starts, how long it is, and how to clean it (normalize: striptrims spaces,normalize: intturns0000393786into393786).filter: which records to check. In NACHA, only lines starting with6.duplicate_rules: which fields must match for two records to be duplicates.
Positions are 1-based. start: 1 is the first character, the same as in
the NACHA spec. Python counts from 0, so the engine converts the positions later. The config
stays readable next to the spec.
Validation in two stages.
- Shape. Pydantic checks that keys exist, types are right, values are allowed and there are no unknown keys.
- Meaning. Our own checks look at how the parts fit: fields fit inside the record, fields don't overlap, and rules only use fields that exist.
Stage 2 only runs once stage 1 passes. Within each stage, every problem is reported at once.
Code walkthrough
All the code is in src/echo_check/config.py.
- Models.
FieldSpec,RecordFilter,Tolerance,DuplicateRuleandLayoutConfigare small Pydantic classes. Each one matches a part of the YAML. StrictModel. All models inheritextra="forbid". A typo likenormalise: stripbecomes an error instead of being silently ignored.Normalize = Literal["none", "strip", "int"]. Pydantic rejects any other value.FieldSpec.end. A property that works out the last position, so no other code repeatsstart + length - 1.check_meaning. A Pydanticmodel_validatorthat runs after stage 1. It callsfind_problems, which calls one smallcheck_...function per idea. Each check returns a list of sentences.load_configandparse_config. These read the file and run validation. Every failure becomes oneConfigErrorwith a list of plain problems.describe_errorandformat_location. These turn Pydantic's raw errors into lines likefields[2] (account).normalize: Input should be 'none', 'strip' or 'int'.
Try it:
uv run pytest tests/test_config.py -v
uv run python -c "from echo_check.config import load_config; print(load_config('configs/nacha.yaml').field_by_name()['amount'])"
Exercise
Copy configs/nacha.yaml to data/broken.yaml (ignored by Git). Then make
three mistakes at once:
- change
amounttostart: 29, - rename
trace_numberin the rules totrace, - write
equals: 6without quotes.
Load it. Do you get all three problems in one error? Now add a fourth mistake,
normalise: strip on any field. Why do the other three problems disappear?
What can go wrong
- Off-by-one positions. If you copy positions from code that counts from 0,
every field is shifted by one.
start: 0is caught, but a shift of one inside the record isn't. Check one real line by hand. - Overlapping fields. Usually a wrong length. The error shows both ranges so you can see where they collide.
equals: 6vsequals: "6". YAML reads6as a number. A text field never equals a number, so the filter would skip every record. The config check catches this.- Silent typos. Without
extra="forbid", a misspelled key does nothing, and you only notice when results look wrong. - Fixing errors in rounds. Shape errors hide meaning errors. Fix the first round, then load again.