Aller au contenu principal

ensureAsync()

ensureAsync<T>(schema, input): ResultAsync<T, string>

Parses a value against a Kanon schema and returns a Zygos ResultAsync.

Async variant of ensure — designed for ResultAsync chains where validation is one step in an async pipeline.


Type Parameters

T: T

The expected output type of the schema.


Parameters

schema: Schema<T>

Kanon schema to validate against.

input: unknown

Value to validate.


Returns: ResultAsync<T, string>

OkAsync<T> if validation succeeds, ErrAsync<string> otherwise.


Since

2.1.0


Example

import { ensureAsync } from "pithos/bridges/ensureAsync";
import { object, string, number } from "pithos/kanon";

safeFetch("/api/user")
.andThen(res => safeJson(res))
.andThen(data => ensureAsync(UserSchema, data))
.map(user => user.name);