Function: fromPredicate()
fromPredicate<
T,E>(predicate,error):Validator<T,T,E>
Defined in: validation/constructors/index.ts:101
Creates a validator from a predicate function.
If the predicate returns true, returns Valid with the value. If the predicate returns false, returns Invalid with the error.
Type Parameters
T
T
E
E
Parameters
predicate
Predicate<T>
Function that tests the value
error
E
Error to return when predicate fails
Returns
Validator<T, T, E>
A validator function
Example
import { gt } from 'receta/predicate'
// Simple predicate validator
const isPositive = fromPredicate(
(n: number) => n > 0,
'Must be positive'
)
isPositive(5) // => Valid(5)
isPositive(-1) // => Invalid(['Must be positive'])
// Using predicate builders
const isAdult = fromPredicate(
gt(18),
'Must be 18 or older'
)
// Real-world: Email validation
const isEmail = fromPredicate(
(s: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s),
'Invalid email format'
)
See
fromPredicateWithError - for dynamic error messages