Skip to content

Commit 6df435e

Browse files
committed
skip_on_field_errors defaults to False
Closes marshmallow-code#352 Also addresses marshmallow-code#596
1 parent 596015a commit 6df435e

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Features:
88

99
- ``fields.Nested`` respects ``only='field'`` when deserializing (:issue:`307`). Thanks :user:`erlingbo` for the suggestion and the PR.
1010

11+
Other changes:
12+
13+
- *Backwards-incompatible*: ``skip_on_field_errors`` defaults to ``True`` for ``validates_schema`` (:issue:`352`).
14+
1115

1216
3.0.0a1 (2017-02-26)
1317
++++++++++++++++++++

docs/upgrading.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,62 @@ Use a `post_dump <marshmallow.decorators.post_dump>` to add additional data on s
131131
# => {'z': 123, 'y': 2, 'x': 1}
132132
133133
134+
Schema-level validators are skipped when field validation fails
135+
***************************************************************
136+
137+
By default, schema validator methods decorated by `validates_schema <marshmallow.decorators.validates_schema>` will not be executed if any of the field validators fails (including ``required=True`` validation).
138+
139+
.. code-block:: python
140+
141+
from marshmallow import Schema, fields, validates_schema, ValidationError
142+
143+
class MySchema(Schema):
144+
x = fields.Int(required=True)
145+
y = fields.Int(required=True)
146+
147+
@validates_schema
148+
def validate_schema(self, data):
149+
if data['x'] <= data['y']:
150+
raise ValidationError('x must be greater than y')
151+
152+
153+
schema = MySchema(strict=True)
154+
155+
# 2.x
156+
# A KeyError is raised in validate_schema
157+
schema.load({'x': 2})
158+
159+
# 3.x
160+
# marshmallow.exceptions.ValidationError: {'y': ['Missing data for required field.']}
161+
# validate_schema is not run
162+
schema.load({'x': 2})
163+
164+
If you want a schema validator to run even if a field validator fails, pass ``skip_on_field_errors=False``. Make sure your code handles cases where fields are missing from the deserialized data (due to validation errors).
165+
166+
167+
.. code-block:: python
168+
169+
from marshmallow import Schema, fields, validates_schema, ValidationError
170+
171+
class MySchema(Schema):
172+
x = fields.Int(required=True)
173+
y = fields.Int(required=True)
174+
175+
@validates_schema(skip_on_field_errors=False)
176+
def validate_schema(self, data):
177+
if 'x' in data and 'y' in data:
178+
if data['x'] <= data['y']:
179+
raise ValidationError('x must be greater than y')
180+
181+
182+
schema = MySchema(strict=True)
183+
schema.load({'x': 2})
184+
# marshmallow.exceptions.ValidationError: {'y': ['Missing data for required field.']}
185+
186+
187+
188+
189+
134190
Upgrading to 2.3
135191
++++++++++++++++
136192

marshmallow/decorators.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def validates(field_name):
6969
return tag_processor(VALIDATES, None, False, field_name=field_name)
7070

7171

72-
def validates_schema(fn=None, pass_many=False, pass_original=False, skip_on_field_errors=False):
72+
def validates_schema(fn=None, pass_many=False, pass_original=False, skip_on_field_errors=True):
7373
"""Register a schema-level validator.
7474
7575
By default, receives a single object at a time, regardless of whether ``many=True``
@@ -81,6 +81,9 @@ def validates_schema(fn=None, pass_many=False, pass_original=False, skip_on_fiel
8181
8282
If ``skip_on_field_errors=True``, this validation method will be skipped whenever
8383
validation errors have been detected when validating fields.
84+
85+
.. versionchanged:: 3.0.0b1
86+
``skip_on_field_errors`` defaults to `True`.
8487
"""
8588
return tag_processor(VALIDATES_SCHEMA, fn, pass_many, pass_original=pass_original,
8689
skip_on_field_errors=skip_on_field_errors)

0 commit comments

Comments
 (0)