Skip to content

Creating custom Validator instance with _validator_* method raises SchemaError #265

@bryanwweber

Description

@bryanwweber

Used Cerberus version / latest commit: 1.0.1


Use-case abstract

I am trying to follow the example from the docs to create a custom validator. The example defining a function oddity works, but creating the custom subclass of Validator with the equivalent function raises a SchemaError.


from cerberus import Validator
class AValidator(Validator):
    def _validator_oddity(self, field, value):
        if not value & 1:
            self._error(field, "Must be an odd number")

schema = {'amount': {'validator': 'oddity'}}
v = AValidator(schema)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bryan/anaconda3/envs/pyteck/lib/python3.5/site-packages/cerberus/validator.py", line 143, in __init__
    self.schema = kwargs.get('schema', None)
  File "/Users/bryan/anaconda3/envs/pyteck/lib/python3.5/site-packages/cerberus/validator.py", line 432, in schema
    self._schema = DefinitionSchema(self, schema)
  File "/Users/bryan/anaconda3/envs/pyteck/lib/python3.5/site-packages/cerberus/schema.py", line 67, in __init__
    self.validate(schema)
  File "/Users/bryan/anaconda3/envs/pyteck/lib/python3.5/site-packages/cerberus/schema.py", line 186, in validate
    self._validate(schema)
  File "/Users/bryan/anaconda3/envs/pyteck/lib/python3.5/site-packages/cerberus/schema.py", line 208, in _validate
    raise SchemaError(self.schema_validator.errors)
cerberus.schema.SchemaError: {'amount': [{'validator': [{'oneof': ['none or more than one rule validate', {'oneof definition 0': ['must be of callable type'], 'oneof definition 1': ['must be of list type'], 'oneof definition 2': ['unallowed value oddity']}]}]}]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions