Cuarteto 9: Allegro | Brevedad y simplicidad

Cuando creé la biblioteca para la validación de datos quartet, tomé los siguientes puntos de referencia:



En este artículo mostraré cómo la biblioteca quartetentiende lo que significan las palabras "Corto" y "Simple" en el contexto del enfoque de validación.


¿Cómo funciona quartet?


Cuando se necesita Se necesita cuando se necesita validación de datos
¿Qué es validación? Verifique el cumplimiento de los datos. La mayoría de los tipos.
¿Cómo quartetayuda esto? Crea una función de validación basada en una descripción declarativa de requisitos: un esquema. La función del compilador vconvierte el circuito en una función de validación. Además v, los esquemas y métodos preparados se almacenan dentro para crear esquemas más complejos. Consulte la documentación para más detalles .


¿Cómo funciona?


  1. Escribes un esquema
  2. Se lo da a la entrada de la función del compilador vpara obtener la función de validación.
  3. ¡La función de validación está lista para usar!

Cuarteto validemos todos los datos sin forzar el cerebro en siete enfoques.


.



:


type Answer = 42;

quartet:


const answerSchema = 42;

- :


import { v } from "quartet";
const checkAnswer = v(42);

. — .



, . — .


. , , .


const checkNumber = (x) => typeof x === "number";
const checkString = (x) => typeof x === "string";
const checkBoolean = (x) => typeof x === "boolean";
const checkSymbol = (x) => typeof x === "symbol";
// ...

:


const checkSomeType = x => typeof x === "<type>",  <type> –   

, :


'string'v.string
'number'v.number
'boolean'v.boolean
'symbol'v.symbol

, :


const checkNumber = v(v.number);
//  , 
const checkNumber = (x) => typeof x === "number";

quartet


'function'v.function

:


const checkFunction = v(v.function);
//  , 
const checkFunction = (x) => typeof x === "function";

. C — v..


«»


:


type NullableString = string | null;

quartet:


const nullableStringSchema = [v.string, null];

:


const checkNullableString = v([v.string, null]);

:


type VariantSchema = Schema[];

. – .


«»



type Password =  string //     8-      1   .

. v.minLength() v.test(RegExp)


const stringSchema = v.string;
const min8Schema = v.minLength(8);
const atLeastOneDigitSchema = v.test(/\d/);
const atLeastOneLetterSchema = v.test(/[A-Za-z]/);

v.and():


const passwordSchema = v.and(
    stringSchema,
    min8Schema,
    atLeastOneDigitSchema,
    atLeastOneLetterSchema,
)

. v.and(, , ...).



Person:


interface Person {
    name: string
    age: number
}

, Person?
, name age. string number. !


, , .


— !


:


const personSchema = {
  name: v.string,
  age: v.number,
};

:


interface ObjectInterfaceSchema {
  [propertyName: string]: Schema;
}

. — , — , — .



:


type Numbers = number[];

v.arrayOf(). .


const checkNumbers = v(v.arrayOf(v.number));

. v.arrayOf( ).



quartet.


. є v.custom( ):


const evenSchema = v.custom((x) => x % 2 === 0)
const evenNumberSchema = v.and(
  v.number,
  evenSchema,
);

. v.custom( ).



  • — .
  • C — v..
  • — .
  • v.and(, , ...).
  • — , — , — .
  • v.arrayOf( ).
  • v.custom( ).

P.S.


. quartet :
. 4 , .


, !


All Articles