Data and text · Medium · 25 min

Strip personal data before the prompt leaves

DNI, NIE and IBAN carry a check digit. Swap them for placeholders before calling a model and put them back in the answer.

MESSAGE is an email from a customer (made up). PII is a global regular expression with five named groups: iban, nie, dni, email and tel. It finds everything shaped like those data; deciding whether they are the real thing is up to you. Write five functions:

  1. validDni(s): eight digits and a capital letter, and the letter is DNI_LETTERS[number % 23].
  2. validNie(s): X, Y or Z, seven digits and a letter. Replace the initial with 0, 1 or 2 and apply the DNI rule.
  3. validIban(s): remove the spaces; it must be ES and 22 digits. Move the first four characters to the end, write E as 14 and S as 28, and the remainder of that number divided by 97 must be 1. The number does not fit in a Number: go digit by digit with rem = (rem * 10 + digit) % 97.
  4. redact(text): return { text, found }. Replace each item with [TYPE_n] (DNI, NIE, IBAN, TEL, EMAIL), numbering each type in order of appearance. A DNI, NIE or IBAN that fails its check stays as it is: it is an order number or a typo. The same value, spaces aside, always gets the same placeholder. found lists { type, value, placeholder } in order of first appearance, with the value as it was written.
  5. restore(text, found): return the text with every placeholder replaced by its value, each time it appears.

Proper names have no format and this function leaves them alone; that takes the entities exercise.

Challenges 0/6

  • validDni checks the letter, not only the shape
  • validNie swaps X, Y and Z for 0, 1 and 2
  • validIban works out the remainder by 97, with or without spaces
  • redact replaces what is valid and leaves the order number and the fake account
  • found lists each item once, with its placeholder
  • restore puts the values back into the model's answer

function validDni(s) {
  // 8 digits and a capital letter; the letter must be DNI_LETTERS[number % 23]
  return /^\d{8}[A-Z]$/.test(s);
}

function validNie(s) {
  // X, Y or Z count as 0, 1 or 2 in front of the 7 digits; then the DNI rule
  return /^[XYZ]\d{7}[A-Z]$/.test(s);
}

function validIban(s) {
  // no spaces, ES + 22 digits; first 4 characters to the end, E as 14 and S as 28,
  // then rem = (rem * 10 + digit) % 97 over every digit must end at 1
  return /^ES\d{22}$/.test(s.replace(/\s+/g, ''));
}

function redact(text) {
  // text.replace(PII, (match, ...rest) => { const groups = rest[rest.length - 1]; ... })
  // a DNI, NIE or IBAN that fails its check stays; same value (spaces aside) -> same placeholder
  return { text: text.replace(PII, '[PII]'), found: [] };
}

function restore(text, found) {
  // put every value back in place of its placeholder
  return text;
}

console.log(redact(MESSAGE).text);

Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.

Console output appears here (console.log).

Why this matters

Even when your model is local, prompts end up in logs, in debugging traces and in evaluation sets that someone copies to another machine. And if part of the work goes out to an external model, what leaves should not carry anyone's national ID or bank account. Replacing with placeholders before the call and undoing the change afterwards lets the model work the same while the data does not travel. The check digits stop you from covering what is not personal data. This is data minimisation in the GDPR sense, which we explain in Article 25: privacy by design with local AI.

Go deeper: the related guide →

This in production, with your data? Let's talk for 15 minutes →