Spec-Zone.ru › Valibot

assert

Checks if the input matches the scheme.

As this is an assertion function, it can be used as a type guard.

v.assert<TSchema>(schema, input);

Generics

  • TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>

Parameters

  • schema TSchema
  • input unknown

Explanation

assert does not modify the input. Therefore, transformations have no effect and unknown keys of an object are not removed. That is why this approach is not as safe and powerful as parse and safeParse.

Example

The following example show how assert can be used.

const EmailSchema = v.pipe(v.string(), v.email());
const data: unknown = 'jane@example.com';

v.assert(EmailSchema, data);
const email = data; // string

Related

The following APIs can be combined with assert.

Schemas

any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void

Methods

assert, config, fallback, keyof, message, omit, partial, pick, pipe, required, unwrap

© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/assert/

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API