B pSe(@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 ||_ ||_ |_ ||_ ||_||_||_| |_x|D] } || _qWdS)N)superr__init__messagerpath relative_path schema_pathrelative_schema_pathlistcontextcause __cause__ validatorvalidator_valueinstanceschemaparent) selfr rr rrrrrrrerror) __class__rF/opt/alt/python37/lib/python3.7/site-packages/jsonschema/exceptions.pyr s.    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|]}|tkVqdS)N)_unset).0mrrr Dsz%_Error.__unicode__..H)widthzl 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|dS)Nzutf-8)unicodeencode)rrrr__str__^sz_Error.__str__cCs|f|S)N) _contents)clsotherrrr create_fromasz_Error.create_fromcCs2|j}|dkr|jSt|j}|t|j|S)N)rr r extendleftreversed absolute_path)rrr rrrr;es  z_Error.absolute_pathcCs2|j}|dkr|jSt|j}|t|j|S)N)rrrr9r:absolute_schema_path)rrr rrrr<os  z_Error.absolute_schema_pathcKs4x.t|D]"\}}t||tkr t|||q WdS)N)rgetattrrsetattr)rkwargskvrrr_setysz _Error._setcsd}tfdd|DS)N) r rrrrr rrrrc3s|]}|t|fVqdS)N)r=)r attr)rrrr"sz#_Error._contents..)dict)rZattrsr)rrr5~sz_Error._contents)r __module__ __qualname__rr rr1rr4 classmethodr8propertyr;r<rBr5 __classcell__rr)rrrs&  rc@seZdZdZdZdZdS)ValidationErrorz: An instance was invalid under a provided schema. rrN)rrErF__doc__r,r.rrrrrJsrJc@seZdZdZdZdZdS) SchemaErrorzB A schema was invalid under its corresponding metaschema. Z metaschemarN)rrErFrKr,r.rrrrrLsrLT)hashc@s eZdZdZeZddZdS)RefResolutionErrorz& A ref could not be resolved. cCs t|jS)N)str_cause)rrrrr4szRefResolutionError.__str__N)rrErFrKrCZibrPr4rrrrrNsrNc@s2eZdZdZddZddZer&eZnddZdS) UndefinedTypeCheckzN A type checker was asked to check a type it did not have registered. cCs ||_dS)N)type)rrRrrrr szUndefinedTypeCheck.__init__cCs d|jS)Nz'Type %r is unknown to this type checker)rR)rrrrr1szUndefinedTypeCheck.__unicode__cCst|dS)Nzutf-8)r2r3)rrrrr4szUndefinedTypeCheck.__str__N)rrErFrKr r1rr4rrrrrQs rQc@s2eZdZdZddZddZer&eZnddZdS) UnknownTypezP A validator was asked to validate an instance against an unknown type. cCs||_||_||_dS)N)rRrr)rrRrrrrrr 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*r+rRrr-)rr/r0rrrr1s zUnknownType.__unicode__cCst|dS)Nzutf-8)r2r3)rrrrr4szUnknownType.__str__N)rrErFrKr r1rr4rrrrrSs  rScs<eZdZdZd fdd ZddZer,eZnddZZS) FormatErrorz% Validating a format failed. Ncs(tt|||||_||_|_dS)N)r rTr r rr)rr r)rrrr szFormatError.__init__cCs|jS)N)r )rrrrr1szFormatError.__unicode__cCs |jdS)Nzutf-8)r r3)rrrrr4szFormatError.__str__)N) rrErFrKr r1rr4rIrr)rrrTs rTc@sZeZdZdZeZdddZddZddZd d Z d d Z d dZ ddZ e ddZdS) ErrorTreezF ErrorTrees make it easier to check which validations failed. rcCsTi|_t|j|_x<|D]4}|}x|jD] }||}q(W||j|j<|j|_qWdS)N)errorsrrr5r rr _instance)rrVrZ containerelementrrrr s     zErrorTree.__init__cCs ||jkS)zC Check whether ``instance[index]`` has any errors. )r5)rindexrrr __contains__szErrorTree.__contains__cCs&|jtk r||kr|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`. )rWrr5)rrYrrr __getitem__s  zErrorTree.__getitem__cCs||j|<dS)zB Add an error to the tree at the given ``index``. N)r5)rrYvaluerrr __setitem__szErrorTree.__setitem__cCs t|jS)zY Iterate (non-recursively) over the indices in the instance with errors. )iterr5)rrrr__iter__szErrorTree.__iter__cCs|jS)z, Return the `total_errors`. ) total_errors)rrrr__len__szErrorTree.__len__cCsd|jjt|fS)Nz<%s (%s total errors)>)rrlen)rrrrr"szErrorTree.__repr__cCs&tddt|jD}t|j|S)zT The total number of errors in the entire tree, including children. css|]\}}t|VqdS)N)rb)r _Ztreerrrr"+sz)ErrorTree.total_errors..)sumrr5rbrV)rZ child_errorsrrrr`%szErrorTree.total_errorsN)r)rrErFrKrrWr rZr[r]r_rarrHr`rrrrrUs rUcsfdd}|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 |k|kfS)N)rrbr )rr)strongweakrr relevance?szby_relevance..relevancer)rfrergr)rerfr by_relevance/srhcCsRt|}t|d}|dkrdStt|g||d}x|jrLt|j|d}q6W|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)r^nextmax itertoolschainrmin)rVriZbestrrr best_matchHs& ro) rK collectionsrrrlr'r)rCZ jsonschemarZjsonschema.compatrr frozensetZ WEAK_MATCHESZSTRONG_MATCHESZUnsetr ExceptionrrJrLsrNrQrSrTobjectrUrhrgrorrrrs,  q   H