o
    ȳgL`                     @  sh  d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ejr=dd	lmZ ed
ZG dd deZG dd deZG dd deZG dd deZG dd deZejdejdZG dd deZG dd deZG dd deZG dd deZ G dd  d eZ!G d!d" d"eZ"G d#d$ d$e"Z#G d%d& d&e!Z$dS )'z-Validation classes for various types of data.    )annotationsN)ABCabstractmethod)zip_longest)
attrgetter)ValidationError)ChangedInMarshmallow4Warning)types_Tc                   @  s@   e Zd ZU dZdZded< dddZdd	d
ZedddZ	dS )	ValidatorzAbstract base class for validators.

    .. note::
        This class does not provide any validation behavior. It is only used to
        add a useful `__repr__` implementation for validators.
    N
str | Noneerrorreturnstrc                 C  s8   |   }|r| dnd}d| jj d| d| jdS )N,  <(zerror=z)>)
_repr_args	__class____name__r   )selfargs r   P/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/marshmallow/validate.py__repr__   s   zValidator.__repr__c                 C  s   dS )zbA string representation of the args passed to this validator. Used by
        `__repr__`.
        r   r   r   r   r   r   r   %   s   zValidator._repr_argsvalue
typing.Anyc                 C     d S Nr   r   r   r   r   r   __call__+      zValidator.__call__r   r   r   r   r   r   )
r   
__module____qualname____doc__r   __annotations__r   r   r   r"   r   r   r   r   r      s   
 

r   c                   @  s8   e Zd ZdZdZdddd	d
ZdddZdddZdS )Anda3  Compose multiple validators and combine their error messages.

    Example: ::

        from marshmallow import validate, ValidationError


        def is_even(value):
            if value % 2 != 0:
                raise ValidationError("Not an even value.")


        validator = validate.And(validate.Range(min=0), is_even)
        validator(-1)
        # ValidationError: ['Must be greater than or equal to 0.', 'Not an even value.']

    :param validators: Validators to combine.
    :param error: Error message to use when a validator returns ``False``.
    zInvalid value.Nr   
validatorstypes.Validatorr   r   c                G  s   t || _|p	| j| _d S r    )tupler,   default_error_messager   )r   r   r,   r   r   r   __init__F   s   
zAnd.__init__r   r   c                 C     d| j S )Nzvalidators=)r,   r   r   r   r   r   J      zAnd._repr_argsr   r   c                 C  s   g }i }| j D ]I}z||}t|ts$|du r$tjdtdd t| jW q tyP } z||j	 t|j
tr@||j
 n||j
 W Y d }~qd }~ww |r[t|fi ||S )NFzTReturning `False` from a validator is deprecated. Raise a `ValidationError` instead.   )
stacklevel)r,   
isinstancer   warningswarnr   r   r   updatekwargsmessagesdictappendextend)r   r   errorsr9   	validatorrerrr   r   r   r"   M   s.   

zAnd.__call__)r,   r-   r   r   r$   r%   )r   r&   r'   r(   r/   r0   r   r"   r   r   r   r   r*   /   s    
r*   c                   @  sf   e Zd ZdZG dd dZe ZdZh dZdddddd	dddZdddZ	dddZ
dddZdS ) URLa  Validate a URL.

    :param relative: Whether to allow relative URLs.
    :param absolute: Whether to allow absolute URLs.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}`.
    :param schemes: Valid schemes. By default, ``http``, ``https``,
        ``ftp``, and ``ftps`` are allowed.
    :param require_tld: Whether to reject non-FQDN hostnames.
    c                   @  s(   e Zd Zdd Zdd	d
ZdddZdS )zURL.RegexMemoizerc                 C  s
   i | _ d S r    )	_memoizedr   r   r   r   r0   s   s   
zURL.RegexMemoizer.__init__relativeboolabsoluterequire_tldr   typing.Patternc                C  st   g d}|s| d ddddd|dd	f}d
}|r+|r&d|d|f}n
d|f}nd||f}td|tjS )N)zM(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)	localhostz"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}z\[[A-F0-9]*:[A-F0-9:]+\]z+(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.?)r   z(?:[a-z0-9\.\-\+]*)://z1(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?z(?:|)z	(?::\d+)?z(?:/?|[/?]\S+)\Zz^(z)?^)r<   joinrecompile
IGNORECASE)r   rD   rF   rG   hostname_variantsabsolute_partrelative_partpartsr   r   r   _regex_generatorv   s.   


z"URL.RegexMemoizer._regex_generatorc                C  s4   |||f}|| j vr| j|||d| j |< | j | S )NrD   rF   rG   )rC   rU   )r   rD   rF   rG   keyr   r   r   r"      s   


zURL.RegexMemoizer.__call__N)rD   rE   rF   rE   rG   rE   r   rH   )r   r&   r'   r0   rU   r"   r   r   r   r   RegexMemoizerr   s    
6rX   zNot a valid URL.>   ftpftpshttphttpsFTN)rD   rF   schemesrG   r   rD   rE   rF   r]   types.StrSequenceOrSet | NonerG   r   r   c                C  s>   |s|st d|| _|| _|p| j| _|p| j| _|| _d S )Nz>URL validation cannot set both relative and absolute to False.)
ValueErrorrD   rF   default_messager   default_schemesr]   rG   )r   rD   rF   r]   rG   r   r   r   r   r0      s   	
zURL.__init__r   r   c                 C     d| j d| jS )Nz	relative=z, absolute=)rD   rF   r   r   r   r   r         zURL._repr_argsc                 C     | j j|dS N)inputr   formatr!   r   r   r   _format_error      zURL._format_errorr   c                 C  s   |  |}|st|d }d|v r#|dd  }|| jvr#t|| j| j| j| jd}|dkrB|	drB|
|ddd}n|
|}|sMt||S )Nz://r   rV   filezfile:///zfile://localhost/   )ri   r   splitlowerr]   _regexrD   rF   rG   
startswithsearchreplace)r   r   messageschemeregexmatchedr   r   r   r"      s"   


zURL.__call__)
rD   rE   rF   rE   r]   r^   rG   rE   r   r   r$   r   r   r   r   )r   r&   r'   r(   rX   ro   r`   ra   r0   r   ri   r"   r   r   r   r   rB   f   s    E

rB   c                   @  sd   e Zd ZdZedejejB ZedejejB Z	dZ
dZdddd
dZdddZdddZdS )EmailzValidate an email address.

    :param error: Error message to raise in case of a validation error. Can be
        interpolated with `{input}`.
    z(^[-!#$%&'*+/=?^`{}|~\w]+(\.[-!#$%&'*+/=?^`{}|~\w]+)*\Z|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"\Z)z(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,})\Z|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]\Z)rI   zNot a valid email address.Nr+   r   r   c                C  s   |p| j | _d S r    )r`   r   )r   r   r   r   r   r0     s   zEmail.__init__r   r   r   c                 C  rd   re   rg   r!   r   r   r   ri     rj   zEmail._format_errorc                 C  s   |  |}|rd|vrt||dd\}}| j|s!t||| jvrO| j|sOz
|dd}W n t	yB   Y t|w | j|rK|S t||S )N@rl   idnaascii)
ri   r   rsplit
USER_REGEXmatchDOMAIN_WHITELISTDOMAIN_REGEXencodedecodeUnicodeError)r   r   rs   	user_partdomain_partr   r   r   r"     s$   

zEmail.__call__r   r   rw   )r   r&   r'   r(   rN   rO   rP   UNICODEr}   r   r   r`   r0   ri   r"   r   r   r   r   rx      s    



rx   c                   @  sd   e Zd ZdZdZdZdZdZdZdZ	dZ
				dd
d
d	ddddZd ddZd!ddZd"ddZd	S )#Rangea-  Validator which succeeds if the value passed to it is within the specified
    range. If ``min`` is not specified, or is specified as `None`,
    no lower bound exists. If ``max`` is not specified, or is specified as `None`,
    no upper bound exists. The inclusivity of the bounds (if they exist) is configurable.
    If ``min_inclusive`` is not specified, or is specified as `True`, then
    the ``min`` bound is included in the range. If ``max_inclusive`` is not specified,
    or is specified as `True`, then the ``max`` bound is included in the range.

    :param min: The minimum value (lower bound). If not provided, minimum
        value will not be checked.
    :param max: The maximum value (upper bound). If not provided, maximum
        value will not be checked.
    :param min_inclusive: Whether the `min` bound is included in the range.
    :param max_inclusive: Whether the `max` bound is included in the range.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}`, `{min}` and `{max}`.
    zMust be {min_op} {{min}}.zMust be {max_op} {{max}}.z.Must be {min_op} {{min}} and {max_op} {{max}}.zgreater than or equal tozgreater thanzless than or equal toz	less thanNT)min_inclusivemax_inclusiver   r   rE   r   r   r   c                C  s   || _ || _|| _|| _|| _| jj| jr| jn| jd| _| j	j| jr'| j
n| jd| _	| jj| jr6| jn| j| jr>| j
n| jd| _d S )N)min_op)max_op)r   r   )minmaxr   r   r   message_minrh   message_gte
message_gtmessage_maxmessage_lte
message_ltmessage_all)r   r   r   r   r   r   r   r   r   r0   J  s   	zRange.__init__r   r   c                 C  s$   d| j d| jd| jd| jS )Nmin=, max=z, min_inclusive=z, max_inclusive=)r   r   r   r   r   r   r   r   r   e  s   $zRange._repr_argsr   r
   rs   c                 C  s   | j p|j|| j| jdS )N)rf   r   r   )r   rh   r   r   r   r   rs   r   r   r   ri   h  s   zRange._format_errorc                 C  s   | j d ur&| jr|| j k r&n|| j kr&| jd u r| jn| j}t| ||| jd urL| jr4|| jkrLn|| jkrL| j d u rA| jn| j}t| |||S r    )	r   r   r   r   r   r   ri   r   r   r   r   r   r   r"   k  s   

zRange.__call__NN)r   rE   r   rE   r   r   r$   )r   r
   rs   r   r   r   r   r
   r   r
   )r   r&   r'   r(   r   r   r   r   r   r   r   r0   r   ri   r"   r   r   r   r   r   .  s&    

r   _SizedT)boundc                   @  sV   e Zd ZdZdZdZdZdZ		dddddddZdddZ	dddZ
dddZdS ) Lengthah  Validator which succeeds if the value passed to it has a
    length between a minimum and maximum. Uses len(), so it
    can work for strings, lists, or anything with length.

    :param min: The minimum length. If not provided, minimum length
        will not be checked.
    :param max: The maximum length. If not provided, maximum length
        will not be checked.
    :param equal: The exact length. If provided, maximum and minimum
        length will not be checked.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}`, `{min}` and `{max}`.
    z"Shorter than minimum length {min}.z!Longer than maximum length {max}.z'Length must be between {min} and {max}.zLength must be {equal}.N)equalr   r   
int | Noner   r   r   r   c                C  s8   |d urt ||grtd|| _|| _|| _|| _d S )NzVThe `equal` parameter was provided, maximum or minimum parameter must not be provided.)anyr_   r   r   r   r   )r   r   r   r   r   r   r   r   r0     s   
zLength.__init__r   r   c                 C  s   d| j d| jd| jS )Nr   r   z, equal=)r   r   r   r   r   r   r   r     s   zLength._repr_argsr   r   rs   c                 C  s   | j p|j|| j| j| jdS )N)rf   r   r   r   )r   rh   r   r   r   r   r   r   r   ri     s   
zLength._format_errorc                 C  s   t |}| jd ur|| jkrt| || j|S | jd ur6|| jk r6| jd u r+| jn| j}t| ||| jd urS|| jkrS| jd u rH| j	n| j}t| |||S r    )
lenr   r   ri   message_equalr   r   r   r   r   )r   r   lengthrs   r   r   r   r"     s   

zLength.__call__r   )r   r   r   r   r   r   r   r   r$   )r   r   rs   r   r   r   )r   r   r   r   )r   r&   r'   r(   r   r   r   r   r0   r   ri   r"   r   r   r   r   r   ~  s    

r   c                   @  sB   e Zd ZdZdZdddddZdddZdddZdddZdS )Equala  Validator which succeeds if the ``value`` passed to it is
    equal to ``comparable``.

    :param comparable: The object to compare to.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}` and `{other}`.
    zMust be equal to {other}.Nr+   r   r   c                C  s   || _ |p| j| _d S r    )
comparabler`   r   )r   r   r   r   r   r   r0     s   zEqual.__init__r   r   c                 C  r1   )Nzcomparable=)r   r   r   r   r   r     r2   zEqual._repr_argsr   r
   c                 C     | j j|| jdS )N)rf   other)r   rh   r   r!   r   r   r   ri        zEqual._format_errorc                 C  s   || j krt| ||S r    )r   r   ri   r!   r   r   r   r"     s   
zEqual.__call__r   r$   )r   r
   r   r   r   	r   r&   r'   r(   r`   r0   r   ri   r"   r   r   r   r   r     s    

r   c                   @  sd   e Zd ZdZdZ	ddddddZdddZdddZej	dddZ
ej	d ddZ
dd Z
dS )!Regexpa  Validator which succeeds if the ``value`` matches ``regex``.

    .. note::

        Uses `re.match`, which searches for a match at the beginning of a string.

    :param regex: The regular expression string to use. Can also be a compiled
        regular expression pattern.
    :param flags: The regexp flags to use, for example re.IGNORECASE. Ignored
        if ``regex`` is not a string.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}` and `{regex}`.
    z'String does not match expected pattern.r   Nr+   ru   str | bytes | typing.Patternflagsintr   r   c                C  s0   t |ttfrt||n|| _|p| j| _d S r    )r5   r   bytesrN   rO   ru   r`   r   )r   ru   r   r   r   r   r   r0     s   zRegexp.__init__r   r   c                 C  r1   )Nzregex=)ru   r   r   r   r   r     r2   zRegexp._repr_argsr   str | bytesc                 C  s   | j j|| jjdS )N)rf   ru   )r   rh   ru   patternr!   r   r   r   ri     rc   zRegexp._format_errorc                 C  r   r    r   r!   r   r   r   r"     r#   zRegexp.__call__r   c                 C  r   r    r   r!   r   r   r   r"     r#   c                 C  s"   | j |d u rt| ||S r    )ru   r~   r   ri   r!   r   r   r   r"     s   )r   )ru   r   r   r   r   r   r$   )r   r   r   r   rw   )r   r   r   r   )r   r&   r'   r(   r`   r0   r   ri   typingoverloadr"   r   r   r   r   r     s    

r   c                   @  sB   e Zd ZdZdZdddd	d
ZdddZdddZdddZdS )	Predicatea  Call the specified ``method`` of the ``value`` object. The
    validator succeeds if the invoked method returns an object that
    evaluates to True in a Boolean context. Any additional keyword
    argument will be passed to the method.

    :param method: The name of the method to invoke.
    :param error: Error message to raise in case of a validation error.
        Can be interpolated with `{input}` and `{method}`.
    :param kwargs: Additional keyword arguments to pass to the method.
    Invalid input.Nr+   methodr   r   r   c                K  s   || _ |p| j| _|| _d S r    )r   r`   r   r9   )r   r   r   r9   r   r   r   r0     s   
zPredicate.__init__r   c                 C  rb   )Nzmethod=z	, kwargs=)r   r9   r   r   r   r   r     rc   zPredicate._repr_argsr   r   c                 C  r   )N)rf   r   )r   rh   r   r!   r   r   r   ri   !  r   zPredicate._format_errorr
   c                 C  s.   t || j}|di | jst| ||S )Nr   )getattrr   r9   r   ri   )r   r   r   r   r   r   r"   $  s   zPredicate.__call__)r   r   r   r   r$   )r   r   r   r   r   r   r   r   r   r   r     s    

r   c                   @  sB   e Zd ZdZdZdddd	d
ZdddZdddZdddZdS )NoneOfzValidator which fails if ``value`` is a member of ``iterable``.

    :param iterable: A sequence of invalid values.
    :param error: Error message to raise in case of a validation error. Can be
        interpolated using `{input}` and `{values}`.
    r   Nr+   iterabletyping.Iterabler   r   c                C  s.   || _ ddd | j D | _|p| j| _d S )Nr   c                 s      | ]}t |V  qd S r    r   ).0eachr   r   r   	<genexpr>9      z"NoneOf.__init__.<locals>.<genexpr>)r   rM   values_textr`   r   )r   r   r   r   r   r   r0   7  s   zNoneOf.__init__r   r   c                 C  r1   )Nz	iterable=)r   r   r   r   r   r   <  r2   zNoneOf._repr_argsc                 C  r   )N)rf   values)r   rh   r   r!   r   r   r   ri   ?  r   zNoneOf._format_errorr   r   c                 C  s4   z|| j v rt| |W |S  ty   Y |S w r    )r   r   ri   	TypeErrorr!   r   r   r   r"   B  s   
zNoneOf.__call__)r   r   r   r   r$   r%   r   r   r   r   r   r   -  s    

r   c                   @  sT   e Zd ZdZdZ	ddddddZdddZdddZdddZe	fd ddZ
dS )!OneOfaP  Validator which succeeds if ``value`` is a member of ``choices``.

    :param choices: A sequence of valid values.
    :param labels: Optional sequence of labels to pair with the choices.
    :param error: Error message to raise in case of a validation error. Can be
        interpolated with `{input}`, `{choices}` and `{labels}`.
    zMust be one of: {choices}.Nr+   choicesr   labelstyping.Iterable[str] | Noner   r   c                C  sX   || _ ddd | j D | _|d ur|ng | _ddd | jD | _|p(| j| _d S )Nr   c                 s  r   r    r   )r   choicer   r   r   r   _  r   z!OneOf.__init__.<locals>.<genexpr>c                 s  r   r    r   )r   labelr   r   r   r   a  r   )r   rM   choices_textr   labels_textr`   r   )r   r   r   r   r   r   r   r0   W  s
   zOneOf.__init__r   r   c                 C  rb   )Nzchoices=z	, labels=)r   r   r   r   r   r   r   d  rc   zOneOf._repr_argsc                 C  s   | j j|| j| jdS )N)rf   r   r   )r   rh   r   r   r!   r   r   r   ri   g  s   
zOneOf._format_errorr   r   c              
   C  sH   z|| j vrt| |W |S  ty# } zt| ||d }~ww r    )r   r   ri   r   )r   r   r   r   r   r   r"   l  s   
zOneOf.__call__valuegetter/str | typing.Callable[[typing.Any], typing.Any]'typing.Iterable[tuple[typing.Any, str]]c                   s8   t  r nt  t| j| jdd} fdd|D S )a  Return a generator over the (value, label) pairs, where value
        is a string associated with each choice. This convenience method
        is useful to populate, for instance, a form select field.

        :param valuegetter: Can be a callable or a string. In the former case, it must
            be a one-argument callable which returns the value of a
            choice. In the latter case, the string specifies the name
            of an attribute of the choice objects. Defaults to `str()`
            or `str()`.
        r   )	fillvaluec                 3  s     | ]\}} ||fV  qd S r    r   )r   r   r   r   r   r   r     s    z OneOf.options.<locals>.<genexpr>)callabler   r   r   r   )r   r   pairsr   r   r   optionsu  s   zOneOf.optionsr    )r   r   r   r   r   r   r$   r%   )r   r   r   r   )r   r&   r'   r(   r`   r0   r   ri   r"   r   r   r   r   r   r   r   L  s    


r   c                      0   e Zd ZdZdZd fddZdd	d
Z  ZS )ContainsOnlya1  Validator which succeeds if ``value`` is a sequence and each element
    in the sequence is also in the sequence passed as ``choices``. Empty input
    is considered valid.

    :param choices: Same as :class:`OneOf`.
    :param labels: Same as :class:`OneOf`.
    :param error: Same as :class:`OneOf`.

    .. versionchanged:: 3.0.0b2
        Duplicate values are considered valid.
    .. versionchanged:: 3.0.0b2
        Empty input is considered valid. Use `validate.Length(min=1) <marshmallow.validate.Length>`
        to validate against empty inputs.
    z:One or more of the choices you made was not in: {choices}.r   r   c                       d dd |D }t |S )Nr   c                 s  r   r    r   r   valr   r   r   r     r   z-ContainsOnly._format_error.<locals>.<genexpr>rM   superri   r   r   
value_textr   r   r   ri        zContainsOnly._format_errorr   typing.Sequence[_T]c                 C  s&   |D ]}|| j vrt| |q|S r    )r   r   ri   r   r   r   r   r   r   r"     s
   
zContainsOnly.__call__r$   r   r   r   r   r   r&   r'   r(   r`   ri   r"   __classcell__r   r   r   r   r     s
    r   c                      r   )ContainsNoneOfa)  Validator which fails if ``value`` is a sequence and any element
    in the sequence is a member of the sequence passed as ``iterable``. Empty input
    is considered valid.

    :param iterable: Same as :class:`NoneOf`.
    :param error: Same as :class:`NoneOf`.

    .. versionadded:: 3.6.0
    z5One or more of the choices you made was in: {values}.r   r   c                   r   )Nr   c                 s  r   r    r   r   r   r   r   r     r   z/ContainsNoneOf._format_error.<locals>.<genexpr>r   r   r   r   r   ri     r   zContainsNoneOf._format_errorr   r   c                 C  s&   |D ]}|| j v rt| |q|S r    )r   r   ri   r   r   r   r   r"     s
   
zContainsNoneOf.__call__r$   r   r   r   r   r   r   r     s
    
r   )%r(   
__future__r   rN   r   r6   abcr   r   	itertoolsr   operatorr   marshmallow.exceptionsr   marshmallow.warningsr   TYPE_CHECKINGmarshmallowr	   TypeVarr
   r   r*   rB   rx   r   Sizedr   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s8    
7 <MB0"=