Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix validation for certain cases #771

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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 @@ -307,22 +307,25 @@ export default class CreateMonitor extends Component {
)}

<FieldArray name={'triggerDefinitions'} validateOnChange={true}>
{(triggerArrayHelpers) => (
<ConfigureTriggers
edit={edit}
triggerArrayHelpers={triggerArrayHelpers}
monitor={formikToMonitor(values)}
monitorValues={values}
setFlyout={this.props.setFlyout}
triggers={_.get(formikToMonitor(values), 'triggers', [])}
triggerValues={values}
isDarkMode={this.props.isDarkMode}
httpClient={httpClient}
notifications={notifications}
notificationService={notificationService}
plugins={plugins}
/>
)}
{(triggerArrayHelpers) => {
return (
<ConfigureTriggers
edit={edit}
triggerArrayHelpers={triggerArrayHelpers}
monitor={formikToMonitor(values)}
monitorValues={values}
setFlyout={this.props.setFlyout}
triggers={_.get(formikToMonitor(values), 'triggers', [])}
triggerValues={values}
isDarkMode={this.props.isDarkMode}
httpClient={httpClient}
notifications={notifications}
notificationService={notificationService}
plugins={plugins}
isNewMonitor={true}
/>
);
}}
</FieldArray>

<EuiSpacer />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function formikToMonitor(values) {
enabled: !values.disabled,
schedule,
inputs: [formikToInputs(values)],
triggers: [],
triggers: values.triggerDefinitions || [],
ui_metadata: {
schedule: uiSchedule,
monitor_type: values.monitor_type,
Expand Down Expand Up @@ -147,15 +147,8 @@ export function formikToAd(values) {
}

export function formikToUiSearch(values) {
const {
searchType,
timeField,
aggregations,
groupBy,
bucketValue,
bucketUnitOfTime,
filters,
} = values;
const { searchType, timeField, aggregations, groupBy, bucketValue, bucketUnitOfTime, filters } =
values;
return {
searchType,
timeField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class ConfigureTriggers extends React.Component {
triggers,
triggerValues,
isDarkMode,
isNewMonitor,
httpClient,
notificationService,
plugins,
Expand All @@ -207,6 +208,7 @@ class ConfigureTriggers extends React.Component {
monitorValues={monitorValues}
onRun={this.onRunExecute}
setFlyout={setFlyout}
isNewMonitor={isNewMonitor}
triggers={triggers}
triggerValues={triggerValues}
isDarkMode={isDarkMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class DefineTrigger extends Component {
}

onRunExecute = (triggers = []) => {
const { httpClient, monitor, notifications } = this.props;
const { httpClient, monitor, notifications, isNewMonitor } = this.props;
const formikValues = monitorToFormik(monitor);
const searchType = formikValues.searchType;
const monitorToExecute = _.cloneDeep(monitor);
Expand Down Expand Up @@ -141,6 +141,7 @@ class DefineTrigger extends Component {
triggers,
triggerValues,
isDarkMode,
isNewMonitor,
triggerIndex,
httpClient,
notifications,
Expand Down Expand Up @@ -217,7 +218,12 @@ class DefineTrigger extends Component {
<div style={{ padding: '0px 20px', paddingTop: '20px' }}>
<FormikFieldText
name={`${fieldPath}name`}
fieldProps={{ validate: validateTriggerName(triggers, triggerValues, fieldPath) }}
fieldProps={{
validate: (val) => {
var r = validateTriggerName(triggers, triggerValues, fieldPath, isNewMonitor)(val);
return r;
},
}}
formRow
rowProps={defaultRowProps}
inputProps={defaultInputProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import _ from 'lodash';

import { FORMIK_INITIAL_TRIGGER_VALUES, TRIGGER_TYPE } from '../../CreateTrigger/utils/constants';
import { MONITOR_TYPE } from '../../../../../utils/constants';

export const validateTriggerName = (triggers, triggerToEdit, fieldPath, isNewMonitor) => {
return (value) => {
if (!value) return 'Required.';
const { monitor_type } = triggerToEdit;

if (
isNewMonitor &&
(monitor_type === MONITOR_TYPE.QUERY_LEVEL || monitor_type === MONITOR_TYPE.CLUSTER_METRICS)
) {
const triggerName = _.get(triggerToEdit, `${fieldPath}name`, undefined);
const index = +fieldPath.substr(19, 1);
const isNameExistWithIndex = triggers.filter((trigger, i) => {
return i !== index && triggerName === trigger.name;
});
}

export const validateTriggerName = (triggers, triggerToEdit, fieldPath) => (value) => {
if (!value) return '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
const nameExists = triggers.filter((trigger) => {
if (isNewMonitor) {
const triggerName = _.get(triggerToEdit, `${fieldPath}name`, undefined);
const index = +fieldPath.substr(19, 1);
const triggerNameExistWithIndex = triggers.filter((trigger, i) => {
return i !== index && triggerName === trigger.name;
});
if (triggerNameExistWithIndex.length > 0) {
return 'Trigger name already used.';
}
}
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
};
};
108 changes: 97 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,90 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-android-arm-eabi/-/xxhash-android-arm-eabi-1.4.2.tgz#936b6b9b78035e1d136dd7bdf8296ad31fe1f4af"
integrity sha512-6aonl3wNY/axTiW9jSz+Z2RduP2xCZTgX1M1ur8uCJcUVv/kkv+dEMFb3o/k19qBGntPWvGXZTj2mYO6onDr/g==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-android-arm64/-/xxhash-android-arm64-1.4.2.tgz#4a51efb021ffeaad9c36ec85e21233166e27c17c"
integrity sha512-Uhwsk3mp++Op2Sl/TzR1VfFqHx5sGMAwQGhQKThc/w8QSrDNfWKd1R9rjohIDWeNbmRguUCDPEKEOZwGewWacw==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-darwin-arm64/-/xxhash-darwin-arm64-1.4.2.tgz#f51f02ccc0dd734db171c75a00a3cc0b2aceb595"
integrity sha512-0JYLJQ9C8prYbPPr3WlScYnAgIxLwitUwx2FnxNmBNH9bCDFoMdFXylzlxdYPHEYcXMsvWWcOnf9cESBH0WqzA==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-darwin-x64/-/xxhash-darwin-x64-1.4.2.tgz#cf70a5c5558dd825c1eb67e497f6c4dd1f0cc1ca"
integrity sha512-9CMuC7PZqHDaPCnOUrW6AIL+2gzLq22klGKBRXHXVfUELuNEqp7N0HyHheoQ99U0otCNnsA54zkfBw9cL1pcIA==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-freebsd-x64/-/xxhash-freebsd-x64-1.4.2.tgz#5ce9441127fab0709189ccef5a24c328a0de233d"
integrity sha512-pPvuivkR3oyZ6DocgX6LSN2GpgF6Z4DK8M0UwDlY5P2wAUoi1JIOJBLjOrQmiaCUIIGCTTDBKTPwgIDUh6A7mg==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-linux-arm-gnueabihf/-/xxhash-linux-arm-gnueabihf-1.4.2.tgz#897e5612782279aff7d45b1a1a09e68f5b9709b6"
integrity sha512-LzI9WMW2scTG3/87/9j+oE0njz1o/2s7c61h1rEZyP1WBGA1cZ2xn3wAnwqFOPKE3s7QvONXoYjMMqa+BtDNNg==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-linux-arm64-gnu/-/xxhash-linux-arm64-gnu-1.4.2.tgz#46417a0253efd4d0b972f1b501d269590036a109"
integrity sha512-BeOlxfDzdnYRfK0nEyG8gpA6AGpamZVe2FNBl96xB5eVYiPp8PhIFLFsfoPURqa/Abm8+JV+QHzf3sngD1Gr+Q==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-linux-arm64-musl/-/xxhash-linux-arm64-musl-1.4.2.tgz#3ea61d8236c0a36316130d167075a838508a47e7"
integrity sha512-u6l7mE5jmEWwUghZbfVHhhN9pMDkE0pjxjZu52bd8Ohk8LkbFDOJVNG2AWkWkjdInP31PmhvtCgE2laKCCQeRQ==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-linux-x64-gnu/-/xxhash-linux-x64-gnu-1.4.2.tgz#6165e3a5db08f88a0c47ad4d93ab464212e5ecad"
integrity sha512-h3FUuuDBGdJ42HgA9lxX9cNSussF1lO/04OKXkVuJGRPR6mBCqBZ9SPNhzSk2PyYEqHimXIBYr+CdDRCS1xsfQ==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-linux-x64-musl/-/xxhash-linux-x64-musl-1.4.2.tgz#6668fd667f25cd2ff60a7f15e406e2e0cb5c9b56"
integrity sha512-W3/lMn1IBeTAcYVJEycxeyiMLD05M7OGsCZLAE0PB/t8dC+tH5jayVrYRhj+ZznK3V3pZfZb7cCVvZD2eCMb3g==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-win32-arm64-msvc/-/xxhash-win32-arm64-msvc-1.4.2.tgz#674fad808e8569ebd75f38fb22f9bee119864bda"
integrity sha512-53WhZY+khV/d8qmbLimCLXptq6/r1KD61Hi8I96KWxP6WiDl7Himhi5Pm2SbA2YK1WA3xPCaHj/LkPRks8Zi2g==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-win32-ia32-msvc/-/xxhash-win32-ia32-msvc-1.4.2.tgz#8b5b47051c555516fcc23c232497c5af42d31489"
integrity sha512-F1gWrwrNmw9rjxGphsDw9iGttXDEaGnAAj9U14+9n3WREMsmCRsKfoSZ41ln9KHH8hEABsxL15Tiutm++S3LmA==

"@node-rs/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash-win32-x64-msvc/-/xxhash-win32-x64-msvc-1.4.2.tgz#6dc64bd8e3461398e2caebe6b895b687bee9a80c"
integrity sha512-d7CUKK3IFYbED/0w6RosCKWL/RODfmA6yMfQdu3BZvh9Y8GTjRLE+DAIOOklP9Rb9qkKYSyzp2ALrHSkrHdRXQ==

"@node-rs/xxhash@^1.3.0":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@node-rs/xxhash/-/xxhash-1.4.2.tgz#91e4c74db14e7f078c06359a2ff2f85c3a24b84d"
integrity sha512-PvFTpG5z8tdXdZNkHUpjHDnBAKLxn6Xwp4yH8nfuVA2NcO3J970IHnm9yWcs9VVNwLr2aJx0lb8OvBAuLUNiEw==
optionalDependencies:
"@node-rs/xxhash-android-arm-eabi" "1.4.2"
"@node-rs/xxhash-android-arm64" "1.4.2"
"@node-rs/xxhash-darwin-arm64" "1.4.2"
"@node-rs/xxhash-darwin-x64" "1.4.2"
"@node-rs/xxhash-freebsd-x64" "1.4.2"
"@node-rs/xxhash-linux-arm-gnueabihf" "1.4.2"
"@node-rs/xxhash-linux-arm64-gnu" "1.4.2"
"@node-rs/xxhash-linux-arm64-musl" "1.4.2"
"@node-rs/xxhash-linux-x64-gnu" "1.4.2"
"@node-rs/xxhash-linux-x64-musl" "1.4.2"
"@node-rs/xxhash-win32-arm64-msvc" "1.4.2"
"@node-rs/xxhash-win32-ia32-msvc" "1.4.2"
"@node-rs/xxhash-win32-x64-msvc" "1.4.2"

"@samverschueren/stream-to-observable@^0.3.0":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.1.tgz#a21117b19ee9be70c379ec1877537ef2e1c63301"
Expand Down Expand Up @@ -2868,7 +2952,7 @@ loader-runner@^2.4.0:
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.4.0.tgz#ed47066bfe534d7e84c4c7b9998c2a75607d9357"
integrity sha512-Jsmr89RcXGIwivFY21FcRrisYZfvLMTWx5kOLc+JTxtpBOG6xML0vzbc6SEQG2FO9/4Fc3wW4LVcB5DmGflaRw==

[email protected], loader-utils@^1.2.3:
[email protected], loader-utils@^2.0.4:
version "1.4.2"
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-1.4.2.tgz#29a957f3a63973883eb684f10ffd3d151fec01a3"
integrity sha512-I5d00Pd/jwMD2QCduo657+YM/6L3KZu++pmX9VFncxaxvHcru9jx1lBaFft+r4Mt2jK0Yhp41XlRAihzPxHNCg==
Expand Down Expand Up @@ -4407,11 +4491,12 @@ tapable@^1.0.0, tapable@^1.1.3:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==

terser-webpack-plugin@^1.4.3:
version "1.4.5"
resolved "https://registry.yarnpkg.com/terser-webpack-plugin/-/terser-webpack-plugin-1.4.5.tgz#a217aefaea330e734ffacb6120ec1fa312d6040b"
integrity sha512-04Rfe496lN8EYruwi6oPQkG0vo8C+HT49X687FZnpPF0qMAIHONI6HEXYPKDOE8e5HjXTyKfqRd/agHtH0kOtw==
"terser-webpack-plugin@npm:@amoo-miki/terser-webpack-plugin@1.4.5-rc.2":
version "1.4.5-rc.2"
resolved "https://registry.yarnpkg.com/@amoo-miki/terser-webpack-plugin/-/terser-webpack-plugin-1.4.5-rc.2.tgz#046c062ef22c126c2544718674bc6624e3651b9c"
integrity sha512-JFSGSzsWgSHEqQXlnHDh3gw+jdVdVlWM2Irdps9P/yWYNY/5VjuG8sdoW4mbuP8/HM893/k8N+ipbeqsd8/xpA==
dependencies:
"@node-rs/xxhash" "^1.3.0"
cacache "^12.0.2"
find-cache-dir "^2.1.0"
is-wsl "^1.1.0"
Expand Down Expand Up @@ -4704,11 +4789,12 @@ webpack-sources@^1.4.0, webpack-sources@^1.4.1:
source-list-map "^2.0.0"
source-map "~0.6.1"

webpack@^4.41.5:
version "4.46.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542"
integrity sha512-6jJuJjg8znb/xRItk7bkT0+Q7AHCYjjFnvKIWQPkNIOyRqoCGvkOs0ipeQzrqz4l5FtN5ZI/ukEHroeX/o1/5Q==
"webpack@npm:@amoo-miki/[email protected]":
version "4.46.0-rc.2"
resolved "https://registry.yarnpkg.com/@amoo-miki/webpack/-/webpack-4.46.0-rc.2.tgz#36824597c14557a7bb0a8e13203e30275e7b02bd"
integrity sha512-Y/ZqxTHOoDF1kz3SR63Y9SZGTDUpZNNFrisTRHofWhP8QvNX3LMN+TCmEP56UfLaiLVKMcaiFjx8kFb2TgyBaQ==
dependencies:
"@node-rs/xxhash" "^1.3.0"
"@webassemblyjs/ast" "1.9.0"
"@webassemblyjs/helper-module-context" "1.9.0"
"@webassemblyjs/wasm-edit" "1.9.0"
Expand All @@ -4721,15 +4807,15 @@ webpack@^4.41.5:
eslint-scope "^4.0.3"
json-parse-better-errors "^1.0.2"
loader-runner "^2.4.0"
loader-utils "^1.2.3"
loader-utils "^2.0.4"
memory-fs "^0.4.1"
micromatch "^3.1.10"
mkdirp "^0.5.3"
neo-async "^2.6.1"
node-libs-browser "^2.2.1"
schema-utils "^1.0.0"
tapable "^1.1.3"
terser-webpack-plugin "^1.4.3"
terser-webpack-plugin "npm:@amoo-miki/terser-webpack-plugin@1.4.5-rc.2"
watchpack "^1.7.4"
webpack-sources "^1.4.1"

Expand Down