Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix/FIO-8389_editgr…
Browse files Browse the repository at this point in the history
…id_with_conditinal_logic
  • Loading branch information
lane-formio committed May 31, 2024
2 parents 36bb01e + 74ac883 commit 4e24340
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- FIO-8336 fix validation on multiple values
- FIO-8037: added number component normalization
- FIO-8288: do not validate dates in textfield components with calendar widgets
- FIO-8254 fixed available values validation error for Select component
- FIO-8281: fixed sync validation error for select component with url data src


## 2.0.0-rc.24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
simpleSelectOptions,
} from './fixtures/components';
import { generateProcessorContext } from './fixtures/util';
import { validateAvailableItems } from '../validateAvailableItems';
import { validateAvailableItems, validateAvailableItemsSync } from '../validateAvailableItems';

it('Validating a component without the available items validation parameter will return null', async () => {
const component = simpleTextField;
Expand Down Expand Up @@ -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,
Expand All @@ -120,6 +142,43 @@ it('Validating a simple URL select component without the available items validat
expect(result).to.equal(null);
});

it('Validating a simple URL select component synchronously will return null', async () => {
const component: SelectComponent = {
...simpleSelectOptions,
dataSrc: 'url',
data: {
url: 'http://localhost:8080/numbers',
headers: [],
},
validate: { onlyAvailableItems: true },
};
const data = {
component: 'foo',
};
const context = generateProcessorContext(component, data);
const result = validateAvailableItemsSync(context);
expect(result).to.equal(null);
});

it('Validating a multiple URL select component synchronously will return null', async () => {
const component: SelectComponent = {
...simpleSelectOptions,
dataSrc: 'url',
data: {
url: 'http://localhost:8080/numbers',
headers: [],
},
multiple: true,
validate: { onlyAvailableItems: true },
};
const data = {
component: ['foo'],
};
const context = generateProcessorContext(component, data);
const result = validateAvailableItemsSync(context);
expect(result).to.equal(null);
});

it('Validating a simple JSON select component (string JSON) without the available items validation parameter will return null', async () => {
const component: SelectComponent = {
...simpleSelectOptions,
Expand Down
10 changes: 9 additions & 1 deletion src/process/validation/rules/validateAvailableItems.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -156,6 +162,8 @@ function getAvailableSelectValuesSync(component: SelectComponent, context: Valid
'validate:validateAvailableItems'
);
}
case 'url':
return null;
default:
throw new ProcessorError(
`Failed to validate available values in select component '${component.key}': data source ${component.dataSrc} is not valid}`,
Expand Down
2 changes: 1 addition & 1 deletion src/types/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ type StaticValuesSelectData = {
data: {
values: { label: string; value: string }[];
};
dataSrc: 'values';
dataSrc?: undefined | 'values';
};

type JsonValuesSelectData = {
Expand Down

0 comments on commit 4e24340

Please sign in to comment.