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")