Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key mapping for dataclasses #29

Open
binaryDiv opened this issue Jan 5, 2022 · 1 comment
Open

Key mapping for dataclasses #29

binaryDiv opened this issue Jan 5, 2022 · 1 comment
Labels
new feature New feature that is not (only) a validator class

Comments

@binaryDiv
Copy link
Contributor

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

@binaryDiv binaryDiv added the enhancement Improvements to existing features or smaller new features label Jan 5, 2022
@binaryDiv binaryDiv added new feature New feature that is not (only) a validator class and removed enhancement Improvements to existing features or smaller new features labels Apr 6, 2022
@binaryDiv
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature that is not (only) a validator class
Projects
None yet
Development

No branches or pull requests

1 participant