Aller au contenu principal

withValidation()

withValidation<S, Out>(schema, strategy): (input) => Result<Out, string>

Validates input against a kanon schema before executing the strategy. Bridges kanon validation, zygos Result, and the strategy pattern.


Type Parameters

S: S extends GenericSchema

The kanon schema type

Out: Out

The output type of the strategy


Parameters

schema: S

Kanon schema to validate input against

strategy: Strategy<Infer<S>, Out>

Strategy to execute on validated input


Returns

A function accepting unknown input and returning Result<Out, string>


Since

2.4.0


Example

import { number } from "@pithos/core/kanon";

const double = withValidation(
number().min(0),
(n) => n * 2,
);

double(5); // Ok(10)
double(-1); // Err("Number must be >= 0")
double("hello"); // Err("Expected number")