o
    QDix!                     @  s0  d dl mZ d dlZd dl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 d d
lmZ erPd dlmZ d dlmZ d dlmZ eeZejejejdZ	d>d?ddZd@d d!Z	d>dAd%d&ZejdBd,d-ZdCd4d5Z dCd6d7Z!dDd9d:Z"ejdEd<d=Z#dS )F    )annotationsN)Mapping)Any)Callable)	Generator)TYPE_CHECKING)
validators)Draft4Validator)RefResolver)_Error)
_Validator)common)httphttpsfileinstanceobjectschemaMapping[str, Any]instance_clstype[_Validator]clstype[_Validator] | Noneargsr   kwargsreturnNonec                 O  s<   |du r	t |}|| ||g|R i ||  dS )a!  This is a carbon-copy of :method:`jsonschema.validate` except that it
    takes two validator classes instead of just one. In the jsonschema
    implementation, `cls` is used to validate both the schema and the
    instance. This changes the behavior to have a separate validator for
    each of schema and instance. Schema should not be validated with the
    custom validator returned by :method:`create_dereffing_validator` because
    it follows $refs.

    :param instance: the instance to validate
    :param schema: the schema to validate with
    :param instance_cls: Validator class to validate instance.
    :param cls: Validator class to validate schema.

    :raises:
        :exc:`ValidationError` if the instance is invalid
        :exc:`SchemaError` if the schema itself is invalid
    N)r   validator_forcheck_schemavalidate)r   r   r   r   r   r    r    ^/var/www/Datamplify/venv/lib/python3.10/site-packages/swagger_spec_validator/ref_validators.pyr   !   s   

 r   instance_resolverr
   c                   s4   i h d fddt j D }tt |S )a  Create a customized Draft4Validator that follows $refs in the schema
    being validated (the Swagger spec for a service). This is not to be
    confused with $refs that are in the schema that describes the Swagger 2.0
    specification.

    :param instance_resolver: resolver for the swagger service's spec
    :type instance_resolver: :class:`jsonschema.RefResolver`

    :rtype: Its complicated. See jsonschema.validators.create()
    >   nottypeallOfanyOfoneOfrequired
propertiesdependenciesmaxPropertiesminPropertiespatternPropertiesadditionalProperties$refc              	     s0   i | ]\}}||v rt jt |d n|qS ))r"   visited_refsdefault_validator_callable)	functoolspartialvalidator_wrapper).0kvr"   validators_to_boundr0   r    r!   
<dictcomp>]   s    	z.create_dereffing_validator.<locals>.<dictcomp>)r	   
VALIDATORSitemsr   extend)r"   bound_validatorsr    r8   r!   create_dereffing_validator@   s   	r?   valueswagger_resolverRefResolver | Nonec                 C  s,   |d u r	t | }t|| |d| d S )N)resolver)r
   from_schemar?   r   )r   r@   rA   r    r    r!   validate_schema_valuel   s   
rE   r0   dict[str, str]refstrGenerator[None, None, None]c                 c  s&    || |< z	dV  W | |= dS | |= w )zContext manager that keeps track of $refs that we've seen during
    validation.

    :param visited_refs: dict of $refs
    :param ref: string $ref value
    Nr    )r0   rG   r    r    r!   visitingy   s
   rJ   	validatorschema_elementdict[str, Any]r1   r   Generator[_Error, None, None]c                 c  s     t | ||||||E dH  dS )a  Generator function that parameterizes default_validator_callable.

    :type validator: :class:`jsonschema.validators.Validator`
    :param schema_element: The schema element that is passed in to each
        specific validator callable aka the 2nd arg in each
        jsonschema._validators.* callable.
    :param instance: The fragment of the swagger service spec that is being
        validated.
    :param schema: The fragment of the swagger jsonschema spec that describes
        is used for validation.
    :param instance_resolver: Resolves refs in the swagger service spec
    :param visited_refs: Keeps track of visisted refs during validation of
        the swagger service spec.
    :param default_validator_callable: jsonschema._validators.* callable
    N)deref_and_validate)rK   rL   r   r   r"   r0   r1   r    r    r!   r4      s   r4   c           	   	   c  s    t |tred|v ret |d tre|d }t|| ||v r&td| dS t||0 ||}|| |||E dH  W d   n1 sFw   Y  W d   dS W d   dS 1 s^w   Y  dS || |||E dH  dS )ap  Generator function that dereferences instance if it is a $ref before
    passing it downstream for actual validation. When a cyclic ref is detected,
    short-circuit and return.

    :type validator: :class:`jsonschema.validators.Validator`
    :param schema_element: The schema element that is passed in to each
        specific validator callable aka the 2nd arg in each
        jsonschema._validators.* callable.
    :param instance: The fragment of the swagger service spec that is being
        validated.
    :param schema: The fragment of the swagger jsonschema spec that describes
        is used for validation.
    :param instance_resolver: Resolves refs in the swagger service spec
    :param visited_refs: Keeps track of visisted refs during validation of
        the swagger service spec.
    :param default_validator_callable: jsonschema._validators.* callable
    r/   zFound cycle in %sN)
isinstancedictrH   attach_scopelogdebugrJ   	resolving)	rK   rL   r   r   r"   r0   r1   rG   targetr    r    r!   rO      s.   
"rO   ref_dictc                 C  s:   d| v rt d| d  dS t d|  t|j| d< dS )a  Attach scope to each $ref we encounter so that the $ref can be
    resolved by custom validations done outside the scope of jsonscema
    validations.

    :param ref_dict: dict with $ref key
    :type instance_resolver: :class:`jsonschema.RefResolver`
    x-scopez!Ref %s already has scope attachedr/   NzAttaching x-scope to %s)rS   rT   list_scopes_stack)rW   r"   r    r    r!   rR      s
   rR   rC   c                 c  s@    d|vr
dV  dS | j }z|d | _ dV  W || _ dS || _ w )zContext manager to assume the given scope for the passed in resolver.

    The resolver's original scope is restored when exiting the context manager.

    :type resolver: :class:`jsonschema.RefResolver
    :type ref_dict: dict
    rX   N)rZ   )rC   rW   saved_scope_stackr    r    r!   in_scope   s   

r\   )N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r"   r
   r   r   )r   r   r@   r   rA   rB   r   r   )r0   rF   rG   rH   r   rI   )rK   r   rL   r   r   rM   r   r   r"   r
   r0   rF   r1   r   r   rN   )rW   rM   r"   r
   r   r   )rC   r
   rW   rM   r   rI   )$
__future__r   
contextlibr2   loggingcollections.abcr   typingr   r   r   r   
jsonschemar   jsonschema.validatorsr	   r
   jsonschema.exceptionsr   r   swagger_spec_validatorr   	getLogger__name__rS   read_urldefault_handlersr   r?   rE   contextmanagerrJ   r4   rO   rR   r\   r    r    r    r!   <module>   sB    

/

#
5