Validation
Criterion uses Zod for schema validation at every step.
What Gets Validated
- Input — Before rules are evaluated
- Profile — Before rules are evaluated
- Output — After a rule emits a result
Validation Flow
Input → Validate → Profile → Validate → Rules → Output → Validate → ResultIf any validation fails, the engine returns an error result instead of throwing.
Input Validation
typescript
const inputSchema = z.object({
amount: z.number().positive(),
currency: z.string().length(3),
customerId: z.string().uuid(),
});Invalid input returns:
typescript
{
status: "INVALID_INPUT",
data: null,
meta: {
explanation: "Input validation failed: amount must be positive"
}
}Profile Validation
typescript
const profileSchema = z.object({
threshold: z.number().min(0),
maxRetries: z.number().int().positive(),
});Output Validation
typescript
const outputSchema = z.object({
risk: z.enum(["HIGH", "MEDIUM", "LOW"]),
score: z.number().min(0).max(100),
});If a rule emits invalid output, you get INVALID_OUTPUT — this indicates a bug in your rule logic.
Best Practices
- Be specific — Use
.positive(),.min(),.max()to catch edge cases - Use enums —
z.enum()for finite sets of values - Document with descriptions —
z.string().describe("ISO currency code") - Validate early — Stricter schemas catch bugs faster