diff --git a/public/pages/CreateTrigger/containers/DefineBucketLevelTrigger/DefineBucketLevelTrigger.js b/public/pages/CreateTrigger/containers/DefineBucketLevelTrigger/DefineBucketLevelTrigger.js index 970680b20..52897644b 100644 --- a/public/pages/CreateTrigger/containers/DefineBucketLevelTrigger/DefineBucketLevelTrigger.js +++ b/public/pages/CreateTrigger/containers/DefineBucketLevelTrigger/DefineBucketLevelTrigger.js @@ -309,7 +309,14 @@ class DefineBucketLevelTrigger extends Component {
+ validateTriggerName( + triggerValues?.triggerDefinitions, + triggerIndex, + setFlyout != null + )(val), + }} formRow rowProps={defaultRowProps} inputProps={defaultInputProps} diff --git a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/DefineCompositeLevelTrigger.js b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/DefineCompositeLevelTrigger.js index 852f97a88..8ac117204 100644 --- a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/DefineCompositeLevelTrigger.js +++ b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/DefineCompositeLevelTrigger.js @@ -12,6 +12,7 @@ import { hasError, isInvalid } from '../../../../utils/validate'; import { DEFAULT_TRIGGER_NAME } from '../../utils/constants'; import CompositeTriggerCondition from '../../components/CompositeTriggerCondition/CompositeTriggerCondition'; import TriggerNotifications from './TriggerNotifications'; +import { validateTriggerName } from '../DefineTrigger/utils/validation'; import { FORMIK_COMPOSITE_INITIAL_TRIGGER_VALUES } from '../CreateTrigger/utils/constants'; import { titleTemplate } from '../../../../utils/helpers'; import { SEVERITY_OPTIONS } from '../../../../utils/constants'; @@ -101,7 +102,7 @@ class DefineCompositeLevelTrigger extends Component { validateTriggerName(values?.triggerDefinitions, triggerIndex)(val), }} formRow rowProps={{ diff --git a/public/pages/CreateTrigger/containers/DefineDocumentLevelTrigger/DefineDocumentLevelTrigger.js b/public/pages/CreateTrigger/containers/DefineDocumentLevelTrigger/DefineDocumentLevelTrigger.js index 759c760b7..25ca45af5 100644 --- a/public/pages/CreateTrigger/containers/DefineDocumentLevelTrigger/DefineDocumentLevelTrigger.js +++ b/public/pages/CreateTrigger/containers/DefineDocumentLevelTrigger/DefineDocumentLevelTrigger.js @@ -281,7 +281,14 @@ class DefineDocumentLevelTrigger extends Component {
+ validateTriggerName( + triggerValues?.triggerDefinitions, + triggerIndex, + setFlyout !== null + )(val), + }} formRow rowProps={defaultRowProps} inputProps={defaultInputProps} diff --git a/public/pages/CreateTrigger/containers/DefineTrigger/DefineTrigger.js b/public/pages/CreateTrigger/containers/DefineTrigger/DefineTrigger.js index 41c936892..20ff4f243 100644 --- a/public/pages/CreateTrigger/containers/DefineTrigger/DefineTrigger.js +++ b/public/pages/CreateTrigger/containers/DefineTrigger/DefineTrigger.js @@ -244,7 +244,8 @@ class DefineTrigger extends Component { + validateTriggerName(triggerValues?.triggerDefinitions, triggerIndex, flyoutMode)(val), }} formRow rowProps={{ ...defaultRowProps, ...(flyoutMode ? { style: {} } : {}) }} diff --git a/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.js b/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.js index 664a68a78..f4610eec4 100644 --- a/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.js +++ b/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.js @@ -2,29 +2,19 @@ * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ -import _ from 'lodash'; -import { FORMIK_INITIAL_TRIGGER_VALUES, TRIGGER_TYPE } from '../../CreateTrigger/utils/constants'; +export const validateTriggerName = (triggers = [], index, isFlyOut = false) => { + return (value) => { + const trimmedValue = value.trim(); + if (!trimmedValue) return isFlyOut ? 'Required.' : 'Trigger name is required.'; + const triggerNameExistWithIndex = triggers.some((trigger, i) => { + return i !== index && trimmedValue === trigger.name.trim(); + }); + if (triggerNameExistWithIndex) { + return 'Trigger name already used.'; + } -export const validateTriggerName = (triggers, triggerToEdit, fieldPath, isFullText) => (value) => { - if (!value) return isFullText ? 'Trigger name is required.' : 'Required.'; - const nameExists = triggers.filter((trigger) => { - const triggerId = _.get( - trigger, - `${TRIGGER_TYPE.BUCKET_LEVEL}.id`, - _.get(trigger, `${TRIGGER_TYPE.QUERY_LEVEL}.id`) - ); - const triggerName = _.get( - trigger, - `${TRIGGER_TYPE.BUCKET_LEVEL}.name`, - _.get(trigger, `${TRIGGER_TYPE.QUERY_LEVEL}.name`, FORMIK_INITIAL_TRIGGER_VALUES.name) - ); - const triggerToEditId = _.get(triggerToEdit, `${fieldPath}id`, triggerToEdit.id); - return triggerToEditId !== triggerId && triggerName.toLowerCase() === value.toLowerCase(); - }); - if (nameExists.length > 0) { - return 'Trigger name already used.'; - } - // TODO: character restrictions - // TODO: character limits + // TODO: character restrictions + // TODO: character limits + }; }; diff --git a/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.test.js b/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.test.js index 480606be4..475e04133 100644 --- a/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.test.js +++ b/public/pages/CreateTrigger/containers/DefineTrigger/utils/validation.test.js @@ -4,28 +4,24 @@ */ import { validateTriggerName } from './validation'; -import { TRIGGER_TYPE } from '../../CreateTrigger/utils/constants'; describe('validateTriggerName', () => { test('returns undefined if no error', () => { - expect(validateTriggerName([], {})('valid trigger name')).toBeUndefined(); + expect(validateTriggerName([], 0)('valid trigger name')).toBeUndefined(); }); test('returns Required string if falsy value', () => { - expect(validateTriggerName([], {})()).toBe('Required.'); - expect(validateTriggerName([], {})('')).toBe('Required.'); + expect(validateTriggerName([], 0)('')).toBe('Trigger name is required.'); }); - test('returns false if name already exists in monitor while creates new trigger', () => { - const triggers = [{ [TRIGGER_TYPE.QUERY_LEVEL]: { id: '123', name: 'Test' } }]; - expect(validateTriggerName(triggers, { [TRIGGER_TYPE.QUERY_LEVEL]: {} })('Test')).toBe( - 'Trigger name already used.' - ); + test('returns Required short version string if falsy value', () => { + expect(validateTriggerName([], 0, true)('')).toBe('Required.'); }); - test('returns undefined if editing trigger and name is the same', () => { - const triggers = [{ id: '123', name: 'Test' }]; - expect( - validateTriggerName(triggers, { [TRIGGER_TYPE.QUERY_LEVEL]: { id: '123' } })('Test') - ).toBeUndefined(); + const triggers = [{ name: 'Test' }]; + expect(validateTriggerName(triggers, 0)('Test')).toBeUndefined(); + }); + test('returns false if name already exists in monitor while creates new trigger', () => { + const triggers = [{ name: 'Test' }]; + expect(validateTriggerName(triggers, 1)('Test')).toBe('Trigger name already used.'); }); });