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

Adding a not_ or not_in validator? #956

Closed
nicktimko opened this issue May 4, 2022 · 2 comments
Closed

Adding a not_ or not_in validator? #956

nicktimko opened this issue May 4, 2022 · 2 comments
Labels

Comments

@nicktimko
Copy link
Contributor

I've had cases where it's nice to be able to compose the standard validators to do what I need without needing to write a custom function. It generally ends up looking more declarative and less imperative, so easy to read later. Often, however, there are cases where I'd like to be able to invert a standard operator, for instance, if I want a field to match a regex, but also not be a reserved value, I'd like to be do something like:

reserved_values = {"id", "timestamp"}
attr.validators.and_(
    attr.validators.matches_re(r"^[\w\.\-]+$"), 
    not_(attr.validators.in_(reserved_values)),  # not_ TBD.
)

It could maybe be added as just a specific variant of in_, i.e. not_in without much trouble, but I don't know if something like as generic as not_ would be a good idea. It could then maybe be used when composed with other validators. I'd be willing to give not_in a stab, but if it should be generalized to not_, /shrug.

@hynek
Copy link
Member

hynek commented May 25, 2022

I suppose if we go the negative road, a generic not_ makes more sense than starting to add a bunch of validators with a not_ prefix.

@hynek hynek added the Feature label Aug 11, 2022
@hynek
Copy link
Member

hynek commented Sep 30, 2022

fixed by #1010

@hynek hynek closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants