From bf2626f1017dbdaa0987676fdc4506c9c80641e1 Mon Sep 17 00:00:00 2001 From: Mark Hopkin Date: Tue, 25 Jan 2022 13:49:01 +0000 Subject: [PATCH] [Fleet] Allow empty strings for required text fields in package policies (#123610) * Allow empty strings for required text fields in package policies * make empty yaml check more explicit --- .../services/validate_package_policy.test.ts | 53 ++++++++++++++++++- .../services/validate_package_policy.ts | 2 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts index 306d31879e0c6f..98056d6906c592 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.test.ts @@ -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: [ @@ -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)', () => { diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.ts index 8e2744d9703712..b6befdf8c790e9 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.ts @@ -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',