`d,UdZddlmZddlmZmZddlmZddlm Z m Z ddl Z ddl Z ddl Z ddlmZedd gZd ed <eZd ed <ej(ZGd deZGddeZGddeZe j4dGddeZGddeZGddeZGddeZGddeZ eefdZ!e!Z"e"fd Z#y)!z2 Validation errors, and some surrounding helpers. ) annotations) defaultdictdeque)pformat)dedentindentN)_utilsanyOfoneOfzfrozenset[str] WEAK_MATCHESSTRONG_MATCHESc eZdZedddeeeddef fd ZdZdZedZe dZ e dZ e d Z d d Z d Zd ZxZS)_ErrorNc Ttt| ||||||||| | ||_t |x|_|_t | x|_|_t||_ |x|_ |_ ||_ ||_||_||_| |_| |_|D] } || _ yN)superr__init__messagerpath relative_path schema_pathrelative_schema_pathlistcontextcause __cause__ validatorvalidator_valueinstanceschemaparent _type_checker)selfrrrrrrr r!rr" type_checkererror __class__s 7/usr/lib/python3/dist-packages/jsonschema/exceptions.pyrz_Error.__init__s fd$            ).t4 D&7<[7II44G} &++ T^".    ) EEL cPd|jjd|jdS)N)r'__name__rr$s r(__repr__z_Error.__repr__@s'4>>**+2dll-=Q??r)c|j|j|j|jf}t d|Dr |j St j|jt|jdd}t j|j|j}d}td|j d|jd|dtt|jd |j!d |dtt|jd |j!d j#S) Nc3,K|] }|tuywr)_unset).0ms r( z!_Error.__str__..Gs:qqF{:s) containerindices z z Failed validating z in z: Hwidthz On )rrr r!anyrr format_as_index!_word_for_schema_in_error_messagerr#_word_for_instance_in_error_messagerrrrlstriprstrip)r$essential_for_verboser instance_pathprefixs r(__str__z_Error.__str__Cs5 NND00$--!  :$9: :<< ,,<<223CR8 ..>>&&  \\N#~~0[MB 26?FFHIJ R8&AHHJKL    r)c.|di|jS)Nr _contents)clsothers r( create_fromz_Error.create_from`s'U__&''r)c|j}| |jSt|j}|jt |j |Sr)r"rr extendleftreversed absolute_pathr$r"rs r(rQz_Error.absolute_pathdsH >%% %T''( !5!567 r)c|j}| |jSt|j}|jt |j |Sr)r"rrrOrPabsolute_schema_pathrRs r(rTz_Error.absolute_schema_pathnsH >,, ,T../ ! r)cd}|jD]/}t|tr|dt|zdzz }(|d|zz }1|S)N$[].)rQ isinstanceintstr)r$relems r( json_pathz_Error.json_pathxsR&& #D$$c$i#--d "  #  r)c ||jtur||_|jD]%\}}t||tust |||'yr)r#r2itemsgetattrsetattr)r$r%kwargskvs r(_setz _Error._setsQ  #(:(:f(D!-D LLN $DAqtQ6)a# $r)c0d}tfd|DS)N) rrrrrrrr r!r"c3:K|]}|t|fywr)ra)r3attrr$s r(r5z#_Error._contents..sBDT74./Bs)dict)r$attrss` r(rJz_Error._contentss BEBBBr)c jd}t|tr&j j j|Stfd|DS#ttf$rYywxYw)NtypeFc3jK|]*}jjj|,ywr)r#is_typer )r3 expected_typer$s r(r5z'_Error._matches_type..s0     & &t}}m D s03) r!KeyError TypeErrorrZr\r#ror r>)r$expecteds` r( _matches_typez_Error._matches_typesr {{6*H h $%%--dmmXF F !)   )$  sAA.-A.r)r- __module__ __qualname__r2rr/rG classmethodrMpropertyrQrTr^rfrJrt __classcell__r's@r(rrs ' R@ :(($C  r)rceZdZdZdZdZy)ValidationErrorz: An instance was invalid under a provided schema. r!r Nr-rurv__doc__r@rArr)r(r|r|s)1%*4'r)r|ceZdZdZdZdZy) SchemaErrorzB A schema was invalid under its corresponding metaschema. metaschemar!Nr}rr)r(rrs)5%*2'r)rT)hashc8eZdZdZej ZdZy)RefResolutionErrorz& A ref could not be resolved. c,t|jSr)r\_causer.s r(rGzRefResolutionError.__str__s4;;r)N)r-rurvr~riibrrGrr)r(rrsTWWYF r)rceZdZdZdZdZy)UndefinedTypeCheckzN A type checker was asked to check a type it did not have registered. c||_yrrm)r$rms r(rzUndefinedTypeCheck.__init__s  r)c"d|jdS)NzType z is unknown to this type checkerrr.s r(rGzUndefinedTypeCheck.__str__styym#CDDr)Nr-rurvr~rrGrr)r(rrsEr)rceZdZdZdZdZy) UnknownTypezP A validator was asked to validate an instance against an unknown type. c.||_||_||_yr)rmr r!)r$rmr r!s r(rzUnknownType.__init__s    r)c d}td|jdtt|jd|j dtt|j d|j djS)Nr9z Unknown type z, for validator with schema: r:r;z7 While checking instance: r=)rrmrrr!rBr rC)r$rFs r(rGzUnknownType.__str__s))' 26?FFHIJ R8&AHHJKL     r)Nrrr)r(rrs  r)rc*eZdZdZdfd ZdZxZS) FormatErrorz% Validating a format failed. cXtt| ||||_|x|_|_yr)rrrrrr)r$rrr's r(rzFormatError.__init__s) k4)'59 &++ T^r)c|jSr)rr.s r(rGzFormatError.__str__s ||r)r)r-rurvr~rrGryrzs@r(rrs, r)rcPeZdZdZeZd dZdZdZdZ dZ dZ dZ e d Zy ) ErrorTreezF ErrorTrees make it easier to check which validations failed. ci|_t|j|_|D]D}|}|jD]}||} ||j|j <|j |_Fyr)errorsrr'rJrrr _instance)r$rr&r7elements r(rzErrorTree.__init__sj $T^^4 1EI :: /%g.  /05I  U__ -"'..I   1r)c||jvS)zC Check whether ``instance[index]`` has any errors. rIr$indexs r( __contains__zErrorTree.__contains__s &&r)cj|jtur||vr|j||j|S)aQ Retrieve the child tree one level down at the given ``index``. If the index is not in the instance that this tree corresponds to and is not known by this tree, whatever error would be raised by ``instance.__getitem__`` will be propagated (usually this is some subclass of `LookupError`. )rr2rJrs r( __getitem__zErrorTree.__getitem__ s2 >> 'E,= NN5 !~~e$$r)c"||j|<y)zB Add an error to the tree at the given ``index``. NrI)r$rvalues r( __setitem__zErrorTree.__setitem__s!&ur)c,t|jS)zY Iterate (non-recursively) over the indices in the instance with errors. )iterrJr.s r(__iter__zErrorTree.__iter__s DNN##r)c|jS)z, Return the `total_errors`. ) total_errorsr.s r(__len__zErrorTree.__len__%s   r)cNd|jjdt|dS)Nr+z (z total errors)>)r'r-lenr.s r(r/zErrorTree.__repr__+s&4>>**+2c$i[HHr)ctd|jjD}t|j|zS)zT The total number of errors in the entire tree, including children. c38K|]\}}t|ywr)r)r3_trees r(r5z)ErrorTree.total_errors..4sKD3t9Ks)sumrJr`rr)r$ child_errorss r(rzErrorTree.total_errors.s5 KDNN4H4H4JKK 4;;,..r)N)r)r-rurvr~r2rrrrrrrr/rxrrr)r(rrsFI 1' %& $! I//r)rcfd}|S)az Create a key function that can be used to sort errors by relevance. Arguments: weak (set): a collection of validation keywords to consider to be "weak". If there are two errors at the same level of the instance and one is in the set of weak validation keywords, the other error will take priority. By default, :kw:`anyOf` and :kw:`oneOf` are considered weak keywords and will be superseded by other same-level validation errors. strong (set): a collection of validation keywords to consider to be "strong" cv|j}t|j |v|v|j fSr)rrrrt)r&rstrongweaks r( relevancezby_relevance..relevanceIsCOO _  T !  ##% %   r)r)rrrs`` r( by_relevancer8s"  r)cLt|}t|d}|yttj|g||}|j r[t jd|j |}t|dk(r||d||dk(r|S|d}|j r[|S)a Try to find an error that appears to be the best match among given errors. In general, errors that are higher up in the instance (i.e. for which `ValidationError.path` is shorter) are considered better matches, since they indicate "more" is wrong with the instance. If the resulting match is either :kw:`oneOf` or :kw:`anyOf`, the *opposite* assumption is made -- i.e. the deepest error is picked, since these keywords only need to match once, and any other errors may not be relevant. Arguments: errors (collections.abc.Iterable): the errors to select from. Do not provide a mixture of errors from different validation attempts (i.e. from different instances or schemas), since it won't produce sensical output. key (collections.abc.Callable): the key to use when sorting errors. See `relevance` and transitively `by_relevance` for more details (the default is to sort with the defaults of that function). Changing the default is only useful if you want to change the function that rates errors but still want the error context descent done by this function. Returns: the best matching error, or ``None`` if the iterable was empty .. note:: This function is a heuristic. Its return value may change for a given set of inputs from version to version if better heuristics are added. N)keyr) rnextmax itertoolschainrheapq nsmallestr)rrbestsmallests r( best_matchrWsL&\F  D | yvv.C 8D ,,??1dll< x=A #hqk"2c(1+6F"FK{ ,, Kr))$r~ __future__r collectionsrrpprintrtextwraprrrrri jsonschemar frozensetr __annotations__r Unsetr2 Exceptionrr|rsrrrrobjectrrrrrr)r(rs#*#  ('7);< n8 N %3r)