diff --git a/frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx b/frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx index 4c68b138..bd9ac656 100644 --- a/frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx +++ b/frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx @@ -24,6 +24,7 @@ import type { PydanticFormsContextConfig, } from '@/types'; import { PydanticFormFieldFormat, PydanticFormFieldType } from '@/types'; +import { toOptionalObjectProperty } from '@/utils'; import { useRefParser } from './useRefParser'; @@ -109,7 +110,7 @@ const getPydanticFormField = ( required, validations, columns: 6, // TODO: Is this still relevant? https://github.com/workfloworchestrator/orchestrator-ui-library/issues/1891 - ...(addConstValue && { const: flatSchema.const }), + ...toOptionalObjectProperty({ const: flatSchema.const }, addConstValue), properties, ...fieldDetailProvider?.[propertyId], }; diff --git a/frontend/packages/pydantic-forms/src/utils.spec.ts b/frontend/packages/pydantic-forms/src/utils.spec.ts index 307c9404..b49f100a 100644 --- a/frontend/packages/pydantic-forms/src/utils.spec.ts +++ b/frontend/packages/pydantic-forms/src/utils.spec.ts @@ -8,6 +8,7 @@ import { getFormFieldValue, insertItemAtIndex, itemizeArrayItem, + toOptionalObjectProperty, } from './utils'; describe('insertItemAtIndex', () => { @@ -196,7 +197,7 @@ describe('getFormFieldValue', () => { }); }); -describe.only('getFormFieldIdWithPath', () => { +describe('getFormFieldIdWithPath', () => { it('returns fieldname when no path is supplied', () => { expect(getFormFieldIdWithPath('', 'name')).toBe('name'); }); @@ -385,3 +386,32 @@ describe('disableField', () => { expect(disabledField.attributes?.disabled).toBe(true); }); }); + +describe('toOptionalObjectProperty', () => { + const flatSchema = { const: 'CONST_VAL' }; + + function withSpread(addConstValue: boolean) { + return { + ...(addConstValue && { const: flatSchema.const }), + }; + } + + function withHelper(addConstValue: boolean) { + return { + ...toOptionalObjectProperty( + { const: flatSchema.const }, + addConstValue, + ), + }; + } + + it('adds const when addConstValue = true', () => { + expect(withSpread(true)).toEqual(withHelper(true)); + expect(withSpread(true)).toHaveProperty('const', 'CONST_VAL'); + }); + + it('omits const when addConstValue = false', () => { + expect(withSpread(false)).toEqual(withHelper(false)); + expect(withSpread(false)).not.toHaveProperty('const'); + }); +}); diff --git a/frontend/packages/pydantic-forms/src/utils.ts b/frontend/packages/pydantic-forms/src/utils.ts index 558bd14c..8c5030c6 100644 --- a/frontend/packages/pydantic-forms/src/utils.ts +++ b/frontend/packages/pydantic-forms/src/utils.ts @@ -142,3 +142,8 @@ export function getFormFieldIdWithPath( return ''; } + +export const toOptionalObjectProperty = ( + entries: T, + condition: boolean, +): T | object => (condition ? entries : {});