Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dynamic Pydantic model with typed optional values

I want to create a dynamic model from my data, create JSON from it, and load it in another location.

I can't find a way to define a field in a way, that should it is optional, but when data exists for that field - it would be validated.

This is for required field: fields[col_name] = (data_type, None)

# resolve types for data
data_type = resolve_type(data)
required = is_required(data)
if required:
  fields[col_name] = (data_type, ...)
else:
  fields[col_name] = (data_type, None) <--- unclear

...

pydantic.create_model(name, **fields)

The above configuration generates JSON model that makes fields optional and typed, but then I validate by using the input data I can't pass None values - '$.inputs.0.Field', 'message': "None is not of type 'string'"

So my question - how to declare a field that would validate input, but only when it's not None. And in such a manner, that I could create JSON schema and load it in another location.

like image 300
Martynas Jurkus Avatar asked Mar 10 '26 01:03

Martynas Jurkus


1 Answers

Implemented workaround based on suggestions in https://github.com/samuelcolvin/pydantic/issues/990

optional_fields = list()
if required:
  fields[col_name] = (data_type, ...)
else:
  fields[col_name] = (data_type, None)
  optional_fields.append(col_name)

model = pydantic.create_model(name, **fields)

def schema_extra(schema, model):
    for column in optional_fields:
        original_type = schema["properties"][column]["type"]
        schema["properties"][column].update({"type": ["null", original_type]})

model.__config__.schema_extra = schema_extra

schema_json = model.schema_json()
like image 173
Martynas Jurkus Avatar answered Mar 12 '26 15:03

Martynas Jurkus