attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Adding a `not_` or `not_in` validator?

Open nicktimko opened this issue 2 years ago • 1 comments

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.

nicktimko avatar May 04 '22 18:05 nicktimko

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 avatar May 25 '22 06:05 hynek

fixed by #1010

hynek avatar Sep 30 '22 13:09 hynek