Kind: Interface
Source: packages/common/interfaces/external/validation-error.interface.ts
Part of: Common
Validation error description.
ValidationError describes a failed validation result for a single property on an object. It captures the validated target, invalid value, violated constraints, optional validation contexts, and nested errors for child objects or arrays.
Properties
| Property | Type |
|---|---|
target | Record<string, any> |
property | string |
value | any |
constraints | { [type: string]: string; } |
children | ValidationError[] |
contexts | { [type: string]: any; } |
Diagram
mermaidgraph LR A[ValidationError] --> B[target: Record] A --> C[property: string] A --> D[value: any] A --> E[constraints: constraint messages] A --> F[contexts: constraint metadata] A --> G[children: ValidationError[]] G --> H[Nested property errors] E --> I[Validation failure messages]
Usage
tsimport type { ValidationError } from '@your-package/common';
const error: ValidationError = {
target: {
email: 'invalid-email',
},
property: 'email',
value: 'invalid-email',
constraints: {
isEmail: 'email must be a valid email address',
},
contexts: {
isEmail: {
code: 'INVALID_EMAIL',
},
},
children: [],
};
function formatValidationError(validationError: ValidationError): string[] {
const messages = Object.values(validationError.constraints ?? {});
return [
...messages,
...validationError.children.flatMap(formatValidationError),
];
}
console.log(formatValidationError(error));
// ['email must be a valid email address']
AI Coding Instructions
- Preserve nested validation failures in
children; do not discard them when formatting errors for nested DTOs or array elements. - Treat
constraintsandcontextsas potentially absent or empty when consuming validation results. - Use
propertytogether with parent error paths to build complete field paths such asaddress.street. - Avoid exposing
targetor rawvaluedirectly in API responses when they may contain sensitive data. - Keep constraint keys aligned with the validation library or custom validator names that produced the error.
Was this page helpful?