Skip to main content

Overview

rich-domain provides built-in validation support using the Standard Schema specification. This means you can use your favorite validation library - Zod, Valibot, ArkType, or any other Standard Schema-compatible library.

Why Standard Schema?

Library Agnostic

Use Zod, Valibot, ArkType, or any Standard Schema-compatible library

Type Inference

Get full TypeScript inference from your schemas

Validation Timing

Control when validation runs (create, update, or both)

Error Handling

Choose between throwing errors or collecting them

Supported Libraries

Any library implementing the Standard Schema spec works:

Basic Usage

1. Define Your Schema

2. Apply to Entity/Aggregate

3. Validation in Action

Validation Configuration

onCreate

When true, validates the entire entity when constructed:

onUpdate

When true, validates after every property change:

throwOnError

Controls error handling behavior:

persistInvalidMutations

Only applies when throwOnError is false. Controls how updates behave while the entity is invalid:
With persistInvalidMutations: true, toJSON(), getChanges(), and repository save() may see invalid props. Check hasValidationErrors before persisting.

Validation for Value Objects

Value Objects also support validation:

Non-Throwing Mode

Collect errors without throwing:

ValidationError

The ValidationError class provides rich error information:

Complete Example

Next Steps

Hooks

Lifecycle hooks for custom validation rules

Error Handling

Working with ValidationError and error responses