a
    `                     @   s<   d dl mZmZ d dlmZ d dlmZ G dd deZdS )   )CodeGeneratorDraft04JSON_TYPE_TO_PYTHON_TYPE)JsonSchemaDefinitionException)enforce_listc                       s~   e Zd Zeejfi ddddZdi df f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  ZS )CodeGeneratorDraft06z^(/(([^/~])|(~[01]))*)*\Zz'^(\w+:(\/?\/?))?[^#\\\s]*(#[^\\\s]*)?\Zz^(?:(?:[^\x00-\x20\"\'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*\Z)zjson-pointerzuri-referencezuri-templateNTc                    sJ   t  |||| | jd| jfd| jfd| jfd| jfd| jff d S )NexclusiveMinimumexclusiveMaximumpropertyNamescontainsconst)	super__init__Z_json_keywords_to_functionupdategenerate_exclusive_minimumgenerate_exclusive_maximumgenerate_property_namesgenerate_containsgenerate_const)self
definitionZresolverformatsZuse_default	__class__ 5lib/python3.9/site-packages/fastjsonschema/draft06.pyr      s    zCodeGeneratorDraft06.__init__c                 C   s4   t |tr|   nd|v r&|   n
| | d S )Nz$ref)
isinstanceboolgenerate_boolean_schemaZgenerate_refZrun_generate_functions)r   r   r   r   r   _generate_func_code_block   s
    


z.CodeGeneratorDraft06._generate_func_code_blockc                 C   s   | j du r| d dS )z
        Means that schema can be specified by boolean.
        True means everything is valid, False everything is invalid.
        F{name} must not be thereN)_definitionexcr   r   r   r   r   %   s    
z,CodeGeneratorDraft06.generate_boolean_schemac              
   C   s   t | jd }zddd |D }W n2 tyX } ztd|W Y d}~n
d}~0 0 d}d|v rx|d	j| jd
7 }d|v sd|v rd|vr|dj| jd
7 }| d||& | jdd|dd W d   n1 s0    Y  dS )z
        Validation of type. Can be one type or list of types.

        Since draft 06 a float without fractional part is an integer.

        .. code-block:: python

            {'type': 'string'}
            {'type': ['string', 'number']}
        typez, c                 s   s   | ]}t | V  qd S )N)r   ).0tr   r   r   	<genexpr>:       z5CodeGeneratorDraft06.generate_type.<locals>.<genexpr>zUnknown type: {}N ZintegerzD and not (isinstance({variable}, float) and {variable}.is_integer()))variableZnumberZbooleanz  or isinstance({variable}, bool)z&if not isinstance({variable}, ({})){}:z{name} must be {}z or Zrule)	r   r    joinKeyErrorr   format	_variablelr!   )r   typesZpython_typesr!   Zextrar   r   r   generate_type-   s    $z"CodeGeneratorDraft06.generate_typec              	   C   s   |  dd t| jd ttfs(td|  d | jddd W d    n1 sV0    Y  W d    n1 st0    Y  d S )N(if isinstance({variable}, (int, float)):r   z.exclusiveMinimum must be an integer or a floatz$if {variable} <= {exclusiveMinimum}:z-{name} must be bigger than {exclusiveMinimum}r*   r/   r   r    intfloatr   r!   r"   r   r   r   r   K   s
    z/CodeGeneratorDraft06.generate_exclusive_minimumc              	   C   s   |  dd t| jd ttfs(td|  d | jddd W d    n1 sV0    Y  W d    n1 st0    Y  d S )Nr2   r   z.exclusiveMaximum must be an integer or a floatz$if {variable} >= {exclusiveMaximum}:z.{name} must be smaller than {exclusiveMaximum}r*   r3   r"   r   r   r   r   R   s
    z/CodeGeneratorDraft06.generate_exclusive_maximumc                 C   s  | j di }|du rn|du rf|   | d | jddd W d   n1 sX0    Y  nV|   | d0 |   | d	 | d
 | d | d, | j|d| j	| j
dd W d   n1 s0    Y  | d | d W d   n1 s0    Y  W d   n1 s80    Y  | d | jddd W d   n1 sr0    Y  W d   n1 s0    Y  W d   n1 s0    Y  dS )aB  
        Means that keys of object must to follow this definition.

        .. code-block:: python

            {
                'propertyNames': {
                    'maxLength': 3,
                },
            }

        Valid keys of object for this definition are foo, bar, ... but not foobar for example.
        r	   TFzif {variable}_keys:r   r*   Nzif {variable}_is_dict:zif {variable}_len != 0:z {variable}_property_names = True!for {variable}_key in {variable}:try:{}_keyZclear_variablesz except JsonSchemaValueException:z!{variable}_property_names = Falsez!if not {variable}_property_names:z/{name} must be named by propertyName definition)r    getZcreate_variable_keysr/   r!   Zcreate_variable_is_dictZcreate_variable_with_lengthgenerate_func_code_blockr-   r.   _variable_name)r   Zproperty_names_definitionr   r   r   r   Y   s0    0

$Jz,CodeGeneratorDraft06.generate_property_namesc              
   C   st  |    | dJ | jd }|du r:| jddd n|du r|| d | jddd W d	   n1 sp0    Y  n| d
 | dt | d@ | j|d| j| jdd | d | d W d	   n1 s0    Y  | d W d	   n1 s0    Y  | d | jddd W d	   n1 sF0    Y  W d	   n1 sf0    Y  d	S )a  
        Means that array must contain at least one defined item.

        .. code-block:: python

            {
                'contains': {
                    'type': 'number',
                },
            }

        Valid array is any with at least one number.
        zif {variable}_is_list:r
   Fz{name} is always invalidr*   Tzif not {variable}:z{name} must not be emptyNz{variable}_contains = Falser6   r7   r8   r9   z{variable}_contains = Truebreakz%except JsonSchemaValueException: passzif not {variable}_contains:z.{name} must contain one of contains definition)Zcreate_variable_is_listr/   r    r!   r;   r-   r.   r<   )r   Zcontains_definitionr   r   r   r      s,    
.


(*z&CodeGeneratorDraft06.generate_containsc                 C   sb   | j d }t|tr$d| |}| d| | jddd W d   n1 sT0    Y  dS )z
        Means that value is valid when is equeal to const definition.

        .. code-block:: python

            {
                'const': 42,
            }

        Only valid value is 42 in this example.
        r   z"{}"zif {variable} != {}:z:{name} must be same as const definition: {definition_rule}r*   N)r    r   strr-   er/   r!   )r   r   r   r   r   r      s
    

z#CodeGeneratorDraft06.generate_const)__name__
__module____qualname__dictr   ZFORMAT_REGEXSr   r   r   r1   r   r   r   r   r   __classcell__r   r   r   r   r      s   


	((r   N)Zdraft04r   r   
exceptionsr   	generatorr   r   r   r   r   r   <module>   s   