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

1
  • How are you creating the enum? Using something like this? Commented Jun 17, 2022 at 3:10

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.