ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
7.01k stars 224 forks source link

[Feature] Discriminated unions (fast lookup) #1183

Open jakubwolny opened 1 year ago

jakubwolny commented 1 year ago

Motivation: I have a union of around 50 types, from which some are very complex. In this scenario input assertion takes significant time to be evaluated. As I understand superstruct union - it iterates over all union members until there's match. All of these types have a common field "type" which could be used for faster lookup.

Idea: When checking around I noticed "zod" has something exactly like this https://github.com/colinhacks/zod#discriminated-unions

Is this a feature you'd welcome in superstruct?

birtles commented 1 year ago

I made something up over in https://github.com/ianstormtaylor/superstruct/issues/897#issuecomment-906226796 for this but I've never got around to polishing it up to be merged