Skip to content

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 ​

FieldDescription
errorsThe first error of each field, keyed by dot path. As in validate
issuesAll errors: { code, message, path }
messageAll errors in one line: name: Required; email: Invalid email — handy for logs
nameAlways '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);

See also ​