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
Add validator support to condition registration and FlagStateForm #61
Conversation
Unfortunately, this implementation causes an error any time the Django admin is used to edit any condition with a required parameter like I'm not sure there's a simple way to validate values in a generic way, as opposed to on a condition-by-condition case. As a side note, I noticed when testing this using Wagtail that wagtail-flags seem to have its own copy of |
@chosak Yeah, I see that now. I'll use this opportunity to add general validation for conditions, I think, something we should have anyway. |
db1c6ce
to
ad04cf9
Compare
FlagStateForm
Just throwing out an alternate design: class MyCondition:
def __call__(value, **kwargs):
# check condition
def validate(value):
pass Allow conditions to be optionally defined as callable objects with an optional |
@chosak I thought about that, but I think I'd prefer to be able to reuse Django's validators as much as possible — and when not the actual validators, the same approach to validators. That, to me, requires them to be decoupled from the conditions. It's possible we could support both means through introspection when a condition is registered... but that seems to more complexity without a lot of gain. |
@chosak It turns out, it's pretty easy to support that behavior, as well as the separate condition/validators by storing separate validators as |
34f9009
to
b706418
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a minor suggestion that could be done in this PR, but it is not something that has to be done.
When I ran git log
and tox
I was pleasantly surprised to see that Django 3.0 is supported.
My change would be to remove support for Django 1.11 as cfgov-refresh is now on Django 2.2.
The file tox.ini
could be updated to remove py36-dj111
and py38-dj30
.
This change supports adding a `validator` callable when registering a condition. This validator is used by the `FlagStateForm` to ensure that values entered for conditions are valid for that condition, and to provide feedback if not. To support that functionality, this change also refactors the conditions code, breaking the registry and the built-in conditions into their own modules, and adding a new module for validators. The API remains the same.
This change adds support for custom user models to the user condition and its validator.
This change replaces the separate global `_validators` registry by using the condition callable to store the validator as a `validate` attribute on the callable. This allows for specifying conditions and validators as either separate callables or as a single callable object that encapsulates both.
8287c76
to
ecd6b7c
Compare
Closes #59. Flag state values were not previously validated in the
FlagStateForm
, resulting in potential exceptions being raised on flag state checks.Implementation is loosely based on the code suggested by @Weltraum.
This change supports adding a
validator
callable when registering a condition. This validator is used by theFlagStateForm
to ensure that values entered for conditions are valid for that condition, and to provide feedback if not.To support that functionality, this change also refactors the conditions code, breaking the registry and the built-in conditions into their own modules, and adding a new module for validators. The API remains the same.