
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    `S                     @   s  d Z ddlmZ ddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZ ed	d
ZeddZeddZdZeddZeddZeddZeddZeddZeddZdZeddZeddZeddZeddZed d!Zed"d#Z ed$d%Z!ed&d'Z"ed(d)Z#ed*d+Z$ed,d-Z%ed.d-Z&ed/d0Z'ed1d0Z(ed2d3Z)ed4dZ*ed5d6Z+ed7d8Z,ed9d:Z-ed;d<Z.ed=dZ/ed>dZ0ed?dZ1ed@dA Z2Z3edBdC Z4Z5edDd!Z6edEdZ7edFdGZ8edHdIZ9edJdKZ:edLdMZ;dNZ<dOZ=G dPdQ dQe>Z?G dRdS dSe@ZAG dTdU dUeZBG dVdW dWeBZCG dXdY dYeCZDG dZd[ d[eCZEG d\d] d]e>ZFG d^d_ d_eFZGd`da ZHG dbdc dceFZIG ddde deeIZJdS )fz? This module contains the error-related constants and classes.     )absolute_import)defaultdict
namedtuple)copydeepcopy)wraps)pformat)PYTHON_VERSIONMutableMapping)compare_paths_ltquote_stringErrorDefinitionz
code, ruleN   document is missing   required      Zdependencies      Zexcludes!   ''{0}' is not a document, must be a dict"   empty#   Znullable$   type%   schema&   items'   Z	minlength(   Z	maxlengthA   ZregexB   minC   maxD   allowedE   F   Z	forbiddenG   H   contains`   a   Zcoerceb   Zrename_handlerc   readonlyd   Zdefault_setter            Z	keysrules   Zvaluesrules         Znoneof   Zoneof   Zanyof   Zallofz0schema definition for field '{0}' must be a dictzvalidation schema missingc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
e	dd Ze	dd Ze	dd Ze	dd Ze	dd ZdS )ValidationErrorz:A simple class to store and query basic error information.c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S N)document_pathschema_pathcoderule
constraintvalueinfo)selfrB   rC   rD   rE   rF   rG   rH    rJ   @/opt/alt/python38/lib/python3.8/site-packages/cerberus/errors.py__init__^   s    zValidationError.__init__c                 C   s   t | t |kS )z:Assumes the errors relate to the same document and schema.)hashrI   otherrJ   rJ   rK   __eq__q   s    zValidationError.__eq__c                 C   s   t | jt | jA t | jA S )z>Expects that all other properties are transitively determined.)rM   rB   rC   rD   rI   rJ   rJ   rK   __hash__u   s    zValidationError.__hash__c                 C   s,   | j |j krt| j |j S t| j|jS d S rA   )rB   r   rC   rN   rJ   rJ   rK   __lt__y   s    zValidationError.__lt__c              
   C   s>   dj | jjtt| | j| jt| jt| j	t| j
| jdS )Nz{class_name} @ {memptr} ( document_path={document_path},schema_path={schema_path},code={code},constraint={constraint},value={value},info={info} ))
class_nameZmemptrrB   rC   rD   rF   rG   rH   )format	__class____name__hexidrB   rC   rD   r   rF   rG   rH   rQ   rJ   rJ   rK   __repr__   s    
zValidationError.__repr__c                 C   s   | j r| jd S dS )zX
        A list that contains the individual errors of a bulk validation error.
        r   N)is_group_errorrH   rQ   rJ   rJ   rK   child_errors   s    zValidationError.child_errorsc                 C   s@   | j s
dS tt}| jD ]"}|jt| j }|| | q|S )z
        Dictionary with errors of an \*of-rule mapped to the index of the definition it
        occurred in. Returns :obj:`None` if not applicable.
        N)is_logic_errorr   listr\   rC   lenappend)rI   resulterrorirJ   rJ   rK   definitions_errors   s    
z"ValidationError.definitions_errorsc                 C   s   | j r| j d S dS dS )z6Field of the contextual mapping, possibly :obj:`None`.N)rB   rQ   rJ   rJ   rK   field   s    
zValidationError.fieldc                 C   s   t | jtj@ S )z(``True`` for errors of bulk validations.)boolrD   ERROR_GROUPrQ   rJ   rJ   rK   r[      s    zValidationError.is_group_errorc                 C   s   t | jtjtj @ S )z[
        ``True`` for validation errors against different schemas with \*of-rules.
        )rg   rD   LOGICALrh   rQ   rJ   rJ   rK   r]      s    zValidationError.is_logic_errorc                 C   s   t | jtj@ S )z"``True`` for normalization errors.)rg   rD   NORMALIZATIONrQ   rJ   rJ   rK   is_normalization_error   s    z&ValidationError.is_normalization_errorN)rW   
__module____qualname____doc__rL   rP   rR   rS   rZ   propertyr\   rd   rf   r[   r]   rk   rJ   rJ   rJ   rK   r@   [   s$   




r@   c                   @   s   e Zd ZdZdd ZdS )	ErrorListz
    A list for :class:`~cerberus.errors.ValidationError` instances that can be queried
    with the ``in`` keyword for a particular :class:`~cerberus.errors.ErrorDefinition`.
    c                    s*   t |tst|j t fdd| D S )Nc                 3   s   | ]}|j  kV  qd S rA   )rD   ).0xZwanted_coderJ   rK   	<genexpr>   s     z)ErrorList.__contains__.<locals>.<genexpr>)
isinstancer   	TypeErrorrD   any)rI   Zerror_definitionrJ   rs   rK   __contains__   s    
zErrorList.__contains__N)rW   rl   rm   rn   rx   rJ   rJ   rJ   rK   rp      s   rp   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zedd Zedd Zdd Zdd ZdS )ErrorTreeNode)descendantserrorsparent_nodepath	tree_rootc                 C   s8   || _ | j j| _|d | j jd  | _t | _i | _d S Nr   )r|   r~   depthr}   rp   r{   rz   )rI   r}   r|   rJ   rJ   rK   rL      s
    
zErrorTreeNode.__init__c                 C   s"   t |tr|| jkS || jkS d S rA   )ru   r   r{   rz   )rI   itemrJ   rJ   rK   rx      s    

zErrorTreeNode.__contains__c                 C   s   | j |= d S rA   rz   )rI   keyrJ   rJ   rK   __delitem__   s    zErrorTreeNode.__delitem__c                 C   s
   t | jS rA   )iterr{   rQ   rJ   rJ   rK   __iter__   s    zErrorTreeNode.__iter__c                 C   s>   t |tr.| jD ]}|j|jkr|  S qd S | j|S d S rA   )ru   r   r{   rD   rz   get)rI   r   rb   rJ   rJ   rK   __getitem__   s    


zErrorTreeNode.__getitem__c                 C   s
   t | jS rA   )r_   r{   rQ   rJ   rJ   rK   __len__   s    zErrorTreeNode.__len__c                 C   s   |   S rA   )__str__rQ   rJ   rJ   rK   rZ      s    zErrorTreeNode.__repr__c                 C   s   || j |< d S rA   r   )rI   r   rG   rJ   rJ   rK   __setitem__   s    zErrorTreeNode.__setitem__c                 C   s   t | jd t | j S )N,)strr{   rz   rQ   rJ   rJ   rK   r      s    zErrorTreeNode.__str__c                 C   s
   t | jS rA   )r_   r}   rQ   rJ   rJ   rK   r      s    zErrorTreeNode.depthc                 C   s   | j jS rA   )r~   	tree_typerQ   rJ   rJ   rK   r      s    zErrorTreeNode.tree_typec                 C   s   |  |}|| j }|| jkr,t|| | |< | | }t|| jd kr||j| |j  |jr|j	D ]}| j
| qhn
|| d S r   )	_path_of_r   rz   ry   r_   r{   r`   sortr[   r\   r~   add)rI   rb   Z
error_pathr   nodechild_errorrJ   rJ   rK   r     s    




zErrorTreeNode.addc                 C   s   t || jd S )N_path)getattrr   rI   rb   rJ   rJ   rK   r     s    zErrorTreeNode._path_of_N)rW   rl   rm   	__slots__rL   rx   r   r   r   r   rZ   r   r   ro   r   r   r   r   rJ   rJ   rJ   rK   ry      s    	

ry   c                       s:   e Zd ZdZdddZ fddZdd Zd	d
 Z  ZS )	ErrorTreezw
    Base class for :class:`~cerberus.errors.DocumentErrorTree` and
    :class:`~cerberus.errors.SchemaErrorTree`.
    rJ   c                 C   s8   d | _ | | _d| _t | _i | _|D ]}| | q$d S )NrJ   )r|   r~   r}   rp   r{   rz   r   rI   r{   rb   rJ   rJ   rK   rL      s    zErrorTree.__init__c                    s6   |  |s"| j| | j  ntt| | dS )zl
        Add an error to the tree.

        :param error: :class:`~cerberus.errors.ValidationError`
        N)r   r{   r`   r   superr   r   r   rV   rJ   rK   r   )  s    
zErrorTree.addc                 C   s"   |  |}|dk	r|jS t S dS )z
        Returns all errors for a particular path.

        :param path: :class:`tuple` of :term:`hashable` s.
        :rtype: :class:`~cerberus.errors.ErrorList`
        N)fetch_node_fromr{   rp   )rI   r}   r   rJ   rJ   rK   fetch_errors_from5  s    
zErrorTree.fetch_errors_fromc                 C   s&   | }|D ]}|| }|dkr q"q|S )z
        Returns a node for a path.

        :param path: Tuple of :term:`hashable` s.
        :rtype: :class:`~cerberus.errors.ErrorTreeNode` or :obj:`None`
        NrJ   )rI   r}   contextr   rJ   rJ   rK   r   B  s    zErrorTree.fetch_node_from)rJ   )	rW   rl   rm   rn   rL   r   r   r   __classcell__rJ   rJ   r   rK   r     s
   
	r   c                   @   s   e Zd ZdZdZdS )DocumentErrorTreezv
    Implements a dict-like class to query errors by indexes following the structure of a
    validated document.
    ZdocumentNrW   rl   rm   rn   r   rJ   rJ   rJ   rK   r   Q  s   r   c                   @   s   e Zd ZdZdZdS )SchemaErrorTreezq
    Implements a dict-like class to query errors by indexes following the structure of
    the used schema.
    r   Nr   rJ   rJ   rJ   rK   r   Z  s   r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )BaseErrorHandlerziBase class for all error handlers.
    Subclasses are identified as error-handlers with an instance-test.c                 O   s   dS )z%Optionally initialize a new instance.NrJ   rI   argskwargsrJ   rJ   rK   rL   g  s    zBaseErrorHandler.__init__c                 C   s   t dS )a,  
        Returns errors in a handler-specific format.

        :param errors: An object containing the errors.
        :type errors: :term:`iterable` of
                      :class:`~cerberus.errors.ValidationError` instances or a
                      :class:`~cerberus.Validator` instance
        NNotImplementedErrorrI   r{   rJ   rJ   rK   __call__k  s    	zBaseErrorHandler.__call__c                 C   s   t dS )z5Be a superhero and implement an iterator over errors.Nr   rQ   rJ   rJ   rK   r   v  s    zBaseErrorHandler.__iter__c                 C   s   t dS )z
        Add an error to the errors' container object of a handler.

        :param error: The error to add.
        :type error: :class:`~cerberus.errors.ValidationError`
        Nr   r   rJ   rJ   rK   r   z  s    zBaseErrorHandler.addc                 C   s   dS )z
        Optionally emits an error in the handler's format to a stream. Or light a LED,
        or even shut down a power plant.

        :param error: The error to emit.
        :type error: :class:`~cerberus.errors.ValidationError`
        NrJ   r   rJ   rJ   rK   emit  s    zBaseErrorHandler.emitc                 C   s   dS )z
        Gets called when a validation ends.

        :param validator: The calling validator.
        :type validator: :class:`~cerberus.Validator`
        NrJ   rI   	validatorrJ   rJ   rK   end  s    zBaseErrorHandler.endc                 C   s   |D ]}|  | qdS )z
        Adds all errors to the handler's container object.

        :param errors: The errors to add.
        :type errors: :term:`iterable` of
                      :class:`~cerberus.errors.ValidationError` instances
        N)r   r   rJ   rJ   rK   extend  s    zBaseErrorHandler.extendc                 C   s   dS )z
        Gets called when a validation starts.

        :param validator: The calling validator.
        :type validator: :class:`~cerberus.Validator`
        NrJ   r   rJ   rJ   rK   start  s    zBaseErrorHandler.startN)rW   rl   rm   rn   rL   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   r   c  s   	
	r   c                   @   s   e Zd Zdd Zdd ZdS )ToyErrorHandlerc                 O   s   t dd S )NzThis is not supposed to happen.)RuntimeErrorr   rJ   rJ   rK   r     s    zToyErrorHandler.__call__c                 C   s   d S rA   rJ   rQ   rJ   rJ   rK   clear  s    zToyErrorHandler.clearN)rW   rl   rm   r   r   rJ   rJ   rJ   rK   r     s   r   c                    s$   t   fdd}tdk r |S  S )zCerberus error messages expect regular binary strings.
    If unicode is used in a ValidationError message can't be printed.

    This decorator ensures that if legacy Python is used unicode
    strings are encoded before passing to a function.
    c                    sV   dd }t |}||j|_||j|_||j|_||j|_||j|_ | |S )Nc                 S   s   t | tr| dS | S )z1Helper encoding unicode strings into binary utf-8zutf-8)ru   unicodeencode)rG   rJ   rJ   rK   _encode  s    

z0encode_unicode.<locals>.wrapped.<locals>._encode)r   rB   rC   rF   rG   rH   )objrb   r   frJ   rK   wrapped  s    zencode_unicode.<locals>.wrappedr   )r   r	   )r   r   rJ   r   rK   encode_unicode  s    r   c                %   @   s   e Zd ZdZdddddddd	d
ddddddddddddddddddddddddd d!d"d#$ZdDd%d&Zd'd( Zd)d* Zed+d, Z	e
d-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< ZdEd>d?ZdFd@dAZdGdBdCZd$S )HBasicErrorHandlerz
    Models cerberus' legacy. Returns a :class:`dict`. When mangled through :class:`str`
    a pretty-formatted representation of that tree is returned.
    z{0}r   zrequired fieldzunknown fieldzfield '{0}' is requiredz%depends on these values: {constraint}z&{0} must not be present with '{field}'r   zempty values not allowedznull value not allowedzmust be of {constraint} typezmust be of dict typez'length of list should be {0}, it is {1}zmin length is {constraint}zmax length is {constraint}z)value does not match regex '{constraint}'zmin value is {constraint}zmax value is {constraint}zunallowed value {value}zunallowed values {0}zmissing members {0}z&field '{field}' cannot be coerced: {0}z&field '{field}' cannot be renamed: {0}zfield is read-onlyz.default value for '{field}' cannot be set: {0}z'mapping doesn't validate subschema: {0}z.one or more sequence-items don't validate: {0}z2one or more keys of a mapping  don't validate: {0}z3one or more values in a mapping don't validate: {0}z one or more definitions validatez#none or more than one rule validatezno definitions validatez&one or more definitions don't validate)$r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r"   r#   r$   r&   r(   r*   r+   r,   r-   r0   r1   r2   r4   r6   r7   r8   r9      r<   r=   r>   r?   Nc                 C   s   |d kri n|| _ d S rA   tree)rI   r   rJ   rJ   rK   rL     s    zBasicErrorHandler.__init__c                 C   s   |    | | | jS rA   )r   r   pretty_treer   rJ   rJ   rK   r     s    
zBasicErrorHandler.__call__c                 C   s
   t | jS rA   )r   r   rQ   rJ   rJ   rK   r     s    zBasicErrorHandler.__str__c                 C   s&   t | j}|D ]}| ||  q|S rA   )r   r   _purge_empty_dicts)rI   Zprettyrf   rJ   rJ   rK   r     s    
zBasicErrorHandler.pretty_treec                 C   s^   t |}| | |jr$| | n6|jr6| | n$|j| jkrZ| |j	| 
|j| d S rA   )r   _rewrite_error_pathr]   _insert_logic_errorr[   _insert_group_errorrD   messages_insert_errorrB   _format_messagerf   r   rJ   rJ   rK   r     s    
 zBasicErrorHandler.addc                 C   s
   i | _ d S rA   r   rQ   rJ   rJ   rK   r     s    zBasicErrorHandler.clearc                 C   s   |    d S rA   )r   r   rJ   rJ   rK   r   !  s    zBasicErrorHandler.startc                 C   s"   | j |j j|j|j||jdS )N)rF   rf   rG   )r   rD   rU   rH   rF   rG   )rI   rf   rb   rJ   rJ   rK   r   $  s       z!BasicErrorHandler._format_messagec                 C   s   |d }t |dkrT|| jkrD| j|  }| j|  ||g7  < q|i g| j|< nnt |dkr|| jkrvi g| j|< | j| d }|r| jt|d}n|  }||dd | ||j dS )z
        Adds an error or sub-tree to :attr:tree.

        :param path: Path to the error.
        :type path: Tuple of strings and integers.
        :param node: An error message or a sub-tree.
        :type node: String or dictionary.
        r   r   re   r   N)r_   r   poprV   r   r   update)rI   r}   r   rf   subtreenewrJ   rJ   rK   r   )  s    	

zBasicErrorHandler._insert_errorc                 C   sL   |j D ]@}|jr| | q|jr.| | q| |j| |j| qd S rA   )	r\   r]   r   r[   r   r   rB   r   rf   )rI   rb   r   rJ   rJ   rK   r   E  s    
z%BasicErrorHandler._insert_group_errorc              	   C   st   |j }| |j| || |j D ]H}|D ]>}|jrD| | q.|jrV| 	| q.| |j| || q.q&d S rA   )
rf   r   rB   r   rd   valuesr]   r   r[   r   )rI   rb   rf   definition_errorsr   rJ   rJ   rK   r   Q  s    
z%BasicErrorHandler._insert_logic_errorc                 C   s6   |d }|d s|   n|D ]}| ||  qd S )Nre   )r   r   )rI   Z
error_listr   r   rJ   rJ   rK   r   a  s
    
z$BasicErrorHandler._purge_empty_dictsr   c                 C   s*   |j r| || n|jr&| || dS )zY
        Recursively rewrites the error path to correctly represent logic errors
        N)r]   _rewrite_logic_error_pathr[   _rewrite_group_error_path)rI   rb   offsetrJ   rJ   rK   r   i  s    z%BasicErrorHandler._rewrite_error_pathc                 C   sD   t |j| }|jD ]*}|j|d  }|j| |_| || qd S rA   )r_   rB   r\   r   )rI   rb   r   child_startr   relative_pathrJ   rJ   rK   r   r  s
    
z+BasicErrorHandler._rewrite_group_error_pathc           
      C   sx   t |j| }|j D ]Z\}}|s&qd|j|f }|j|f }|D ],}|j|d  }	||	 |_| ||d  qDqd S )Nz%s definition %sr   )r_   rB   rd   r    rE   r   )
rI   rb   r   r   rc   r   nodenamer}   r   rel_pathrJ   rJ   rK   r   {  s    
z+BasicErrorHandler._rewrite_logic_error_path)N)r   )r   )r   )rW   rl   rm   rn   r   rL   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   r     sn   '



	
	r   c                   @   s   e Zd Zej Zded< dS )SchemaErrorHandlerzunknown ruler   N)rW   rl   rm   r   r   r   rJ   rJ   rJ   rK   r     s   
r   )Krn   
__future__r   collectionsr   r   r   r   	functoolsr   pprintr   Zcerberus.platformr	   r
   Zcerberus.utilsr   r   r   ZCUSTOMZDOCUMENT_MISSINGZREQUIRED_FIELDZUNKNOWN_FIELDZDEPENDENCIES_FIELDZDEPENDENCIES_FIELD_VALUEZEXCLUDES_FIELDZDOCUMENT_FORMATZEMPTY_NOT_ALLOWEDZNOT_NULLABLEZBAD_TYPEZBAD_TYPE_FOR_SCHEMAZITEMS_LENGTHZ
MIN_LENGTHZ
MAX_LENGTHZREGEX_MISMATCHZ	MIN_VALUEZ	MAX_VALUEZUNALLOWED_VALUEZUNALLOWED_VALUESZFORBIDDEN_VALUEZFORBIDDEN_VALUESZMISSING_MEMBERSrj   ZCOERCION_FAILEDZRENAMING_FAILEDZREADONLY_FIELDZSETTING_DEFAULT_FAILEDrh   ZMAPPING_SCHEMAZSEQUENCE_SCHEMAZ	KEYSRULESZ	KEYSCHEMAZVALUESRULESZVALUESCHEMAZ	BAD_ITEMSri   ZNONEOFZONEOFZANYOFZALLOFZSCHEMA_ERROR_DEFINITION_TYPEZSCHEMA_ERROR_MISSINGobjectr@   r^   rp   ry   r   r   r   r   r   r   r   r   rJ   rJ   rJ   rK   <module>   s~   





































hI7		H ?