Composing Enforce Rules
You can combine multiple enforce rules into a single reusable validator using compose.
Why Compose?​
Sometimes you have a set of rules that always go together. For example, a "valid age" might always need to be:
- A number
- At least 18
- Less than 120
Instead of repeating these three checks every time, you can compose them into a single isValidAge rule. This promotes reuse and keeps your schemas clean.
import { enforce, compose } from 'vest';
const isValidAge = compose(
enforce.isNumber(),
enforce.greaterThanOrEquals(18),
enforce.lessThan(120),
);
// Usage
isValidAge.run(20); // { pass: true }
isValidAge.run(15); // { pass: false }
// Inside a schema
const userSchema = enforce.shape({
age: isValidAge,
});
Composed rules act just like standard enforce rules and can be used for type guards, schema validation, or standalone assertions.