mini-effect
    Preparing search index...

    Function validate

    • Creates a curried validation function that checks a value against a Valibot Schema and returns an Effect.

      Type Parameters

      • const S extends Schema

        The Valibot schema type (inferred from the argument)

      Parameters

      • schema: S

        A Valibot schema (sync or async) to validate against

      Returns (
          value: unknown,
      ) => Effect<InferOutput<S>, Failure<"FailedToValidate">, never, never>

      A function (value: unknown) => Effect that validates value against schema and resolves with the inferred output type

      validate is a two-step (curried) function: the first call accepts a Valibot schema, and the second call accepts the value to validate. The returned Effect resolves with the validated and typed output on success, or fails with a FailedToValidate tagged error (an instance of FaildToValidateError) if validation fails. The underlying Valibot error is available on the cause property of the tagged error.

      import { run } from "mini-effect";
      import { validate, object, string, number } from "mini-effect/schema";

      const User = object({ name: string(), age: number() });

      const user = await run(validate(User)({ name: "Alice", age: 30 }));
      // user is typed as { name: string; age: number }
      import { pipe, catchTags, succeed, run } from "mini-effect";
      import { validate, string } from "mini-effect/schema";

      const result = await run(
      pipe(
      validate(string())(42),
      catchTags({
      FailedToValidate: (e) => succeed("invalid input"),
      }),
      ),
      );
      // => "invalid input"