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.

Nested Custom Coercers Not Applied

See original GitHub issue

Used Cerberus version / latest commit: Cerberus==1.1

  • I consulted these documentations:

  • I consulted these sections of the docs (add more lines as necessary):

  • I found nothing relevant to my problem in the docs.

  • I found the documentation not helpful to my problem.

  • 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.


Support request / Bug report

I am trying to apply a custom coercer to a nested item. I do not believe this is working properly. To simplify this, I modified the example code from the documentation to demonstrate the issue:

class MyNormalizer(Validator):
    def __init__(self, multiplier, *args, **kwargs):
        super(MyNormalizer, self).__init__(multiplier=multiplier, *args, **kwargs)
        self.multiplier = multiplier

    def _normalize_coerce_multiply(self, value):
        return value * self.multiplier

schema = {
    'foo': {
        'type': 'list',
        'schema': {
            'sub_foo': {
                'coerce': 'multiply'
            }
        }
    }
}

document = {'foo': [{'sub_foo': 2}]}
MyNormalizer(2).normalized(document, schema)

# Expected Output: {'foo': [{'sub_foo': 4}]}
# Output: {'foo': [{'sub_foo': 2}]}

I believe the schema definition / custom Validator class is accurate. Please let me know if this is not the case.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
funkyfuturecommented, Oct 3, 2017

@fermuch normalization rules in *of rules are not applied. see #344 / 17e786b

@sireliah at the time values are coerced, the document will not have a field with any rules in the schema. see #345

1reaction
sireliahcommented, Oct 3, 2017

Hey, I found similar issue with nested structures when you:

  • rename the parent element
  • try to change child element
from cerberus import Validator

schema = {
    "rows": {
        "type": "list",
        "rename": "ROWS",
        "schema": {
            "type": "dict",
            "schema": {
                "reference": {
                    "type": "string",
                    "coerce": lambda x: "%s MODIFIED HERE " % x
                },
            }
        }
    }
}


to_normalize = {"rows": [{"reference": "aaaaa"}]}

v = Validator(schema)

v.normalized(to_normalize)

Out[29]: {'ROWS': [{'reference': 'aaaaa'}]}


Expected:
 {'ROWS': [{'reference': 'aaaaa MODIFIED HERE '}]}

It would be nice this to work in such way that you are able to modify the parent element first and then apply changes to all child elements. If there is no easy fix, I can try to find a way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javax validation on nested objects - not working
In my Spring Boot project I have two DTO's which I'm trying to validate, LocationDto and BuildingDto. The LocationDto has a nested object...
Read more >
Rules for nested data not applying if parent schema has errors ...
Describe the bug When applying a rule for nested data, like rule("user.name"), it will fail to run if any of the other user...
Read more >
Cerberus Documentation - manpages.ubuntu!
Simple custom errors A simpler form is to call _error() with the field and a string as message. However the resulting error will...
Read more >
Cerberus Changelog
New function-based custom validation mode (Luo Peng). Fields with empty definitions in schema were reported as non-existent. Now they are considered as valid, ......
Read more >
valico - crates.io: Rust Package Registry
What is Valico? Build Status. Valico is a validation and coercion tool for JSON objects, written in Rust. It designed to be a...
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