ValidationError
The exception thrown by parse and parseAsync when data is invalid. Extends Error.
Signature
ts
class ValidationError extends Error {
name: 'ValidationError';
issues: Issue[];
errors: Record<string, string>;
message: string;
}Fields
| Field | Description |
|---|---|
errors | The first error of each field, keyed by dot path. As in validate |
issues | All errors: { code, message, path } |
message | All errors in one line: name: Required; email: Invalid email — handy for logs |
name | Always 'ValidationError' |
Examples
Express error handler
ts
import { ValidationError } from 'tinyval';
app.use((err, req, res, next) => {
if (err instanceof ValidationError) {
return res.status(422).json({ message: 'Validation failed', errors: err.errors });
}
next(err);
});Laravel-style response
ts
// { "message": "...", "errors": { "email": ["Invalid email"] } }
const errors = {};
for (const issue of err.issues) (errors[issue.path.join('.')] ??= []).push(issue.message);