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.