Skip to content

feat: Introduce Value Types for Context Fields #10026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

ivarconr
Copy link
Member

@ivarconr ivarconr commented May 23, 2025

(Warning: This entire PR was implemented with Copilot Agent with Gemini 2.5 Pro model.)

feat: Introduce Value Types for Context Fields

This PR introduces the ability to define a specific valueType for context fields in Unleash. This enhancement allows the UI to display only relevant operators for a selected context field, improving user experience and reducing potential configuration errors.

Problem

Previously, all operators were available for every context field, regardless of the field's intended data type. This could lead to confusion and the selection of incompatible operators (e.g., using a string operator on a date field).

Solution

We've added a new optional valueType property to context fields, which can be one of "String", "Number", "Semver", or "Date".

  • When a valueType is defined for a context field, the UI (specifically in constraint creation/editing) will now only show operators that are compatible with that type.
  • For backward compatibility, if valueType is not set (i.e., null or undefined), all operators will continue to be available for that context field.
  • The backend validates the valueType upon creation of new context fields.

Key Changes

1. Database

-   Added a new `value_type` column (TEXT, DEFAULT NULL) to the `context_fields` table via a database migration (`20250523100000-add-value-type-to-context-fields.js`).

2. Backend (src/lib)

-   Updated OpenAPI schemas (`create-context-field-schema.ts`, `context-field-schema.ts`) to include the new `valueType` field (string, nullable, enum: 'String', 'Number', 'Semver', 'Date').
-   Modified `IContextFieldDto` and `IContextField` in `context-field-store-type.ts` to include `value_type`.
-   Updated `context-field-store.ts` to handle the `value_type` column in database operations and map it to `valueType` in the DTO.
-   The `context.ts` controller now handles the mapping between the API's `valueType` and the service/DB's `value_type`, and includes validation for `valueType` during context field creation.

3. Frontend Core Logic (frontend/src)

-   Introduced `ContextFieldType` (enum-like object) and a helper function `getOperatorsForContextFieldType` in `constants/operators.ts` to define and retrieve type-specific operators.
-   Added `valueType` to the `IUnleashContextDefinition` interface in `interfaces/context.ts`.
-   Exported `getContextField` from `utils/operatorsForContext.ts` for easier access to context field definitions.

4. Frontend UI & State Management (frontend/src/component)

  • Context Field Forms:
    • useContextForm.ts: Added valueType to the form state, initialization logic, and included it in the submission payload.
    • ContextForm.tsx: Added a Select dropdown for choosing the valueType when creating or editing a context field.
    • CreateUnleashContext.tsx & EditContext.tsx: Updated to pass valueType and its setter to the ContextForm.
  • Operator Filtering in Constraints:
    • ConstraintOperatorSelect.tsx (in common/NewConstraintAccordion and feature/FeatureStrategy): Modified to accept a contextFieldType prop and filter the displayed operators using getOperatorsForContextFieldType.
    • ProjectActionsFilterItem.tsx: Fetches the context definition to determine and pass the contextFieldType to the operator select.
    • EditableConstraint.tsx:
      • Determines the contextFieldType of the selected context field.
      • Passes contextFieldType and allOperators to its ConstraintOperatorSelect component.
    • constraint-reducer.ts (in useEditableConstraint):
      • Updated the 'set context field' action payload to include valueType.
      • When a context field is changed, it now selects the first appropriate operator based on the new field's valueType.
      • Improved handling for the currentTime context field and transitions to/from date-type fields to ensure appropriate date operators are selected.
  • Context List Display:
    • ContextList.tsx: Updated to display the valueType of each context field in the list.

Commits

The changes were implemented across two commits:

  1. feat: Add value_type to context_fields table (Database migration)
  2. feat: Implement context field value types (Backend and frontend implementation)

This feature aims to make strategy constraint configuration more intuitive and robust.

Images of the new feature in action:

CRUD context fields with new valueType option:
image

Use with a feature flag:
image
image

Copy link

vercel bot commented May 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview May 26, 2025 1:54pm

Copy link
Contributor

github-actions bot commented May 23, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new valueType property to context fields to restrict operators based on the field’s type, improving UI behavior and reducing configuration errors. Key changes include:

  • Database migration and backend adjustments to support a new value_type column.
  • Frontend modifications adding a dropdown for selecting a context field’s valueType and updating operator filtering.
  • Updates to types, schemas, and reducer logic to map and propagate the new valueType.

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 2 comments.

File Description
src/lib/openapi/spec/context-field-schema.ts Added a new valueType property in the OpenAPI schema.
src/lib/features/context/context.ts Updated controller logic to map and validate valueType.
src/lib/features/context/context-field-store.ts and context-field-store-type.ts Added valueType support in DB row mapping and type definitions.
frontend/src/* Updated utilities, interfaces, and UI components (forms, constraint selectors, etc.) to include valueType and filter operators accordingly.

@coveralls
Copy link

coveralls commented May 23, 2025

Coverage Status

coverage: 91.239%. first build
when pulling 87d509c on feat/context-value-type
into e52fcd1 on main.

@@ -57,11 +57,20 @@ export const contextFieldSchema = {
},
legalValues: {
description:
'Allowed values for this context field schema. Can be used to narrow down accepted input',
'Allowed values for this context field. Can be used to extend the context field schema',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change seems unrelated.

Copy link
Contributor

@gastonfournier gastonfournier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that I notice is that it may inadvertedly break backward compatibility. I think with these agents it becomes more relevant to have better guardrails and tests.

Maybe if it does a lint:fix at the end would output PRs in better shape

Comment on lines +218 to +224
Do you want to define a set of legal values for this
context field? (optional)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these improvements are impressive, making the UX clearer and more usable. Perhaps some UI dimension has to be adapted, but I'm sure the agent made this decision.

Comment on lines +271 to +278
<Typography variant='body2' sx={{ mb: 1 }}>
Do you want this context field to be sticky? (optional)
</Typography>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, on the other hand, it got rid of the stickiness explanation and also modified the switch by removing the on/off label

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue why it changed this tbh. Seems like it was too eager. I will ask it to redo this.

@ivarconr ivarconr force-pushed the feat/context-value-type branch from 62c52d4 to 8ab0f30 Compare May 26, 2025 09:22
@ivarconr ivarconr force-pushed the feat/context-value-type branch from 8ab0f30 to b2757e9 Compare May 26, 2025 11:04
@chriswk chriswk moved this from New to Todo in Issues and PRs May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants