Methods

Apart from parse, safeParse and is I offer some more methods to add additional features to your schemas. I distinguish in the following between schema, object and pipeline methods.

Schema methods

My schema methods either add additional functionality, simplify the handling or help you to use a schema, for example for validation.

For more on methods for validation, see the parse data guide.

Coerce

coerce can be used to transform an input before it is validated. It is therefore perfect for enforcing a specific data type using the built-in data type objects like Boolean, String, Number and Date.

import * as v from 'valibot';

const BooleanSchema = v.coerce(v.boolean(), Boolean);
const booleanOutput = v.parse(BooleanSchema, null); // false

const StringSchema = v.coerce(v.string(), String);
const stringOutput = v.parse(StringSchema, 1234); // '1234'

const NumberSchema = v.coerce(v.number(), Number);
const numberOutput = v.parse(NumberSchema, '1234'); // 1234

const DateSchema = v.coerce(v.date(), (i) => new Date(i));
const dateOutput = v.parse(DateSchema, '2023-07-31'); // Date

Transform

transform can be used to transform data after validation. Compared to the transformations you can perform inside pipelines, there are no rules here. So the input type can be completely different from the output type.

import * as v from 'valibot';

const StringSchema = v.transform(v.string(), (input) => input.length);

type StringInput = v.Input<typeof StringSchema>; // string
type StringOutput = v.Output<typeof StringSchema>; // number

const stringOutput = v.parse(StringSchema, 'hello'); // 5

Fallback

If an issue occurs while validating your schema, you can catch it with fallback to return a predefined value instead.

import * as v from 'valibot';

const StringSchema = v.fallback(v.string(), 'hello');
const stringOutput = v.parse(StringSchema, 123); // 'hello'

Object methods

My object methods make it easier for you to work with object schemas. They are strongly oriented towards the functionality of TypeScript.

TypeScript similarities

I offer almost the same options as TypeScript. For example, you can make the values of an object optional with partial or make them required with required. With merge, you can join multiple object schemas and with pick or omit, you can include or exclude certain values of an existing schema.

import * as v from 'valibot';

// TypeScript
type Object1 = Partial<{ key1: string; key2: number }>;

// Valibot
const object1 = v.partial(v.object({ key1: v.string(), key2: v.number() }));

// TypeScript
type Object2 = Pick<Object1, 'key1'>;

// Valibot
const object2 = v.pick(object1, ['key1']);

Pipeline methods

My pipeline methods help you to modify the results of validations and transformations within a pipeline.

For more infos about our pipeline feature, see the pipelines guide.

Forward

forward allows you to associate an issue with a nested schema. For example, if you want to check that both password entries in a registration form match, you can use it to forward the issue to the second password field in case of an error. This allows you to display the error message in the correct place.

import * as v from 'valibot';

const RegisterSchema = v.object(
  {
    email: v.string([
      v.minLength(1, 'Please enter your email.'),
      v.email('The email address is badly formatted.'),
    ]),
    password1: v.string([
      v.minLength(1, 'Please enter your password.'),
      v.minLength(8, 'Your password must have 8 characters or more.'),
    ]),
    password2: v.string(),
  },
  [
    v.forward(
      v.custom(
        (input) => input.password1 === input.password2,
        'The two passwords do not match.'
      ),
      ['password2']
    ),
  ]
);