Validation
Overview
mobx-formkit ships with a pluggable validation system. You can choose one or more validation backends (called drivers) and attach them to the form at initialization. Each driver is responsible for validating each field and pushing error messages into the field's validationErrorStack.
Validation can be triggered:
- Automatically — on field blur, change, form init, submit, clear, reset (configurable via options)
- Manually — via
form.validate(),field.validate(), orform.submit()
For a deeper dive into the validation lifecycle (what happens when, in what order), see the Validation Lifecycle page.
Choosing a Validation Plugin
| Style | Driver | Best For | Example |
|---|---|---|---|
| Custom functions | VJF | Full control, custom logic, server checks | ({ field }) => [field.value.length > 3, 'Too short'] |
| Declarative rules | DVR | Simple string-based rules (like Laravel) | 'required or email or min:5' |
| JSON Schema | AJV | JSON-first projects, API compatibility | { type: 'string', minLength: 3 } |
| Object schema | YUP | Modern JS/TS, chainable API | y.string().required().min(3) |
| Object schema | JOI | Enterprise-grade, rich error messages | j.string().min(3).required() |
| TypeScript schema | ZOD | TypeScript-first, type inference | z.string().min(3) |
| TypeScript schema | VALIBOT | Tiny TS-first schemas, sync only | v.pipe(v.string(), v.minLength(3)) |
| TypeScript schema | VINEJS | Sync rules, async validation API | vine.string().minLength(3) |
When to use what
| Use Case | Recommended |
|---|---|
| Simple forms with basic rules | DVR — fastest to write |
| Full control over validation logic | VJF — write any function |
| JSON Schema compliance (API validation) | AJV — standards-based |
| Modern object-oriented validation | YUP — popular, good DX |
| Enterprise / complex schemas | JOI — battle-tested, rich errors |
| TypeScript-native with infer | ZOD — best TS integration |
| Tiny TypeScript-first schemas (sync) | VALIBOT — smallest bundle, sync pipes |
| Sync rules with async API | VINEJS — promise-based validation |
Can I use multiple plugins?
Yes. You can enable multiple plugins simultaneously. Validation runs through all enabled drivers in sequence (configurable via validationPluginsOrder). If stopValidationOnError is true, subsequent drivers are skipped once a field has an error.
Plugin Comparison
| Feature | VJF | DVR | AJV | YUP | JOI | ZOD | VALIBOT | VINEJS |
|---|---|---|---|---|---|---|---|---|
| Custom validation functions | ✅ Native | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| String-based rules | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| JSON Schema | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| Chainable API | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Async validation | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
| Extend with custom rules | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ |
| Automatic error messages | User-defined | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| TypeScript inference | No | No | No | Partial | Partial | ✅ | ✅ | ✅ |
VJF, DVR, AJV, VALIBOT and VINEJS support custom extension (via the
extendcallback). VJF, DVR, AJV and VINEJS support the async validation pipeline. YUP, JOI and ZOD do not support theextendcallback — they rely on their own native APIs for those features. VALIBOT is sync-only (use nativepipe/check/custom()for async-style logic).
Setup Pages
Choose your plugin
- Setup VJF — Vanilla JavaScript Functions
- Setup DVR — Declarative Validation Rules
- Setup AJV — Schema Validation Keywords
- Setup YUP — Object Schema Validator
- Setup JOI — Object Schema Validator
- Setup ZOD — TypeScript-First Schema Validation
- Setup VALIBOT — Tiny TypeScript-First Schema Validation
- Setup VINEJS — Sync Rules, Async Validation API
Extend with custom rules
Available only for VJF, DVR, AJV, VALIBOT and VINEJS.
- Extend VJF — Custom Validation Functions
- Extend DVR — Custom Declarative Rules
- Extend AJV — Custom JSON Schema Keywords
- Extend VALIBOT — Custom Valibot Checks
- Extend VINEJS — Custom VineJS Rules
Async validation
Available only for VJF, DVR, AJV and VINEJS.
Related Guides
- Validation Lifecycle — when validation fires, field filtering, plugin ordering
- Error Handling —
submitThrowsError,defaultGenericError, server error mapping - Performance & SSR —
validationDebounceWait,stopValidationOnError, validation triggers - Validation & Submit Actions —
validate(),submit(),check(),invalidate() - Validation Hooks —
onSuccess,onError, extending hooks