U
    M8c5                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZmZ dd Z	dd	 Z
d
d ZG dd dZG dd dZG dd dZdS )aQ  User input parameter validation.

This module handles user input parameter validation
against a provided input model.

Note that the objects in this module do *not* mutate any
arguments.  No type version happens here.  It is up to another
layer to properly convert arguments to any required types.

Validation Errors
-----------------


    N)datetime)ParamValidationError)is_json_value_headerparse_to_aware_datetimec                 C   s,   t  }|| |}| r(t| ddS )av  Validates input parameters against a schema.

    This is a convenience function that validates parameters against a schema.
    You can also instantiate and use the ParamValidator class directly if you
    want more control.

    If there are any validation errors then a ParamValidationError
    will be raised.  If there are no validation errors than no exception
    is raised and a value of None is returned.

    :param params: The user provided input parameters.

    :type shape: botocore.model.Shape
    :param shape: The schema which the input parameters should
        adhere to.

    :raise: ParamValidationError

    reportN)ParamValidatorvalidate
has_errorsr   generate_report)paramsshapeZ	validatorr    r   5/tmp/pip-unpacked-wheel-ozje0y8b/botocore/validate.pyvalidate_parameters   s    r   c                    s    fdd}|S )Nc                    s    fdd}fdd |S )Nc                    s     |||r| ||||S d S Nr   selfparamr   errorsname_type_checkfuncr   r   _on_passes_type_check4   s    zKtype_check.<locals>._create_type_check_guard.<locals>._on_passes_type_checkc                    s2   t |  s.dd  D }|j|d| |d dS dS )Nc                 S   s   g | ]}t |qS r   str.0tr   r   r   
<listcomp>:   s     zUtype_check.<locals>._create_type_check_guard.<locals>._type_check.<locals>.<listcomp>invalid typer   valid_typesFT)
isinstancer   )r   r   r   valid_type_namesr#   r   r   r   8   s    
zAtype_check.<locals>._create_type_check_guard.<locals>._type_checkr   )r   r   r&   r   r   _create_type_check_guard3   s    z,type_check.<locals>._create_type_check_guardr   )r#   r'   r   r&   r   
type_check2   s    r(   c                 C   sn   d}t d}d|jkr.|jd }||k rTd}n&t|drT|jdrTd}||k rTd}|rj|j| |||d d S )	NFz-infminTserializationZ	hostLabel   )r   min_allowed)floatmetadatahasattrr*   getr   )r   valuer   
error_typer   failedr,   r   r   r   range_checkI   s    


r4   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ValidationErrorsc                 C   s
   g | _ d S r   _errorsr   r   r   r   __init__[   s    zValidationErrors.__init__c                 C   s   | j r
dS dS NTFr6   r8   r   r   r   r
   ^   s    zValidationErrors.has_errorsc                 C   s*   g }| j D ]}|| | q
d|S )N
)r7   append_format_errorjoin)r   Zerror_messageserrorr   r   r   r   c   s    
z ValidationErrors.generate_reportc                 C   s  |\}}}|  |}|dkr2d| d|d  dS |dkrf|d }d|d	 }d
| d| d| S |dkr|d }t|}d|d }	d| d| d| d|	 S |dkr|d }|d }
d| d| d|
 S |dkr|d }|d }
d| d| d|
 S |dkr d||d S |dkrd|d }t|}d|d }	d| d| d| d|	 S |dkrd|d  }d!| d"| d#S |d$krd|d  }d%| d&| d#S d S )'Nmissing required fieldzMissing required parameter in z: "required_name"unknown fieldunknown_paramz, valid_nameszUnknown parameter in z", must be one of: r!   r   r#   zInvalid type for parameter z	, value: z, type: z, valid types: invalid ranger,   zInvalid value for parameter z, valid min value: invalid lengthzInvalid length for parameter z, valid min length: unable to encode to jsonz2Invalid parameter {} must be json serializable: {}
type_errorinvalid type for documentz$Invalid type for document parameter more than one inputmembersz<Invalid number of parameters set for tagged union structure z*. Can only set one of the following keys: .empty inputz=Must set one of the following keys for tagged unionstructure z: )	_get_namer>   typeformat)r   r?   r2   r   
additionalrD   rE   r   
param_typer#   r,   rL   r   r   r   r=   i   sZ    






zValidationErrors._format_errorc                 C   s&   |sdS | dr|dd  S |S d S )NinputrM   r+   )
startswith)r   r   r   r   r   rO      s
    
zValidationErrors._get_namec                 K   s   | j |||f d S r   )r7   r<   )r   r   reasonkwargsr   r   r   r      s    zValidationErrors.reportN)	__name__
__module____qualname__r9   r
   r   r=   rO   r   r   r   r   r   r5   Z   s   @r5   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	e
fddd Ze	efddd Ze	eefddd Ze	e
fddd Ze	efddd Zdd Ze	efddd Ze	eejfef ddd ZeZe	efddd Zdd  Zd!d" Zd#S )$r   z+Validates parameters against a shape model.c                 C   s   t  }| j|||dd |S )a  Validate parameters against a shape model.

        This method will validate the parameters against a provided shape model.
        All errors will be collected before returning to the caller.  This means
        that this method will not stop at the first error, it will return all
        possible errors.

        :param params: User provided dict of parameters
        :param shape: A shape model describing the expected input.

        :return: A list of errors.

         )r   )r5   	_validate)r   r   r   r   r   r   r   r	      s    zParamValidator.validatec                 C   s(   t |r| jS |jdkr$|jr$| jS d S )NZ	structure)r   _validate_jsonvalue_string	type_nameZis_document_type_validate_document)r   r   r   r   r   _check_special_validation_cases   s    z.ParamValidator._check_special_validation_casesc                 C   s<   |  |}|r||||| nt| d|j |||| d S )Nz_validate_%s)r`   getattrr^   )r   r   r   r   r   Zspecial_validatorr   r   r   r\      s    
   zParamValidator._validatec              
   C   sJ   zt | W n6 ttfk
rD } z|j|d|d W 5 d }~X Y nX d S )NrH   )rI   )jsondumps
ValueError	TypeErrorr   )r   r   r   r   r   er   r   r   r]      s    z)ParamValidator._validate_jsonvalue_stringc           
   	   C   s   |d krd S t |tr6|D ]}| || ||| qnt |trlt|D ] \}}| |||d||f  qHnJt |tftttfsttttttf}dd |D }	|j	|d|t
||	d d S )Nz%s[%d]c                 S   s   g | ]}t |qS r   r   r   r   r   r   r       s     z5ParamValidator._validate_document.<locals>.<listcomp>rJ   )r   rS   r#   )r$   dictr_   list	enumerater   intboolr-   r   rP   )
r   r   r   r   r   keyindexentityr#   r%   r   r   r   r_      s.    

   
z!ParamValidator._validate_documentr&   c           	   	   C   s   |j rDt|dkr&|j|d|jd nt|dkrD|j|d|jd |jdg D ]}||krR|j|d||d qR|j}g }|D ].}||kr|j|d	|t|d
 q|| q|D ](}| || |j| || d|  qd S )Nr   rN   )rL   r+   rK   requiredr@   )rA   Zuser_paramsrC   )rD   rE   rM   )	Zis_tagged_unionlenr   rL   r.   r0   rh   r<   r\   )	r   r   r   r   r   Zrequired_memberrL   Zknown_paramsr   r   r   r   _validate_structure   sF      z"ParamValidator._validate_structurec                 C   s   t |t||d| d S )NrG   )r4   rp   r   r   r   r   _validate_string!  s    
zParamValidator._validate_stringc              
   C   sL   |j }t|t||d| t|D ]$\}}| |||| d| d q"d S )NrG   [])memberr4   rp   ri   r\   )r   r   r   r   r   Zmember_shapeiitemr   r   r   _validate_list-  s    zParamValidator._validate_listc           	   
   C   sX   |j }|j}| D ]>\}}| |||| d| d | |||| d|  qd S )Nz (key: )rM   )rl   r1   itemsr\   )	r   r   r   r   r   Z	key_shapeZvalue_shaperl   r1   r   r   r   _validate_map4  s
    zParamValidator._validate_mapc                 C   s   t |||d| d S NrF   r4   r   r   r   r   _validate_integer<  s    z ParamValidator._validate_integerc                 C   sF   t |tttfrd S t|dr"d S |j|d|ttttdgd d S )Nreadr!   zfile-like objectr"   )r$   bytes	bytearrayr   r/   r   r   r   r   r   _validate_blob@  s    
zParamValidator._validate_blobc                 C   s   d S r   r   r   r   r   r   _validate_booleanN  s    z ParamValidator._validate_booleanc                 C   s   t |||d| d S r|   r}   r   r   r   r   _validate_doubleR  s    zParamValidator._validate_doublec                 C   s   t |||d| d S r|   r}   r   r   r   r   _validate_longX  s    zParamValidator._validate_longc                 C   s0   |  |}|s,ttdg}|j|d||d d S )Nztimestamp-stringr!   r"   )_type_check_datetimer   r   r   )r   r   r   r   r   Zis_valid_typer%   r   r   r   _validate_timestamp\  s    
   z"ParamValidator._validate_timestampc              
   C   s0   zt | W dS  tttfk
r*   Y dS X d S r:   )r   re   rd   AttributeError)r   r1   r   r   r   r   g  s
    z#ParamValidator._type_check_datetimeN)rX   rY   rZ   __doc__r	   r`   r\   r]   r_   r(   rg   rq   r   rr   rh   tuplerx   r{   rj   r~   r   rk   r   r-   decimalDecimalr   Z_validate_floatr   r   r   r   r   r   r   r      s4   	

(











r   c                   @   s   e Zd Zdd Zdd ZdS )ParamValidationDecoratorc                 C   s   || _ || _d S r   )_param_validator_serializer)r   Zparam_validator
serializerr   r   r   r9   r  s    z!ParamValidationDecorator.__init__c                 C   sB   |j }|d k	r4| j||j }| r4t| d| j||S )Nr   )input_shaper   r	   r
   r   r   r   serialize_to_request)r   
parametersZoperation_modelr   r   r   r   r   r   v  s      z-ParamValidationDecorator.serialize_to_requestN)rX   rY   rZ   r9   r   r   r   r   r   r   q  s   r   )r   r   rb   r   Zbotocore.exceptionsr   Zbotocore.utilsr   r   r   r(   r4   r5   r   r   r   r   r   r   <module>   s   [ =