ValidationErrors
ValidationErrors is the collection returned by entities.validate(). Obtain it from validate() or from InstantiationError.errors.
API
| Surface | Returns | Best for |
|---|---|---|
.items | readonly ValidationErrorType[] | Direct access to raw error objects |
.ok | boolean | Quick valid/invalid check |
.length | number | Error count |
[Symbol.iterator] | Iterator<ValidationErrorType> | for...of iteration |
aggregate() | { count, paths, keywords } | Structured logs, metric labels |
report() | ProblemDetailsType | HTTP 422 response bodies (RFC 7807) |
Usage examples
Common projections from errs.items:
/**
* ValidationErrors — common projection recipes
*
* Three frequently-asked projections off `errs.items`:
* • path-prefixed message strings
* • group-by-path map
* • field-vs-form bucketing
*
* All operate on the canonical Bastian-rates-Neverending review,
* deliberately submitted with a too-short body and an out-of-range
* rating to exercise both field and form errors.
*/
import type { ValidationErrorType } from '../../../src/types/index.js';
import {
bookstoreEntities, ReviewSchema
} from '../bookstore/index.js';
const errs = bookstoreEntities.validate(ReviewSchema.$id, {
'body': 'no',
'bookIsbn': '9783522128001',
'customerId': 'c1a2b3d4-e5f6-7890-abcd-ef1234567890',
'id': 'a4d3c2b1-a098-7654-a210-fedcba987654',
'postedAt': '2026-04-20T09:15:00Z',
'rating': 12
});
// Path-prefixed message strings.
const messages = errs.items.map((err) => {
return `${err.path}: ${err.message}`;
});
// Group by path.
const grouped: Record<string, ValidationErrorType[]> = {};
for (const err of errs) {
(grouped[err.path || '_root'] ??= []).push(err);
}
// Field vs form errors.
const fieldErrors: ValidationErrorType[] = [];
const formErrors: ValidationErrorType[] = [];
for (const err of errs) {
if (err.path) {
fieldErrors.push(err);
} else {
formErrors.push(err);
}
}
console.assert(Array.isArray(messages));
console.assert(typeof grouped === 'object');
console.assert(Array.isArray(fieldErrors));
console.assert(Array.isArray(formErrors));
// Path-prefixed message strings.
console.log('path-prefixed messages:');
for (const msg of messages) {
console.log(' ', msg);
}
// Group by path.
console.log('grouped by path:');
for (const [
path,
items
] of Object.entries(grouped)) {
console.log(` ${path}: [${items.map((entry) => {
return entry.message;
}).join(', ')}]`);
}
// Field vs form.
console.log('field errors:', fieldErrors.length, ' form errors:', formErrors.length);
Output
Press Execute to run this example against the real library.All examples use the bookstore domain. See entities.validate() for how to obtain the collection.
Related
validate- returns theValidationErrorscollectioninstantiate-InstantiationError.errorscarries the same collectionValidationErrorsviews -aggregate,report- Error class hierarchy -
BaseError,InstantiationError,CoercionError,TransformError,DecodeError,EncodeError,SchemaError,GraphError,MaterializationError
See also
- Bookstore domain - schema definitions used in examples
- Picking a method - when to use
validatevsinstantiatevsis