Export & lint

  1. enumx export
    1. Output paths
  2. enumx lint
    1. What counts as an enum
    2. Opting out

The enumx binary does three things: generate writes the Go enum, export writes the same enum for the rest of your stack, and lint checks that Go switch statements over it stay exhaustive.

go install github.com/bakhod1r/enumx/cmd/enumx@latest

enumx export

The enum your Go service validates is the same enum your frontend, your API schema, and your database column need. export generates them from the definition file you already have, so they cannot drift.

enumx export -lang=ts,zod,openapi,sql -out=gen enums.go
-lang Output
go the same file enumx generate writes
ts const object, union type, value list, is… guard, and a transition table when an FSM is declared
zod z.enum([...]) schema plus the inferred type
openapi a components/schemas fragment with type: string and enum:
sql PostgreSQL CREATE TYPE ... AS ENUM, plus a portable CHECK variant
mermaid stateDiagram-v2 of the declared transitions
json machine-readable description, for generating a language enumx does not target

For var Status = enumx.Define("pending", "in-progress", "done"):

// enums.enums.ts
export const StatusType = {
  Pending: "pending",
  InProgress: "in-progress",
  Done: "done",
} as const;

export type StatusType = (typeof StatusType)[keyof typeof StatusType];

export function isStatusType(v: unknown): v is StatusType { /* ... */ }
-- enums.enums.sql
CREATE TYPE status_type AS ENUM ('pending', 'in-progress', 'done');

Output paths

  • With a single -lang, -out names a file.
  • With several, -out names a directory, created if missing, and each target keeps its conventional extension (.enums.ts, .enums.sql, …).
  • Omit -out entirely and each target is written next to the definition file.

With no file argument the GOFILE environment variable is used, which is what //go:generate sets.

A batch is rendered in full: one failing target does not stop the ones after it, and every failure is reported together. mermaid on a file that declares no transitions is skipped with a note inside a batch, and is an error only when it is the sole target.

enumx lint

Adding a value to an enum is silent: every switch that forgot to handle it keeps compiling and starts falling through. lint finds them.

$ enumx lint ./...
order/handler.go:41:2: switch on OrderStatusType is not exhaustive: missing OrderStatusTypeCancelled
enumx lint: 1 non-exhaustive switch(es)

It exits non-zero on findings, so it belongs in CI next to go vet:

- run: go install github.com/bakhod1r/enumx/cmd/enumx@latest
- run: enumx lint ./...

What counts as an enum

Any named string type in the package under analysis with at least two declared constants — enumx-generated types and hand-written type X string enums alike. Both case styles are understood:

case OrderStatusTypeCancelled:  // the constant
case Order.Cancelled:           // the generated accessor

Types from other packages are left alone, so a switch that deliberately picks out two values of someone else’s enum stays quiet.

Opting out

  • A default clause excuses a switch. Pass -strict to report it anyway.
  • //enumx:ignore above a switch silences that one.

The checker is built on go/types from the standard library — enumx still has zero dependencies.