conditional normalization using oneof
See original GitHub issueUsed Cerberus version / latest commit: current master
- 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.
- My question does not concern a practical use-case that I can’t figure out to solve.
Use-case abstract
So far Cerberus does not allow for conditional default values which is supported by other validators (e.g. in Schema using Or
). Here pledge for a new feature that adds the possibility to define more complex defaults i.g. where the default value depends on another field. This feature can be added without any new rule simply by considering oneof
during normalization.
Feature request
It would be nice to consider oneof
also for normalization. While other of-rules (anyof
, noneof
, alloff
) are not unambiguous in the context of normalization oneof
instead could be used to insert conditional default values.
e.g.
schema = {
'foo': {'type': 'string'},
'bar' :{'oneof':[{'dependencies':{"foo":"B"}, 'default':"B"}, {'dependencies':{"foo":"A"}, 'default':"C"}]}
}
document = {'foo': 'A'}
expected_normalization_result = {'foo': 'A','bar':"C"}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
typescript - JSON Schema conditional oneOf - Stack Overflow
I have a oneOf statement in my JSON Schema, but the validator should only ... Your use of const inside an if statement...
Read more >Conditional Batch Normalization Explained | Papers With Code
Conditional Batch Normalization (CBN) is a class-conditional variant of batch normalization. The key idea is to predict the and of the batch normalization...
Read more >Validation Rules — Cerberus is a lightweight and extensible ...
Validates if none of the provided constraints validates the field. oneof, Validates if exactly one of the provided constraints applies. Note. Normalization ......
Read more >17.7.0 API Reference - joi.dev
Returns an object with the following keys: value - the validated and normalized value. error - the validation errors if found. warning -...
Read more >Transforms (augmentations.transforms) - Albumentations
Augment RGB image using FancyPCA from Krizhevsky's paper "ImageNet Classification with Deep ... Normalization is applied by the formula: img = (img -...
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 am not aware of any progress on this, For the moment we are using a patched Validator that does
oneof
normalization that is somehow customized to our project but of cause it would be great to have this sort of possibility in cerberus itself.In case you want to have a look, here is what we use: https://gitlab.esrf.fr/bliss/bliss/-/blob/master/bliss/common/validator.py https://gitlab.esrf.fr/bliss/bliss/-/blob/master/tests/common/test_validator.py
To me Cerberus is already quite a strong tool that manages to deal with rather complex problems using a simple grammar, that is one of the reasons why I really like Cerberus.
Looking at my proposal I, currently define a schema like this:
how would you like if the normalization role of
oneof
would be more explicit e.g. by defining adefault_setter
like this:like this the there is a clear cut between the role of ‘oneof’ in the validation phase and in the normalization phase. I actually would have liked to go this way, but unfortunately the signature of the function called by
_normalize_default_setter
does not allow for the transmission of a schema. Would it be imaginable to extend the signature of thesetter
called in_normalize_default_setter
to something likemapping, schema, field
instead of justmapping
(at least in the case where the setter is provided as extension of the validator and not as an external callable)? In that case I could rewrite my proposal … if it is considered as potentially sustainable…