14-nif-validate-locally

Validate the customer's tax ID before the network does.

14-nif-validate-locally

Validate the customer's tax ID before the network does.

Most invoicing SDKs send the NIF to the API, wait, and let the server decide. The round trip costs hundreds of milliseconds and a spinner on the user's screen. The result, more often than not, is "invalid NIF" — something I could have known locally.

The Portuguese NIF has a checksum. So does the Brazilian CPF. So do most national identifiers. The validation is a small modulo-11 calculation — microseconds on any machine.

In vendus-python I run that check before the API call. Three consequences:

→ Invalid input fails in microseconds, not in network round-trip latency.
→ The error message is precise — "checksum invalid, position 8" — instead of a vague gateway 400.
→ Fewer API calls in total. Smaller bill at scale.

This is the cheap-validation-before-expensive-validation pattern, and it works far beyond NIFs. IBAN, ISBN, MAC, UUID, JSON schemas. Anything that can fail locally should fail locally.

The principle in one line: validate cheap before validating expensive.

What's the most pointless network round trip in your codebase?

Repo: github.com/bilouro/vendus-python

P.S. New tech post every Wednesday.

#OpenSource #Python #SoftwareArchitecture

Comments