Pydantic: Error generate schema for model with custom validators.

Created on 20 Jan 2019  路  5Comments  路  Source: samuelcolvin/pydantic

Bug

  • OS: macOS mojave 10.14 (18A391)
  • Python version import sys; print(sys.version): 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29)
    [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.18.1

This 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.

bug

All 5 comments

@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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krzysieqq picture krzysieqq  路  3Comments

engstrom picture engstrom  路  3Comments

ashpreetbedi picture ashpreetbedi  路  3Comments

sbv-trueenergy picture sbv-trueenergy  路  3Comments

vvoody picture vvoody  路  3Comments