Closed cdeil closed 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.
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.
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.
I'd like this to raise an error: