Skip to main content
eLearner.app
Module 8 · Lesson 3 of 431/32 in the course~15 min
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.

JS
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:

JS
function valida(utente, regole) {
  const errori = [];
  for (const regola of regole) {
    const msg = regola(utente);
    if (msg) errori.push(msg);
  }
  return errori;
}

Functional style:

JS
const valida = (obj, regole) => regole.map((r) => r(obj)).filter((m) => m !== null);

Deciding if it is valid

JS
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:

JS
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

Exercise#js.m8.l3.e1
Attempts: 0Loading…

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.

Loading editor…
Show hint

rules.map((r) => r(obj)).filter((m) => m !== null)

Solution available after 3 attempts

Review exercise

Exercise#js.m8.l3.e2
Attempts: 0Loading…

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'.

Loading editor…
Show hint

Return a closure that reads obj[field].

Solution available after 3 attempts