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

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 @@ -872,6 +872,46 @@ 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',
load
);

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',
load
);

expect(res).toEqual(['myvariable is required']);
});
});

describe('Integer', () => {
it('should return an error message for invalid integer', () => {
const res = validatePackagePolicyConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,20 @@ 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) => 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));
Expand Down
Loading