You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
classMySchema(Schema):
144
+
x = fields.Int(required=True)
145
+
y = fields.Int(required=True)
146
+
147
+
@validates_schema
148
+
defvalidate_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
+
classMySchema(Schema):
172
+
x = fields.Int(required=True)
173
+
y = fields.Int(required=True)
174
+
175
+
@validates_schema(skip_on_field_errors=False)
176
+
defvalidate_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.']}
0 commit comments