דילוג לתוכן המרכזי
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 ניסיונות