Skip to content

Commit

Permalink
improve error message for missing required props
Browse files Browse the repository at this point in the history
only include _missing properties in the error message for required props

```
>>> fastjsonschema.validate({'required': ['foo', 'bar']}, {'foo': 1})
```

before:

```
JsonSchemaValueException: data must contain ['foo', 'bar'] properties
```

after:

```
JsonSchemaValueException: data must contain ['bar'] properties
```
  • Loading branch information
davidszotten committed Jul 13, 2023
1 parent aa7be27 commit abfa170
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 4 additions & 3 deletions fastjsonschema/draft04.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,10 @@ def generate_required(self):
with self.l('if {variable}_is_dict:'):
if not isinstance(self._definition['required'], (list, tuple)):
raise JsonSchemaDefinitionException('required must be an array')
self.create_variable_with_length()
with self.l('if not all(prop in {variable} for prop in {required}):'):
self.exc('{name} must contain {} properties', self.e(self._definition['required']), rule='required')
self.l('{variable}__missing_keys = set({required}) - {variable}.keys()')
with self.l('if {variable}__missing_keys:'):
dynamic = 'str(sorted({variable}__missing_keys)) + " properties"'
self.exc('{name} must contain ', self.e(self._definition['required']), rule='required', append_to_msg=dynamic)

def generate_properties(self):
"""
Expand Down
6 changes: 5 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@
[9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
JsonSchemaValueException('data[2][1] must be string', value=2, name='data[2][1]', definition={'type': 'string'}, rule='type'),
),
(
[9, 'hello', [1], {'q': 'q', 'x': 'x', 'y': 'y'}, 'str', 5],
JsonSchemaValueException('data[3] must contain [\'a\', \'b\'] properties', value={'q': 'q', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
),
(
[9, 'hello', [1], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
JsonSchemaValueException('data[3] must contain [\'a\', \'b\'] properties', value={'a': 'a', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
JsonSchemaValueException('data[3] must contain [\'b\'] properties', value={'a': 'a', 'x': 'x', 'y': 'y'}, name='data[3]', definition=definition['items'][3], rule='required'),
),
(
[9, 'hello', [1], {}, 'str', 5],
Expand Down
7 changes: 4 additions & 3 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def test_min_properties(asserter, value, expected):
}, value, expected)


exc = JsonSchemaValueException('data must contain [\'a\', \'b\'] properties', value='{data}', name='data', definition='{definition}', rule='required')
def make_exc(missing):
return JsonSchemaValueException('data must contain {} properties'.format(missing), value='{data}', name='data', definition='{definition}', rule='required')
@pytest.mark.parametrize('value, expected', [
({}, exc),
({'a': 1}, exc),
({}, make_exc(['a', 'b'])),
({'a': 1}, make_exc(['b'])),
({'a': 1, 'b': 2}, {'a': 1, 'b': 2}),
])
def test_required(asserter, value, expected):
Expand Down

0 comments on commit abfa170

Please sign in to comment.