question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating custom Validator instance with `_validator_*` method raises SchemaError

See original GitHub issue

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']}]}]}]}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
joshbennercommented, Sep 12, 2016

I believe this is a bug.

I traced the behavior to InspectedValidator.__init__ as it is building the schema to validate a schema. It has a coded assumption that all validation methods begin with _validate_.

Upon changing the custom validator from _validator_foo to _validate_validator_foo, the schema validation now picked up my custom validator – however, __get_rule_handler() requires that the function name be as documented (ie: _validator_foo).

So either InspectedValidator.__init__ needs to be updated to discover custom validators correctly, or the documentation and __get_rule_handler() need to be updated to match what InspectedValidator.__init__ enforces. My opinion would be to implement the former.

For now, a workaround is to create an alias of your custom validator:

class MyValidatorClass(cerberus.Validator):
    def _validator_my_validator(self, field, value):
        if my_test(value) is False:
            self._error(field, "Nope!")

    _validate_validator_my_validator = _validator_my_validator
0reactions
bryanwwebercommented, Oct 27, 2016

About a day ago, I was looking at this issue again to see if I could work out a fix. Got beaten to it! Thanks @nicolaiarocci and @funkyfuture!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema Validation - python-jsonschema - Read the Docs
The Basics: The simplest way to validate an instance under a given schema is to use the validate function. The Validator Protocol: jsonschema...
Read more >
Adding a custom 'type' - Google Groups
I'm attempting to use the 'redefine' method of a TypeChecker to create a new 'type'. import os. from jsonschema import validators, Draft4Validator.
Read more >
The best way to implement custom validators - Angular inDepth
Learning best practices on how to build your custom validator in Angular by reverse engineering the built-in Angular validators.
Read more >
Cerberus Documentation - Read the Docs
be easily extensible, allowing for custom validation. ... A SchemaError is raised when an invalid validation schema is encountered.
Read more >
API Documentation - Cerberus
A custom error containing the message will be created and added to _errors . ... Creates a new instance of Validator-(sub-)class. All initial...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found