Description
Add a way to define a key mapping in a validataclass, that is then used by the DataclassValidator to translate dictionary keys before validation. If a key in the input dictionary does not exist in the mapping, the key won't be modified and the field will be validated as usual.
This can be used, for example, to have a more consistent naming in dataclasses while the API looks different (e.g. if the API uses camelCase keys but you want to use snake_case keys in your dataclass), or to allow multiple keys for the same field.
Example how this could look like (the exact syntax and naming might change):
@validataclass
class MyDataclass:
__key_mapping__ = {
# This would map "somenumber", "someNumber", "SomeNumber" etc. to "some_number"
'somenumber': 'some_number',
# This would allow both "colour" and "color" as key for the field "color"
'colour': 'color',
}
name: str = StringValidator()
some_number: int = IntegerValidator()
color: str = StringValidator()
Optionally (or always?) a case-insensitive mapping should be possible. This could either be always the case when mapping keys, or be an additional option that can be set (e.g. by defining something like __lowercase_keys__ = True
, or by passing a parameter to the @validataclass
decorator).
(I think this should be independent from the user defined mapping, so that you can allow case-insensitive keys for a dataclass without defining a redundant {'foo': 'foo', 'bar': 'bar', ...}
mapping...)