enum
A type that allows only a fixed list of named values, such as admin or user.
In more detail
Using an enum instead of free text stops typos and unexpected values. Validation libraries reject any value outside the list.
Examples
z.enum(["admin", "user"]) rejects any other role at run time.Java: enum Light { RED, GREEN }; each constant is a single object, so == comparison is safe.enum Light { RED, GREEN }Enums make events easy to count and compare, but they lose detail: "Failure" does not say whether the password was wrong or the account unknown.Related terms
- Zod — A TypeScript library for declaring validation schemas that check data at run time and also produce matching static types.
- validation schema — A description of what valid data looks like, checked by code while the program runs so bad data is rejected at the door.
- switch expression — A switch construct that produces a value, with one arm per case and no fall-through between them.
- exhaustive switch — A switch that the compiler has proven handles every possible value, so it needs no default branch.
- sealed type — A class or interface that lists exactly which types may extend or implement it, so the compiler knows the complete family.
- OCSF — Open Cybersecurity Schema Framework: a vendor-neutral schema that describes security events as numbered classes with typed attributes.
- schema — A formal description of the structure of data: which fields or columns exist, their types and which are required.
Taught in
- Structured Output — AI Engineering: LLM Apps, RAG & Agents
- Function Calling — AI Engineering: LLM Apps, RAG & Agents
- The Field The Registry Refused — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- API Contract Architecture: OpenAPI 3.1 & Schema Truth — Production Full-Stack Engineering
- Sealed Types And Exhaustiveness — Modern Java: Virtual Threads & High-Throughput Services
- Switch, Loops And Labels — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Instrumentation Sprint: Sixteen Calls On Code And Exporters — Prometheus Certified Associate (PCA) Exam Prep
- Log Parsing, Schema Normalization (OCSF/ECS) & Extraction — Security Operations & Threat Hunting (SOC)
References
- Zod: Enums
- TypeScript: Enums
- Java Tutorials: Enum Types
- dev.java: Enums
- Java SE 21 API: Enum
- OCSF schema browser