Skip to content

Commit e250e43

Browse files
authored
Merge pull request #171 from workfloworchestrator/2084-to-optional-object-property
2084 to optional object property
2 parents 9bb8bbd + 107e65a commit e250e43

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
PydanticFormsContextConfig,
2525
} from '@/types';
2626
import { PydanticFormFieldFormat, PydanticFormFieldType } from '@/types';
27+
import { toOptionalObjectProperty } from '@/utils';
2728

2829
import { useRefParser } from './useRefParser';
2930

@@ -109,7 +110,7 @@ const getPydanticFormField = (
109110
required,
110111
validations,
111112
columns: 6, // TODO: Is this still relevant? https://github.com/workfloworchestrator/orchestrator-ui-library/issues/1891
112-
...(addConstValue && { const: flatSchema.const }),
113+
...toOptionalObjectProperty({ const: flatSchema.const }, addConstValue),
113114
properties,
114115
...fieldDetailProvider?.[propertyId],
115116
};

frontend/packages/pydantic-forms/src/utils.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getFormFieldValue,
99
insertItemAtIndex,
1010
itemizeArrayItem,
11+
toOptionalObjectProperty,
1112
} from './utils';
1213

1314
describe('insertItemAtIndex', () => {
@@ -196,7 +197,7 @@ describe('getFormFieldValue', () => {
196197
});
197198
});
198199

199-
describe.only('getFormFieldIdWithPath', () => {
200+
describe('getFormFieldIdWithPath', () => {
200201
it('returns fieldname when no path is supplied', () => {
201202
expect(getFormFieldIdWithPath('', 'name')).toBe('name');
202203
});
@@ -385,3 +386,32 @@ describe('disableField', () => {
385386
expect(disabledField.attributes?.disabled).toBe(true);
386387
});
387388
});
389+
390+
describe('toOptionalObjectProperty', () => {
391+
const flatSchema = { const: 'CONST_VAL' };
392+
393+
function withSpread(addConstValue: boolean) {
394+
return {
395+
...(addConstValue && { const: flatSchema.const }),
396+
};
397+
}
398+
399+
function withHelper(addConstValue: boolean) {
400+
return {
401+
...toOptionalObjectProperty(
402+
{ const: flatSchema.const },
403+
addConstValue,
404+
),
405+
};
406+
}
407+
408+
it('adds const when addConstValue = true', () => {
409+
expect(withSpread(true)).toEqual(withHelper(true));
410+
expect(withSpread(true)).toHaveProperty('const', 'CONST_VAL');
411+
});
412+
413+
it('omits const when addConstValue = false', () => {
414+
expect(withSpread(false)).toEqual(withHelper(false));
415+
expect(withSpread(false)).not.toHaveProperty('const');
416+
});
417+
});

frontend/packages/pydantic-forms/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,8 @@ export function getFormFieldIdWithPath(
142142

143143
return '';
144144
}
145+
146+
export const toOptionalObjectProperty = <T extends object>(
147+
entries: T,
148+
condition: boolean,
149+
): T | object => (condition ? entries : {});

0 commit comments

Comments
 (0)