Skip to main content
Enum is an enumeration type that can only select from a predefined list of values. It provides type safety and makes it easy to configure selection options in user interfaces.

What is an Enum?

Enum defines a limited set of values to provide:

Type Safety

Prevents undefined value inputAutomatically converts to Union type in TypeScript

Auto UI Generation

Automatic selection list configurationImmediately usable in Select, ButtonSet components

Label Management

Separation of keys and display namesEasy multi-language support

Maintainability

Centralized managementOnly one place to modify when values change

Defining Enums

Defining in entity.json

Define as key-label pairs in the Entity’s enums section.
user.entity.json
Structure:
  • Enum ID: PascalCase (e.g., UserRole, PostStatus)
  • Key: lowercase, numbers, underscores (e.g., admin, draft_saved)
  • Label: Display text (e.g., β€œAdministrator”, β€œDraft Saved”)

Defining in Sonamu UI

1

Navigate to Entity edit page

Select an Entity in Sonamu UI.
2

Scroll to Enums section

The Enums section is below Props, Indexes, Subsets.
3

Click Add Enum

Add a new Enum.Enter Enum ID:
  • Enter in PascalCase
  • Use {Entity} pattern to associate with Entity
    • Example: $ModelRole β†’ UserRole (auto-converted)
4

Add Enum values

Each Enum is displayed as a separate tab, add key-label pairs with the β€œAdd Row” button.
  • Key: lowercase letters, numbers, underscores only
  • Label: Display name
5

Save

When you save the Entity, the Enum is saved together.

πŸ“Έ Needed: Sonamu UI Enums section - multiple Enums displayed as tabs

Auto-Generated Code

When you define an Enum, Sonamu automatically generates the following:

1. Zod Schema

sonamu.generated.ts

2. TypeScript Type

user.types.ts

3. Label Helper Function

sonamu.generated.ts

Using Enums

Using in Props

Database:
  • Column type: text
  • Stored value: Key value (e.g., "admin")
TypeScript:

Using in API

Displaying Labels

Common Enum Patterns

1. OrderBy Enum

Define sorting options.
Usage:

2. SearchField Enum

Define searchable fields.
Usage:

3. Status Enum

Define statuses.
Usage:

4. Type/Category Enum

Define classifications.

Enum Array Type

You can store multiple Enum values as an array.
Database:
  • Column type: text[]
  • Stored value: ["read", "write"]
TypeScript:
Usage:

Using Enums in Frontend

Select Component

ButtonSet Component

Enum Validation

Zod automatically validates Enum values.

Enum Naming Conventions

$Model Pattern

Use $Model to dynamically include the Entity name:
Conversion result:
  • User Entity β†’ UserRole, UserStatus
  • Post Entity β†’ PostRole, PostStatus

Cautions

Caution When Changing Enum ValuesChanging an Enum key that’s already stored in the database will cause mismatch with existing data.How to change:
  1. Add new key
  2. Data migration (old key β†’ new key)
  3. Remove old key
Key Naming RestrictionsEnum Keys can only use:
  • Lowercase letters (a-z)
  • Numbers (0-9)
  • Underscores (_)
Hyphens (-) cannot be used.
Labels Can Be Changed FreelyLabels are for display purposes, so they can be changed anytime. Only Keys are stored in the database.

Enum vs Separate Table

Enum use examples:
  • User roles (admin, user, guest)
  • Post status (draft, published, deleted)
  • Notification types (email, sms, push)
Separate table use examples:
  • Region list (Seoul, Busan, …)
  • Product categories (hierarchical structure)
  • Country codes (many additional info)

Next Steps

Field Types

Learn about other field types

Validation

Validate data with Zod

Frontend Components

Use generated components

API Development

API development using Enums