ValidationError
ValidationError is the standard error type thrown when validation fails. It contains structured information about all validation issues.
ValidationError API
Properties
Methods
getMessages()
Get all error messages as a flat array:getFormattedErrors()
Get errors formatted for UI/API consumption:path is a dot-joined string (empty string for global errors).
getErrorsForPath(path)
Get errors for a specific field:hasErrorsForPath(path)
Check if a field has errors:toJSON()
Serialize for API responses:Static Methods
isValidationError(error)
Type-safe check that works across module boundaries:Always use
ValidationError.isValidationError() instead of instanceof for
reliable detection across module boundaries.Throwing Validation Errors
In Hooks
Use thethrowValidationError helper:
Manually
Create and throw directly:Non-Throwing Mode
Configure entities to collect errors instead of throwing:Collecting Issues in rules (without throwing)
WhenthrowOnError is false, use addValidationIssue inside rules to accumulate business rule violations:
addValidationIssue are merged into validationErrors.
Use throwValidationError when you want fail-fast behavior (always throws, regardless of throwOnError).
persistInvalidMutations
WhenthrowOnError is false, this flag defines whether invalid updates are kept on the entity:
persistInvalidMutations: true (default)
- Property changes apply even when schema or
rulesfail. validationErrorsreflects the latest validation of the current props (all fields).- Matches form UX: what the user typed is what
toJSON()returns.
persistInvalidMutations: false
- While
validationErrorsexists, further mutations are blocked (includingdelete). - A single update that fails validation is reverted; errors are still recorded.
When to Use Non-Throwing Mode
Form Validation
Collect all errors to display to user at once
Import/Migration
Log errors but continue processing
Partial Validation
Allow incomplete entities during construction
Error Aggregation
Combine errors from multiple sources
API Error Responses
Express.js Example
Response Format
Domain Exceptions
BeyondValidationError, rich-domain provides a comprehensive set of domain exceptions for different error scenarios. All exceptions extend a common DomainException base class.