question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

conditional normalization using oneof

See original GitHub issue

Used 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 oneofduring 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
linupicommented, Oct 21, 2020

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

1reaction
linupicommented, Dec 2, 2019

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:

schema = {
    'foo': {'type': 'string'},
    'bar': {
        'oneof': [
            {'dependencies': {"foo": "B"}, 'default': "B"},
            {'dependencies': {"foo": "A"}, 'default': "C"},
        ]
    },
}

how would you like if the normalization role of oneof would be more explicit e.g. by defining a default_setter like this:

schema = {
    'foo': {'type': 'string'},
    'bar': {
        'oneof': [
            {'dependencies': {"foo": "B"}, 'default': "B"},
            {'dependencies': {"foo": "A"}, 'default': "C"},
        ]
    },
    default_setter = 'oneof'           # explicitly defining that oneof is setting the default
}

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 the setter called in _normalize_default_setter to something like mapping, schema, field instead of just mapping (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…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found