| 129 | |
| 130 | @attrs(repr=False, frozen=True, slots=True) |
| 131 | class _MatchesReValidator: |
| 132 | pattern = attrib() |
| 133 | match_func = attrib() |
| 134 | |
| 135 | def __call__(self, inst, attr, value): |
| 136 | """ |
| 137 | We use a callable class to be able to change the ``__repr__``. |
| 138 | """ |
| 139 | if not self.match_func(value): |
| 140 | msg = f"'{attr.name}' must match regex {self.pattern.pattern!r} ({value!r} doesn't)" |
| 141 | raise ValueError( |
| 142 | msg, |
| 143 | attr, |
| 144 | self.pattern, |
| 145 | value, |
| 146 | ) |
| 147 | |
| 148 | def __repr__(self): |
| 149 | return f"<matches_re validator for pattern {self.pattern!r}>" |
| 150 | |
| 151 | |
| 152 | def matches_re(regex, flags=0, func=None): |