dmarkham / enumer

A Go tool to auto generate methods for your enums
Other
410 stars 66 forks source link

Add optional "Validate() error" method #82

Open mbyio opened 8 months ago

mbyio commented 8 months ago

Changes

Why

We currently have a IsA<Type>() bool method, but the name is different for every enum type. That basically makes it impossible to integrate it with Go interfaces or generics.

I return an error instead of a bool, because returning an error lets us provide more information on why the value is invalid. And again, it would also compose better with any generic validation code (you could, for example, recursively validate fields in a struct, and combine all the errors using errors.Join).

Note

I didn't see any more well known method signatures than Validate() error, but if there are any, we could switch and use that instead.