From 7f662e7bfffa5307b52a8b983402b4eaa8c2c453 Mon Sep 17 00:00:00 2001 From: Hanna Kurban Date: Mon, 20 May 2024 15:52:47 +0300 Subject: [PATCH] FIO-8254 fixed available values validation error for Select component --- .../__tests__/validateAvailableItems.test.ts | 22 +++++++++++++++++++ .../rules/validateAvailableItems.ts | 8 ++++++- src/types/Component.ts | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateAvailableItems.test.ts b/src/process/validation/rules/__tests__/validateAvailableItems.test.ts index f35f2641..5ec100e1 100644 --- a/src/process/validation/rules/__tests__/validateAvailableItems.test.ts +++ b/src/process/validation/rules/__tests__/validateAvailableItems.test.ts @@ -103,6 +103,28 @@ it('Validating a simple static values select component with the available items expect(result).to.equal(null); }); +it('Validating a simple static values select component with the available items validation parameter will return null if the selected item is valid and dataSrc is not specified', async () => { + const component: SelectComponent = { + ...simpleSelectOptions, + dataSrc: undefined, + data: { + values: [ + { label: 'foo', value: 'foo' }, + { label: 'bar', value: 'bar' }, + { label: 'baz', value: 'baz' }, + { label: 'baz', value: 'baz' }, + ], + }, + validate: { onlyAvailableItems: true }, + }; + const data = { + component: 'foo', + }; + const context = generateProcessorContext(component, data); + const result = await validateAvailableItems(context); + expect(result).to.equal(null); +}); + it('Validating a simple URL select component without the available items validation parameter will return null', async () => { const component: SelectComponent = { ...simpleSelectOptions, diff --git a/src/process/validation/rules/validateAvailableItems.ts b/src/process/validation/rules/validateAvailableItems.ts index 069311e4..07511a77 100644 --- a/src/process/validation/rules/validateAvailableItems.ts +++ b/src/process/validation/rules/validateAvailableItems.ts @@ -1,4 +1,4 @@ -import isEmpty from 'lodash/isEmpty'; +import { isEmpty, isUndefined} from 'lodash'; import { FieldError, ProcessorError } from 'error'; import { Evaluator } from 'utils'; import { RadioComponent, SelectComponent, RuleFn, RuleFnSync, ValidationContext } from 'types'; @@ -37,6 +37,9 @@ function mapStaticValues(values: { label: string; value: string }[]) { } async function getAvailableSelectValues(component: SelectComponent, context: ValidationContext) { + if (isUndefined(component.dataSrc) && component.data.hasOwnProperty('values')) { + component.dataSrc = 'values'; + }; switch (component.dataSrc) { case 'values': if (Array.isArray(component.data.values)) { @@ -107,6 +110,9 @@ async function getAvailableSelectValues(component: SelectComponent, context: Val } function getAvailableSelectValuesSync(component: SelectComponent, context: ValidationContext) { + if (isUndefined(component.dataSrc) && component.data.hasOwnProperty('values')) { + component.dataSrc = 'values'; + }; switch (component.dataSrc) { case 'values': if (Array.isArray(component.data?.values)) { diff --git a/src/types/Component.ts b/src/types/Component.ts index c050eb8d..f76f79f8 100644 --- a/src/types/Component.ts +++ b/src/types/Component.ts @@ -344,7 +344,7 @@ type StaticValuesSelectData = { data: { values: { label: string; value: string }[]; }; - dataSrc: 'values'; + dataSrc?: undefined | 'values'; }; type JsonValuesSelectData = {