[8.x] Fix required validation for multi text input field (#208699)

Backport https://github.com/elastic/kibana/pull/205768 to 8.x

Combining with the fix https://github.com/elastic/kibana/pull/208528

---------

Co-authored-by: Jusheng Huang <117657272+viajes7@users.noreply.github.com>
This commit is contained in:
Julia Bardi 2025-01-29 14:28:26 +01:00 committed by GitHub
parent ee02936fe3
commit c2647b51d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 9 deletions

View file

@ -1209,6 +1209,65 @@ describe('Fleet - validationHasErrors()', () => {
});
describe('Fleet - validatePackagePolicyConfig', () => {
describe('Multi Text', () => {
it('should return required error message for empty string', () => {
const res = validatePackagePolicyConfig(
{
type: 'text',
value: [''],
},
{
name: 'myvariable',
type: 'text',
multi: true,
required: true,
},
'myvariable',
safeLoad
);
expect(res).toEqual(['myvariable is required']);
});
it('should return required error message for blank spaces', () => {
const res = validatePackagePolicyConfig(
{
type: 'text',
value: ['value1', ' '],
},
{
name: 'myvariable',
type: 'text',
multi: true,
required: true,
},
'myvariable',
safeLoad
);
expect(res).toEqual(['myvariable is required']);
});
it('should accept integer', () => {
const res = validatePackagePolicyConfig(
{
type: 'text',
value: [1],
},
{
name: 'myvariable',
type: 'text',
multi: true,
required: true,
},
'myvariable',
safeLoad
);
expect(res).toBeNull();
});
});
describe('Integer', () => {
it('should return an error message for invalid integer', () => {
const res = validatePackagePolicyConfig(

View file

@ -368,15 +368,21 @@ export const validatePackagePolicyConfig = (
);
return errors;
}
if (varDef.required && Array.isArray(parsedValue) && parsedValue.length === 0) {
errors.push(
i18n.translate('xpack.fleet.packagePolicyValidation.requiredErrorMessage', {
defaultMessage: '{fieldName} is required',
values: {
fieldName: varDef.title || varDef.name,
},
})
);
if (varDef.required && Array.isArray(parsedValue)) {
const hasEmptyString =
varDef.type === 'text' &&
parsedValue.some((item) => typeof item === 'string' && item.trim() === '');
if (hasEmptyString || parsedValue.length === 0) {
errors.push(
i18n.translate('xpack.fleet.packagePolicyValidation.requiredErrorMessage', {
defaultMessage: '{fieldName} is required',
values: {
fieldName: varDef.title || varDef.name,
},
})
);
}
}
if (varDef.type === 'text' && parsedValue) {
const invalidStrings = parsedValue.filter((cand: any) => /^[*&]/.test(cand));