o
    6ti                  
   @   s.  d dl Z d dlZd dlZd dlZd dlmZmZ zd dlZd dlmZm	Z	m
Z
 W n ey9 Z zededZ[ww eeZdefddZe	 Zedd	d
 Zeddd Zeddd Zdeeef deeef defddZdee dee defddZdee dee defddZdS )    N)AnyDict)Draft202012ValidatorFormatCheckerValidationErrorzUjsonschema is not installed. Please install it using 'pip install jsonschema[format]'schemac                 C   s*   z	t j|  W dS  t jy   Y dS w )z
    Check if a JSON schema is valid.

    :param schema: A JSON schema.
    :return: True if the schema is valid, False otherwise.
    TF)
jsonschemar   check_schemaSchemaError)r    r   Z/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/jsonschema_bench/metrics.pyis_json_schema_valid   s   r   ipv4c                 C      t |  d S N)	ipaddressIPv4Addressvaluer   r   r   
ipv4_check(      r   ipv6c                 C   r   r   )r   IPv6Addressr   r   r   r   
ipv6_check-   r   r   uuidc                 C   r   r   )r   UUIDr   r   r   r   
uuid_check2   r   r   instancereturnc              
   C   sP   t |stdt|td}z||  W dS  ty' } zt|jd}~ww )z
    Validate a JSON instance against a schema with enhanced format checking.

    :param schema: The JSON schema to validate against.
    :param instance: The JSON instance to validate.
    :raises ValidationError: If the validation fails.
    zThe JSON schema is invalid.)format_checkerNT)r   r   r   r   validatemessage)r   r   	validatorer   r   r   "schema_conform_with_format_checker7   s   
r$   
referencespredictionsc              
   C   s   t | dks
J dt |dksJ d| d }|d }t| }zt| dd}W n tjy=   Y dS w zt||}W |S  tya } ztd|  W Y d }~dS d }~ww )	N   zCWe only have one reference for this task, which is the JSON schema.>Currently, we don't support pass@k for JSON schema validation.r   ```jsonFzError: )	lenr*   loadsstripJSONDecodeErrorr$   	Exceptioneval_loggererror)r%   r&   	reference
predictionjson_schemajson_objschema_conformr#   r   r   r   schema_complianceL   s,   r7   c                 C   sX   t |dks
J d|d }zt| dd  W dS  tjy+   Y dS w )Nr'   r(   r   r)   r*   FT)r+   r*   r,   r-   r.   )r%   r&   r3   r   r   r   json_validitye   s    r8   )r   r*   loggingr   typingr   r   r   r   r   r   ImportErrorr#   	getLogger__name__r0   dictr   r   checksr   r   r   strboolr$   listr7   r8   r   r   r   r   <module>   sD    






"