From 89288f28354c22240eaf0dae83f2a2381337ce33 Mon Sep 17 00:00:00 2001 From: olav Date: Wed, 20 Apr 2022 15:57:01 +0200 Subject: [PATCH] fix: ignore empty legal values arrays (#893) --- .../useConstraintInput/useConstraintInput.tsx | 10 +++++----- frontend/src/utils/exists.ts | 3 --- frontend/src/utils/nonEmptyArray.ts | 3 +++ 3 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 frontend/src/utils/exists.ts create mode 100644 frontend/src/utils/nonEmptyArray.ts diff --git a/frontend/src/component/common/ConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/useConstraintInput/useConstraintInput.tsx b/frontend/src/component/common/ConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/useConstraintInput/useConstraintInput.tsx index 52ac9a23ed..3503c69ce5 100644 --- a/frontend/src/component/common/ConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/useConstraintInput/useConstraintInput.tsx +++ b/frontend/src/component/common/ConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/useConstraintInput/useConstraintInput.tsx @@ -8,7 +8,6 @@ import { import { IUnleashContextDefinition } from 'interfaces/context'; import { IConstraint } from 'interfaces/strategy'; import React, { useCallback, useEffect, useState } from 'react'; -import { exists } from 'utils/exists'; import { oneOf } from 'utils/oneOf'; import { @@ -18,6 +17,7 @@ import { dateValidatorGenerator, ConstraintValidatorOutput, } from './constraintValidators'; +import { nonEmptyArray } from 'utils/nonEmptyArray'; interface IUseConstraintInputProps { contextDefinition: IUnleashContextDefinition; @@ -75,22 +75,22 @@ export const useConstraintInput = ({ const resolveInputType = useCallback(() => { if ( - exists(contextDefinition.legalValues) && + nonEmptyArray(contextDefinition.legalValues) && oneOf(inOperators, localConstraint.operator) ) { setInput(IN_OPERATORS_LEGAL_VALUES); } else if ( - exists(contextDefinition.legalValues) && + nonEmptyArray(contextDefinition.legalValues) && oneOf(stringOperators, localConstraint.operator) ) { setInput(STRING_OPERATORS_LEGAL_VALUES); } else if ( - exists(contextDefinition.legalValues) && + nonEmptyArray(contextDefinition.legalValues) && oneOf(numOperators, localConstraint.operator) ) { setInput(NUM_OPERATORS_LEGAL_VALUES); } else if ( - exists(contextDefinition.legalValues) && + nonEmptyArray(contextDefinition.legalValues) && oneOf(semVerOperators, localConstraint.operator) ) { setInput(SEMVER_OPERATORS_LEGAL_VALUES); diff --git a/frontend/src/utils/exists.ts b/frontend/src/utils/exists.ts deleted file mode 100644 index 1c044f155d..0000000000 --- a/frontend/src/utils/exists.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const exists = (value: any) => { - return Boolean(value); -}; diff --git a/frontend/src/utils/nonEmptyArray.ts b/frontend/src/utils/nonEmptyArray.ts new file mode 100644 index 0000000000..af9852ef8a --- /dev/null +++ b/frontend/src/utils/nonEmptyArray.ts @@ -0,0 +1,3 @@ +export const nonEmptyArray = (value: unknown): boolean => { + return Boolean(value) && Array.isArray(value) && value.length > 0; +};