AI invoice reader project (TypeScript) · Chapter 5 of 12

Validation rules

By the end of this chapter every invoice the model reads is checked against the rules an invoice must obey: each line multiplies out, the lines and taxes add up to the total, the GSTIN's check character fits and the date is believable. Each broken rule comes back as a problem with its field and a sentence, and no rule ever changes a number. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Checking the model
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Of Vaishali's 11 mistakes in September, three were of one kind: ₹48,600 typed as ₹46,800, a GSTIN typo that would have lost the GST input credit on Kavach Security's ₹13,593.60 of IGST, and a date typed month first. The model makes the same kind of mistakes, and chapter 4's POST /invoices/extract answers 200 for all of them. Here is what it read from Swarnim Sound's swarnim-ssl-2207.pdf, which prints Date: 01/10/2026, a plain Total 31,100.00 before the taxes and a Grand Total 36,698.00 after them (the fields that matter, from its answer):

{"supplier_gstin":"27ABFPS4418K1ZB","invoice_date":"2026-01-10","subtotal":31100,"cgst":2799,"sgst":2799,"igst":null,"total":31100}

The date is read month first (10 January), and the total is the plain Total, ₹5,598 of GST short. Models really do both. Two more from the inbox: Joshi Farsan's joshi-jfb-4459.pdf prints line 3, Chakli, as 50 packets at ₹120.00 for ₹6,500.00, and Kavach's kavach-kss-1017.pdf prints the GSTIN 29AAHFK7812P1ZK, which is not a valid GSTIN. The model copies both faithfully, because they are printed.

If Vaishali has to check every field herself, the reader saves her nothing.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    rules.ts: do the amounts add up?

    In src/validate/rules.ts write checkLines(inv): for each line i (from 0) whose quantity times rate, in whole paise (Math.round(quantity * rate * 100)), differs from paise(amount) by more than config.tolerancePaise (100: Rs 1.00), a Problem { field: line_items[${i}].amount, code: 'line_amount', message }. Then checkTotals(inv): the lines' amounts added up in paise; when there is a subtotal and they differ from it, { field: 'subtotal', code: 'subtotal', message }; when the subtotal (or, with none, the lines' sum) plus cgst, sgst and igst (null as 0) differs from the total, { field: 'total', code: 'total', message }. Each message is a sentence with the amounts in it (rupees() shows paise as ₹1,23,456.50).

    The Check runs both on eleven invoices: a good one, a wrong line, 75 paise off (fine), Rs 1.50 off, a wrong subtotal, a total without the taxes, no subtotal, IGST, a bill of supply with no taxes, 2000 x 4.80.

  2. Task 2

    gstin.ts: the check character and look-alikes

    In src/validate/gstin.ts write checkChar(first14): each character's value is its index in CHARS; multiply the values by 1, 2, 1, 2, ... (1 at index 0), add up Math.floor(product / 36) + product % 36 for each, and return CHARS[(36 - sum % 36) % 36]. Then gstinProblem(gstin): null when SHAPE matches it and it ends in checkChar of its first 14; otherwise a sentence (the wrong shape, or what it ends in and what its check character should be). Then repairGstin(raw): spaces and dashes removed, upper case; if that is not 15 characters or is already valid, return it; else swap look-alikes (AS_DIGIT at DIGITS_AT, AS_LETTER at LETTERS_AT) and return the swapped one only if it is valid, the tidied one otherwise.

    The Check works out the check character of every vendor's GSTIN, tries four bad ones and eight repairs.

  3. Task 3 · interview question

    Interview: rules.ts, every rule in order

    Write checkDates(inv, today): today is ISO text; an invoice_date after today gives [{ field: 'invoice_date', code: 'date_future', message }]; more than config.maxAgeDays (120) days before today gives [{ field: 'invoice_date', code: 'date_old', message }]; otherwise []. daysBetween (given) counts the days. Then checkInvoice(inv, today = config.today): when there is a supplier_gstin, a copy of the invoice with it repaired ({ ...inv, supplier_gstin: repairGstin(...) }); return { invoice, problems }: the problems of checkGstin, checkDates, checkLines and checkTotals, in that order.

    The Check tries four dates against 31 October, then a messy invoice (a spaced lower-case GSTIN, a future date, a wrong line and a wrong total), then a GSTIN with a typo with today left out, then POST /invoices/validate for joshi-jfb-4459.pdf (its line 3 says 50 x 120 = 6,500).

    Nikhil will ask: line 3 says 50 x 120 = 6,500. Your code knows the right amount is 6,000. Why not just set it?

What you will learn

  • A problem, not an exception: a rule returns an array of Problems.
  • Money in paise: why 0.1 + 0.2 is not 0.3, and why a tolerance of Rs 1.00.
  • Lines and totals: quantity times rate; lines plus taxes make the total.
  • The GSTIN: its shape, its check character, and repairing only characters that look alike.
  • Dates: not after today, not more than 120 days before it.
  • Why a rule never fixes a number.

More in Checking the model

Build "Validation rules" yourself

Parda's venues get invoices from sound, printing and canteen vendors, typed and messy. You build the pipeline that reads them, checks every rupee, and sends the doubtful ones to a person. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.