Skip to content

Commit

Permalink
[Fleet] Allow empty strings for required text fields in package polic…
Browse files Browse the repository at this point in the history
…ies (elastic#123610)

* Allow empty strings for required text fields in package policies

* make empty yaml check more explicit
  • Loading branch information
hop-dev authored Jan 25, 2022
1 parent 10c96e5 commit bf2626f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('Fleet - validatePackagePolicy()', () => {
enabled: true,
vars: {
'foo-input-var-name': { value: undefined, type: 'text' },
'foo-input2-var-name': { value: '', type: 'text' },
'foo-input2-var-name': { value: undefined, type: 'text' },
'foo-input3-var-name': { value: [], type: 'text' },
},
streams: [
Expand Down Expand Up @@ -555,6 +555,57 @@ describe('Fleet - validatePackagePolicy()', () => {
inputs: null,
});
});

it('returns no errors when required field is present but empty', () => {
expect(
validatePackagePolicy(
{
...validPackagePolicy,
inputs: [
{
type: 'foo',
policy_template: 'pkgPolicy1',
enabled: true,
vars: {
'foo-input-var-name': { value: '', type: 'text' },
'foo-input2-var-name': { value: '', type: 'text' },
'foo-input3-var-name': { value: ['test'], type: 'text' },
},
streams: [
{
data_stream: { dataset: 'foo', type: 'logs' },
enabled: true,
vars: { 'var-name': { value: 'test_yaml: value', type: 'yaml' } },
},
],
},
],
},
mockPackage,
safeLoad
)
).toEqual({
name: null,
description: null,
namespace: null,
inputs: {
foo: {
streams: {
foo: {
vars: {
'var-name': null,
},
},
},
vars: {
'foo-input-var-name': null,
'foo-input2-var-name': null,
'foo-input3-var-name': null,
},
},
},
});
});
});

describe('works for packages with multiple policy templates (aka integrations)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const validatePackagePolicyConfig = (
}

if (varDef.required) {
if (parsedValue === undefined || (typeof parsedValue === 'string' && !parsedValue)) {
if (parsedValue === undefined || (varDef.type === 'yaml' && parsedValue === '')) {
errors.push(
i18n.translate('xpack.fleet.packagePolicyValidation.requiredErrorMessage', {
defaultMessage: '{fieldName} is required',
Expand Down

0 comments on commit bf2626f

Please sign in to comment.