import sys; print(sys.version): 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29)import pydantic; print(pydantic.VERSION): 0.18.1This behavior only when have custom fields validators
from pydantic import BaseModel
from typing import Optional
class TestCustomValidatorModelWithOptionalField(BaseModel):
field: Optional[int] = None
@validator('field')
def check_field(cls, v, *, values, config, field):
return v
Output:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/.../lib/python3.6/site-packages/pydantic/main.py", line 292, in schema
s = model_schema(cls, by_alias=by_alias)
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 193, in model_schema
model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 463, in model_process_schema
model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 484, in model_type_schema
f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 240, in field_schema
ref_prefix=ref_prefix,
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 442, in field_type_schema
ref_prefix=ref_prefix,
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 610, in field_singleton_schema
ref_prefix=ref_prefix,
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 534, in field_singleton_sub_fields_schema
ref_prefix=ref_prefix,
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 442, in field_type_schema
ref_prefix=ref_prefix,
File "/Users/.../lib/python3.6/site-packages/pydantic/schema.py", line 645, in field_singleton_schema
raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
ValueError: Value not declarable with JSON Schema, field: field_NoneType type=NoneType required
If remove @validator - all ok.
@tiangolo any chance you could look at this?
Yes, I can check it, but I'll take a bit (next week?) 'cause I'm on a business trip for a few days.
no problem.
Thank you! I will wait:)
Done, here's the PR #375 :slightly_smiling_face: