2025-06-24 18:38:57 +00:00
|
|
|
/**
|
|
|
|
|
* ValidationErrorCollection
|
|
|
|
|
*
|
|
|
|
|
* Esta clase representa un error de validación que agrega múltiples detalles de errores
|
|
|
|
|
* en un formato estructurado. Es útil para manejar múltiples errores de validación
|
|
|
|
|
* en una sola respuesta, permitiendo a los clientes entender qué campos fallaron y por qué.
|
|
|
|
|
*
|
|
|
|
|
* Ejemplo de uso:
|
|
|
|
|
*
|
|
|
|
|
* const errors: ValidationErrorDetail[] = [
|
|
|
|
|
* { path: "lines[1].unitPrice.amount", message: "Amount must be a positive number" },
|
|
|
|
|
* { path: "lines[1].unitPrice.scale", message: "Scale must be a non-negative integer" },
|
|
|
|
|
* ];
|
|
|
|
|
* const validationError = new ValidationErrorCollection(errors);
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
import { DomainError } from "./domain-error";
|
|
|
|
|
|
2025-06-24 18:38:57 +00:00
|
|
|
export interface ValidationErrorDetail {
|
2025-09-16 11:29:45 +00:00
|
|
|
path?: string; // ejemplo: "lines[1].unitPrice.amount"
|
2025-08-25 17:42:56 +00:00
|
|
|
message: string; // ejemplo: "Amount must be a positive number",
|
2025-09-16 11:29:45 +00:00
|
|
|
value?: unknown; // valor inválido opcional
|
2025-06-24 18:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
/**
|
|
|
|
|
* Error de validación múltiple. Agrega varios fallos de una sola vez.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export class ValidationErrorCollection extends DomainError {
|
|
|
|
|
public readonly code = "VALIDATION" as const;
|
2025-06-24 18:38:57 +00:00
|
|
|
public readonly details: ValidationErrorDetail[];
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
constructor(message: string, details: ValidationErrorDetail[], options?: ErrorOptions) {
|
|
|
|
|
super(message, options);
|
2025-06-24 18:38:57 +00:00
|
|
|
Object.setPrototypeOf(this, ValidationErrorCollection.prototype);
|
|
|
|
|
|
|
|
|
|
this.name = "ValidationErrorCollection";
|
|
|
|
|
this.details = details;
|
2025-08-25 17:42:56 +00:00
|
|
|
Object.freeze(this);
|
2025-06-24 18:38:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-25 17:42:56 +00:00
|
|
|
|
|
|
|
|
export const isValidationErrorCollection = (e: unknown): e is ValidationErrorCollection =>
|
|
|
|
|
e instanceof ValidationErrorCollection;
|