Martynas Jurkus
Martynas Jurkus

Reputation: 9301

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.

Upvotes: 3

Views: 6520

Answers (2)

alex li
alex li

Reputation: 734

Foo = create_model("Foo", foo = (int, None))

Foo(foo=None)
# Foo(foo=None)

Foo()
# Foo(foo=None)

Foo(foo=3)
# Foo(foo=3)

Is this your desired result?

Upvotes: 0

Martynas Jurkus
Martynas Jurkus

Reputation: 9301

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()

Upvotes: 2

Related Questions