'Marshmallow ValidationError 'field_name' issue
I have one question concerning newer versions of Marhsmallow API. In our project we are currently using Marshmallow version 3.0.0b14; and we have the following tests functions defined (work perfectly):
from marshmallow.exceptions import ValidationError
from hamcrest import assert_that, calling, has_properties, has_item, not_
def test_invalid_expiration(self):
invalid_values = [None, True, False, 'foobar', 0]
for value in invalid_values:
body = {'expiration': value}
assert_that(
calling(self.schema.load).with_args(body),
raises(ValidationError).matching(
has_properties(field_names=has_item('expiration'))
),
)
def test_that_acces_type_offline_requires_a_client_id(self):
body = {'access_type': 'offline'}
assert_that(
calling(self.schema.load).with_args(body),
raises(ValidationError).matching(
has_properties(field_names=has_item('_schema'))
),
)
def test_that_the_access_type_is_online_when_using_a_refresh_token(self):
body = {'refresh_token': 'foobar', 'client_id': 'x'}
assert_that(calling(self.schema.load).with_args(body), not_(raises(Exception)))
assert_that(
calling(self.schema.load).with_args({'access_type': 'online', **body}),
not_(raises(Exception)),
)
assert_that(
calling(self.schema.load).with_args({'access_type': 'offline', **body}),
raises(ValidationError).matching(
has_properties(field_names=has_item('_schema'))
),
)
def test_that_a_refresh_token_requires_a_client_id(self):
body = {'refresh_token': 'the-token'}
assert_that(
calling(self.schema.load).with_args({'client_id': 'x', **body}),
not_(raises(Exception)),
)
assert_that(
calling(self.schema.load).with_args(body),
raises(ValidationError).matching(
has_properties(field_names=has_item('_schema'))
),
)
Now, we are trying to bump the version of Marshmallow in order to upgrade it to 3.10.0; however, in this version, field_names from ValidationError has been removed and replaced by field_name (which is always equal to _schema if i am correct).
So my question is the following: how can we change/modify the tests posted above so that we don't have any error when executing them. This is what I have tried so far:
raises(ValidationError).matching(
has_properties(messages=has_item('expiration'))
),
raises(ValidationError).matching(
has_properties(messages=has_item('_schema'))
),
We did not match on the new field_name since it is always equal to _schema; so we went on matching/probing the messages field; the following issue relates to a similar issue to what we are facing:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
