Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use discriminated unions to improve validation errors #244

Closed
mvandenburgh opened this issue Jun 21, 2024 · 0 comments · Fixed by #245
Closed

Use discriminated unions to improve validation errors #244

mvandenburgh opened this issue Jun 21, 2024 · 0 comments · Fixed by #245

Comments

@mvandenburgh
Copy link
Member

We recently saw these metadata validation errors on a dandiset in production dandi/dandi-archive#1958. These are the errors that were reported:

contributor: String should match pattern '^([\w\s\-\.']+),\s+([\w\s\-\.']+)$'
contributor: Input should be 'Organization'
contributor: String should match pattern 'https://ror.org/[a-z0-9]+$'

The invalid contributor in question turned out to be a Person with an invalid name field; in other words, the first validation error was the actual issue, while the other two were not relevant and somewhat misleading. What's happening here is pydantic has no idea from a validation perspective whether the object is intended to be a Person or an Organization , as contributor is of type List[Union[Person, Organization]], so it's checking both cases (i.e., first it validates the object as if it were a Person and gets the first error, then it validates it as a Organization and gets the other two errors).

I propose that we use discriminated unions on the schemaKey field of each pydantic model so we can avoid this in the future. This would allow pydantic to scope down the validation to the specific type of the object based on its schemaKey. If we had this in the above mentioned scenario, pydantic would have recognized that the invalid contributor is supposed to be a Person and would not have reported the additional misleading validation errors that assume it's an Organization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant