a $](@sdZddlmZmZddlZddlZddlZddlZddlm Z ddl m Z m Z e ddgZe Ze ZGdd d eZGd d d eZGd d d eZejddGdddeZGdddeZGdddeZGdddeZGdddeZeefddZeZefddZdS)z2 Validation errors, and some surrounding helpers. ) defaultdictdequeN)_utils)PY3 iteritemsZanyOfZoneOfc seZdZedddeeeddf fdd ZddZddZerBeZnd d Ze d d Z e d dZ e ddZ ddZddZZS)_ErrorNc stt|||||||||| | ||_t||_|_t| |_|_t ||_ ||_ |_ ||_ ||_||_||_| |_|D] } || _qdSN)superr__init__messagerpath relative_path schema_pathrelative_schema_pathlistcontextcause __cause__ validatorvalidator_valueinstanceschemaparent) selfr rr rrrrrrrerror __class__r9/usr/lib/python3.9/site-packages/jsonschema/exceptions.pyr s0    z_Error.__init__cCsd|jj|jfS)Nz<%s: %r>)r__name__r rrrr__repr__=sz_Error.__repr__c Cs|j|j|j|jf}tdd|Dr,|jStj|jdd}tj|jdd}|jt d |j|j t t|jddt ||jt |jt |fS)Ncss|]}|tuVqdSr )_unset).0mrrr Dz%_Error.__unicode__..Hwidthzl Failed validating %r in %s%s: %s On %s%s: %s )rrrranyr pprintpformattextwrapdedentrstrip!_word_for_schema_in_error_messagerZformat_as_indexrrindent#_word_for_instance_in_error_messager)rZessential_for_verbosepschema pinstancerrr __unicode__@s   z_Error.__unicode__cCst|dSNzutf-8Zunicodeencoder rrr__str__^sz_Error.__str__cCs|fi|Sr  _contents)clsotherrrr create_fromasz_Error.create_fromcCs2|j}|dur|jSt|j}|t|j|Sr )rrr extendleftreversed absolute_pathrrr rrrrBes  z_Error.absolute_pathcCs2|j}|dur|jSt|j}|t|j|Sr )rrrr@rAabsolute_schema_pathrCrrrrDos  z_Error.absolute_schema_pathcKs0t|D]"\}}t||turt|||qdSr )rgetattrr"setattr)rkwargskvrrr_setysz _Error._setcsd}tfdd|DS)N) r rrrrr rrrrc3s|]}|t|fVqdSr )rE)r#attrr rrr%r&z#_Error._contents..)dict)rattrsrr rr<~sz_Error._contents)r __module__ __qualname__r"r r!r6rr: classmethodr?propertyrBrDrJr< __classcell__rrrrrs.'   rc@seZdZdZdZdZdS)ValidationErrorz: An instance was invalid under a provided schema. rrNrrNrO__doc__r1r3rrrrrSsrSc@seZdZdZdZdZdS) SchemaErrorzB A schema was invalid under its corresponding metaschema. Z metaschemarNrTrrrrrVsrVT)hashc@s eZdZdZeZddZdS)RefResolutionErrorz& A ref could not be resolved. cCs t|jSr )str_causer rrrr:szRefResolutionError.__str__N)rrNrOrUrKZibrZr:rrrrrXsrXc@s2eZdZdZddZddZer&eZnddZdS) UndefinedTypeCheckzN A type checker was asked to check a type it did not have registered. cCs ||_dSr type)rr]rrrr szUndefinedTypeCheck.__init__cCs d|jS)Nz'Type %r is unknown to this type checkerr\r rrrr6szUndefinedTypeCheck.__unicode__cCst|dSr7r8r rrrr:szUndefinedTypeCheck.__str__NrrNrOrUr r6rr:rrrrr[s r[c@s2eZdZdZddZddZer&eZnddZdS) UnknownTypezP A validator was asked to validate an instance against an unknown type. cCs||_||_||_dSr )r]rr)rr]rrrrrr szUnknownType.__init__cCsFtj|jdd}tj|jdd}td|jt |t |fS)Nr'r(z Unknown type %r for validator with schema: %s While checking instance: %s ) r,r-rrr.r/r0r]rr2)rr4r5rrrr6s  zUnknownType.__unicode__cCst|dSr7r8r rrrr:szUnknownType.__str__Nr^rrrrr_s  r_cs<eZdZdZd fdd ZddZer,eZnddZZS) FormatErrorz% Validating a format failed. Ncs(tt|||||_||_|_dSr )r r`r r rr)rr rrrrr szFormatError.__init__cCs|jSr )r r rrrr6szFormatError.__unicode__cCs |jdSr7)r r9r rrrr:szFormatError.__str__)N) rrNrOrUr r6rr:rRrrrrr`s r`c@sZeZdZdZeZdddZddZddZd d Z d d Z d dZ ddZ e ddZdS) ErrorTreezF ErrorTrees make it easier to check which validations failed. rcCsLi|_t|j|_|D]0}|}|jD] }||}q$||j|j<|j|_qdSr )errorsrrr<r rr _instance)rrbr containerelementrrrr s    zErrorTree.__init__cCs ||jvS)zC Check whether ``instance[index]`` has any errors. r;rindexrrr __contains__szErrorTree.__contains__cCs&|jtur||vr|j||j|S)a\ 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 `exceptions.LookupError`. )rcr"r<rfrrr __getitem__s  zErrorTree.__getitem__cCs||j|<dS)zB Add an error to the tree at the given ``index``. Nr;)rrgvaluerrr __setitem__szErrorTree.__setitem__cCs t|jS)zY Iterate (non-recursively) over the indices in the instance with errors. )iterr<r rrr__iter__szErrorTree.__iter__cCs|jS)z, Return the `total_errors`. ) total_errorsr rrr__len__szErrorTree.__len__cCsd|jjt|fS)Nz<%s (%s total errors)>)rrlenr rrrr!"szErrorTree.__repr__cCs&tddt|jD}t|j|S)zT The total number of errors in the entire tree, including children. css|]\}}t|VqdSr )rp)r#_Ztreerrrr%+r&z)ErrorTree.total_errors..)sumrr<rprb)rZ child_errorsrrrrn%szErrorTree.total_errorsN)r)rrNrOrUr"rcr rhrirkrmror!rQrnrrrrras racsfdd}|S)aq Create a key function that can be used to sort errors by relevance. Arguments: weak (set): a collection of validator names 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 validator names, the other error will take priority. By default, :validator:`anyOf` and :validator:`oneOf` are considered weak validators and will be superseded by other same-level validation errors. strong (set): a collection of validator names to consider to be "strong" cs |j}t|j |v|vfSr )rrpr )rrstrongweakrr relevance?szby_relevance..relevancer)rurtrvrrsr by_relevance/srwcCsNt|}t|d}|durdStt|g||d}|jrJt|j|d}q4|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 :validator:`oneOf` or :validator:`anyOf`, the *opposite* assumption is made -- i.e. the deepest error is picked, since these validators only need to match once, and any other errors may not be relevant. Arguments: errors (collections.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.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)key)rlnextmax itertoolschainrmin)rbrxZbestrrr best_matchHs& r~) rU collectionsrrr{r,r.rKZ jsonschemarZjsonschema.compatrr frozensetZ WEAK_MATCHESZSTRONG_MATCHESZUnsetr" ExceptionrrSrVsrXr[r_r`objectrarwrvr~rrrrs,  q   H