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
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):
@validataclassclassMyDataclass:
__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...)
The text was updated successfully, but these errors were encountered:
validataclass 0.10.0 introduces pre-validation hooks in dataclasses that can be used for something like this.
We might still implement a dedicated feature for mapping keys, but for now, the __pre_validate__ hook can be used for this purpose. (You could also write a mixin class that works like the code example above if you need this more often.)
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):
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...)The text was updated successfully, but these errors were encountered: