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

Add strip_whitespce as an option to Meta #782

Open
bdoms opened this issue Dec 11, 2024 · 0 comments
Open

Add strip_whitespce as an option to Meta #782

bdoms opened this issue Dec 11, 2024 · 0 comments

Comments

@bdoms
Copy link

bdoms commented Dec 11, 2024

Description

Right now Meta supports min_length and max_length, but those are mostly meaningless without first stripping out whitespace.

Currently, we have to do variations on this all over the place:

class Foo(Struct):
        bar: Annotated[str, Meta(min_length=1, max_length=20)]

        def __post_init__(self):
            self.bar = self.bar.strip()
            if len(self.bar) < 1:
                raise ValueError('too short')
            if len(self.bar) > 20:
                raise ValueError('too long')

This is obviously bad because it repeats the length checks and adds a lot of boilerplate. Something like this would be much simpler:

class Foo(Struct):
        bar: Annotated[str, Meta(min_length=1, max_length=20, strip_whitespace=True)]

Even without the length checks, having a built in way to strip whitespace would be very useful and cut down on boilerplate. Other than things like passwords and tokens we need to strip whitespace on every single string field on its way in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant