Skip to content

Commit 97634bd

Browse files
authored
Merge pull request #176 from davidszotten/improve-required-error-message
improve error message for missing required props
2 parents aa7be27 + abfa170 commit 97634bd

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

fastjsonschema/draft04.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,10 @@ def generate_required(self):
457457
with self.l('if {variable}_is_dict:'):
458458
if not isinstance(self._definition['required'], (list, tuple)):
459459
raise JsonSchemaDefinitionException('required must be an array')
460-
self.create_variable_with_length()
461-
with self.l('if not all(prop in {variable} for prop in {required}):'):
462-
self.exc('{name} must contain {} properties', self.e(self._definition['required']), rule='required')
460+
self.l('{variable}__missing_keys = set({required}) - {variable}.keys()')
461+
with self.l('if {variable}__missing_keys:'):
462+
dynamic = 'str(sorted({variable}__missing_keys)) + " properties"'
463+
self.exc('{name} must contain ', self.e(self._definition['required']), rule='required', append_to_msg=dynamic)
463464

464465
def generate_properties(self):
465466
"""

tests/test_integration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@
8484
[9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
8585
JsonSchemaValueException('data[2][1] must be string', value=2, name='data[2][1]', definition={'type': 'string'}, rule='type'),
8686
),
87+
(
88+
[9, 'hello', [1], {'q': 'q', 'x': 'x', 'y': 'y'}, 'str', 5],
89+
JsonSchemaValueException('data[3] must contain [\'a\', \'b\'] properties', value={'q': 'q', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
90+
),
8791
(
8892
[9, 'hello', [1], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
89-
JsonSchemaValueException('data[3] must contain [\'a\', \'b\'] properties', value={'a': 'a', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
93+
JsonSchemaValueException('data[3] must contain [\'b\'] properties', value={'a': 'a', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
9094
),
9195
(
9296
[9, 'hello', [1], {}, 'str', 5],

tests/test_object.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ def test_min_properties(asserter, value, expected):
4343
}, value, expected)
4444

4545

46-
exc = JsonSchemaValueException('data must contain [\'a\', \'b\'] properties', value='{data}', name='data', definition='{definition}', rule='required')
46+
def make_exc(missing):
47+
return JsonSchemaValueException('data must contain {} properties'.format(missing), value='{data}', name='data', definition='{definition}', rule='required')
4748
@pytest.mark.parametrize('value, expected', [
48-
({}, exc),
49-
({'a': 1}, exc),
49+
({}, make_exc(['a', 'b'])),
50+
({'a': 1}, make_exc(['b'])),
5051
({'a': 1, 'b': 2}, {'a': 1, 'b': 2}),
5152
])
5253
def test_required(asserter, value, expected):

0 commit comments

Comments
 (0)