mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
7a06736ead
* Add additional parameters to Schema for validation and annotation (#311) * Add tests for validation declared via Schema class in defaults * Add validations to field from declarations in Schema * Add annotations in generated JSON Schema from validations via Schema * Augment tests for schema generation * Simplify validations from Schema in fields, from coverage hints * Update schema test to use the spec plural "examples" * Add docs and simple example of the additional parameters for Schema * Update history * Fix number of PR in HISTORY * Refactor check for numeric types, remove break to make coverage happy * Fix typo in docs, I confused gt with maximum * Finish docstring for Schema (I had forgotten about it) * Implement code review requests and lenient_issubclass with tests * Move Schema to its now file to extract from fields.py but avoid circular imports * Control coverage * Schema fixes (#318) * rearrange code * cleanup get_annotation_from_schema * fix typo * rename _schema to schema
42 lines
814 B
Python
42 lines
814 B
Python
from enum import Enum
|
|
from pydantic import BaseModel, Schema
|
|
|
|
class FooBar(BaseModel):
|
|
count: int
|
|
size: float = None
|
|
|
|
class Gender(str, Enum):
|
|
male = 'male'
|
|
female = 'female'
|
|
other = 'other'
|
|
not_given = 'not_given'
|
|
|
|
class MainModel(BaseModel):
|
|
"""
|
|
This is the description of the main model
|
|
"""
|
|
foo_bar: FooBar = Schema(...)
|
|
gender: Gender = Schema(
|
|
None,
|
|
alias='Gender',
|
|
)
|
|
snap: int = Schema(
|
|
42,
|
|
title='The Snap',
|
|
description='this is the value of snap',
|
|
gt=30,
|
|
lt=50,
|
|
)
|
|
|
|
class Config:
|
|
title = 'Main'
|
|
|
|
print(MainModel.schema())
|
|
# > {
|
|
# 'type': 'object',
|
|
# 'title': 'Main',
|
|
# 'properties': {
|
|
# 'foo_bar': {
|
|
# ...
|
|
print(MainModel.schema_json(indent=2))
|