s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
166 stars 38 forks source link

Data validation on attribute setting? #117

Closed cdeil closed 4 years ago

cdeil commented 4 years ago

Is there a way to get data validation on attribute setting?

Can it be achieved with your package e.g. by subclassing and overwriting __setattribute__?

Or if this not in scope, is there some other package you can recommend?

@bultako and I are looking for a solution to have hierarchical config objects that offer dot access and do type validation (or more, a la JSON schema) as users interact with the object. Basically the structure of the config should be frozen, types should be fixed, only values should be allowed to change.

from dataclasses import dataclass
from dataclasses_jsonschema import JsonSchemaMixin

@dataclass
class Point(JsonSchemaMixin):
    "A 2D point"
    x: float
    y: float

p  = Point(3, 4)

I'd like this to raise an error:

p.x = "not a number, please no"
s-knibbs commented 4 years ago

This wouldn't be easy to implement because all validation is handled by the jsonschema library which only validates the entire object.

You can use mypy to catch any type mismatches at compile time though. Mypy will generate an error for the example above.

cdeil commented 4 years ago

Our use case is for configuration, where users sometimes write it in YAML, and sometimes they then interactively change config items at runtime, e.g.:

config.analysis.debug = True

and we want this to fail immediately

config.analysis.debug = "yes, please"

For our use case performance isn't a concern at all, always re-validating the whole object would be fine. But I guess it's not possible or easy if the schema is for config and then it should run when config.analysis.debug.__setattr__ runs.

Thanks for the quick reply!

I found this just now : https://github.com/samuelcolvin/pydantic/pull/94 will keep looking for options.