Перейти до основного вмісту
eLearner.app
Модуль 8 · Урок 3 із 431/32 у курсі~15 min
Модульні уроки (3/4)

Завдання: валідатор даних

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

вправи#js.m8.l3.e1
Спроби: 0Завантаження…

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.

Завантаження редактора…
Показати підказку

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

Рішення доступне після 3 спроб

Review exercise

вправи#js.m8.l3.e2
Спроби: 0Завантаження…

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

Завантаження редактора…
Показати підказку

Return a closure that reads obj[field].

Рішення доступне після 3 спроб