U `@sdZddlmZddlmZddlmZddlmZmZddlZddl m Z ddl m Z dd l mZmZmZmZmZmZmZmZdd lmZmZmZmZdd lmZmZmZe Z d d Z!Gddde"Z#Gddde"Z$Gddde%Z&dZ'Gddde(Z)e)de&fiZ*dS)a  Extensible validation for Python dictionaries. This module implements Cerberus Validator class :copyright: 2012-2016 by Nicola Iarocci. :license: ISC, see LICENSE for more details. Full documentation is available at http://python-cerberus.org )absolute_import) literal_eval)copy)datedatetimeN)warn)errors) _int_types _str_type ContainerHashableIterableMappingSequenceSized)schema_registryrules_set_registryDefinitionSchema SchemaError)drop_item_from_tuplereadonly_classpropertyTypeDefinitioncCsdd}|}||_|S)NcSs tddS)NzcDummy method called. Its purpose is to hold justvalidation constraints for a rule in its docstring.) RuntimeError)self constraintfieldvaluerC/opt/alt/python38/lib/python3.8/site-packages/cerberus/validator.pydummy*sz(dummy_for_rule_validation..dummy)__doc__)Zrule_constraintsrfrrrdummy_for_rule_validation)sr"c@seZdZdZdS) DocumentErrorzBRaised when the target document is missing or has the wrong formatN__name__ __module__ __qualname__r rrrrr#6sr#c@seZdZdZdS)_SchemaRuleTypeErrorzw Raised when a schema (list) validation encounters a mapping. Not supposed to be used outside this module. Nr$rrrrr(<sr(csJeZdZdZdZdZedeefdede fdede fe fede fded e fded efded eefded efded efe fedeefe fedefdede fdd ZeZfddZeddZddZeddZddZdddZdd Zd!d"Zd#d$Zd%d&Z d'd(Z!e"d)d*Z#e#j$d+d*Z#e"d,d-Z%e%j$d.d-Z%e"d/d0Z&e"d1d2Z'e'j$d3d2Z'e"d4d5Z(e"d6d7Z)e)j$d8d7Z)e"d9d:Z*e*j$d;d:Z*e"dd=Z+e"d?d@Z,e"dAdBZ-e"dCdDZ.e"dEdFZ/e/j$dGdFZ/e"dHdIZ0e"dJdKZ1e1j$dLdKZ1e"dMdNZ2e2j$dOdNZ2e3dPdQZ4ddRdSZ5dTdUZ6ddWdXZ7dYdZZ8d[d\Z9d]d^Z:d_d`Z;dadbZdgdhZ?didjZ@edkdlZAedmdnZBdodpZCdqdrZDdsdtZEdudvZFdwdxZGdydzZHd{d|ZIdd~dZJeJZKddZLddZMddZNeOdZPddZQddZRddZSddZTddZUddZVddZWddZXddZYddZZddZ[ddZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbeOdZcddZdddZeddZfddZgddZheOdZieOdZjddZkddZlddZmddZnddZodd„ZpZqS) BareValidatora Validator class. Normalizes and/or validates any mapping against a validation-schema which is provided as an argument at class instantiation or upon calling the :meth:`~cerberus.Validator.validate`, :meth:`~cerberus.Validator.validated` or :meth:`~cerberus.Validator.normalized` method. An instance itself is callable and executes a validation. All instantiation parameters are optional. There are the introspective properties :attr:`types`, :attr:`validators`, :attr:`coercers`, :attr:`default_setters`, :attr:`rules`, :attr:`normalization_rules` and :attr:`validation_rules`. The attributes reflecting the available rules are assembled considering constraints that are defined in the docstrings of rules' methods and is effectively used as validation schema for :attr:`schema`. :param schema: See :attr:`~cerberus.Validator.schema`. Defaults to :obj:`None`. :type schema: any :term:`mapping` :param ignore_none_values: See :attr:`~cerberus.Validator.ignore_none_values`. Defaults to ``False``. :type ignore_none_values: :class:`bool` :param allow_unknown: See :attr:`~cerberus.Validator.allow_unknown`. Defaults to ``False``. :type allow_unknown: :class:`bool` or any :term:`mapping` :param require_all: See :attr:`~cerberus.Validator.require_all`. Defaults to ``False``. :type require_all: :class:`bool` :param purge_unknown: See :attr:`~cerberus.Validator.purge_unknown`. Defaults to to ``False``. :type purge_unknown: :class:`bool` :param purge_readonly: Removes all fields that are defined as ``readonly`` in the normalization phase. :type purge_readonly: :class:`bool` :param error_handler: The error handler that formats the result of :attr:`~cerberus.Validator.errors`. When given as two-value tuple with an error-handler class and a dictionary, the latter is passed to the initialization of the error handler. Default: :class:`~cerberus.errors.BasicErrorHandler`. :type error_handler: class or instance based on :class:`~cerberus.errors.BaseErrorHandler` or :class:`tuple` )nullable)r*readonlytypeemptybinaryrboolean containerrrdictfloatintegerlistnumbersetstring) r.r/r0rrr1r2r3r4r5r6r7csd|_t|_d|_t|_t|_d|_ d|_ d|_ | ||_ ||||dd|_|dd|_|dd|_g|_tt|dS)a4 The arguments will be treated as with this signature: __init__(self, schema=None, ignore_none_values=False, allow_unknown=False, require_all=False, purge_unknown=False, purge_readonly=False, error_handler=errors.BasicErrorHandler) NrFschema allow_unknown require_all)documentr ErrorList_errors recent_errorDocumentErrorTreedocument_error_treeSchemaErrorTreeschema_error_tree document_path schema_pathupdate"_BareValidator__init_error_handler error_handler_BareValidator__store_configgetr8r9r:_remaining_rulessuperr)__init__)rargskwargs __class__rrrLs      zBareValidator.__init__cCsb|dtj}t|tr"|\}}ni}t|trFt|tjrF|f|St|tjrV|StddS)NrGzInvalid error_handler.) poprZBasicErrorHandler isinstancetupler, issubclassZBaseErrorHandlerr)rNrGZ eh_configrrrZ__init_error_handlers     z"BareValidator.__init_error_handlercCsNd}t|dt|D]*\}}||kr6td|q||||<q||_dS)z.Assign args to kwargs and store configuration.)r8ignore_none_valuesr9r: purge_unknownpurge_readonlyNz.__init__ got multiple values for argument '%s') enumeratelen TypeError_config)rrMrNZ signatureiprrrZ__store_configszBareValidator.__store_configcCs|jdS)z'Purge the cache of known valid schemas.N)_valid_schemasclearclsrrr clear_cachesszBareValidator.clear_cachesc Gst|dkr\|j|d|j|dD](}|j||j||j|q.n:t|dkrt |dt r| |dt j |dnt|dkr|d}|dj}|dj}|dd}|j|f}|j}|t jjkr|dk r|||f7}|sd} n^|||j|} |dkr4| |d} n0|dkr\| ||j} || krdd}n| |} |j|} t ||||| | ||_| |jgdS) a Creates and adds one or multiple errors. :param args: Accepts different argument's signatures. *1. Bulk addition of errors:* - :term:`iterable` of :class:`~cerberus.errors.ValidationError`-instances The errors will be added to :attr:`~cerberus.Validator._errors`. *2. Custom error:* - the invalid field's name - the error message A custom error containing the message will be created and added to :attr:`~cerberus.Validator._errors`. There will however be fewer information contained in the error (no reference to the violated rule and its constraint). *3. Defined error:* - the invalid field's name - the error-reference, see :mod:`cerberus.errors` - arbitrary, supplemental information about the error A :class:`~cerberus.errors.ValidationError` instance will be created and added to :attr:`~cerberus.Validator._errors`. rNr*FrequiredZ__require_all__)rYr=extendsortr@addrBrGemitrRr _errorrZCUSTOMcoderulerCrD UNKNOWN_FIELD_resolve_rules_set_resolve_schemar8rIr:r;ZValidationErrorr>) rrMerrorrrkrlinforCrDr rules_setrrrrrjsR&              zBareValidator._errorNcKs|j}|||jsRd|d<t|d<|j|d<|j|d<|j|d<|j|d<|j f|}|dkrp|j |_ nt |t s|f}|j ||_ |dkr|j |_ nt |t s|f}|j ||_ |S) a@ Creates a new instance of Validator-(sub-)class. All initial parameters of the parent are passed to the initialization, unless a parameter is given as an explicit *keyword*-parameter. :param document_crumb: Extends the :attr:`~cerberus.Validator.document_path` of the child-validator. :type document_crumb: :class:`tuple` or :term:`hashable` :param schema_crumb: Extends the :attr:`~cerberus.Validator.schema_path` of the child-validator. :type schema_crumb: :class:`tuple` or hashable :param kwargs: Overriding keyword-arguments for initialization. :type kwargs: :class:`dict` :return: an instance of ``self.__class__`` Tis_childrGroot_allow_unknownroot_require_all root_document root_schemaN)r[rrErstoy_error_handlerr9r:r;r8rPrCrRrSrD)rdocument_crumb schema_crumbrNZ child_configZchild_validatorrrr_get_child_validatorEs*             z"BareValidator._get_child_validatorcCs<d||dd}t||d}|dkr8td|||S)Nz_{0}_{1} _z/There's no handler for '{}' in the '{}' domain.)formatreplacegetattrr)rZdomainrlZ methodnameresultrrrZ__get_rule_handlerts z BareValidator.__get_rule_handlercCst|j}t|j}|D]b}t|ddD]}t|j|||_q(t|ddD]}t|j|||_qL|jr||j||qdS)a{ Removes nodes by index from an errorpath, relatively to the basepaths of self. :param errors: A list of :class:`errors.ValidationError` instances. :param dp_items: A list of integers, pointing at the nodes to drop from the :attr:`document_path`. :param sp_items: Alike ``dp_items``, but for :attr:`schema_path`. T)reverseN)rYrCrDsortedrZ child_errors_drop_nodes_from_errorpaths)rr=Zdp_itemsZsp_itemsZ dp_basedepthZ sp_basedepthrpr\rrrr~s  z)BareValidator._drop_nodes_from_errorpathscCsn|dr.|dd}|dr&|jn|j}n|j}|d}|D]}||krTdS||i}qB|d|fS)a Searches for a field as defined by path. This method is used by the ``dependency`` evaluation logic. :param path: Path elements are separated by a ``.``. A leading ``^`` indicates that the path relates to the document root, otherwise it relates to the currently evaluated document, which is possibly a subdocument. The sequence ``^^`` at the start will be interpreted as a literal ``^``. :type path: :class:`str` :returns: Either the found field name and its value or :obj:`None` for both. :rtype: A two-value :class:`tuple`. ^rcN.)NN) startswithr;rvsplitrI)rpathcontextpartspartrrr _lookup_fields   zBareValidator._lookup_fieldcCs(t|tr|St|tr$|j|SdSN)rRrr rrI)rrrrrrrns    z BareValidator._resolve_rules_setcCs(t|tr|St|tr$|j|SdSr)rRrr rrIrr8rrrros    zBareValidator._resolve_schemacCs|jddS)a2 If ``True`` unknown fields that are not defined in the schema will be ignored. If a mapping with a validation schema is given, any undefined field will be validated against its rules. Also see :ref:`allowing-the-unknown`. Type: :class:`bool` or any :term:`mapping` r9Fr[rIrrrrr9szBareValidator.allow_unknowncCs0|js"t|ttfs"t|d|i||jd<dS)Nr9)rsrRboolrr[rrrrrr9scCs|jddS)zw If ``True`` known fields that are defined in the schema will be required. Type: :class:`bool` r:Frrrrrr:szBareValidator.require_allcCs||jd<dS)Nr:r[rrrrr:scCs ||jS)z The errors of the last processing formatted by the handler that is bound to :attr:`~cerberus.Validator.error_handler`. )rGr=rrrrrszBareValidator.errorscCs|jddS)zm Whether to not process :obj:`None`-values in a document or not. Type: :class:`bool` rUFrrrrrrUsz BareValidator.ignore_none_valuescCs||jd<dS)NrUrrrrrrUscCs|jddS)z ``True`` for child-validators obtained with :meth:`~cerberus.Validator._get_child_validator`. Type: :class:`bool` rsFrrrrrrsszBareValidator.is_childcCs|jddS)z/``True`` if the document is already normalized._is_normalizedFrrrrrrszBareValidator._is_normalizedcCs||jd<dS)NrrrrrrrscCs|jddS)z If ``True``, unknown fields will be deleted from the document unless a validation is called with disabled normalization. Also see :ref:`purging-unknown-fields`. Type: :class:`bool` rVFrrrrrrVszBareValidator.purge_unknowncCs||jd<dS)NrVrrrrrrVscCs|jddS)z If ``True``, fields declared as readonly will be deleted from the document unless a validation is called with disabled normalization. Type: :class:`bool` rWFrrrrrrWszBareValidator.purge_readonlycCs||jd<dS)NrWrrrrrrWscCs|jd|jS)z The :attr:`~cerberus.Validator.allow_unknown` attribute of the first level ancestor of a child validator. rt)r[rIr9rrrrrt sz BareValidator.root_allow_unknowncCs|jd|jS)z The :attr:`~cerberus.Validator.require_all` attribute of the first level ancestor of a child validator. ru)r[rIr:rrrrru(szBareValidator.root_require_allcCs|jd|jS)z~ The :attr:`~cerberus.Validator.document` attribute of the first level ancestor of a child validator. rv)r[rIr;rrrrrv0szBareValidator.root_documentcCs|jdtS)zj The registry that holds referenced rules sets. Type: :class:`~cerberus.Registry` r)r[rIrrrrrr8sz BareValidator.rules_set_registrycCs||jd<dS)Nrrrregistryrrrr@scCs|jd|jS)z| The :attr:`~cerberus.Validator.schema` attribute of the first level ancestor of a child validator. rw)r[rIr8rrrrrwDszBareValidator.root_schemacCs|jS)z The validation schema of a validator. When a schema is passed to a method, it replaces this attribute. Type: any :term:`mapping` or :obj:`None` )_schemarrrrr8LszBareValidator.schemacCs8|dkrd|_n$|js t|tr(||_n t|||_dSr)rrsrRrrrrrr8Us cCs|jdtS)zg The registry that holds referenced schemas. Type: :class:`~cerberus.Registry` r)r[rIrrrrrr^szBareValidator.schema_registrycCs||jd<dS)NrrrrrrrfscCs4t|jt|j@}|r$td|t|j|jS)zi The constraints that can be used for the 'type' rule. Type: A tuple of strings. zcThese types are defined both with a method and in the'types_mapping' property of this validator: %s)r6 types_mapping_types_from_methodsrrS)raZredundant_typesrrrtypeslszBareValidator.typescCst|_d|_t|_t|_t||_ |j s:d|_ |dk rPt |||_ n(|j dkrxt|jtrni|_n ttj|dkrttjt|tsttj||j|dS)NF)rr<r=r>r?r@rArBrr;rsrrr8rRr9rrrZSCHEMA_ERROR_MISSINGr#ZDOCUMENT_MISSINGZDOCUMENT_FORMATr~rGstart)rr;r8rrrZ__init_processing}s$         zBareValidator.__init_processingc Gs@|r6|D]*}z|j|Wqtk r0YqXqng|_dS)z Drops rules from the queue of the rules that still need to be evaluated for the currently processed field. If no arguments are given, the whole queue is emptied. N)rJremove ValueError)rrulesrlrrr_drop_remaining_ruless z#BareValidator._drop_remaining_rulesFcCs@|||||j|j|j||jr6|s6dS|jSdS)a Returns the document normalized according to the specified rules of a schema. :param document: The document to normalize. :type document: any :term:`mapping` :param schema: The validation schema. Defaults to :obj:`None`. If not provided here, the schema must have been provided at class instantiation. :type schema: any :term:`mapping` :param always_return_document: Return the document, even if an error occurred. Defaults to: ``False``. :type always_return_document: :class:`bool` :return: A normalized copy of the provided mapping or :obj:`None` if an error occurred during normalization. N)_BareValidator__init_processing!_BareValidator__normalize_mappingr;r8rGendr=)rr;r8always_return_documentrrr normalizeds    zBareValidator.normalizedcCst|tr||}|}|D]}|||||<q ||||jr\|js\||||j rn| ||| ||| ||| |||||d|_|S)NT)rRr rorrn'_BareValidator__normalize_rename_fieldsrVr9_normalize_purge_unknownrW(_BareValidator__normalize_purge_readonly(_BareValidator__validate_readonly_fields(_BareValidator__normalize_default_fields_normalize_coerce$_BareValidator__normalize_containersrrmappingr8rrrrZ__normalize_mappings           z!BareValidator.__normalize_mappingc Cstj}|D]}||krRd||krR|||d|||||dd|||<q t|jtr d|jkr ||jd||||jdd|||<q dS) {'oneof': [ {'type': 'callable'}, {'type': 'list', 'schema': {'oneof': [{'type': 'callable'}, {'type': 'string'}]}}, {'type': 'string'} ]} coercer*FN)rCOERCION_FAILED _BareValidator__normalize_coercerIrRr9r)rrr8rprrrrrs*     zBareValidator._normalize_coercec Cst|tr|d|}nLt|trd|}|D]4}||||||}tj|j|j |fkr*q`q*|Sz ||WSt k r}z*|r|dks| ||t ||WYSd}~XYnXdS)NZnormalize_coerce) rRr _BareValidator__get_rule_handlerr rrrr@fetch_errors_fromrC Exceptionrjstr) rZ processorrrr*rprr]errrZ__normalize_coerces(     z BareValidator.__normalize_coercec Cs|D]}t||d}t||trd|krD|||||dd|krb|||||d|td@szt|jtrz||||Wqtk rYqXqt||t rqqt||t rd|kr| |||qd|kr| |||qdS)Nr keysrules valuesrules)r9rVr8r8items) r6rIrRr/_BareValidator__normalize_mapping_per_keysrules1_BareValidator__normalize_mapping_per_valuesrulesr9,_BareValidator__normalize_mapping_per_schemar(r r-_BareValidator__normalize_sequence_per_schema,_BareValidator__normalize_sequence_per_items)rrr8rrrrrZ__normalize_containers s>   z$BareValidator.__normalize_containersc s tfdd||D}tdd||D}|j||df|d}|j|dd}|jrx||jgdd g||j|D]}|||krq|||||krtd jd d d|j |fD|d |||||||<q||||||||<|||=q|dS)Nc3s|]}|fVqdSrr.0kproperty_rulesrr +szBBareValidator.__normalize_mapping_per_keysrules..css|]}||fVqdSrrrrrrr,srryrzr8TrrdzHNormalizing keys of {path}: {key} already exists, its value is replaced.rcss|]}t|VqdSr)rrxrrrr;s)rkey) r1r{rr=rrjrr~joinrC) rrrrr8r; validatorrrrrrZ!__normalize_mapping_per_keysrules*s0  z/BareValidator.__normalize_mapping_per_keysrulescsltfdd||D}|j||df|d}|j||dd||<|jrh||jgdg||jdS)Nc3s|]}|fVqdSrrr value_rulesrrrEszDBareValidator.__normalize_mapping_per_valuesrules..rrTrrd)r1r{rr=rrj)rrrrr8rrrrZ#__normalize_mapping_per_valuesrulesDs z1BareValidator.__normalize_mapping_per_valuesrulesc Cs||i}|s"t|jtr"|j}|j||df|di|d|j|d|j|d|jd}t||}|j||dd}||||<|j r| |j dS)Nr8r9rVr:)ryrzr8r9rVr:Tr) rIrRr9rr{rVr:r,rr=rj)rrrr8rr value_typeZ result_valuerrrZ__normalize_mapping_per_schemaPs        z,BareValidator.__normalize_mapping_per_schemacstfddtt|Dtddt|D}|jdfd}t|}|j|dd}|||<|jr| |jgdg| |jdS) Nc3s|]}|dfVqdS)r8Nrrrr8rrrdsz@BareValidator.__normalize_sequence_per_schema..css|]\}}||fVqdSrrrrvrrrrfsr8rTrrd) r1rangerYrXr{r,rvaluesr=rrj)rrrr8r;rrrrrrZ__normalize_sequence_per_schemabs z-BareValidator.__normalize_sequence_per_schemac Cs||d||}}t|t|kr*dStddt|D}tddt|D}|j||df|d}t||}|j|dd} || ||<|jr||jgdg| |jdS) Nrcss|]\}}||fVqdSrrrrrrrusz?BareValidator.__normalize_sequence_per_items..css|]\}}||fVqdSrrrrrrrvsrTrrd) rYr1rXr{r,rrr=rrj) rrrr8rrr;rrrrrrZ__normalize_sequence_per_itemsqs  z,BareValidator.__normalize_sequence_per_itemscs&fdd|DD]}||q|S)Ncs$g|]}|iddr|qS)r+FrIrr8rr sz.rQrr8rrrrZ__normalize_purge_readonlys z(BareValidator.__normalize_purge_readonlycs&fdd|DD]}||q|S){'type': 'boolean'}csg|]}|kr|qSrrrrrrrsz:BareValidator._normalize_purge_unknown..rrrrrrs z&BareValidator._normalize_purge_unknowncCsbt|D]T}||kr2||||||||qt|jtrd|jkr||||ji|q|S)Nrename_handler)rS_normalize_rename_normalize_rename_handlerrRr9rrrrrZ__normalize_rename_fieldss  z'BareValidator.__normalize_rename_fieldscCs*d||kr&|||||d<||=dS)z{'type': 'hashable'}renameNrrrrrrs zBareValidator._normalize_renamecCsJd||krdS|||d||dtj}||krF||||<||=dS)rrNF)rrZRENAMING_FAILED)rrr8rnew_namerrrrs   z'BareValidator._normalize_rename_handlercs:fddDD]}|d||qdS)Nc3s.|]&}|kr|dr|VqdS)r+N)rnrIrrr8rrrrsz;BareValidator.__validate_readonly_fields..r+)_validate_readonlyrrrrZ__validate_readonly_fieldss z(BareValidator.__validate_readonly_fieldsc s2fddD}zfdd|D}Wntk rBtYnX|D]}||qHt}fdd|D}|r.|d}z||WnRtk r||Yn6tk r}z| |t j t |W5d}~XYnXt t|} | |kr"|D]}| |t j dqq.qt|| qtdS)Ncs4g|],}|ks,|dkr|dds|qS)Nr*Frrrr8rrrs  z.csg|]}d|kr|qS)defaultrrrrrrs csg|]}d|kr|qS)default_setterrrrrrrs rz)Circular dependencies of default setters.)rZr(_normalize_defaultr6rQ_normalize_default_setterKeyErrorappendrrjrZSETTING_DEFAULT_FAILEDrhashrSrh) rrr8Z empty_fieldsZfields_with_defaultrZknown_fields_statesZfields_with_default_setterrZfields_processing_staterrrZ__normalize_default_fieldss>     &  z(BareValidator.__normalize_default_fieldscCs||d||<dS)z{'nullable': True}rNrrrrrrsz BareValidator._normalize_defaultcCs>d||kr:||d}t|tr.|d|}||||<dS)zi {'oneof': [ {'type': 'callable'}, {'type': 'string'} ]} rZnormalize_default_setterN)rRr r)rrr8rsetterrrrrs     z'BareValidator._normalize_default_setterTcCs||_t|_||||r.||j|j|jD]F}|jrN|j|dkrNq4|j|}|dk rp| ||q4| |q4|js| |j|j |t|j S)aO Normalizes and validates a mapping against a validation-schema of defined rules. :param document: The document to normalize. :type document: any :term:`mapping` :param schema: The validation schema. Defaults to :obj:`None`. If not provided here, the schema must have been provided at class instantiation. :type schema: any :term:`mapping` :param update: If ``True``, required fields won't be checked. :type update: :class:`bool` :param normalize: If ``True``, normalize the document before validation. :type normalize: :class:`bool` :return: ``True`` if validation succeeds, otherwise ``False``. Check the :func:`errors` property for a list of processing errors. :rtype: :class:`bool` N)rEr6_unrequired_by_excludesrrr;r8rUrI$_BareValidator__validate_definitions'_BareValidator__validate_unknown_fields(_BareValidator__validate_required_fieldsrGrrr=)rr;r8rE normalizer definitionsrrrvalidates       zBareValidator.validatecOs0|dd}|j|||jr&|s&dS|jSdS)z Wrapper around :meth:`~cerberus.Validator.validate` that returns the normalized and validated document or :obj:`None` if validation failed. rFN)rQrr=r;)rrMrNrrrr validated's    zBareValidator.validatedcCsr|jr`|j|}t|jttfrn|jr*dnd}|j|||jid}|||iddsn||jn||t j dS)Nr9Z__allow_unknown__)rzr8Fr) r9r;rRrr rsr{rjr=rrm)rrrrzrrrrZ__validate_unknown_fields3s z'BareValidator.__validate_unknown_fieldscsfdd}jfddjDfddjDfddD_jrjd}z||}|rWqWqvtk rYqYqvXqvd S) z3Validate a field's value against its defined rules.cs d|}||dS)Nr)rrI)rlr)rrrrrr validate_ruleEs z;BareValidator.__validate_definitions..validate_rulecs"g|]}|ks|jkr|qSr)mandatory_validationsr)rrrrrLs z8BareValidator.__validate_definitions..c3s|]}|kr|VqdSrrr) rules_queuerrrQsz7BareValidator.__validate_definitions..c3s,|]$}|kr|jkr|dkr|VqdS))r9r:metareN)normalization_rulesr)rrrrrTs  rN) rnr;priority_validationsrfrrJrQr(r)rrrrrlrr)rrrrrrZ__validate_definitionsBs,     z$BareValidator.__validate_definitionsz {'oneof': [{'type': 'boolean'}, {'type': ['dict', 'string'], 'check_with': 'bulk_schema'}]} cs\t|tr@t|ts@tfdd|D}|rX||tj|n|krX||tj|dS)z{'type': 'container'}c3s|]}|kr|VqdSrrrallowed_valuesrrrusz2BareValidator._validate_allowed..N)rRr r rSrjrZUNALLOWED_VALUESZUNALLOWED_VALUE)rrrrZ unallowedrrr_validate_allowedrs zBareValidator._validate_allowedcCst|trRz|d|}Wn*tk rD|d|}tdtYnX|||n2t|trv|D]}||||q`n||||jdS)r check_withrziThe 'validator' rule was renamed to 'check_with'. Please update your schema and method names accordingly.N) rRr rrrDeprecationWarningr _validate_check_withrj)rZchecksrrZ value_checkerrrrrr|s     z"BareValidator._validate_check_withcCsZt|tsdSt|tr"t|tr.t|f}nt|}|t|}|rV||tj|dS)z{'empty': False }N)rRr r r6rjrZMISSING_MEMBERS)rZexpected_valuesrrZmissing_valuesrrr_validate_containss    z BareValidator._validate_containscCsnt|tst|ttfs|f}t|tr6|||nt|trL||||j|j |dfdk rjdSdS)zD{'type': ('dict', 'hashable', 'list'), 'check_with': 'dependencies'} dependenciesNT) rRr r rr._BareValidator__validate_dependencies_sequence-_BareValidator__validate_dependencies_mappingr@Zfetch_node_fromrD)rrrrrrr_validate_dependenciess      z$BareValidator._validate_dependenciesc Csd}i}|D]P\}}t|tr,t|tr2|g}||\}}||krR|d7}q|||iq|t|kr~||tj |dS)Nrrc) rrRrr rrErYrjrZDEPENDENCIES_FIELD_VALUE) rrrZvalidated_dependencies_counterZ error_infoZdependency_nameZdependency_valuesZ wanted_fieldZwanted_field_valuerrrZ__validate_dependencies_mappings   z-BareValidator.__validate_dependencies_mappingcCs0|D]&}||ddkr||tj|qdS)Nr)rrjrZDEPENDENCIES_FIELD)rrrZ dependencyrrrZ __validate_dependencies_sequencesz.BareValidator.__validate_dependencies_sequencec CsBt|tr>t|dkr>|ddddddd|s>||tjd S) rrallowed forbiddenr minlength maxlengthregexrN)rRrrYrrjrZEMPTY_NOT_ALLOWED)rr-rrrrr_validate_emptys zBareValidator._validate_emptycst|tr|g}j|djr0j||D].}|jkr4j|djr4j|q4tfdd|Drddd|D} |t j |dS)z>{'type': ('hashable', 'list'), 'schema': {'type': 'hashable'}}rec3s|]}|jkVqdSrr;)rexcluded_fieldrrrrsz3BareValidator._validate_excludes..z, css|]}d|VqdS)z'{0}'N)r~rrrrrrsN) rRr r8rIr:rrhanyrrjrZEXCLUDES_FIELD)rZexcluded_fieldsrrrZ exclusion_strrrr_validate_excludess   z BareValidator._validate_excludescCsZt|tr>t|ts>t|t|@}|rV||tjt|n||krV||tj|dS)z{'type': 'list'}N) rRrr r6rjrZFORBIDDEN_VALUESr4ZFORBIDDEN_VALUE)rZforbidden_valuesrrrrrr_validate_forbiddens z!BareValidator._validate_forbiddencCst|t|kr,||tjt|t|n^tddt|D}|j||df|d}|tddt|D|jdds||tj|j dS) z'{'type': 'list', 'check_with': 'items'}css|]\}}||fVqdSrr)rr\ definitionrrrrsz0BareValidator._validate_items..rrcss|]\}}||fVqdSrr)rr\rrrrrsFrErN) rYrjrZ ITEMS_LENGTHr1rXr{rEZ BAD_ITEMSr=)rrrrr8rrrr_validate_itemss zBareValidator._validate_itemsc Csd}t}t|D]\}}||i} dD]4} | | |kr,| |j|kr,|j|| | || <q,d| |kr||j| |d<|j|||f| dd} | |j|jddr|d7}q| | j gd g| | j q||fS) zl Validates value against all definitions and logs errors according to the operator. r)r9r,r9T)rzr8r9Frrc) rr<rXrr8r9r{r;rErr=rf) roperatorrrrZ valid_counterr=r\rr8rlrrrrZ__validate_logicals&   z BareValidator.__validate_logicalcCs8|d|||\}}|dkr4||tj||t|dS)z${'type': 'list', 'logical': 'anyof'}ZanyofrcN) _BareValidator__validate_logicalrjrZANYOFrYrrrrZvalidsr=rrr_validate_anyof2szBareValidator._validate_anyofcCs<|d|||\}}|t|kr8||tj||t|dS)z${'type': 'list', 'logical': 'allof'}ZallofN)rrYrjrZALLOFrrrr_validate_allof8s zBareValidator._validate_allofcCs8|d|||\}}|dkr4||tj||t|dS)z%{'type': 'list', 'logical': 'noneof'}ZnoneofrN)rrjrZNONEOFrYrrrr_validate_noneof>szBareValidator._validate_noneofcCs8|d|||\}}|dkr4||tj||t|dS)z${'type': 'list', 'logical': 'oneof'}oneofrcN)rrjrZONEOFrYrrrr_validate_oneofDszBareValidator._validate_oneofcCs4z||kr||tjWntk r.YnXdSz{'nullable': False }N)rjrZ MAX_VALUErZ)rZ max_valuerrrrr _validate_maxJs zBareValidator._validate_maxcCs4z||kr||tjWntk r.YnXdSr)rjrZ MIN_VALUErZ)rZ min_valuerrrrr _validate_minRs zBareValidator._validate_mincCs.t|tr*t||kr*||tjt|dSz{'type': 'integer'}N)rRr rYrjrZ MAX_LENGTH)rZ max_lengthrrrrr_validate_maxlengthZsz!BareValidator._validate_maxlengthcCs.t|tr*t||kr*||tjt|dSr")rRr rYrjrZ MIN_LENGTH)rZ min_lengthrrrrr_validate_minlengthasz!BareValidator._validate_minlengthcCs@|dkr<|s||tj|dddddddd d d d d d dS)rNrr-rrrminmaxr r r r8r,r)rjrZ NOT_NULLABLEr)rr*rrrrr_validate_nullablefs$z BareValidator._validate_nullablecs|t|trx|j||dftfdd|Dd}|tdd|Dddsx||jgdd g||tj |jd S) {'type': ['dict', 'string'], 'check_with': 'bulk_schema', 'forbidden': ['rename', 'rename_handler']} rc3s|]}|fVqdSrrrrrrrsz4BareValidator._validate_keysrules..rcss|]}||fVqdSrrrrrrrsFrrdrN) rRrr{r1keysrr=rjrZ KEYSRULES)rr8rrrrrr_validate_keysrules{s z!BareValidator._validate_keysrulescCsH|rD|js||tjtj|j|j|fk}|jrD|rD|dS)rN)rrjrZREADONLY_FIELDr@rrCr)rr+rrZ has_errorrrrrs  z BareValidator._validate_readonlycCsFt|tsdS|ds |d7}t|}||sB||tjdS)z{'type': 'string'}N$) rRr endswithrecompilematchrjrZREGEX_MISMATCH)rpatternrrZre_objrrr_validate_regexs    zBareValidator._validate_regexz {'type': 'boolean'} csz tfddjD}Wn0tk rPjrJjddkrJtnYnX|j8}|tfddD}|D]}|t j q|jrtfddD}j |rֈj|D]}|t j qdS)zy Validates that required fields are not missing. :param document: The document being validated. c3s.|]&\}}|djdkr|VqdS)reTN)rnrIr:)rrrrrrrs z;BareValidator.__validate_required_fields..rr8c3s&|]}|dk sjs|VqdSr)rIrUrr;rrrrsc3s |]}|dk r|VqdSrrrr rrrsN) r6r8rAttributeErrorrsrDr(rrjrZREQUIRED_FIELD isdisjoint)rr;reZmissingrZfieldsrr3rZ__validate_required_fieldss&     z(BareValidator.__validate_required_fieldscCsL|dkr dSt|tr0t|ts0||||nt|trH||||dS)z {'type': ['dict', 'string'], 'anyof': [{'check_with': 'schema'}, {'check_with': 'bulk_schema'}]} N)rRrr (_BareValidator__validate_schema_sequencer'_BareValidator__validate_schema_mapping)rr8rrrrr_validate_schemas  zBareValidator._validate_schemacCs||}|j|d|j}|j|d|j}|j||df|||d}z&|||jddsn||tj |j Wn$t k r||tj YnXdS)Nr9r:r8)ryrzr8r9r:Fr) ror8rIr9r:r{rErjrZMAPPING_SCHEMAr=r(ZBAD_TYPE_FOR_SCHEMA)rrr8rr9r:rrrrZ__validate_schema_mappings  z'BareValidator.__validate_schema_mappingcstfddtt|D|j||df|jd}|tddt|D|jdd|jr||jgdg| |t j |jdS) Nc3s|]}|fVqdSrr)rr\rrrrsz;BareValidator.__validate_schema_sequence..r8)ryrzr8r9css|]\}}||fVqdSrr)rr\rrrrrsFrrd) r1rrYr{r9rXrEr=rrjrZSEQUENCE_SCHEMA)rrr8rrrrrZ__validate_schema_sequencesz(BareValidator.__validate_schema_sequencec Cs|sdSt|tr|fn|}|D]R}|j|}|dk rTt||joPt||j }n|d|}||}|r dSq ||tj | dS)zM {'type': ['string', 'list'], 'check_with': 'type'} NZ validate_type) rRr rrIZincluded_typesZexcluded_typesrrjrZBAD_TYPEr) rZ data_typerrrZ_typeZtype_definitionZmatchedZ type_handlerrrr_validate_types$    zBareValidator._validate_typecsr|df}t|trn|j||tfdd|Dd}|||jdd|jrn||jgdg||tj |jdS) r)rc3s|]}|fVqdSrrrrrrr-sz6BareValidator._validate_valuesrules..rFrrdN) rRrr{r1rEr=rrjrZ VALUESRULES)rr8rrrzrrrr_validate_valuesrules"s z#BareValidator._validate_valuesrules)NN)N)NF)NFT)rr%r&r'r rrrbytes bytearrayrr r rrrr2r rr6rr^rL staticmethodrFrH classmethodrbrjr{rrrrnropropertyr9rr:rrUrsrrVrWrtrurvrrwr8rrrrrrrrrrrrrrrrrrrrrrrrr__call__rrrr"Z_validate_allow_unknownrrrrrrr rrrrrrrrr r!r#_validate_metar%r(r+rr2Z_validate_requiredZ_validate_require_allrr8r7r6r9r: __classcell__rrrOrr)Es/        3  Q /                             !   + * *   $"r)z7The rule's arguments are validated against this schema:cs4eZdZdZfddZfddZddZZS)InspectedValidatorzMetaclass for all validatorscs>d|dkr(|dd|ddjitt|j|f|S)Nr rdrcr)rEr rKrC__new__)rarMrOrrrD;s zInspectedValidator.__new__csfdd}ttj|di__|dD]@}|dr^j|tddf7_q2d|j|<q2jrtdt t dd |d Dt dd |d D_ jd d }j |d dd d d<|dd<dd j DD]}dj|d<qddi_ __|dD]n}|drVj |tddf7_ n>|drj|tddf7_nd|j|<q(dD]:}j|d }j |d dd d d<|dd<qjjdd d d<i_jjjjdS)NcstfddtDS)Nc3s4|],}|ddr|tddVqdS)r}rdN)rrYrprefixrrrBszNInspectedValidator.__init__..attributes_with_prefix..)rSdirrEr`rErattributes_with_prefixAs z;InspectedValidator.__init__..attributes_with_prefixrrZtype_Z _validate_z}Methods for type testing are deprecated, use TypeDefinition and the 'types_mapping'-property of a Validator-instance instead.css|] }|VqdSrrrrrrr^sz.InspectedValidator.__init__..rrrrcr8rrdcss|]}|dkr|VqdS)r*NrrrrrrdsTrerZcoerce_Zdefault_setter_Z _normalize_)rrr)rKrCrLrZvalidation_rulesrrY$_InspectedValidator__get_rule_schemarrrSZcheckersrZcoercersZdefault_settersrrrE)rarMrHZ attributerrlrOr`rrL@sP     &   *zInspectedValidator.__init__cCst||j}|dkri}n@t|kr0|td}zt|}Wntk rXi}YnX|s~|dkr~td|ddd|S)NrcrAz>No validation schema is defined for the arguments of rule '%s'r}rdr)rr RULE_SCHEMA_SEPARATORrrstriprr)raZ method_nameZ docstringrrrrZ__get_rule_schema}s    z$InspectedValidator.__get_rule_schema)r%r&r'r rDrLrIrBrrrOrrC8s  =rC Validator)+r Z __future__rZastrrrrr.warningsrZcerberusrZcerberus.platformr r r r r rrrZcerberus.schemarrrrZcerberus.utilsrrrZToyErrorHandlerrxr"rr#r(objectr)rJr,rCrLrrrrs:     (   {Z