Enforce: The Assertion Library for Vest
Enforce is a powerful assertion library that powers Vest's validations. It's designed to be:
- Fluent - Chain multiple assertions together naturally
- Composable - Build reusable validators from smaller pieces
- Extensible - Add your own custom rules
Basic Usage​
Import enforce from Vest and use it inside your tests:
import { enforce, test } from 'vest';
test('username', 'Must be at least three characters long', () => {
enforce(username).longerThan(2);
});
When an assertion fails, it throws an error that Vest catches and records as a failed test.
Fluent Chaining​
Chain multiple assertions together to test various conditions:
All assertions must pass for the test to pass. If any assertion fails, the test stops at that point.
Common Patterns​
Validating Strings​
// Required field
enforce(email).isNotBlank();
// Email format
enforce(email).isEmail();
// Length constraints
enforce(password).longerThanOrEquals(8).shorterThanOrEquals(128);
Validating Numbers​
// Range check
enforce(age).isNumber().greaterThanOrEquals(18).lessThan(120);
// Positive number
enforce(price).isPositive();
Validating Objects​
// Check shape/structure
enforce(user).shape({
name: enforce.isString(),
email: enforce.isEmail(),
age: enforce.isNumber(),
});
Composing Rules​
For rules you use together frequently, create reusable validators:
import { enforce, compose } from 'vest';
const isValidAge = compose(
enforce.isNumber(),
enforce.greaterThanOrEquals(18),
enforce.lessThan(120),
);
// Use like any other rule
test('age', 'Must be a valid age', () => {
enforce(data.age).condition(isValidAge);
});
Enforce rules are just functions. The compose utility lets you build complex validators from simple, testable pieces - exactly like function composition in FP.
Learn more about composing rules →
Available Rules​
Enforce comes with a rich set of built-in rules:
| Category | Examples |
|---|---|
| Type Checks | isString(), isNumber(), isBoolean(), isArray() |
| String Rules | isNotBlank(), isEmail(), matches(), startsWith() |
| Number Rules | greaterThan(), lessThan(), isPositive(), isNegative() |
| Comparison | equals(), notEquals(), inside(), notInside() |
| Collection | lengthEquals(), longerThan(), shorterThan(), isEmpty() |
| Shape | shape(), loose(), isArrayOf() |
Custom Rules​
Need validation logic that isn't built-in? Create your own:
import { enforce } from 'vest';
enforce.extend({
isValidUsername(value) {
return /^[a-zA-Z0-9_]+$/.test(value);
},
});
// Now use it anywhere
enforce(username).isValidUsername();
Learn more about custom rules →
Next Steps​
- All Built-in Rules - Complete reference
- Composing Rules - Build reusable validators
- Custom Rules - Extend with your own logic