bloomberg / attrs-strict

Provides runtime validation of attributes specified in Python 'attr'-based data classes.
Apache License 2.0
52 stars 19 forks source link

Assign validators for a whole class #83

Closed max-arnold closed 2 years ago

max-arnold commented 2 years ago

Assigning validators to each field is too verbose and repetitive attrs.field(validator=type_validator()).

Is there a way to validate a whole class?

@attrs.define
class MyClass:
    id: str
    attr1: int
    attr2: str
    fields: Dict[str, Any] = attrs.field(default={})

myc = MyClass('123', 1, '345')
attrs_strict.validate(myc)

Or maybe through a class decorator?

gaborbernat commented 2 years ago

There isn't, and we don't plan to provide within this library itself for now.

erikseulean commented 2 years ago

As a class decorator, I don't think it's doable, unless it's being done in attrs directly.

Second is doable but arguably not very elegant.