Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for any iterable (instead of just list) in "allowed" rule. #384

Closed
crunk1 opened this issue Apr 21, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@crunk1
Copy link
Contributor

commented Apr 21, 2018

Used Cerberus version / latest commit: 1.2
Python version: 3.6.5


Use-case abstract

I suggest adding support for using iterables in the "allowed" validation rule. In particular, I have a string enum class derived from enum.Enum. An enum.Enum is iterable. I'll try to demonstrate my use case:

class MyEnum(StrEnum):
  FOO = 'FOO'

# Behavior of MyEnum
# list(MyEnum)         -> [<MyEnum.FOO: 'FOO'>]
# 'FOO' in MyEnum      -> True
# 'BAR' in MyEnum      -> False
# MyEnum.FOO in MyEnum -> True
# MyEnum.FOO == 'FOO'  -> True
# MyEnum.FOO == 'BAR'  -> False

# This works, but is undesirable:
schema = {
  'foo': {'type': 'list', 'allowed': list(MyEnum)},
}
v = cerberus.Validator(schema)
# v.validate({'foo': [MyEnum.FOO, 'FOO']}) -> True

# This raises an error:
schema = {
  'foo': {'type': 'list', 'allowed': MyEnum},
}
v = cerberus.Validator(schema)
# Traceback (most recent call last):
#   File "<input>", line 1, in <module>
#   File "C:\Python36\lib\site-packages\cerberus\validator.py", line 169, in __init__
#     self.schema = kwargs.get('schema', None)
#   File "C:\Python36\lib\site-packages\cerberus\validator.py", line 509, in schema
#     self._schema = DefinitionSchema(self, schema)
#   File "C:\Python36\lib\site-packages\cerberus\schema.py", line 69, in __init__
#     self.validate(schema)
#   File "C:\Python36\lib\site-packages\cerberus\schema.py", line 197, in validate
#     self._validate(schema)
#   File "C:\Python36\lib\site-packages\cerberus\schema.py", line 219, in _validate
#     raise SchemaError(self.schema_validator.errors)
# cerberus.schema.SchemaError: {'foo': [{'allowed': ['must be of list type']}]}
@funkyfuture

This comment has been minimized.

Copy link
Member

commented Apr 21, 2018

afaict, supporting this would just require to change the rule's constraint schema to type.collections.Container. therefore the schema validator would need a type definition for that. that shouldn't be too tricky once you wrapped your head around the schema validation mechanics.

@crunk1

This comment has been minimized.

Copy link
Contributor Author

commented Apr 21, 2018

Mind filling me in on the mechanics? I've been looking for usages of _validate_allowed and can't see where it is used. Is there some autogen tooling built around the rule schemas?

@crunk1

This comment has been minimized.

Copy link
Contributor Author

commented Apr 21, 2018

Nvm, think I found it :)

@crunk1

This comment has been minimized.

Copy link
Contributor Author

commented Apr 21, 2018

PR out. #388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.