Methods
Apart from parse
and safeParse
I offer you some more methods to add additional features to your schemas. I distinguish in the following between schema and object methods.
Schema methods
My schema methods either add additional functionality, simplify the handling or help you to use a schema, for example for validation.
Schema methods: brand
, coerce
, coerceAsync
, fallback
, getDefault
, fallbackAsync
, is
, parse
, parseAsync
, safeParse
, safeParseAsync
, transform
, transformAsync
, unwrap
For more on methods for validation, see the parse data guide.
Coerce
coerce
and coerceAsync
can be used to transform an input before it is validated. They are therefore perfect for enforcing a specific data type using the built-in data type objects like String
, Number
and Date
.
import { coerce, date, number, parse, string } from 'valibot'; // 0.63 kB
const StringSchema = coerce(string(), String);
const stringOutput = parse(StringSchema, 1234); // '1234'
const NumberSchema = coerce(number(), Number);
const numberOutput = parse(NumberSchema, '1234'); // 1234
const DateSchema = coerce(date(), (i) => new Date(i));
const dateOutput = parse(DateSchema, '2023-07-31'); // Date
Transform
transform
and transformAsync
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 { type Input, type Output, parse, string, transform } from 'valibot'; // 0.58 kB
const StringSchema = transform(string(), (input) => input.length);
type StringInput = Input<typeof StringSchema>; // string
type StringOutput = Output<typeof StringSchema>; // number
const stringOutput = 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 { parse, fallback, string } from 'valibot'; // 0.59 kB
const StringSchema = fallback(string(), 'hello');
const stringOutput = 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.
Object methods: keyof
, merge
, mergeAsync
, omit
, omitAsync
, partial
, partialAsync
, passthrough
, passthroughAsync
, pick
, pickAsync
, required
, requiredAsync
, strict
, strictAsync
, strip
, stripAsync
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 { number, object, partial, pick, string } from 'valibot'; // 0.8 kB
// TypeScript
type Object1 = Partial<{ key1: string; key2: number }>;
// Valibot
const object1 = partial(object({ key1: string(), key2: number() }));
// TypeScript
type Object2 = Pick<Object1, 'key1'>;
// Valibot
const object2 = pick(object1, ['key1']);