enumer icon indicating copy to clipboard operation
enumer copied to clipboard

Add optional "Validate() error" method

Open mbyio opened this issue 2 years ago • 0 comments

Changes

  • Add a new validate flag
  • When the validate flag is true, generate a Validate() error method that verifies the enum is set to a proper value
  • Some small quality of life improvements to the golden test runner
  • Run gofmt on some of the files (this just happened automatically because of my editor)

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.

mbyio avatar Dec 02 '23 07:12 mbyio