Creating custom Validator instance with `_validator_*` method raises SchemaError
See original GitHub issueUsed Cerberus version / latest commit: 1.0.1
- I consulted these documentations:
- I consulted these sections of the docs (add more lines as necessary):
- I found nothing relevant to my problem in the docs.
- I found the documentation not helpful to my problem.
- I have the capacity to improve the docs when my problem is solved.
- I have the capacity to submit a patch when a bug is identified.
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:
- Created 7 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 whatInspectedValidator.__init__
enforces. My opinion would be to implement the former.For now, a workaround is to create an alias of your custom validator:
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!