
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr  SSKJ	r	  SSK
Jr  SSKr " S	 S
\R                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)zJSON schema YAML validator module.

Usage:

  # Get a validator for the JSON schema in the file schema_path.
  validator = yaml_validator.Validator(schema_path)
  # Validate parsed YAML data.
  validator.Validate(parsed_yaml_data)
    )absolute_import)division)unicode_literalsN)
exceptions)yaml)pkg_resourcesc                       \ rS rSrSrSrg)Error(   zErrors for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       )lib/googlecloudsdk/core/yaml_validator.pyr
   r
   (       r   r
   c                       \ rS rSrSrSrg)InvalidSchemaError,   zJSON schema is invalid.r   Nr   r   r   r   r   r   ,   r   r   r   c                       \ rS rSrSrSrg)InvalidSchemaVersionError0   zJSON schema version is invalid.r   Nr   r   r   r   r   r   0   s    'r   r   c                       \ rS rSrSrSrg)RefError4   z&Ref error -- YAML $ref path not found.r   Nr   r   r   r   r   r   4   s    .r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ValidationError8   zValidation error -- YAML data does not match the schema.

Attributes:
    message: A user-readable error message describing the validation error.
c                 N   > [         [        U ]  U5        UR                  U l        g )N)superr!   __init__message)selferror	__class__s     r   r%   ValidationError.__init__?   s    	/4)%0==DLr   )r&   )r   r   r   r   r   r%   r   __classcell__)r)   s   @r   r!   r!   8   s    ! !r   r!   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
	ValidatorD   zJSON schema validator.c                   ^ ^ [         R                  R                  U5      m " UU 4S jS[        R                  5      n [
        R                  " [        R                  " U5      5      nT R                  X15        UR                  U5      n[        R                  R                  U5      " X5S9T l        T R                  R                   T l        g! [         a  n[        U5      eSnAff = f)a  "Initilaizes the schema and validator for schema_path.

The validator resolves references to all other schemas in the directory
of schema_path.

Yes, it's really this ugly defining a validator with a resolver to
pkg_resources resources.

Raises:
  IOError: if schema not found in installed resources.
  files.Error: if schema file not found.
  SchemaError: if the schema is invalid.

Args:
  schema_path: JSON schema file path.

Returns:
  The schema to validate and the validator.
c                   2   > \ rS rSrSr\U U4S j5       rSrg)'Validator.__init__.<locals>.RefResolver]   z+$ref: resolver that consults pkg_resources.c                    > [         R                  R                  TU 5      n[        R                  " U5      n [
        R                  " U5      nTR                  X15        U$ ! [         a  n[        U5      eSnAff = f)z>pkg_resources $ref override -- schema_dir closure needed here.N)
ospathjoinr   GetResourceFromFiler   load	Exceptionr   ValidateSchemaVersion)refr5   dataschemae
schema_dirr'   s        r   resolve_remote6Validator.__init__.<locals>.RefResolver.resolve_remote`   sm     ww||J,006	&99T?& 	""60  	&"1%
%	&s   A" "
A<,A77A<r   N)r   r   r   r   r   staticmethodr@   r   )r?   r'   s   r   RefResolverr1   ]   s    7	 	r   rC   N)resolver)r4   r5   dirname
jsonschemarC   r   r8   r   r7   r9   r   r:   from_schema
validatorsvalidator_for
_validatorvalidate	_validate)r'   schema_pathrC   r=   r>   rD   r?   s   `     @r   r%   Validator.__init__G   s    ( -J j,, "yy::;GHf 	v3&&v.H ++99&A#DO__--DN  "q!!"s    *C 
C*C%%C*c                      UR                  S5      nU(       a,  UR                  S5      (       a  UR                  S5      (       d  [	        SR                  X#5      5      eg! [         a    Sn N\f = f)z.Validates the parsed_yaml JSON schema version.z$schemaNzhttp://json-schema.org/z/schema#zZSchema [{}] version [{}] is invalid. Expected "$schema: http://json-schema.org/*/schema#".)getAttributeError
startswithendswithr   format)r'   r=   r5   versions       r   r:   Validator.ValidateSchemaVersionv   sv    

9%g 899Z((%//5vd/DF F )	  gs   A! !A0/A0c                      U R                  U5        g! [        R                   a  n[        U5      eSnAf[        R                   a  n[	        U5      eSnAff = f)zValidates parsed_yaml against JSON schema.

Args:
  parsed_yaml: YAML to validate

Raises:
  ValidationError: if the template doesn't obey the schema.
N)rL   rF   RefResolutionErrorr   r!   )r'   parsed_yamlr>   s      r   ValidateValidator.Validate   sM    
nn[!(( QK%% As    A3A
AAc                     U R                  U5        g! [        R                   a  n[        U5      eSnAf[        R                  R
                   Ga2  n[        R                  " 5       nUR                  SR                  U5      5        UR                  (       a  SR                  UR                  5      nOSnUR                  (       a  UR                  S   OSnU(       a#  SR                  S UR                   5       5      nOSnUR                  SR                  U(       a  UR                  OSU(       a  UR                  OSU(       a  UR                   OSUUS95        UR#                  5       Ul        [        U5      eSnAff = f)	zValidates parsed_yaml against JSON schema.

Provides details of validation failure in the returned error message.
Args:
  parsed_yaml: YAML to validate

Raises:
  ValidationError: if the template doesn't obey the schema.
Nz%ERROR: Schema validation failed: {}

zRoot Exception: {} c              3   D   #    U  H  nS R                  U5      v   M     g7f)z[{}]N)rT   ).0elems     r   	<genexpr>6Validator.ValidateWithDetailedError.<locals>.<genexpr>   s     F-ETV]]4  -Es    zAdditional Details:
Error Message: {msg}

Failing Validation Schema: {schema}

Failing Element: {instance}

Failing Element Path: {path}

{additional_cause}
)msginstancer=   r5   additional_cause)rL   rF   rX   r   r   r!   ioStringIOwriterT   causecontextr6   absolute_pathr&   re   r=   getvalue)r'   rY   r>   verd   additional_exception
root_error
error_paths           r   ValidateWithDetailedError#Validator.ValidateWithDetailedError   s2     
nn[!(( QK  00  KKMc	ii9@@DE	3::288D!%'ZZ2::b>Tj	WWFZ-E-EFH
 
	ii '
 (.v.8
**d4>Z00D0::,,#%9 (. (;
< <<>bjB9 s    F3"FD-FFc                 8    U R                   R                  U5      $ )zValidates parsed_yaml against JSON schema and returns all errors.

Args:
  parsed_yaml: YAML to validate

Raises:
  ValidationError: if the template doesn't obey the schema.

Returns:
  A list of all errors, empty if no validation errors.
)rJ   iter_errors)r'   rY   s     r   IterateValidator.Iterate   s     ??&&{33r   )rL   rJ   N)r   r   r   r   r   r%   r:   rZ   rr   rv   r   r   r   r   r-   r-   D   s!    -.^F * X4r   r-   )r   
__future__r   r   r   rg   r4   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   rF   r
   r   r   r   r!   objectr-   r   r   r   <module>r|      sw     '  ' 	 	 * $ 2  J     ( (/u /	!e 	!G4 G4r   