export interface FieldValidationMessage {
  /**
   * The field that did not pass validation.
   */
  field: string
  /**
   * The error message
   */
  message: string
  /**
   * The rule that was violated.
   */
  rule: string
}

/**
 * Validation error is a superset of Error class with validation
 * error messages
 */
export class ValidationError extends Error {
  /**
   * Http status code for the validation error
   */
  status = 422

  /**
   * Internal code for handling the validation error
   * exception
   */
  code = 'E_VALIDATION_ERROR'

  constructor(
    public messages: FieldValidationMessage[],
    options?: ErrorOptions & { message?: string }
  ) {
    const msg = options?.message || 'Validation failure'
    super(msg, options)
    const ErrorConstructor = this.constructor as typeof ValidationError
    if ('captureStackTrace' in Error) {
      Error.captureStackTrace(this, ErrorConstructor)
    }
  }

  get [Symbol.toStringTag]() {
    return this.constructor.name
  }

  override toString(): string {
    return `${this.name} [${this.code}]: ${this.message}`
  }
}
