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
schema load None #511
Comments
Is it a feature or a bug? |
Although this behavior is debatable, it is still easy to check on your side because it only affects top-level objects, for nested objects it is handled by fields.Nested field type. |
I agree that this is an error, especially if required fields are set. If I set required=True, I expect that any structure coming out of marshmallow has that field in it, so I should not need to check for its existence. |
I would be open to changing the behavior for deserializing
Thoughts? |
I think there would be two level validation. Just my subjective opinion, for yours' discussion. |
Wouldn't it be better if Currently, this is what happens:
I'm not sure whether |
I vote for option 1, return an empty dict, with an error thrown for missing required fields. Any non-required fields would return a KeyError on access, just as if they weren't provided. |
I would be okay with option 1 as long as there is a way to opt-in for passing None to raise an exception. |
I think I like option 2 better. More explicit. And it doesn't break symmetry ( Anybody can provide a use case for @sloria, I guess this should be labeled backwards_incompat. |
I just opened a PR with an implementation of option 2 (raise By just removing the @pytest.mark.parametrize('data', [True, False, 42, None, []])
def test_deserialize_raises_exception_if_input_type_is_incorrect(data):
class MySchema(Schema):
foo = fields.Field()
bar = fields.Field()
with pytest.raises(ValidationError) as excinfo:
MySchema().load(data)
assert 'Invalid input type.' in str(excinfo)
exc = excinfo.value
assert exc.field_names == ['_schema']
assert exc.fields == [] @sloria, do you think we need a specific On second thought, I was wrong about symmetry and As is, it makes no sense, what I meant was more like MyObject(**load(dump(input_obj)) == input_obj and I don't think there's a way to achieve this in all cases.
An object can be instantiated from the load of the dump of its own data only if the schema loads all necessary data and it the object class knows how to create it with this data. |
If I load None to schema, it will not return validate error
The text was updated successfully, but these errors were encountered: