From 4df69518821486164b6e3a7b8b3d7bc780457de7 Mon Sep 17 00:00:00 2001 From: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:23:33 +0100 Subject: [PATCH] [Fleet] fix output host validator when host url is empty (#174086) ## Summary Closes https://github.com/elastic/kibana/issues/174076 Fix output host url validation when url is empty. To verify: - Go to Add output flyout - Click Add another URL - Delete the second URL box - Expect to see validation message `URL is required` image ### Checklist Delete any items that are not applicable to this PR. - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../output_form_validators.test.tsx | 11 +++++++ .../output_form_validators.tsx | 29 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.test.tsx index d5da6f553cf89..4352a3a6b8248 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.test.tsx @@ -83,6 +83,12 @@ describe('Output form validation', () => { expect(res).toEqual([{ message: 'URL is required' }]); }); + it('should not work with empty url', () => { + const res = validateESHosts(['']); + + expect(res).toEqual([{ index: 0, message: 'URL is required' }]); + }); + it('should work with valid url', () => { const res = validateESHosts(['https://test.fr:9200']); @@ -117,6 +123,11 @@ describe('Output form validation', () => { { index: 1, message: 'Duplicate URL' }, ]); }); + it('should return an error when invalid protocol', () => { + const res = validateESHosts(['ftp://test.fr']); + + expect(res).toEqual([{ index: 0, message: 'Invalid protocol' }]); + }); }); describe('validateLogstashHosts', () => { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.tsx index 26e63803df0e7..330d5c5d20122 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_validators.tsx @@ -88,14 +88,29 @@ export function validateKafkaHosts(value: string[]) { export function validateESHosts(value: string[]) { const res: Array<{ message: string; index?: number }> = []; const urlIndexes: { [key: string]: number[] } = {}; + const urlRequiredMessage = i18n.translate( + 'xpack.fleet.settings.outputForm.elasticUrlRequiredError', + { + defaultMessage: 'URL is required', + } + ); value.forEach((val, idx) => { try { if (!val) { - throw new Error('Host URL required'); - } - const urlParsed = new URL(val); - if (!['http:', 'https:'].includes(urlParsed.protocol)) { - throw new Error('Invalid protocol'); + res.push({ + message: urlRequiredMessage, + index: idx, + }); + } else { + const urlParsed = new URL(val); + if (!['http:', 'https:'].includes(urlParsed.protocol)) { + res.push({ + message: i18n.translate('xpack.fleet.settings.outputForm.invalidProtocolError', { + defaultMessage: 'Invalid protocol', + }), + index: idx, + }); + } } } catch (error) { res.push({ @@ -125,9 +140,7 @@ export function validateESHosts(value: string[]) { if (value.length === 0) { res.push({ - message: i18n.translate('xpack.fleet.settings.outputForm.elasticUrlRequiredError', { - defaultMessage: 'URL is required', - }), + message: urlRequiredMessage, }); }