o
    5)iJ                     @   sD   d Z ddlmZmZmZmZ ddlmZ ddlZG dd de	Z
dS )z
Laravel-style validation exception for Flask API with Pydantic
Similar to Laravel's ValidationException for consistent error handling
    )DictListAnyOptional)ValidationErrorNc                       s   e Zd ZdZd deeee f dedef fddZe	d!d	e
ded
d fddZe	d!deeee f ded
d fddZd
efddZded
ee fddZdeded
dfddZd
eeef fddZd
efddZd
efddZd
efddZ  ZS )"ValidationExceptionz
    Laravel-style validation exception that converts Pydantic validation errors
    to Laravel's format for consistent API responses.
    The given data was invalid.  errorsmessagestatus_codec                    s$   || _ || _|| _t | j dS )z
        Initialize ValidationException

        Args:
            errors: Dictionary of field validation errors
            message: Main error message
            status_code: HTTP status code (default: 422)
        N)r
   r   r   super__init__)selfr
   r   r   	__class__ A/var/www/html/oms-prod-mcp/app/exceptions/validation_exception.pyr      s   	zValidationException.__init__validation_errorreturnc                 C   sn  i }|  D ]}ddd |d D }|dd}|dd}|d	kr'd
}n{|dkr.|}nt|dkr?|dd}d| d}nc|dv r\|di d|di dd}	d|	 d}nF|dv ry|di d|di dd}	d|	 d}n)|dkr|di dd}
d|
 d}n|dkr|di dd}d| d}||vrg ||< || | q| ||S ) a  
        Convert Pydantic ValidationError to Laravel-style ValidationException

        Args:
            validation_error: Pydantic ValidationError instance
            message: Custom error message

        Returns:
            ValidationException instance
        .c                 s   s    | ]}t |V  qd S )N)str).0locr   r   r   	<genexpr>/   s    z:ValidationException.from_pydantic_error.<locals>.<genexpr>r   msgzValidation failedtype missingzThis field is required.value_error
type_error
input_typezvalid valuezThis field must be a valid )greater_thangreater_than_equalctxgtgez This field must be greater than )	less_thanless_than_equalltlezThis field must be less than string_too_short
min_lengthzThis field must be at least z characters.string_too_long
max_lengthzThis field must not exceed )r
   joingetappend)clsr   r   r
   error
field_path	error_msg
error_typeexpected_typelimitr,   r.   r   r   r   from_pydantic_error   s8   $$
z'ValidationException.from_pydantic_errorc                 C   s
   | ||S )z
        Create ValidationException from custom error dictionary

        Args:
            errors: Dictionary of field errors
            message: Main error message

        Returns:
            ValidationException instance
        r   )r2   r
   r   r   r   r   from_custom_errorsR   s   
z&ValidationException.from_custom_errorsc                 C   s
   t | jS )z!Check if exception has any errors)boolr
   r   r   r   r   
has_errors`   s   
zValidationException.has_errorsfieldc                 C   s   | j |g S )zGet errors for a specific field)r
   r0   )r   r>   r   r   r   get_errors_for_fieldd   s   z(ValidationException.get_errors_for_fieldr3   Nc                 C   s(   || j vr
g | j |< | j | | dS )z!Add an error for a specific fieldN)r
   r1   )r   r>   r3   r   r   r   	add_errorh   s   

zValidationException.add_errorc                 C   s   | j | jdS )z6Convert exception to dictionary for JSON serializationr   r
   rA   r<   r   r   r   to_dictn   s   zValidationException.to_dictc                 C   s   t j|  ddS )z Convert exception to JSON stringF)ensure_ascii)jsondumpsrB   r<   r   r   r   to_jsonu   s   zValidationException.to_jsonc                 C   s   d| j  d| j S )NzValidationException: z - Errors: rA   r<   r   r   r   __str__y   s   zValidationException.__str__c                 C   s   d| j  d| j d| j dS )NzValidationException(message='z
', errors=z, status_code=))r   r
   r   r<   r   r   r   __repr__|   s   zValidationException.__repr__)r   r	   )r   )__name__
__module____qualname____doc__r   r   r   intr   classmethodr   r9   r:   r;   r=   r?   r@   r   rB   rF   rG   rI   __classcell__r   r   r   r   r      s    (2&r   )rM   typingr   r   r   r   pydanticr   rD   	Exceptionr   r   r   r   r   <module>   s
    