pyeve / cerberus

Lightweight, extensible data validation library for Python
http://python-cerberus.org
ISC License
3.15k stars 238 forks source link

Rules sets with normalization rules fail #283

Closed dkellner closed 7 years ago

dkellner commented 7 years ago

Used Cerberus version / latest commit: current master (3f673c9527cebc9a4af2ebc5cf4b0ac3e6f4f5d5)


Bug report

Normalization rules like default cannot be part of a rules set. Consider these failings tests:

def test_rules_set_simple_with_default():
    rules_set_registry.add('foo', {'default': 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})

def test_rules_set_simple_with_default_setter():
    rules_set_registry.add('foo', {'default_setter': lambda _: 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})

The reason is - in my opinion - that the normalization code uses schema[field] directly without calling self._resolve_rules_set where appropriate. This causes other misbehaviour, too:

def test_rules_set_simple_with_none_value():
    rules_set_registry.add('foo', {'type': 'integer', 'nullable': True})
    assert_success({'bar': None}, {'bar': 'foo'})

This will fail because code in __normalize_default_fields will do schema['bar'].get('nullable', False) - which will fail because schema['bar'] will be the string 'foo'.

Is this actually a bug or am I missing something? Are rules sets just meant to be used for validation rules?

funkyfuture commented 7 years ago

it's a bug, the same is probably true for schema_registry.

funkyfuture commented 7 years ago

i'm itchy for a bug-fixing release, @dkellner are you on this issue?

dkellner commented 7 years ago

No, I'm not currently working on this and I'm afraid I will not be able to spend time on this the next couple of weeks.

nicolaiarocci commented 7 years ago

Solved with #294