-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Initial Checks
- I have searched Google & GitHub for similar requests and couldn't find anything
- I have read and followed the docs and still think this feature is missing
Description
I searched but didn't find where/how this would be possible already, especially in a simple straightforward way.
Suppose I have a JSON string, then I can do MyModel.model_validate_json(json_str, strict=True) to validate the JSON using 'looser' JSON rules to get an instance of MyModel (as per here). How can I do exactly the same if I only have access to json.loads(json_str)?
Calling MyModel.model_validate() uses different (Python) validation rules, and MyModel.model_validate_json() only accepts the raw string. Converting the loaded JSON data back to string just to validate it again seems backwards. The feature request would be to have a simple top-level way of passing already-loaded JSON data (Python objects) through JSON-style validation rules.
Affected Components
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.model_dump()and.model_dump_json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
model_construct(), pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.