Skip to content

Commit b49bf60

Browse files
committed
Merge branch 'fix/ui-dont-create-empty-string-config-fields' into 'develop'
fix: prevent creation of empty configuration fields in UI See merge request genaiic-reusable-assets/engagement-artifacts/genaiic-idp-accelerator!183
2 parents 4bf7f95 + 9ec3dfa commit b49bf60

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SPDX-License-Identifier: MIT-0
4646

4747
### Fixed
4848
- Defend against non-numeric confidence_threshold values in the configuration - avoid float conversion or numeric comparison exceptions in Assessement step
49+
- Prevent creation of empty configuration fields in UI
4950

5051
## [0.3.3]
5152

src/ui/src/components/configuration-layout/FormView.jsx

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,39 @@ const FormView = ({ schema, formValues, defaultConfig, isCustomized, onResetToDe
360360
return;
361361
}
362362

363-
// Create a new empty item
363+
// Create a new item with only required properties and meaningful defaults
364364
let newItem;
365365
if (property && property.items && property.items.type === 'object') {
366366
newItem = {};
367367
if (property.items.properties) {
368368
Object.entries(property.items.properties).forEach(([propKey, propSchema]) => {
369369
if (propKey === 'name') {
370+
// Always include the name
370371
newItem[propKey] = name.trim();
371-
} else if (propSchema.type === 'list' || propSchema.type === 'array') {
372-
newItem[propKey] = [];
373-
} else if (propSchema.type === 'object') {
374-
newItem[propKey] = {};
375-
} else {
376-
newItem[propKey] = '';
372+
} else if (propSchema.enum && propSchema.enum.length > 0) {
373+
// Include enum properties with their first option as default
374+
const [firstEnumValue] = propSchema.enum;
375+
newItem[propKey] = firstEnumValue;
376+
} else if (
377+
propSchema.default !== undefined &&
378+
propSchema.default !== '' &&
379+
propSchema.default !== null &&
380+
!(Array.isArray(propSchema.default) && propSchema.default.length === 0) &&
381+
!(
382+
typeof propSchema.default === 'object' &&
383+
propSchema.default !== null &&
384+
!Array.isArray(propSchema.default) &&
385+
Object.keys(propSchema.default).length === 0
386+
)
387+
) {
388+
// Only include properties with meaningful non-empty default values
389+
newItem[propKey] = propSchema.default;
377390
}
391+
// Skip ALL other properties including:
392+
// - Empty strings, arrays, objects
393+
// - Properties without defaults
394+
// - Properties with empty/null defaults
395+
// They will be added later when the user actually fills them in
378396
});
379397
}
380398
} else {
@@ -439,6 +457,36 @@ const FormView = ({ schema, formValues, defaultConfig, isCustomized, onResetToDe
439457
};
440458

441459
const updateValue = (path, value) => {
460+
// Don't create properties for empty/meaningless values
461+
if (
462+
value === '' ||
463+
value === null ||
464+
(Array.isArray(value) && value.length === 0) ||
465+
(typeof value === 'object' && value !== null && !Array.isArray(value) && Object.keys(value).length === 0)
466+
) {
467+
// Instead of setting empty values, check if we should remove the property entirely
468+
const newValues = { ...formValues };
469+
const segments = path.split(/[.[\]]+/).filter(Boolean);
470+
let current = newValues;
471+
472+
// Navigate to the parent of the property we want to delete
473+
for (let i = 0; i < segments.length - 1; i += 1) {
474+
if (!current[segments[i]]) {
475+
// Parent doesn't exist, so we can't delete anything
476+
return;
477+
}
478+
current = current[segments[i]];
479+
}
480+
481+
const [lastSegment] = segments.slice(-1);
482+
// Only delete the property if it exists
483+
if (current && typeof current === 'object' && lastSegment in current) {
484+
delete current[lastSegment];
485+
onChange(newValues);
486+
}
487+
return;
488+
}
489+
442490
const newValues = { ...formValues };
443491
const segments = path.split(/[.[\]]+/).filter(Boolean);
444492
let current = newValues;
@@ -456,7 +504,8 @@ const FormView = ({ schema, formValues, defaultConfig, isCustomized, onResetToDe
456504
current = current[segment];
457505
});
458506

459-
current[segments[segments.length - 1]] = value;
507+
const [lastSegment] = segments.slice(-1);
508+
current[lastSegment] = value;
460509
onChange(newValues);
461510
};
462511

0 commit comments

Comments
 (0)