Enums and Query Filtering

Introduction

In the previous unit, you learned how to build complete features that require coordination across multiple files. You discovered how to plan features and coordinate changes across controllers, routes, services, and TypeScript types.

Now, you'll learn how to prompt Codex effectively for Express - specific features like enum types and query filtering. To prompt Codex well, you need to understand the Express and TypeScript concepts you're asking for. This lesson teaches you both the concepts and how to prompt Codex effectively for them.

Understanding TypeScript Enums for Predefined Options

TypeScript enums let you define a set of predefined options for a field. Instead of allowing any text, your application accepts only values from a fixed list. This ensures data consistency and type safety.

An enum has two parts:

  • Value (stored in the database): work, personal, shopping.
  • Label (displayed to users): Work, Personal, Shopping.

When prompting Codex, be specific about both the values and how they should be defined:

plaintext
In src/types/task.ts, create a TaskCategory enum with values: 
WORK, PERSONAL, SHOPPING, HEALTH, and OTHER. Use string enums so the values are 'work', 'personal', 'shopping', 'health', and 'other'.

Codex will generate the proper TypeScript structure. You can also ask Codex to explain:

plaintext
Explain TypeScript string enums and show me how to create a TaskCategory enum with values: work, personal, shopping, health, and other.

Codex will explain the structure, which helps you understand what you're asking for.

Prompting Codex for Enum Validation

After defining an enum type, you need to validate incoming data against those values. When prompting Codex, specify both the enum and the validation approach:

plaintext
In src/middleware/validators.ts, create a validateTaskCategory middleware that checks if the category field matches a valid TaskCategory enum value. 
Return a 400 error if the category is invalid.

Codex understands the TypeScript context and will create validation correctly. You can also ask for specific validation libraries like Zod:

plaintext
In src/middleware/validators.ts, use Zod to validate that the category field is one of the TaskCategory enum values. 
Create a taskSchema with category as an optional field that must be a valid enum value if provided.

Being specific about the validation approach helps Codex implement the right solution.

Prompting Codex for API Endpoints That Return Enum Choices

Your api endpoints should tell clients what enum values are available. When prompting Codex, specify the endpoint structure and response format:

plaintext
In src/routes/tasks.ts, add a GET /api/categories endpoint that returns all available TaskCategory values as an array of objects with 'value' and 'label' properties.

Codex will create the endpoint correctly. You can also ask for specific formatting:

plaintext
Make the categories endpoint return objects like { value: 'work', label: 'Work' }. 
The label should be the capitalized version of the value.

Specifying the response structure helps Codex match your API design.

Prompting Codex for Utility Functions That Map Enums to Display Labels

When working with enums, you often need utility functions to convert values to human-readable labels. When prompting Codex, specify the mapping logic:

plaintext
In src/utils/enumHelpers.ts, create a getCategoryLabel function that takes a TaskCategory enum value and returns its display label. 
For example, 'work' returns 'Work', 'personal' returns 'Personal'.

Codex will implement the mapping function. You can also ask for more complex transformations:

plaintext
In src/utils/enumHelpers.ts, create a formatCategoryForResponse function that takes a TaskCategory value 
and returns an object with both value and label properties. Use this when sending task data to clients.

Being specific about the function purpose and return type helps Codex implement exactly what you need.

Understanding Express Query Filtering

Express query filtering lets you retrieve data based on query parameters. You can filter by exact matches, partial matches, and more. When prompting Codex for filtering, specify the parameter source and filter logic:

plaintext
In src/controllers/taskController.ts, update the getTasks function to filter tasks by category when a category query parameter is provided. 
Use req.query.category to get the value and filter the results.

Codex will implement the filtering correctly. You can also ask for TypeScript type safety:

plaintext
In src/controllers/taskController.ts, add TypeScript types for the query parameters. 
Define an interface TaskQueryParams with optional category and priority fields. Use this to type req.query.

Being specific about types and behavior helps Codex implement type - safe filtering.

Prompting Codex for Filtering in Route Handlers

When asking Codex to add filtering to route handlers, be clear about:

  • Which query parameters to use.
  • What happens when no filter is provided.
  • How filters combine (if multiple).
  • TypeScript types for query parameters.
plaintext
In src/controllers/taskController.ts, update the getTasks function to filter by both category and priority. 
If req.query.category exists, filter by category. If req.query.priority exists, filter by priority. 
If both exist, filter by both. If neither exists, return all tasks.

Codex will implement the filtering logic correctly. You can also specify the database query approach using TypeORM:

plaintext
In src/services/taskService.ts, create a findTasksWithFilters function that takes category and priority as optional parameters. 
Use TypeORM's where clause to build the query dynamically based on which parameters are provided.

Specifying the service layer and ORM helps Codex understand the architecture.

Prompting Codex for Query Parameter Validation

When adding query - based filtering, you need to validate that parameters are correct. When prompting Codex, specify validation requirements:

plaintext
In src/middleware/validators.ts, create a validateTaskQuery middleware that checks if category and priority query parameters (when provided) are valid enum values. 
Use Zod to define a schema for req.query.

Codex will create the validation middleware. You can also ask for specific error handling:

plaintext
Make the validateTaskQuery middleware return a 400 error with a clear message 
listing the valid enum values when an invalid category or priority is provided.

Specifying error behavior helps Codex implement user - friendly validation.

Prompting Strategies for Express-Specific Features

  • Specify typescript types: When asking for enums, mention string enums and the specific values you want.
  • Use validation libraries: When asking for validation, specify Zod or Joi and the validation rules you need.
  • Specify request syntax: When asking for query parameters, use req.query.category to be clear about Express syntax.
  • Explain response structure: When asking for api endpoints, specify the exact JSON structure you want returned.
  • Be specific about database queries: When asking for filtering, specify the ORM you're using (TypeORM, Prisma) and the query approach.
  • Include typescript interfaces: When asking for typed parameters, specify interface names and property types.

Summary and What's Next

In this lesson, you learned how to prompt Codex effectively for Express - specific features. You discovered how to understand TypeScript enums for predefined options (values - labels), prompt Codex for enum definitions by specifying string enum values, prompt Codex for validation middleware using libraries like Zod, prompt Codex for api endpoints that return available enum choices, create utility functions that map enum values to display labels, understand Express query filtering with TypeScript types, prompt Codex for filtering in route handlers by specifying parameter sources and query logic, and prompt Codex for query parameter validation. You also learned effective prompting strategies: specifying TypeScript types, using validation libraries like Joi, using correct Express request syntax such as req.query, explaining JSON response structures, being specific about database queries with TypeORM or Prisma, and including TypeScript interfaces.

Next, you'll practice adding these features to your task tracker: categories for organization, priorities for focus, and filtering to help users find tasks through api endpoints.

Move on to the practice exercises to start adding enum types and filtering!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal