o
    QDi&                     @  s4  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	Z	ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ eeZg dZd@ddZdAddZedBddZdCddZ		 		 dDdEd(d)ZdFd,d-ZdGd/d0ZdHd2d3ZdId5d6ZdJd7d8Z dKd9d:Z!edLd>d?Z"dS )Ma\  
Validate Swagger Specs against the Swagger 1.2 Specification.  The
validator aims to check for full compliance with the Specification.

The validator uses the published jsonschema files for basic structural
validation, augmented with custom validation code where necessary.

https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md
    )annotationsN)Any)urlparse)RefResolver)get_uri_from_file_path)read_resource_file)read_url)SwaggerValidationError)wrap_exception)default_handlers)integernumberstringbooleanapi_declarationdict[str, Any]return	list[str]c                 C  s   |  di }dd | D S )Nmodelsc                 S  s   g | ]}|d  qS )id ).0modelr   r   [/var/www/Datamplify/venv/lib/python3.10/site-packages/swagger_spec_validator/validator12.py
<listcomp>#   s    z!get_model_ids.<locals>.<listcomp>)getvalues)r   r   r   r   r   get_model_ids!   s   r   urlstrresourcec                 C  sF   t | jdkrtj| }ddd}tj|||}|S | | }|S )	a9  Fetch the complete resource path to get the api declaration.

    :param url: A file or http uri hosting the resource listing.
    :type url: string
    :param resource: Resource path starting with a '/'. eg. '/pet'
    :type resource: string
    :returns: Complete resource path hosting the api declaration.
    filer    r   r   c                 S  s   |  dsJ | dd  d S )N/   z.json)
startswith)r    r   r   r   resource_file_name2   s   z-get_resource_path.<locals>.resource_file_nameN)r    r   r   r   )r   schemeospathdirnamejoin)r   r    
parent_dirr%   r(   r   r   r   get_resource_path&   s   	
r,   Nonec                 C  s   t d|  tt| |  dS )a6  Simple utility function to perform recursive validation of a Resource
    Listing and all associated API Declarations.

    This is trivial wrapper function around
    :py:func:`swagger_spec_validator.validate_resource_listing` and
    :py:func:`swagger_spec_validator.validate_api_declaration`.  You are
    encouraged to write your own version of this if required.

    :param url: the URL of the Resource Listing.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    Validating %sN)loginfovalidate_specr   )r   r   r   r   validate_spec_url=   s   r2   resource_listingc                 C  s@   t |  | d D ]}t||d }td| tt| qdS )a  
    Validates the resource listing, fetches the api declarations and
    consequently validates them as well.

    :type resource_listing: dict
    :param url: url serving the resource listing; needed to resolve api
                declaration path.
    :type url: string

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    apisr(   r.   N)validate_resource_listingr,   r/   r0   validate_api_declarationr   )r3   r   apir(   r   r   r   r1   R   s   r1   TFobj	model_idsallow_arraysboolallow_voids
allow_refs
allow_filec           	      C  s   |  d}|  d}|dur\|tv rdS |r|dkrdS |dkr<|s&td|  d}|du r3tdt||d	d
 dS |dkrH|sFtddS ||v rV|rTtd| dS td| |durr|sftd||vrptd| dS td)u  Validate an object that contains a data type (§4.3.3).

    Params:
    - obj: the dictionary containing the data type to validate
    - model_ids: a list of model ids
    - allow_arrays: whether an array is permitted in the data type.  This is
      used to prevent nested arrays.
    - allow_voids: whether a void type is permitted.  This is used when
      validating Operation Objects (§5.2.3).
    - allow_refs: whether '$ref's are permitted.  If true, then 'type's
      are not allowed to reference model IDs.
    typez$refNvoidarrayz"array" not alloweditemsz"items" not foundF)r:   Filez-Type "File" is only valid for form parametersz$must use "$ref" for referencing "%s"zunknown type "%s"z"$ref" not allowedzunknown model id "%s"zno "$ref" or "type" present)r   PRIMITIVE_TYPESr	   validate_data_type)	r8   r9   r:   r<   r=   r>   typrefrB   r   r   r   rE   h   sF   


rE   r   
model_namec                 C  s   |  dg D ]}|| d vrtd||q|| d kr)d|| d }t||  di  D ](\}}z	t||dd W q1 tyY } ztd| d	| d
t| d}~ww dS )u"   Validate a Model Object (§5.2.7).required
propertiesz,Model "{}": required property "{}" not foundr   z*model name: {} does not match model id: {}T)r=   zModel "z", property "z": N)r   r	   formatrB   rE   r   )r   rH   r9   rI   error	prop_nameproper   r   r   validate_model   s.   rP   	parameterc                 C  s"   |  ddk}t| |d|d dS )u&   Validate a Parameter Object (§5.2.4).	paramTypeformF)r=   r>   N)r   rE   )rQ   r9   r>   r   r   r   validate_parameter   s   rT   	operationc                 C  s   z
t | |ddd W n ty" } ztd| d t|d}~ww | d D ]$}zt|| W q' tyK } ztd| d |d	 t|f d}~ww dS )
u'   Validate an Operation Object (§5.2.3).FT)r=   r<   zOperation "{}": {}nicknameN
parametersz"Operation "%s", parameter "%s": %sname)rE   r	   rK   r   rT   )rU   r9   rO   rQ   r   r   r   validate_operation   s(   rY   r7   c                 C  s   | d D ]}t || qdS )u!   Validate an API Object (§5.2.2).
operationsN)rY   )r7   r9   rU   r   r   r   validate_api   s   r[   c                 C  sT   t | d t| }| d D ]}t|| q| di  D ]
\}}t||| qdS )uJ  Validate an API Declaration (§5.2).

    :param api_declaration: a dictionary respresentation of an API Declaration.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    z schemas/v1.2/apiDeclaration.jsonr4   r   N)validate_jsonr   r[   r   rB   rP   )r   r9   r7   rH   r   r   r   r   r6      s   

r6   c                 C  s   t | d dS )u  Validate a Resource Listing (§5.1).

    :param resource_listing: a dictionary respresentation of a Resource Listing.

    Note that you will have to invoke `validate_api_declaration` on each
    linked API Declaration.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    z!schemas/v1.2/resourceListing.jsonN)r\   )r3   r   r   r   r5      s   r5   json_documentlist[Any] | dict[str, Any]schema_pathc                 C  s2   t |\}}tt||td}tj| ||d dS )zValidate a json document against a json schema.

    :param json_document: json document in the form of a list or dict.
    :param schema_path: package relative path of the json schema file.
    )base_urireferrerhandlers)resolverN)r   r   r   r   
jsonschemavalidate)r]   r_   schemarc   r   r   r   r\   	  s   r\   )r   r   r   r   )r   r   r    r   r   r   )r   r   r   r-   )r3   r   r   r   r   r-   )TFTF)r8   r   r9   r   r:   r;   r<   r;   r=   r;   r>   r;   r   r-   )r   r   rH   r   r9   r   r   r-   )rQ   r   r9   r   r   r-   )rU   r   r9   r   r   r-   )r7   r   r9   r   r   r-   )r   r   r   r-   )r3   r   r   r-   )r]   r^   r_   r   r   r-   )#__doc__
__future__r   loggingr'   typingr   urllib.parser   rd   r   swagger_spec_validator.commonr   r   r   r	   r
   %swagger_spec_validator.ref_validatorsr   	getLogger__name__r/   rD   r   r,   r2   r1   rE   rP   rT   rY   r[   r6   r5   r\   r   r   r   r   <module>   sD    	




@




