PythiloLesson 20 of 20 · progress kept in this browser

Derive a schema from the validated model

A schema describes a data contract in a machine-readable form. Deriving it from the same model used for validation reduces the chance that documentation and actual input rules disagree. JSON Schema includes types, required fields and constraints; it is not itself a successful validation of any particular input.

Inspect the part of the schema your integration needs instead of comparing an entire generated document byte for byte. Titles and definition ordering are presentation details. Optional fields with defaults and required fields also need different treatment in forms and client generators.

from pydantic import BaseModel, Field
class Query(BaseModel):
    limit: int = Field(ge=1, le=50)
schema = Query.model_json_schema()
print(schema["required"])
print(schema["properties"]["limit"]["minimum"])
Your task

Implement query_schema() from a Query model: required term:str of length>=1, limit:int default10 in 1..100. Return its JSON Schema. Use model_json_schema instead of hand-writing a separate contract.

The first run loads pydantic into this browser.

All lessons