-
Notifications
You must be signed in to change notification settings - Fork 240
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
Proposal: custom coerce-methods #102
Comments
Design/usage example? Thanks! |
class MyValidator(Validator):
def _corce_none_to_string(self, field, value):
if value is None:
self.document[field] = "N/A"
def _coerce_expand_avatar_path(self, field, value):
self.document[field] = os.path.join(self.avatar_dir, value)
schema = {'avatar': {'coerce': 'expand_avatar_path', 'type': 'accessible_image_file'},
'phone': {'coerce': 'none_to_string'}} |
👍 |
Type coercion allows this already, as you can pass any callable to the
Alternatively you could create a new
Do we really need to add yet another way to achieve this? |
imo yes, as i think that normalization and validation should be clearer segregated in the code. you're proposal is more a workaround where normalization is applied while validation, right? |
Yes, I think you are right. |
👍 One example would be for those fields in which you want a default value. Also, i agree that makes sense the separation of validation (no data changes) versus coerce (data is modified). |
i wouldn't see why a |
it could, but it is more aligned with |
i don't understand. i thought you mean: >>> schema = {'amount': {'default': 1}}
>>> document = {'amount': None}
>>> v.validated(document, schema)
{'amount': 1}
>>> document = {'model': 'The Robots'}
>>> v.normalized(document, schema)
{'model': 'The Robots', 'amount': 1} yes, it alters values like |
yeah that's what i meant. I guess both |
I'd take advantage of this: I'm loading the schema from an external file (actually, from a Ideally I'd like to have a way to inject custom coercer functions on |
closing this in favor of #188 where i argue that this is a 1.0-must-have. |
i think it's better to include this feature before 0.9 is released. and as there is a reliable pattern for types, it wouldn't be much effort i guess.
one could also add an example to the docs and tests that illustrates a subclassed Validator that makes use of coercing in conjunction with contextual instance-properties.
The text was updated successfully, but these errors were encountered: