Package: marshmallow-recipe
marshmallow-recipeThe main goal of this opinionated library is to simplify migration from marshmallow2 to marshmallow3.
Also, it helps with:Stop writing marshmallow schemas completely: it generates them from dataclass.Using different naming cases(camel and capital camel cases are supported).Utilizing best practises on fields configuration.Supported types:str,int,float,bool,datetime.datetime,datetime.date,datetime.time,decimal.Decimal,uuid.UUIDOptional[T],T | NoneAnnotatedlist,dict(with typed keys and values),tuple(only when all elements of the same type),set,frozensetMapping(with typed keys and values),Set,SequenceExample:
class Annotated:
passimportdataclassesimportdatetimeimportdecimalimportmarshmallow_recipeasmrimportuuidfromtypingimportAnnotated@dataclasses.dataclass(frozen=True)classTransaction:id:uuid.UUIDcreated_at:datetime.datetimeprocessed_at:datetime.datetime|Noneamount:decimal.Decimal=dataclasses.field(metadata=mr.decimal_metadata(places=4))transaction_amount:Annotated[decimal.Decimal,mr.decimal_metadata(places=4)]transaction=Transaction(id=uuid.uuid4(),created_at=datetime.datetime.utcnow(),processed_at=None,amount=decimal.Decimal(42),transaction_amount=decimal.Decimal(42),)# dumps the transaction to a dictraw=mr.dump(transaction)# loads a transaction from the dictmr.load(Transaction,raw)# provides a generated marshmallow schema for dataclassmr.schema(Transaction)Update API example:importdecimalimportdataclassesimportmarshmallow_recipeasmr@dataclasses.dataclass(frozen=True)@mr.options(none_value_handling=mr.NoneValueHandling.INCLUDE)classCompanyUpdateData:name:str=mr.MISSINGannual_turnover:decimal.Decimal|None=mr.MISSINGcompany_update_data=CompanyUpdateData(name="updated name")dumped=mr.dump(company_update_data)assertdumped=={"name":"updated name"}# Note: no "annual_turnover" hereloaded=mr.load(CompanyUpdateData,{"name":"updated name"})assertloaded.name=="updated name"assertloaded.annual_turnoverismr.MISSINGloaded=mr.load(CompanyUpdateData,{"annual_turnover":None})assertloaded.nameismr.MISSINGassertloaded.annual_turnoverisNonev0.0.38(2024-01-05)Min support marshmallow is 2.20.5v0.0.38a2(2023-12-15)Almost stop using marshmallow defaults (except Nones)v0.0.38a1(2023-12-11)Use native isoformat/fromisoformatv0.0.37(2023-12-11)Cache get_pre_loads resultsv0.0.36(2023-12-08)Fix nullable with nested annotationv0.0.34(2023-12-07)Support datetime.time:#137v0.0.33(2023-09-30)Add validation field errors:#133,9f13f05v0.0.32(2023-09-28)Validate by regexp/Validate with a custom errorv0.0.31(2023-09-26)Allow to strip whitespaces on load/dump for str fieldsAllow to provide post_load delegate for str fieldsv0.0.30(2023-09-25)Improve validation support: expose ValidationError to raise it from validators, allow to pass a sequence of validatorsAdd meta shortcuts to metadata methodsv0.0.29(2023-09-23)Allow to use metadata as part of Annotated:#126and#127.v0.0.28(2023-09-22)Fix options decoratorv0.0.27(2023-09-22)Basic support of Annotated fieldsStop using typing_inspectv0.0.26(2023-09-18)Support python 3.12v0.0.25(2023-09-11)Support set, set[T], frozenset, frozenset[T], tuple, tuple[T, ...], collections.abc.Set[T], collections.abc.Sequence[T], collections.abc.Mapping[K, V]v0.0.24(2023-09-08)Setmodulefor auto-generated schemasCalling bake_schema should reuse already generated schemasNamingCase should not be propagated through serialisation hierarchyRemoval of DEFAULT_CASEv0.0.23(2023-09-05)Validate dumped data against its schema for marshmallow2v0.0.22(2023-06-27)v0.0.22a2(2023-06-27)Support typed dict keyv0.0.22a1(2023-06-24)Support typed dict valuev0.0.21(2023-06-24)v0.0.21a1(2023-06-08)Cache intermediate schema typesv0.0.20(2023-04-25)Allow to validate list itemv0.0.19(2023-04-24)Allow validations for nested/list/dict/enumv0.0.18(2023-01-05)Allow to specify default_factoryv0.0.17(2022-12-20)Non-optional fields with default value might not present in datav0.0.16(2022-12-02)Do not modify input data for marshmallow<3v0.0.15(2022-12-01)Metadata name should not use others field nameIgnore unknown fields for marshmallow2v0.0.14(2022-11-14)Do not crash if an unknown arg is passed to enum metav0.0.13(2022-10-14)Add pre_load hooksv0.0.12(2022-08-23)Add datetime_metadata to allow to specify a custom formatv0.0.11(2022-06-23)Add options, MISSING, none_value_handlingv0.0.10(2022-06-15)Fix input of load_manyv0.0.9(2022-04-26)Support Anyv0.0.8(2022-03-26)Allow default value for required fieldsv0.0.7(2022-03-20)Support validationv0.0.6(2022-03-20)Support enumAdd empty schemav0.0.5(2022-03-01)Validate before dump for marshmallow3Move many to schemaFix default naming caseFix unhashable type: 'set' errorv0.0.4(2022-02-21)Support decimal fieldSupport metadataSupport int fieldSupport float fieldSupport uuid fieldSupport list and dictSupport marshmallow3Support datetime and dateUnify datetime.tzinfo behaviourv0.0.3 (2022-02-14)Unify behaviour of nested_field in line with str_field/bool_fieldRearrange apiv0.0.2 (2022-02-13)Customize capital camel case and add camel casev0.0.1 (2022-02-13)A first version