Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
PydanticFormsContextConfig,
} from '@/types';
import { PydanticFormFieldFormat, PydanticFormFieldType } from '@/types';
import { toOptionalObjectProperty } from '@/utils';

import { useRefParser } from './useRefParser';

Expand Down Expand Up @@ -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],
};
Expand Down
32 changes: 31 additions & 1 deletion frontend/packages/pydantic-forms/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getFormFieldValue,
insertItemAtIndex,
itemizeArrayItem,
toOptionalObjectProperty,
} from './utils';

describe('insertItemAtIndex', () => {
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('getFormFieldValue', () => {
});
});

describe.only('getFormFieldIdWithPath', () => {
describe('getFormFieldIdWithPath', () => {
it('returns fieldname when no path is supplied', () => {
expect(getFormFieldIdWithPath('', 'name')).toBe('name');
});
Expand Down Expand Up @@ -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');
});
});
5 changes: 5 additions & 0 deletions frontend/packages/pydantic-forms/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ export function getFormFieldIdWithPath(

return '';
}

export const toOptionalObjectProperty = <T extends object>(
entries: T,
condition: boolean,
): T | object => (condition ? entries : {});