Skip to content

pipe ​

Adds rules and transforms to a schema. They run in order after the type check.

Signature ​

ts
function pipe<T>(schema: Schema<T>, ...actions: Action<T>[]): Schema<T>

Parameters ​

ParameterTypeDescription
schemaSchema<T>The base schema: string(), number(), array(...)…
actionsAction<T>[]Rules and transforms

Execution order ​

  1. The base schema is checked. On error the rules don't run.
  2. Rules and transforms run left to right. Each one gets the result of the previous: pipe(string(), trim(), minLength(2)) checks the length of the trimmed string.
  3. Execution stops at the first error. With the { all: true } option of validate all rules are checked.
  4. Async rules (checkAsync) run last and only if the others passed.

What you can pass ​

KindActions
LengthminLength, maxLength, length
Numbersmin, max, between, integer
Formatpattern, email, url, uuid, startsWith, endsWith, includes
DatesminDate, maxDate, after, before
Arraysunique
Checksumsinn, snils, luhn
ComparisonsameAs
Transformstrim, toLowerCase, toUpperCase
Customcheck, checkAsync

TypeScript checks that a rule matches the schema type: pipe(string(), min(1)) is a compile error.

Transforms inside pipe keep the type. To get a value of another type, use transform.

Examples ​

Username ​

ts
const username = pipe(
  string(),
  trim(),
  toLowerCase(),
  minLength(3),
  maxLength(20),
  pattern(/^[a-z0-9_]+$/, 'Only latin letters, digits and _'),
);

With optional ​

ts
// optional goes outside pipe
const phone = optional(pipe(string(), pattern(/^\+1\d{10}$/)));

See also ​