File: upgrading.rst

package info (click to toggle)
python-marshmallow 3.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,076 kB
  • sloc: python: 10,539; makefile: 14
file content (1750 lines) | stat: -rw-r--r-- 53,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
Upgrading to Newer Releases
===========================

This section documents migration paths to new releases.

Upgrading to 3.3
++++++++++++++++

In 3.3, `fields.Nested <marshmallow.fields.Nested>` may take a callable that returns a schema instance. 
Use this to resolve order-of-declaration issues when schemas nest each other.

.. code-block:: python

    from marshmallow import Schema, fields

    # <3.3
    class AlbumSchema(Schema):
        title = fields.Str()
        artist = fields.Nested("ArtistSchema", only=("name",))


    class ArtistSchema(Schema):
        name = fields.Str()
        albums = fields.List(fields.Nested(AlbumSchema))


    # >=3.3
    class AlbumSchema(Schema):
        title = fields.Str()
        artist = fields.Nested(lambda: ArtistSchema(only=("name",)))


    class ArtistSchema(Schema):
        name = fields.Str()
        albums = fields.List(fields.Nested(AlbumSchema))

A callable should also be used when nesting a schema within itself.
Passing ``"self"`` is deprecated.

.. code-block:: python

    from marshmallow import Schema, fields

    # <3.3
    class PersonSchema(Schema):
        partner = fields.Nested("self", exclude=("partner",))
        friends = fields.List(fields.Nested("self"))


    # >=3.3
    class PersonSchema(Schema):
        partner = fields.Nested(lambda: PersonSchema(exclude=("partner")))
        friends = fields.List(fields.Nested(lambda: PersonSchema()))

.. _upgrading_3_0:

Upgrading to 3.0
++++++++++++++++

Python compatibility
********************

The marshmallow 3.x series supports Python >= 3.5.

Python 2.6, 2.7, 3.3, and 3.4 are no longer supported.


Schemas are always strict
*************************

Two major changes were made to (de)serialization behavior:

- The ``strict`` parameter was removed. Schemas are always strict.
- `Schema().load <marshmallow.Schema.load>` and `Schema().dump <marshmallow.Schema.dump>` don't return a ``(data, errors)`` tuple any more. Only ``data`` is returned.

If invalid data are passed, a :exc:`ValidationError <marshmallow.exceptions.ValidationError>` is raised.
The dictionary of validation errors is accessible from the
`ValidationError.messages <marshmallow.exceptions.ValidationError.messages>` attribute,
along with the valid data from the `ValidationError.valid_data
<marshmallow.exceptions.ValidationError.valid_data>` attribute.

.. code-block:: python

    from marshmallow import ValidationError

    # 2.x
    schema = UserSchema()
    data, errors = schema.load({"name": "Monty", "email": "monty@python.org"})
    # OR
    schema = UserSchema(strict=True)
    try:
        data, _ = schema.load({"name": "Monty", "email": "monty@python.org"})
    except ValidationError as err:
        errors = err.messages
        valid_data = err.valid_data

    # 3.x
    schema = UserSchema()
    # There is only one right way
    try:
        data = schema.load({"name": "Monty", "email": "monty@python.org"})
    except ValidationError as err:
        errors = err.messages
        valid_data = err.valid_data

:meth:`Schema.validate() <marshmallow.Schema.validate>` always returns a dictionary of validation errors (same as 2.x with ``strict=False``).

.. code-block:: python

    schema.validate({"email": "invalid"})
    # {'email': ['Not a valid email address.']}

Setting the ``strict`` option on ``class Meta`` has no effect on `Schema` behavior.
Passing ``strict=True`` or ``strict=False`` to the `Schema` constructor
will raise a :exc:`TypeError`.


.. code-block:: python

    # 3.x
    UserSchema(strict=True)
    # TypeError: __init__() got an unexpected keyword argument 'strict'


.. seealso::

    See GitHub issues :issue:`377` and :issue:`598` for the discussions on
    this change.


Decorated methods and ``handle_error`` receive ``many`` and ``partial``
***********************************************************************

Methods decorated with
`pre_load <marshmallow.decorators.pre_load>`, `post_load <marshmallow.decorators.post_load>`,
`pre_dump <marshmallow.decorators.pre_dump>`, `post_dump <marshmallow.decorators.post_dump>`,
and `validates_schema <marshmallow.decorators.validates_schema>` receive
``many`` as a keyword argument. In addition, `pre_load <marshmallow.decorators.pre_load>`, `post_load <marshmallow.decorators.post_load>`,
and `validates_schema <marshmallow.decorators.validates_schema>` receive
``partial``. To account for these additional arguments, add ``**kwargs`` to your methods.

.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        name = fields.Str()
        slug = fields.Str()

        @pre_load
        def slugify_name(self, in_data):
            in_data["slug"] = in_data["slug"].lower().strip().replace(" ", "-")
            return in_data


    # 3.x
    class UserSchema(Schema):
        name = fields.Str()
        slug = fields.Str()

        @pre_load
        def slugify_name(self, in_data, **kwargs):
            in_data["slug"] = in_data["slug"].lower().strip().replace(" ", "-")
            return in_data


`Schema.handle_error <marshmallow.Schema.handle_error>` also receives ``many`` and ``partial`` as keyword arguments.

.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        def handle_error(self, exc, data):
            raise AppError("An error occurred with input: {0}".format(data))


    # 3.x
    class UserSchema(Schema):
        def handle_error(self, exc, data, **kwargs):
            raise AppError("An error occurred with input: {0}".format(data))


Validation does not occur on serialization
******************************************

:meth:`Schema.dump() <marshmallow.Schema.dump>` will no longer validate and collect error messages. You *must* validate
your data before serializing it.

.. code-block:: python

    from marshmallow import Schema, fields, ValidationError

    invalid_data = dict(created_at="invalid")


    class WidgetSchema(Schema):
        created_at = fields.DateTime()


    # 2.x
    WidgetSchema(strict=True).dump(invalid_data)
    # marshmallow.exceptions.ValidationError: {'created_at': ['"invalid" cannot be formatted as a datetime.']}

    # 3.x
    WidgetSchema().dump(invalid_data)
    # AttributeError: 'str' object has no attribute 'isoformat'

    # Instead, validate before dumping
    schema = WidgetSchema()
    try:
        widget = schema.load(invalid_data)
    except ValidationError:
        print("handle errors...")
    else:
        dumped = schema.dump(widget)


Deserializing invalid types raises a ``ValidationError``
********************************************************

Numbers, booleans, strings, and ``None`` are
considered invalid input to `Schema.load
<marshmallow.Schema.load>`.

.. code-block:: python

    # 2.x
    # Passes silently
    schema.load(None)
    schema.load(False)
    schema.load("pass")

    # 3.x
    # marshmallow.exceptions.ValidationError: {'_schema': ['Invalid input type.']}
    schema.load(None)
    schema.load(False)
    schema.load("nope")


When ``many=True``, non-collection types are also considered invalid.


.. code-block:: python

    # 2.x
    # Passes silently
    schema.load(None, many=True)
    schema.load({}, many=True)
    schema.load("pass", many=True)

    # 3.x
    # marshmallow.exceptions.ValidationError: {'_schema': ['Invalid input type.']}
    schema.load(None, many=True)
    schema.load({}, many=True)
    schema.load("invalid", many=True)


``ValidationError.fields`` is removed
*************************************

:exc:`ValidationError <marshmallow.exceptions.ValidationError>` no
longer stores a list of `Field <marshmallow.fields.Field>` instances
associated with the validation errors.

If you need field instances associated with an error, you can access
them from ``schema.fields``.

.. code-block:: python


    from marshmallow import Schema, fields, ValidationError


    class MySchema(Schema):
        foo = fields.Int()


    schema = MySchema()

    try:
        schema.load({"foo": "invalid"})
    except ValidationError as error:
        field = schema.fields["foo"]
        # ...


``ValidationError`` expects a single field name
***********************************************

:exc:`ValidationError <marshmallow.exceptions.ValidationError>` no
longer accepts a list of field names. It expects a single field name. If none
is passed, the error refers to the schema.

To return an error for several fields at once, a `dict` must be used.

.. code-block:: python

    from marshmallow import Schema, fields, validates_schema, ValidationError


    class NumberSchema(Schema):
        field_a = fields.Integer()
        field_b = fields.Integer()

        # 2.x
        @validates_schema
        def validate_numbers(self, data):
            if data["field_b"] >= data["field_a"]:
                raise ValidationError(
                    "field_a must be greater than field_b", ["field_a", "field_b"]
                )

        # 3.x
        @validates_schema
        def validate_numbers(self, data):
            if data["field_b"] >= data["field_a"]:
                raise ValidationError(
                    {
                        "field_a": ["field_a must be greater than field_b"],
                        "field_b": ["field_a must be greater than field_b"],
                    }
                )

``ValidationError`` error messages are deep-merged
**************************************************

When multiple :exc:`ValidationError <marshmallow.exceptions.ValidationError>`
are raised, the error structures are merged in the final :exc:`ValidationError`
raised at the end of the process.

When reporting error messages as `dict`, the keys should refer to subitems
of the item the message refers to, and the values should be error messages.

See the "Schema-level Validation" section of :doc:`Extending Schemas <extending>`
page for an example.

Schemas raise ``ValidationError`` when deserializing data with unknown keys
***************************************************************************

Marshmallow 3.x schemas can deal with unknown keys in three different ways,
configurable with the ``unknown`` option:

- ``EXCLUDE``: drop those keys (same as marshmallow 2)
- ``INCLUDE``: pass those keys/values as is, with no validation performed
- ``RAISE`` (default): raise a ``ValidationError``

The ``unknown`` option can be passed as a Meta option, on Schema instantiation,
or at load time.

.. code-block:: python

    from marshmallow import Schema, fields, EXCLUDE, INCLUDE, RAISE


    class MySchema(Schema):
        foo = fields.Int()

        class Meta:
            # Pass EXCLUDE as Meta option to keep marshmallow 2 behavior
            unknown = EXCLUDE


    MySchema().load({"foo": 42, "bar": "whatever"})  # => ['foo': 42]

    #  Value passed on instantiation overrides Meta option
    schema = MySchema(unknown=INCLUDE)
    schema.load({"foo": 42, "bar": "whatever"})  # => ['foo': 42, 'bar': 'whatever']

    #  Value passed on load overrides instance attribute
    schema.load({"foo": 42, "bar": "whatever"}, unknown=RAISE)  # => ValidationError

Overriding ``get_attribute``
****************************

If your `Schema <marshmallow.Schema>` overrides `get_attribute <marshmallow.Schema.get_attribute>`, you will need to update the method's signature. The positions of the ``attr`` and ``obj`` arguments were switched for consistency with Python builtins, e.g. `getattr`.

.. code-block:: python

    from marshmallow import Schema

    # 2.x
    class MySchema(Schema):
        def get_attribute(self, attr, obj, default):
            return getattr(obj, attr, default)


    # 3.x
    class MySchema(Schema):
        def get_attribute(self, obj, attr, default):
            return getattr(obj, attr, default)

``pass_original=True`` passes individual items when ``many=True``
*****************************************************************

When ``pass_original=True`` is passed to
`validates_schema <marshmallow.decorators.validates_schema>`,
`post_load <marshmallow.decorators.post_load>`, or
`post_dump <marshmallow.decorators.post_dump>`, the `original_data`
argument will be a single item corresponding to the (de)serialized
datum.

.. code-block:: python

    from marshmallow import Schema, fields, post_load, EXCLUDE


    class ShoeSchema(Schema):
        size = fields.Int()

        class Meta:
            unknown = EXCLUDE

        @post_load(pass_original=True)
        def post_load(self, data, original_data, **kwargs):
            # original_data has 'width' but
            # data does not because it's not
            # in the schema
            assert "width" in original_data
            assert "width" not in data
            return data


    input_data = [{"size": 10, "width": "M"}, {"size": 6, "width": "W"}]

    print(ShoeSchema(many=True).load(input_data))
    # [{'size': 10}, {'size': 6}]


``utils.get_func_args`` no longer returns bound arguments
*********************************************************

The `utils.get_func_args <marshmallow.utils.get_func_args>` function will no longer return bound arguments, e.g. `'self'`.

.. code-block:: python

    from marshmallow.utils import get_func_args


    class MyCallable:
        def __call__(self, foo, bar):
            return 42


    callable_obj = MyCallable()

    # 2.x
    get_func_args(callable_obj)  # => ['self', 'foo', 'bar']

    # 3.x
    get_func_args(callable_obj)  # => ['foo', 'bar']


Handling ``AttributeError`` in ``Method`` and ``Function`` fields
*****************************************************************

The `Method <marshmallow.fields.Method>` and `Function <marshmallow.fields.Function>` fields no longer swallow ``AttributeErrors``. Therefore, your methods and functions are responsible for handling inputs such as `None`.

.. code-block:: python

    from marshmallow import Schema, fields, missing

    # 2.x
    class ShapeSchema(Schema):
        area = fields.Method("get_area")

        def get_area(self, obj):
            return obj.height * obj.length


    schema = ShapeSchema()
    # In 2.x, the following would pass without errors
    # In 3.x, and AttributeError would be raised
    result = schema.dump(None)
    result  # => {}


    # 3.x
    class ShapeSchema(Schema):
        area = fields.Method("get_area")

        def get_area(self, obj):
            if obj is None:
                # 'area' will not appear in serialized output
                return missing
            return obj.height * obj.length


    schema = ShapeSchema()
    result = schema.dump(None)
    result  # => {}

Adding additional data to serialized output
*******************************************

Use a `post_dump <marshmallow.decorators.post_dump>` to add additional data on serialization. The ``extra`` argument on `Schema <marshmallow.Schema>` was removed.


.. code-block:: python

    from marshmallow import Schema, fields, post_dump

    # 2.x
    class MySchema(Schema):
        x = fields.Int()
        y = fields.Int()


    schema = MySchema(extra={"z": 123})
    schema.dump({"x": 1, "y": 2})
    # => {'z': 123, 'y': 2, 'x': 1}

    # 3.x
    class MySchema(Schema):
        x = fields.Int()
        y = fields.Int()

        @post_dump
        def add_z(self, output):
            output["z"] = 123
            return output


    schema = MySchema()
    schema.dump({"x": 1, "y": 2})
    # => {'z': 123, 'y': 2, 'x': 1}


Schema-level validators are skipped when field validation fails
***************************************************************

By default, schema validator methods decorated by `validates_schema <marshmallow.decorators.validates_schema>` won't execute if any of the field validators fails (including ``required=True`` validation).

.. code-block:: python

    from marshmallow import Schema, fields, validates_schema, ValidationError


    class MySchema(Schema):
        x = fields.Int(required=True)
        y = fields.Int(required=True)

        @validates_schema
        def validate_schema(self, data):
            if data["x"] <= data["y"]:
                raise ValidationError("x must be greater than y")


    schema = MySchema()

    # 2.x
    # A KeyError is raised in validate_schema
    schema.load({"x": 2})

    # 3.x
    # marshmallow.exceptions.ValidationError: {'y': ['Missing data for required field.']}
    # validate_schema is not run
    schema.load({"x": 2})

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).


.. code-block:: python

    from marshmallow import Schema, fields, validates_schema, ValidationError


    class MySchema(Schema):
        x = fields.Int(required=True)
        y = fields.Int(required=True)

        @validates_schema(skip_on_field_errors=False)
        def validate_schema(self, data):
            if "x" in data and "y" in data:
                if data["x"] <= data["y"]:
                    raise ValidationError("x must be greater than y")


    schema = MySchema()
    schema.load({"x": 2})
    # marshmallow.exceptions.ValidationError: {'y': ['Missing data for required field.']}

`SchemaOpts` constructor receives ``ordered`` argument
******************************************************

Subclasses of `SchemaOpts <marshmallow.SchemaOpts>` receive an additional argument, ``ordered``, which is `True` if the `ordered` option is set to `True` on a Schema or one of its parent classes.

.. code-block:: python

    from marshmallow import SchemaOpts

    # 2.x
    class CustomOpts(SchemaOpts):
        def __init__(self, meta):
            super().__init__(meta)
            self.custom_option = getattr(meta, "meta", False)


    # 3.x
    class CustomOpts(SchemaOpts):
        def __init__(self, meta, ordered=False):
            super().__init__(meta, ordered)
            self.custom_option = getattr(meta, "meta", False)

`ContainsOnly` accepts empty and duplicate values
*************************************************

`validate.ContainsOnly <marshmallow.validate.ContainsOnly>` now accepts duplicate values in the input value.


.. code-block:: python

    from marshmallow import validate

    validator = validate.ContainsOnly(["red", "blue"])

    # in 2.x the following raises a ValidationError
    # in 3.x, no error is raised
    validator(["red", "red", "blue"])


If you don't want to accept duplicates, use a custom validator, like the following.

.. code-block:: python

    from marshmallow import ValidationError
    from marshmallow.validate import ContainsOnly


    class ContainsOnlyNoDuplicates(ContainsOnly):
        def __call__(self, value):
            ret = super(ContainsOnlyNoDuplicates, self).__call__(value)
            if len(set(value)) != len(value):
                raise ValidationError("Duplicate values not allowed")
            return ret

.. note::

    If you need to handle unhashable types, you can use the  `implementation of
    ContainsOnly from marshmallow 2.x <https://github.com/marshmallow-code/marshmallow/blob/2888e6978bc8c409a5fed35da6ece8bdb23384f2/marshmallow/validate.py#L436-L467>`_.

`validate.ContainsOnly <marshmallow.validate.ContainsOnly>` also accepts empty values as valid input.

.. code-block:: python

    from marshmallow import validate

    validator = validate.ContainsOnly(["red", "blue"])

    # in 2.x the following raises a ValidationError
    # in 3.x, no error is raised
    validator([])

To validate against empty inputs, use `validate.Length(min=1) <marshmallow.validate.Length>`.


``json_module`` option is renamed to ``render_module``
******************************************************

The ``json_module`` class Meta option is deprecated in favor of ``render_module``.

.. code-block:: python

    import ujson

    # 2.x
    class MySchema(Schema):
        class Meta:
            json_module = ujson


    # 3.x
    class MySchema(Schema):
        class Meta:
            render_module = ujson


``missing`` and ``default`` ``Field`` parameters are passed in deserialized form
********************************************************************************

.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        id = fields.UUID(missing=lambda: str(uuid.uuid1()))
        birthdate = fields.DateTime(default=lambda: dt.datetime(2017, 9, 19).isoformat())


    # 3.x
    class UserSchema(Schema):
        id = fields.UUID(missing=uuid.uuid1)
        birthdate = fields.DateTime(default=dt.datetime(2017, 9, 19))


Pass ``default`` as a keyword argument
**************************************

`fields.Boolean <marshmallow.fields.Boolean>` now receives additional ``truthy`` and ``falsy`` parameters. Consequently, the ``default`` parameter should always be passed as a keyword argument.


.. code-block:: python

    # 2.x
    fields.Boolean(True)

    # 3.x
    fields.Boolean(default=True)


``Email`` and ``URL`` fields do not validate on serialization
*************************************************************

`fields.Email <marshmallow.fields.Email>` and `fields.URL <marshmallow.fields.URL>` only validate input upon
deserialization. They do not validate on serialization. This makes them
more consistent with the other fields and improves serialization
performance.


``load_from`` and ``dump_to`` are merged into ``data_key``
**********************************************************

The same key is used for serialization and deserialization.

.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        email = fields.Email(load_from="CamelCasedEmail", dump_to="CamelCasedEmail")


    # 3.x
    class UserSchema(Schema):
        email = fields.Email(data_key="CamelCasedEmail")

It is not possible to specify a different key for serialization and deserialization on the same field.
This use case is covered by using two different `Schema`.

.. code-block:: python

    from marshmallow import Schema, fields

    # 2.x
    class UserSchema(Schema):
        id = fields.Str()
        email = fields.Email(load_from="CamelCasedEmail", dump_to="snake_case_email")


    # 3.x
    class BaseUserSchema(Schema):
        id = fields.Str()


    class LoadUserSchema(BaseUserSchema):
        email = fields.Email(data_key="CamelCasedEmail")


    class DumpUserSchema(BaseUserSchema):
        email = fields.Email(data_key="snake_case_email")


Also, when ``data_key`` is specified on a field, only ``data_key`` is checked in the input data. In marshmallow 2.x the field name is checked if ``load_from`` is missing from the input data.

Pre/Post-processors must return modified data
*********************************************

In marshmallow 2.x, ``None`` returned by a pre or post-processor is interpreted as "the data was mutated". In marshmallow 3.x, the return value is considered as processed data even if it is ``None``.

Processors that mutate the data should be updated to also return it.


.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        name = fields.Str()
        slug = fields.Str()

        @pre_load
        def slugify_name(self, in_data):
            # In 2.x, implicitly returning None implied that data were mutated
            in_data["slug"] = in_data["slug"].lower().strip().replace(" ", "-")


    # 3.x
    class UserSchema(Schema):
        name = fields.Str()
        slug = fields.Str()

        @pre_load
        def slugify_name(self, in_data, **kwargs):
            # In 3.x, always return the processed data
            in_data["slug"] = in_data["slug"].lower().strip().replace(" ", "-")
            return in_data

``Nested`` field no longer supports plucking
********************************************

In marshmallow 2.x, when a string was passed to a ``Nested`` field's ```only`` parameter, the field would be plucked. In marshmallow 3.x, the ``Pluck`` field must be used instead.


.. code-block:: python

    # 2.x
    class UserSchema(Schema):
        name = fields.Str()
        friends = fields.Nested("self", many=True, only="name")


    # 3.x
    class UserSchema(Schema):
        name = fields.Str()
        friends = fields.Pluck("self", "name", many=True)


Accessing attributes on objects within a list
*********************************************

In order to serialize attributes on inner objects within a list, use the
``Pluck`` field.

.. code-block:: python

    # 2.x
    class FactorySchema(Schema):
        widget_ids = fields.List(fields.Int(attribute="id"))


    # 3.x
    class FactorySchema(Schema):
        widget_ids = fields.List(fields.Pluck(WidgetSchema, "id"))


``List`` does not wrap single values in a list on serialization
***************************************************************

In marshmallow 2.x, ``List`` serializes a single object as a list with a single
element. In marshmallow 3.x, the object is assumed to be iterable and passing a
non-iterable element results in an error.

.. code-block:: python

    class UserSchema(Schema):
        numbers = fields.List(fields.Int())


    user = {"numbers": 1}
    UserSchema().dump(user)

    # 2.x
    # => {'numbers': [1]}

    # 3.x
    # => TypeError: 'int' object is not iterable


``Float`` field takes a new ``allow_nan`` parameter
***************************************************

In marshmallow 2.x, ``Float`` field would serialize and deserialize special values such as ``nan``, ``inf`` or ``-inf``. In marshmallow 3, those values trigger a ``ValidationError`` unless ``allow_nan`` is ``True``. ``allow_nan`` defaults to ``False``.


.. code-block:: python

    # 2.x
    class MySchema(Schema):
        x = fields.Float()


    MySchema().load({"x": "nan"})
    # => {{'x': nan}}

    # 3.x
    class MySchema(Schema):
        x = fields.Float()
        y = fields.Float(allow_nan=True)


    MySchema().load({"x": 12, "y": "nan"})
    # => {{'x': 12.0, 'y': nan}}

    MySchema().load({"x": "nan"})
    # marshmallow.exceptions.ValidationError: {'x': ['Special numeric values (nan or infinity) are not permitted.']}

``DateTime`` field ``dateformat`` ``Meta`` option is renamed ``datetimeformat``
*******************************************************************************

The ``Meta`` option ``dateformat`` used to pass format to `DateTime <marshmallow.fields.DateTime>` field is renamed as ``datetimeformat``.

`Date <marshmallow.fields.Date>` field gets a new ``format`` parameter to specify the format to use for serialization. ``dateformat`` ``Meta`` option now applies to `Date <marshmallow.fields.Date>` field.

.. code-block:: python

    # 2.x
    class MySchema(Schema):
        x = fields.DateTime()

        class Meta:
            dateformat = "%Y-%m"


    MySchema().dump({"x": dt.datetime(2017, 9, 19)})
    # => {{'x': '2017-09'}}

    # 3.x
    class MySchema(Schema):
        x = fields.DateTime()
        y = fields.Date()

        class Meta:
            datetimeformat = "%Y-%m"
            dateformat = "%m-%d"


    MySchema().dump({"x": dt.datetime(2017, 9, 19), "y": dt.date(2017, 9, 19)})
    # => {{'x': '2017-09', 'y': '09-19'}}

``DateTime`` leaves timezone information untouched during serialization
***********************************************************************

``DateTime`` does not convert naive datetimes to UTC on serialization and
``LocalDateTime`` is removed.

.. code-block:: python

    # 2.x
    class MySchema(Schema):
        x = fields.DateTime()
        y = fields.DateTime()
        z = fields.LocalDateTime()


    MySchema().dump(
        {
            "x": dt.datetime(2017, 9, 19),
            "y": dt.datetime(2017, 9, 19, tzinfo=dt.timezone(dt.timedelta(hours=2))),
            "z": dt.datetime(2017, 9, 19, tzinfo=dt.timezone(dt.timedelta(hours=2))),
        }
    )
    # => {{'x': '2017-09-19T00:00:00+00:00', 'y': '2017-09-18T22:00:00+00:00', 'z': '2017-09-19T00:00:00+02:00'}}

    # 3.x
    class MySchema(Schema):
        x = fields.DateTime()
        y = fields.DateTime()


    MySchema().dump(
        {
            "x": dt.datetime(2017, 9, 19),
            "y": dt.datetime(2017, 9, 19, tzinfo=dt.timezone(dt.timedelta(hours=2))),
        }
    )
    # => {{'x': '2017-09-19T00:00:00', 'y': '2017-09-19T00:00:00+02:00'}}

The ``prefix`` ``Schema`` parameter is removed
**********************************************

The ``prefix`` parameter of ``Schema`` is removed. The same feature can be achieved using a post_dump <marshmallow.decorators.post_dump>` method.


.. code-block:: python

    # 2.x
    class MySchema(Schema):
        f1 = fields.Field()
        f2 = fields.Field()


    MySchema(prefix="pre_").dump({"f1": "one", "f2": "two"})
    # {'pre_f1': 'one', '_pre_f2': 'two'}

    # 3.x
    class MySchema(Schema):
        f1 = fields.Field()
        f2 = fields.Field()

        @post_dump
        def prefix_usr(self, data):
            return {"usr_{}".format(k): v for k, v in iteritems(data)}


    MySchema().dump({"f1": "one", "f2": "two"})
    # {'pre_f1': 'one', '_pre_f2': 'two'}


``fields.FormattedString`` is removed
*************************************

``fields.FormattedString`` field is removed. Use `fields.Function
<marshmallow.fields.Function>` or
`fields.Method <marshmallow.fields.Method>` instead.

.. code-block:: python

    # 2.x
    class MySchema(Schema):
        full_name = fields.FormattedString("{first_name} {last_name}")


    # 3.x
    class MySchema(Schema):
        full_name = fields.Function(lambda u: f"{u.first_name} {u.last_name}")


``attribute`` or ``data_key`` collision triggers an exception
*************************************************************

When a `Schema <marshmallow.Schema>` is instantiated, a check is performed and a ``ValueError`` is triggered if

- several fields have the same ``attribute`` value (or field name if ``attribute`` is not passed), excluding ``dump_only`` fields, or
- several fields have the same ``data_key`` value (or field name if ``data_key`` is not passed), excluding ``load_only`` fields

In marshmallow 2, it was possible to have multiple fields with the same ``attribute``. It would work provided the ``Schema`` was only used for dumping. When loading, the behaviour was undefined. In marshmallow 3, all but one of those fields must be marked as ``dump_only``. Likewise for ``data_key`` (formerly ``dump_to``) for fields that are not ``load_only``.

.. code-block:: python

    # 2.x
    class MySchema(Schema):
        f1 = fields.Field()
        f2 = fields.Field(attribute="f1")
        f3 = fields.Field(attribute="f5")
        f4 = fields.Field(attribute="f5")


    MySchema()
    #  No error

    # 3.x
    class MySchema(Schema):
        f1 = fields.Field()
        f2 = fields.Field(attribute="f1")
        f3 = fields.Field(attribute="f5")
        f4 = fields.Field(attribute="f5")


    MySchema()
    # ValueError: 'Duplicate attributes: ['f1', 'f5]'


    class MySchema(Schema):
        f1 = fields.Field()
        f2 = fields.Field(attribute="f1", dump_only=True)
        f3 = fields.Field(attribute="f5")
        f4 = fields.Field(attribute="f5", dump_only=True)


    MySchema()
    # No error


``Field.fail`` is deprecated in favor of ``Field.make_error``
*************************************************************

`Field.fail <marshmallow.fields.Field.fail>` is deprecated. 
Use `Field.make_error <marshmallow.fields.Field.fail>`. This allows you to
re-raise exceptions using ``raise ... from ...``.

.. code-block:: python

    from marshmallow import fields, ValidationError
    from packaging import version

    # 2.x
    class Version(fields.Field):
        default_error_messages = {"invalid": "Not a valid version."}

        def _deserialize(self, value, *args, **kwargs):
            try:
                return version.Version(value)
            except version.InvalidVersion:
                self.fail("invalid")


    # 3.x
    class Version(fields.Field):
        default_error_messages = {"invalid": "Not a valid version."}

        def _deserialize(self, value, *args, **kwargs):
            try:
                return version.Version(value)
            except version.InvalidVersion as error:
                raise self.make_error("invalid") from error


``python-dateutil`` recommended dependency is removed
*****************************************************

In marshmallow 2, ``python-dateutil`` was used to deserialize RFC or ISO 8601
datetimes if it was installed. In marshmallow 3, datetime deserialization is
done with no additional dependency.

``python-dateutil`` is no longer used by marshmallow.


Custom Fields
*************

To make your custom fields compatible with marshmallow 3, ``_deserialize``
should accept ``**kwargs``:

.. code-block:: python

    from marshmallow import fields, ValidationError
    from packaging import version

    # 2.x
    class MyCustomField(fields.Field):
        def _deserialize(self, value, attr, obj):
            ...


    # 3.x
    class MyCustomField(fields.Field):
        def _deserialize(self, value, attr, obj, **kwargs):
            ...


Upgrading to 2.3
++++++++++++++++

The ``func`` parameter of `fields.Function <marshmallow.fields.Function>` was renamed to ``serialize``.


.. code-block:: python

    # YES
    lowername = fields.Function(serialize=lambda obj: obj.name.lower())
    # or
    lowername = fields.Function(lambda obj: obj.name.lower())

    # NO
    lowername = fields.Function(func=lambda obj: obj.name.lower())

Similarly, the ``method_name`` of `fields.Method <marshmallow.fields.Method>` was also renamed to ``serialize``.

.. code-block:: python

    # YES
    lowername = fields.Method(serialize="lowercase")
    # or
    lowername = fields.Method("lowercase")

    # NO
    lowername = fields.Method(method_name="lowercase")

The ``func`` parameter is still available for backwards-compatibility. It will be removed in marshmallow 3.0.

Both `fields.Function <marshmallow.fields.Function>` and `fields.Method <marshmallow.fields.Method>` will allow the serialize parameter to not be passed, in this case use the ``deserialize`` parameter by name.

.. code-block:: python

    lowername = fields.Function(deserialize=lambda name: name.lower())
    # or
    lowername = fields.Method(deserialize="lowername")

Upgrading to 2.0
++++++++++++++++

Deserializing `None`
********************

In 2.0, validation/deserialization of `None` is consistent across field types. If ``allow_none`` is `False` (the default), validation fails when the field's value is `None`. If ``allow_none`` is `True`, `None` is considered valid, and the field deserializes to `None`.


.. code-block:: python

    from marshmallow import fields

    # In 1.0, deserialization of None was inconsistent
    fields.Int().deserialize(None)  # 0
    fields.Str().deserialize(None)  # ''
    fields.DateTime().deserialize(None)  # error: Could not deserialize None to a datetime.


    # In 2.0, validation/deserialization of None is consistent
    fields.Int().deserialize(None)  # error: Field may not be null.
    fields.Str().deserialize(None)  # error: Field may not be null.
    fields.DateTime().deserialize(None)  # error: Field may not be null.

    # allow_none makes None a valid value
    fields.Int(allow_none=True).deserialize(None)  # None

Default Values
**************

Before version 2.0, certain fields (including `String <marshmallow.fields.String>`, `List <marshmallow.fields.List>`, `Nested <marshmallow.fields.Nested>`, and number fields) had implicit default values that would be used if their corresponding input value was `None` or missing.


In 2.0, these implicit defaults are removed.  A `Field's <marshmallow.fields.Field>` ``default`` parameter is only used if you explicitly set it. Otherwise, missing inputs will be excluded from the serialized output.

.. code-block:: python

    from marshmallow import Schema, fields


    class MySchema(Schema):
        str_no_default = fields.Str()
        int_no_default = fields.Int()
        list_no_default = fields.List(fields.Str)


    schema = MySchema()

    # In 1.0, None was treated as a missing input, so implicit default values were used
    schema.dump(
        {"str_no_default": None, "int_no_default": None, "list_no_default": None}
    ).data
    # {'str_no_default': '', 'int_no_default': 0, 'list_no_default': []}

    # In 2.0, None serializes to None. No more implicit defaults.
    schema.dump(
        {"str_no_default": None, "int_no_default": None, "list_no_default": None}
    ).data
    # {'str_no_default': None, 'int_no_default': None, 'list_no_default': None}


.. code-block:: python

    # In 1.0, implicit default values were used for missing inputs
    schema.dump({}).data
    # {'int_no_default': 0, 'str_no_default': '', 'list_no_default': []}

    # In 2.0, missing inputs are excluded from the serialized output
    # if no defaults are specified
    schema.dump({}).data
    # {}


As a consequence of this new behavior, the ``skip_missing`` class Meta option has been removed.


Pre-processing and Post-processing Methods
******************************************

The pre- and post-processing API was significantly improved for better consistency and flexibility. The `pre_load <marshmallow.decorators.pre_load>`, `post_load <marshmallow.decorators.post_load>`, `pre_dump <marshmallow.decorators.pre_dump>`, and `post_dump <marshmallow.decorators.post_dump>` should be used to define processing hooks. `Schema.preprocessor` and `Schema.data_handler` are removed.


.. code-block:: python

    # 1.0 API
    from marshmallow import Schema, fields


    class ExampleSchema(Schema):
        field_a = fields.Int()


    @ExampleSchema.preprocessor
    def increment(schema, data):
        data["field_a"] += 1
        return data


    @ExampleSchema.data_handler
    def decrement(schema, data, obj):
        data["field_a"] -= 1
        return data


    # 2.0 API
    from marshmallow import Schema, fields, pre_load, post_dump


    class ExampleSchema(Schema):
        field_a = fields.Int()

        @pre_load
        def increment(self, data):
            data["field_a"] += 1
            return data

        @post_dump
        def decrement(self, data):
            data["field_a"] -= 1
            return data

See the :doc:`Extending Schemas <extending>` page for more information on the ``pre_*`` and ``post_*`` decorators.

Schema Validators
*****************

Similar to pre-processing and post-processing methods, schema validators are now defined as methods. Decorate schema validators with `validates_schema <marshmallow.decorators.validates_schema>`. `Schema.validator` is removed.

.. code-block:: python

    # 1.0 API
    from marshmallow import Schema, fields, ValidationError


    class MySchema(Schema):
        field_a = fields.Int(required=True)
        field_b = fields.Int(required=True)


    @ExampleSchema.validator
    def validate_schema(schema, data):
        if data["field_a"] < data["field_b"]:
            raise ValidationError("field_a must be greater than field_b")


    # 2.0 API
    from marshmallow import Schema, fields, validates_schema, ValidationError


    class MySchema(Schema):
        field_a = fields.Int(required=True)
        field_b = fields.Int(required=True)

        @validates_schema
        def validate_schema(self, data):
            if data["field_a"] < data["field_b"]:
                raise ValidationError("field_a must be greater than field_b")

Custom Accessors and Error Handlers
***********************************

Custom accessors and error handlers are now defined as methods. `Schema.accessor` and `Schema.error_handler` are deprecated.

.. code-block:: python

    from marshmallow import Schema, fields

    # 1.0 Deprecated API
    class ExampleSchema(Schema):
        field_a = fields.Int()


    @ExampleSchema.accessor
    def get_from_dict(schema, attr, obj, default=None):
        return obj.get(attr, default)


    @ExampleSchema.error_handler
    def handle_errors(schema, errors, obj):
        raise CustomError("Something bad happened", messages=errors)


    # 2.0 API
    class ExampleSchema(Schema):
        field_a = fields.Int()

        def get_attribute(self, attr, obj, default):
            return obj.get(attr, default)

        # handle_error gets passed a ValidationError
        def handle_error(self, exc, data):
            raise CustomError("Something bad happened", messages=exc.messages)

Use `post_load <marshmallow.decorators.post_load>` instead of `make_object`
***************************************************************************

The `make_object` method was deprecated from the `Schema <marshmallow.Schema>` API (see :issue:`277` for the rationale). In order to deserialize to an object, use a `post_load <marshmallow.decorators.post_load>` method.

.. code-block:: python

    # 1.0
    from marshmallow import Schema, fields, post_load


    class UserSchema(Schema):
        name = fields.Str()
        created_at = fields.DateTime()

        def make_object(self, data):
            return User(**data)


    # 2.0
    from marshmallow import Schema, fields, post_load


    class UserSchema(Schema):
        name = fields.Str()
        created_at = fields.DateTime()

        @post_load
        def make_user(self, data):
            return User(**data)

Error Format when ``many=True``
*******************************

When validating a collection (i.e. when calling ``load`` or ``dump`` with ``many=True``), the errors dictionary will be keyed on the indices of invalid items.

.. code-block:: python

    from marshmallow import Schema, fields


    class BandMemberSchema(Schema):
        name = fields.String(required=True)
        email = fields.Email()


    user_data = [
        {"email": "mick@stones.com", "name": "Mick"},
        {"email": "invalid", "name": "Invalid"},  # invalid email
        {"email": "keith@stones.com", "name": "Keith"},
        {"email": "charlie@stones.com"},  # missing "name"
    ]

    result = BandMemberSchema(many=True).load(user_data)

    # 1.0
    result.errors
    # {'email': ['"invalid" is not a valid email address.'],
    #  'name': ['Missing data for required field.']}

    # 2.0
    result.errors
    # {1: {'email': ['"invalid" is not a valid email address.']},
    #  3: {'name': ['Missing data for required field.']}}

You can still get the pre-2.0 behavior by setting ``index_errors = False`` in a ``Schema's`` *class Meta* options.

Use ``ValidationError`` instead of ``MarshallingError`` and ``UnmarshallingError``
**********************************************************************************

The :exc:`MarshallingError` and :exc:`UnmarshallingError` exceptions are deprecated in favor of a single :exc:`ValidationError <marshmallow.exceptions.ValidationError>`. Users who have written custom fields or are using ``strict`` mode will need to change their code accordingly.

Handle ``ValidationError`` in strict mode
-----------------------------------------

When using `strict` mode, you should handle `ValidationErrors` when calling `Schema.dump` and `Schema.load`.

.. code-block:: python

    from marshmallow import exceptions as exc

    schema = BandMemberSchema(strict=True)

    # 1.0
    try:
        schema.load({"email": "invalid-email"})
    except exc.UnmarshallingError as err:
        handle_error(err)

    # 2.0
    try:
        schema.load({"email": "invalid-email"})
    except exc.ValidationError as err:
        handle_error(err)


Accessing error messages in strict mode
***************************************

In 2.0, `strict` mode was improved so that you can access all error messages for a schema (rather than failing early) by accessing a `ValidationError's` ``messages`` attribute.

.. code-block:: python

    schema = BandMemberSchema(strict=True)

    try:
        result = schema.load({"email": "invalid"})
    except ValidationMessage as err:
        print(err.messages)
    # {
    #     'email': ['"invalid" is not a valid email address.'],
    #     'name': ['Missing data for required field.']
    # }


Custom Fields
*************

Two changes must be made to make your custom fields compatible with version 2.0.

- The `_deserialize <marshmallow.fields.Field._deserialize>` method of custom fields now receives ``attr`` (the key corresponding to the value to be deserialized) and the raw input ``data`` as arguments.
- Custom fields should raise :exc:`ValidationError <marshmallow.exceptions.ValidationError>` in their `_deserialize` and `_serialize` methods when a validation error occurs.

.. code-block:: python

    from marshmallow import fields, ValidationError
    from marshmallow.exceptions import UnmarshallingError

    # In 1.0, an UnmarshallingError was raised
    class PasswordField(fields.Field):
        def _deserialize(self, val):
            if not len(val) >= 6:
                raise UnmarshallingError("Password too short.")
            return val


    # In 2.0, _deserialize receives attr and data,
    # and a ValidationError is raised
    class PasswordField(fields.Field):
        def _deserialize(self, val, attr, data):
            if not len(val) >= 6:
                raise ValidationError("Password too short.")
            return val


To make a field compatible with both marshmallow 1.x and 2.x, you can pass `*args` and `**kwargs` to the signature.

.. code-block:: python

    class PasswordField(fields.Field):
        def _deserialize(self, val, *args, **kwargs):
            if not len(val) >= 6:
                raise ValidationError("Password too short.")
            return val

Custom Error Messages
*********************

Error messages can be customized at the `Field` class or instance level.


.. code-block:: python

    # 1.0
    field = fields.Number(error="You passed a bad number")

    # 2.0
    # Instance-level
    field = fields.Number(error_messages={"invalid": "You passed a bad number."})


    # Class-level
    class MyNumberField(fields.Number):
        default_error_messages = {"invalid": "You passed a bad number."}

Passing a string to ``required`` is deprecated.

.. code-block:: python

    # 1.0
    field = fields.Str(required="Missing required argument.")

    # 2.0
    field = fields.Str(error_messages={"required": "Missing required argument."})


Use ``OneOf`` instead of ``fields.Select``
******************************************

The `fields.Select` field is deprecated in favor of the newly-added `OneOf` validator.

.. code-block:: python

    from marshmallow import fields
    from marshmallow.validate import OneOf

    # 1.0
    fields.Select(["red", "blue"])

    # 2.0
    fields.Str(validate=OneOf(["red", "blue"]))

Accessing Context from Method fields
************************************

Use ``self.context`` to access a schema's context within a ``Method`` field.

.. code-block:: python

    class UserSchema(Schema):
        name = fields.String()
        likes_bikes = fields.Method("writes_about_bikes")

        def writes_about_bikes(self, user):
            return "bicycle" in self.context["blog"].title.lower()


Validation Error Messages
*************************

The default error messages for many fields and validators have been changed for better consistency.

.. code-block:: python

    from marshmallow import Schema, fields, validate


    class ValidatingSchema(Schema):
        foo = fields.Str()
        bar = fields.Bool()
        baz = fields.Int()
        qux = fields.Float()
        spam = fields.Decimal(2, 2)
        eggs = fields.DateTime()
        email = fields.Str(validate=validate.Email())
        homepage = fields.Str(validate=validate.URL())
        nums = fields.List(fields.Int())


    schema = ValidatingSchema()
    invalid_data = {
        "foo": 42,
        "bar": 24,
        "baz": "invalid-integer",
        "qux": "invalid-float",
        "spam": "invalid-decimal",
        "eggs": "invalid-datetime",
        "email": "invalid-email",
        "homepage": "invalid-url",
        "nums": "invalid-list",
    }
    errors = schema.validate(invalid_data)
    # {
    #     'foo': ['Not a valid string.'],
    #     'bar': ['Not a valid boolean.'],
    #     'baz': ['Not a valid integer.'],
    #     'qux': ['Not a valid number.'],
    #     'spam': ['Not a valid number.']
    #     'eggs': ['Not a valid datetime.'],
    #     'email': ['Not a valid email address.'],
    #     'homepage': ['Not a valid URL.'],
    #     'nums': ['Not a valid list.'],
    # }

More
****

For a full list of changes in 2.0, see the :doc:`Changelog <changelog>`.


Upgrading to 1.2
++++++++++++++++

Validators
**********

Validators were rewritten as class-based callables, making them easier to use when declaring fields.

.. code-block:: python

    from marshmallow import fields

    # 1.2
    from marshmallow.validate import Range

    age = fields.Int(validate=[Range(min=0, max=999)])

    # Pre-1.2
    from marshmallow.validate import ranging

    age = fields.Int(validate=[lambda val: ranging(val, min=0, max=999)])


The validator functions from 1.1 are deprecated and will be removed in 2.0.

Deserializing the Empty String
******************************


In version 1.2, deserialization of the empty string (``''``) with `DateTime`, `Date`, `Time`, or `TimeDelta` fields results in consistent error messages, regardless of whether or not `python-dateutil` is installed.

.. code-block:: python

    from marshmallow import fields

    fields.Date().deserialize("")
    # UnmarshallingError: Could not deserialize '' to a date object.


Decimal
*******

The `Decimal` field was added to support serialization/deserialization of `decimal.Decimal` numbers. You should use this field when dealing with numbers where precision is critical. The `Fixed`, `Price`, and `Arbitrary` fields are deprecated in favor the `Decimal` field.


Upgrading to 1.0
++++++++++++++++

Version 1.0 marks the first major release of marshmallow. Many big changes were made from the pre-1.0 releases in order to provide a cleaner API, support object deserialization, and improve field validation.

Perhaps the largest change is in how objects get serialized. Serialization occurs by invoking the :meth:`Schema.dump` method rather than passing the object to the constructor.  Because only configuration options (e.g. the ``many``, ``strict``, and ``only`` parameters) are passed to the constructor, you can more easily reuse serializer instances.  The :meth:`dump <Schema.dump>` method also forms a nice symmetry with the :meth:`Schema.load` method, which is used for deserialization.

.. code-block:: python

    from marshmallow import Schema, fields


    class UserSchema(Schema):
        email = fields.Email()
        name = fields.String()


    user = User(email="monty@python.org", name="Monty Python")

    # 1.0
    serializer = UserSchema()
    data, errors = serializer.dump(user)
    # OR
    result = serializer.dump(user)
    result.data  # => serialized result
    result.errors  # => errors

    # Pre-1.0
    serialized = UserSchema(user)
    data = serialized.data
    errors = serialized.errors

.. note::

    Some crucial parts of the pre-1.0 API have been retained to ease the transition. You can still pass an object to a `Schema` constructor and access the `Schema.data` and `Schema.errors` properties. The `is_valid` method, however, has been completely removed. It is recommended that you migrate to the new API to prevent future releases from breaking your code.

The Fields interface was also reworked in 1.0 to make it easier to define custom fields with their own serialization and deserialization behavior. Custom fields now implement :meth:`Field._serialize` and :meth:`Field._deserialize`.

.. code-block:: python

    from marshmallow import fields, MarshallingError


    class PasswordField(fields.Field):
        def _serialize(self, value, attr, obj):
            if not value or len(value) < 6:
                raise MarshallingError("Password must be greater than 6 characters.")
            return str(value).strip()

        # Similarly, you can override the _deserialize method

Another major change in 1.0 is that multiple validation errors can be stored for a single field. The ``errors`` dictionary returned by :meth:`Schema.dump` and :meth:`Schema.load` is a list of error messages keyed by field name.


.. code-block:: python

    from marshmallow import Schema, fields, ValidationError


    def must_have_number(val):
        if not any(ch.isdigit() for ch in val):
            raise ValidationError("Value must have an number.")


    def validate_length(val):
        if len(val) < 8:
            raise ValidationError("Value must have 8 or more characters.")


    class ValidatingSchema(Schema):
        password = fields.String(validate=[must_have_number, validate_length])


    result, errors = ValidatingSchema().load({"password": "secure"})
    print(errors)
    # {'password': ['Value must have an number.',
    #               'Value must have 8 or more characters.']}

Other notable changes:

- Serialized output is no longer an `OrderedDict` by default. You must explicitly set the `ordered` class Meta option to `True` .
- :class:`Serializer` has been renamed to :class:`Schema`, but you can still import `marshmallow.Serializer` (which is aliased to :class:`Schema`).
- ``datetime`` objects serialize to ISO8601-formatted strings by default (instead of RFC821 format).
- The ``fields.validated`` decorator was removed, as it is no longer necessary given the new Fields interface.
- `Schema.factory` class method was removed.

.. seealso::

    See the :doc:`Changelog <changelog>` for a  more complete listing of added features, bugfixes and breaking changes.