See _metadata_valid_helper in metadata_helpers.py in aind-metadata-viz
if origin_type is list:
item_type = expected_type.__args__[0]
return all([item_type(**item_json) for item_json in json])
elif origin_type is Optional:
# skip optional fields!
return True
elif origin_type is Union:
# Get all possible types in the Union
union_types = get_args(expected_type)
for union_type in union_types:
try:
return union_type(**json)
except ValidationError:
continue
else:
return False
else:
# validate as a pydantic model
return expected_type(**json) is not None
See _metadata_valid_helper in metadata_helpers.py in aind-metadata-viz