attrs
attrs copied to clipboard
Adding a `not_` or `not_in` validator?
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.
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.
fixed by #1010