1

I am trying to create json schema based on pydantic class this class contains enums fields which is dynemic i recive the enums options from external api.

this what i did so far:

class Language(BaseModel):
    language_audio: Optional[EnumMeta] = None
    language_booklet: Optional[EnumMeta] = None
    language_live: Optional[EnumMeta] = None

    @root_validator(pre=True)
    @classmethod
    def check_only_one_language_selected(cls, values):
        if len(values) > 1:
            raise ValueError("Select only one language option")
        elif len(values) == 0:
            raise ValueError("Select language option")
        return values


class GetYourGuideCustomerInfo(BaseModel):
    """Class for customer info validation"""

    language: Optional[Language] = None
    hotel: Optional[str] = None
    supplier_requested_question: Optional[str] = None


def get_GetYourGuideCustomerInfo(lang_enum_dict: Dict[str, Any]) -> GetYourGuideCustomerInfo:
    return GetYourGuideCustomerInfo(language=Language(**lang_enum_dict))

the code where i generate the class:

schema = json.loads(get_GetYourGuideCustomerInfo(enum_dict).schema_json())

the error recived:

Value not declarable with JSON Schema, field: name='language_audio' type=Optional[EnumMeta] required=False default=None

thanx in advanced

Oz Cohen
  • 873
  • 1
  • 8
  • 11

1 Answers1

0

You can refer to this issue on Github: https://github.com/samuelcolvin/pydantic/issues/4079

Suggests controlling the schema directly like this:

    class MyClass: 
        x: int
        y: int
    
        @classmethod
        def __modify_schema__(cls, field_schema, field):
            field_schema['title'] = 'foobar'
Zaffer
  • 1,290
  • 13
  • 32