From c83dbc12132d82e6075317c823cf9a8343e1df26 Mon Sep 17 00:00:00 2001 From: lautaro-eb Date: Thu, 10 Oct 2019 09:55:33 -0300 Subject: [PATCH 1/2] [MINOR] EB-124910 Adding pointer to extra key error dictionary field --- conformity/fields/structures.py | 1 + tests/test_fields.py | 6 +++++- tests/test_settings.py | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/conformity/fields/structures.py b/conformity/fields/structures.py index ace52fc..f41d83d 100644 --- a/conformity/fields/structures.py +++ b/conformity/fields/structures.py @@ -305,6 +305,7 @@ def errors(self, value): # type: (AnyType) -> ListType[Error] Error( 'Extra keys present: {}'.format(', '.join(six.text_type(key) for key in sorted(extra_keys))), code=ERROR_CODE_UNKNOWN, + pointer=six.text_type(sorted(extra_keys)[0]), ), ) diff --git a/tests/test_fields.py b/tests/test_fields.py index 234fab2..5bcf701 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -139,7 +139,11 @@ def test_complex(self): # type: () -> None sorted([ Error('Not an integer', pointer='child_ids.2'), Error('Missing key: address', code=ERROR_CODE_MISSING, pointer='address'), - Error('Extra keys present: another_bad, unsolicited_item', code=ERROR_CODE_UNKNOWN), + Error( + 'Extra keys present: another_bad, unsolicited_item', + code=ERROR_CODE_UNKNOWN, + pointer='another_bad', + ), Error('Not a set or frozenset', pointer='unique_things'), ]), ) diff --git a/tests/test_settings.py b/tests/test_settings.py index f1a0020..08181cb 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -368,7 +368,7 @@ def test_validation(self): }, }) - assert '- inner_qux: Extra keys present: not_defined' in error_context.value.args[0] + assert '- inner_qux.not_defined: Extra keys present: not_defined' in error_context.value.args[0] with pytest.raises(Settings.ImproperlyConfigured) as error_context: SettingsTwo({ From ff578ea95f7eefdd80f4b9c3fbab616182cf5359 Mon Sep 17 00:00:00 2001 From: lautaro-eb Date: Thu, 10 Oct 2019 11:03:47 -0300 Subject: [PATCH 2/2] Keeping consistency with missing key errors --- conformity/fields/structures.py | 11 +++++------ tests/test_fields.py | 7 ++----- tests/test_settings.py | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/conformity/fields/structures.py b/conformity/fields/structures.py index f41d83d..c8ef07c 100644 --- a/conformity/fields/structures.py +++ b/conformity/fields/structures.py @@ -301,13 +301,12 @@ def errors(self, value): # type: (AnyType) -> ListType[Error] # Check for extra keys extra_keys = set(value.keys()) - set(self.contents.keys()) if extra_keys and not self.allow_extra_keys: - result.append( - Error( - 'Extra keys present: {}'.format(', '.join(six.text_type(key) for key in sorted(extra_keys))), + for extra_key in sorted(extra_keys): + result.append(Error( + 'Extra key present: {}'.format(six.text_type(extra_key)), code=ERROR_CODE_UNKNOWN, - pointer=six.text_type(sorted(extra_keys)[0]), - ), - ) + pointer=six.text_type(extra_key), + )) if not result and self.additional_validator: return self.additional_validator.errors(value) diff --git a/tests/test_fields.py b/tests/test_fields.py index 5bcf701..229c7ab 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -139,11 +139,8 @@ def test_complex(self): # type: () -> None sorted([ Error('Not an integer', pointer='child_ids.2'), Error('Missing key: address', code=ERROR_CODE_MISSING, pointer='address'), - Error( - 'Extra keys present: another_bad, unsolicited_item', - code=ERROR_CODE_UNKNOWN, - pointer='another_bad', - ), + Error('Extra key present: another_bad', code=ERROR_CODE_UNKNOWN, pointer='another_bad'), + Error('Extra key present: unsolicited_item', code=ERROR_CODE_UNKNOWN, pointer='unsolicited_item'), Error('Not a set or frozenset', pointer='unique_things'), ]), ) diff --git a/tests/test_settings.py b/tests/test_settings.py index 08181cb..498c399 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -368,7 +368,7 @@ def test_validation(self): }, }) - assert '- inner_qux.not_defined: Extra keys present: not_defined' in error_context.value.args[0] + assert '- inner_qux.not_defined: Extra key present: not_defined' in error_context.value.args[0] with pytest.raises(Settings.ImproperlyConfigured) as error_context: SettingsTwo({