Digital Nerve
Explained

CSV Validation Before Import: What a Schema Check Catches

· 5 min read

Importing a bad file into your accounting, stock or customer system is easy to do and hard to undo. A check before the import catches many structural problems while they are still cheap to fix. This guide explains what a rule-based CSV check can and cannot tell you, and how to set one up so that a failure actually stops the next step.

Agree on a Narrow Contract First

A checker can only enforce what you wrote down. Decide the dialect and the rules before you run anything: the file is UTF-8, comma-delimited, and has an invoice_id column and a total column. Totals use a dot for decimals, with no currency signs and no thousands separators. Those are choices for this exercise, not universal invoice requirements.

If your export uses semicolons, tab characters or decimal commas, do not force it through this contract. Build a documented conversion step, test it on known examples, and keep the original file. Python’s CSV documentation notes that CSV dialects differ, which is why a checker should use a real CSV parser rather than splitting each line on a comma: a quoted supplier name can itself contain commas.

What the Rules Can Cover

  • Exact, case-sensitive header names, and whether extra columns are allowed.
  • Required cells. A blank or whitespace-only cell fails when the column is required.
  • Types: text, integer, decimal and date. Dates must match one chosen format exactly: year-month-day, day-month-year or month-day-year.
  • Inclusive minimum and maximum values for numbers.
  • A list of permitted values, matched exactly and case-sensitively.
  • A maximum length for a field.
  • Uniqueness, including a combination of columns such as supplier plus invoice number.

The checker reads a copy of the file, compares every row with the rules and reports each finding with its record and line number. It does not fix anything and does not import anything.

A Worked Example

The rules for a fictional invoice file say that the invoice ID, supplier, total, date and status are required, that the total is a decimal from 0 to 1,000,000, that the status is OPEN or PAID, and that the supplier and invoice ID together must be unique.

The sample file has three rows, and the documented result is six findings: an invalid decimal and an invalid date in the first row; a value below the minimum, a status that is not allowed and a duplicate key in the second; and a blank required ID in the third. The tool reports exit status 1. A clean file reports exit status 0. The file itself is left byte-for-byte unchanged, which the tests check by comparing the source before and after.

Line numbers in the report come from the parser, so a quoted field that spans two physical lines can make the reported line differ from the row number your spreadsheet shows. Say which numbering your report uses, or the first argument about a finding will be about counting.

Exit Status Matters More Than the Report

An automation must check the exit status, not look for text in the output. Status 0 means the selected checks passed. Status 1 means the file was read and problems were found. Status 2 means the file or the configuration was not fully checked, for example because the file could not be read as UTF-8 or the row limit was exceeded.

A pipeline should stop on status 2 and not treat it as a pass. Printing a warning while the next command imports the file anyway is the failure mode this whole exercise is meant to prevent. Test it deliberately: point the import at a file that fails the check and confirm the import never starts.

What a Pass Does Not Mean

  • It does not prove an amount, a name or an identity is true.
  • It does not check tax rules, currencies or formulas.
  • It does not repair rows, detect near-duplicates or validate a calculation.
  • It does not prove the invoice exists or that the payment is authorized.

A passing report says the file matches that schema, not that the data is right. A finite 999.00 can still be the wrong amount.

What We Tested

Our own test suite covers valid input, missing and blank fields, invalid and non-finite decimals, duplicate headers, extra cells, a UTF-8 BOM and Arabic text, and it compares the source bytes before and after each check. Those tests ran locally on Linux, with Python 3.11 to 3.14. They are not an acceptance test on Windows, on Excel exports or on your supplier’s file format. Run the checker on a copy of your own export before you trust it with anything that matters.

Where It Fits

Copy the export, check the copy, log the corrections with a reviewer, rerun, and only then use your receiving system’s own approved import procedure. Keep the original and the report together, so that a question six months from now can be answered from the file rather than from memory.

Sources

Python documentation, csv module: docs.python.org/3/library/csv.html

Python documentation, decimal module: docs.python.org/3/library/decimal.html

Both read on 24 November 2026. Example data is invented.

Explained

Related articles