From a99b4654d065335fc59ef90768d8128ce9976b38 Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 7 Apr 2020 12:12:46 -0500 Subject: [PATCH 1/4] [Alerting] Add validation support for nested IErrorObjects --- .../public/application/sections/alert_form/alert_add.tsx | 8 +++++++- x-pack/plugins/triggers_actions_ui/public/types.ts | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx index e44e20751b315..62f72ec7f1d20 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx @@ -83,7 +83,7 @@ export const AlertAdd = ({ ...(alertType ? alertType.validate(alert.params).errors : []), ...validateBaseProperties(alert).errors, } as IErrorObject; - const hasErrors = !!Object.keys(errors).find(errorKey => errors[errorKey].length >= 1); + const hasErrors = parseErrors(errors); const actionsErrors: Array<{ errors: IErrorObject; @@ -213,3 +213,9 @@ export const AlertAdd = ({ ); }; + +const parseErrors: (errors: IErrorObject) => boolean = errors => + !!Object.values(errors).find(errorList => { + if (Array.isArray(errorList)) return errorList.length >= 1; + return parseErrors(errorList); + }); diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index 7dfaa7b918f70..be1c04b20078d 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -111,5 +111,5 @@ export interface AlertTypeModel { } export interface IErrorObject { - [key: string]: string[]; + [key: string]: string[] | IErrorObject; } From 89ca51ec8fa8cbca0dae1aefdf4149bd0729c1c8 Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 7 Apr 2020 12:24:39 -0500 Subject: [PATCH 2/4] Typecheck fix --- .../triggers_actions_ui/public/common/expression_items/of.tsx | 3 ++- .../public/common/expression_items/threshold.tsx | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/of.tsx b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/of.tsx index d399f6136690b..3de0a99ccbbd9 100644 --- a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/of.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/of.tsx @@ -17,13 +17,14 @@ import { } from '@elastic/eui'; import { builtInAggregationTypes } from '../constants'; import { AggregationType } from '../types'; +import { IErrorObject } from '../../types'; import { ClosablePopoverTitle } from './components'; import './of.scss'; interface OfExpressionProps { aggType: string; aggField?: string; - errors: { [key: string]: string[] }; + errors: IErrorObject; onChangeSelectedAggField: (selectedAggType?: string) => void; fields: Record; customAggTypesOptions?: { diff --git a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx index fb3ff9ceb0926..99dd7b63383fb 100644 --- a/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/common/expression_items/threshold.tsx @@ -18,11 +18,12 @@ import { } from '@elastic/eui'; import { builtInComparators } from '../constants'; import { Comparator } from '../types'; +import { IErrorObject } from '../../types'; import { ClosablePopoverTitle } from './components'; interface ThresholdExpressionProps { thresholdComparator: string; - errors: { [key: string]: string[] }; + errors: IErrorObject; onChangeSelectedThresholdComparator: (selectedThresholdComparator?: string) => void; onChangeSelectedThreshold: (selectedThreshold?: number[]) => void; customComparators?: { From b6d89a63b485173f5b522dfa29220fdfb6f01316 Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 7 Apr 2020 14:32:34 -0500 Subject: [PATCH 3/4] Fix recursion crash when errors are strings --- .../public/application/sections/alert_form/alert_add.tsx | 5 +++-- x-pack/plugins/triggers_actions_ui/public/types.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx index 62f72ec7f1d20..e025248fad52e 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import React, { useCallback, useReducer, useState } from 'react'; +import { isObject } from 'lodash'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiTitle, @@ -216,6 +217,6 @@ export const AlertAdd = ({ const parseErrors: (errors: IErrorObject) => boolean = errors => !!Object.values(errors).find(errorList => { - if (Array.isArray(errorList)) return errorList.length >= 1; - return parseErrors(errorList); + if (isObject(errorList)) return parseErrors(errorList as IErrorObject); + return errorList.length >= 1; }); diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index be1c04b20078d..b2ba7f0166085 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -56,7 +56,7 @@ export interface ActionTypeModel { } export interface ValidationResult { - errors: Record; + errors: IErrorObject; } export interface ActionConnector { @@ -111,5 +111,5 @@ export interface AlertTypeModel { } export interface IErrorObject { - [key: string]: string[] | IErrorObject; + [key: string]: string | string[] | IErrorObject; } From 5d099f2881d67e274314186added05dcba48961a Mon Sep 17 00:00:00 2001 From: Zacqary Xeper Date: Tue, 7 Apr 2020 15:23:50 -0500 Subject: [PATCH 4/4] Typecheck fix --- x-pack/plugins/triggers_actions_ui/public/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index b2ba7f0166085..14f886035b2b7 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -56,7 +56,7 @@ export interface ActionTypeModel { } export interface ValidationResult { - errors: IErrorObject; + errors: Record; } export interface ActionConnector {