| 537 | |
| 538 | @attrs(repr=False, frozen=True, slots=True) |
| 539 | class _MaxLengthValidator: |
| 540 | max_length = attrib() |
| 541 | |
| 542 | def __call__(self, inst, attr, value): |
| 543 | """ |
| 544 | We use a callable class to be able to change the ``__repr__``. |
| 545 | """ |
| 546 | if len(value) > self.max_length: |
| 547 | msg = f"Length of '{attr.name}' must be <= {self.max_length}: {len(value)}" |
| 548 | raise ValueError(msg) |
| 549 | |
| 550 | def __repr__(self): |
| 551 | return f"<max_len validator for {self.max_length}>" |
| 552 | |
| 553 | |
| 554 | def max_len(length): |