pydantic
pydantic copied to clipboard
How about adding model tests for all of the json schema tests?
Initial Checks
- [X] I have searched Google & GitHub for similar requests and couldn't find anything
- [X] I have read and followed the docs and still think this feature is missing
Description
How about adding model tests for all of the json schema tests?
They are at https://github.com/json-schema-org/JSON-Schema-Test-Suite/tree/main/tests
This will demonstrate the supported feature set
Affected Components
- [ ] Compatibility between releases
- [X] Data validation/parsing
- [ ] Data serialization -
.dict()
and.json()
- [X] JSON Schema
- [ ] Dataclasses
- [ ] Model Config
- [ ] Field Types - adding or changing a particular data type
- [ ] Function validation decorator
- [ ] Generic Models
- [ ] Other Model behaviour -
construct()
, pickling, private attributes, ORM mode - [ ] Settings Management
- [ ] Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.
Sounds for V2.
What do you think @dmontagu ?
If I'm understanding correctly, these tests seem to be checking whether instances match a specific JSON schema.
However, pydantic doesn't actually do any JSON schema validation, and (purposely) does not provide guarantees that its validation perfectly matches JSON schema, considering JSON schema is much less expressive. That's not a judgement against JSON schema — it's just a reflection of the fact that pydantic was designed to be more powerful — for example, being able to coerce types, transform inputs, apply general purpose logic during validation, etc. The JSON schema we generate is intended to be useful for things like generating API clients, etc., but is not tied to any aspect of runtime data validation in pydantic.
Because of this, I'm not sure this request makes sense — how would we make use of these tests in pydantic? That's not clear to me.
That said, I would like to add an array of tests that:
- the JSON schemas we produce are valid according to JSON schema
- data that validates into a BaseModel subclass passes validation against the JSON schema and vice versa
- instances of a BaseModel subclass serialize into data that passes validation against the JSON schema
However, I've already created a separate issue for this (don't remember the exact title, but it's along the lines of "use jsonschema for testing instances against their model schemas").
Do you mean this issue? https://github.com/pydantic/pydantic/issues/5164