Module lessons (3/4)
Challenge: data validator
Validating input data is one of the things you will do most often: no form, no API can blindly trust what it receives. Let's build a small validator that combines composable rules and returns all the errors found, not just the first one.
One rule = one function
A rule takes the object and returns null (all good) or a string with the error message.
const nomeRichiesto = (utente) =>
utente.nome && utente.nome.length > 0 ? null : 'nome obbligatorio';
const etaMaggiore = (utente) => (utente.eta >= 18 ? null : 'devi essere maggiorenne');Composing multiple rules
The validator runs all the rules and collects the errors:
function valida(utente, regole) {
const errori = [];
for (const regola of regole) {
const msg = regola(utente);
if (msg) errori.push(msg);
}
return errori;
}Functional style:
const valida = (obj, regole) => regole.map((r) => r(obj)).filter((m) => m !== null);Deciding if it is valid
const errori = valida(utente, [nomeRichiesto, etaMaggiore]);
const ok = errori.length === 0;Useful pattern: rule factory
When you have many fields to validate with the same logic, write a factory:
const richiesto = (campo) => (obj) =>
obj[campo] != null && obj[campo] !== '' ? null : `${campo} obbligatorio`;
const minimo = (campo, n) => (obj) => (obj[campo] >= n ? null : `${campo} deve essere >= ${n}`);
const regole = [richiesto('nome'), richiesto('email'), minimo('eta', 18)];Each factory returns a pre-configured rule function. It is exactly like using a parser generator or a validator such as Zod, just in miniature.
Try it
Define `validate(obj, rules)`: apply each rule (function obj -> string|null) and return an array containing only the non-null messages, in the same order as the rules.
Show hint
rules.map((r) => r(obj)).filter((m) => m !== null)
Solution available after 3 attempts
Review exercise
Define the factory `required(field)`: it returns a rule that, given an object, returns null if obj[field] is non-empty (not null, not undefined, not an empty string), otherwise the string '<field> obbligatorio'.
Show hint
Return a closure that reads obj[field].
Solution available after 3 attempts