From d028df3b950f11e3453b1e5e146799f339e7f652 Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Mon, 9 Sep 2024 11:25:29 -0500 Subject: [PATCH 1/5] update normalization to utilize component model types if applicable; fix tests --- src/components/_classes/multivalue/Multivalue.js | 15 +++++++++------ src/components/day/Day.js | 2 +- src/utils/formUtils.js | 2 -- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/_classes/multivalue/Multivalue.js b/src/components/_classes/multivalue/Multivalue.js index 09ef67f4a4..c20eb74202 100644 --- a/src/components/_classes/multivalue/Multivalue.js +++ b/src/components/_classes/multivalue/Multivalue.js @@ -1,31 +1,34 @@ import Field from '../field/Field'; import _ from 'lodash'; +import { Utils } from '@formio/core'; export default class Multivalue extends Field { /** * Normalize values coming into updateValue. * @param {*} value - The value to normalize before setting. - * @param {Object} flags - Flags to use when normalizing the value. + * @param {object} flags - Flags to use when normalizing the value. * @param {*} emptyValue - The empty value for the field. * @returns {*} - The normalized value. */ normalizeValue(value, flags = {}, emptyValue = this.emptyValue) { + const underlyingValueShouldBeArray = Utils.getModelType(this.component) === 'array' || this.component.storeas === 'array' || Array.isArray(emptyValue); if (this.component.multiple) { if (Array.isArray(value)) { + if (underlyingValueShouldBeArray) { + if (value.length === 0 || !Array.isArray(value[0])) { + return [value]; + } + } if (value.length === 0) { return [emptyValue]; } - if (this.component.storeas === 'array') { - return super.normalizeValue([value], flags); - } - return super.normalizeValue(value, flags); } else { return super.normalizeValue(value == null ? [emptyValue] : [value], flags); } } else { - if (Array.isArray(value) && !Array.isArray(emptyValue)) { + if (Array.isArray(value) && !underlyingValueShouldBeArray) { if (this.component.storeas === 'string') { return super.normalizeValue(value.join(this.delimiter || ''), flags); } diff --git a/src/components/day/Day.js b/src/components/day/Day.js index 5ac35d0e3a..680e53a01d 100644 --- a/src/components/day/Day.js +++ b/src/components/day/Day.js @@ -630,7 +630,7 @@ export default class DayComponent extends Field { isPartialDay(value) { if (!value) { - return false; + return true; } const [DAY, MONTH, YEAR] = this.component.dayFirst ? [0, 1, 2] : [1, 0, 2]; const values = value.split('/'); diff --git a/src/utils/formUtils.js b/src/utils/formUtils.js index 30d6d1ba41..9bd0f6e7ba 100644 --- a/src/utils/formUtils.js +++ b/src/utils/formUtils.js @@ -7,7 +7,6 @@ const { getModelType, getComponentAbsolutePath, getComponentPath, - isComponentModelType, isComponentNestedDataType, componentPath, componentChildPath, @@ -58,7 +57,6 @@ export { getModelType, getComponentAbsolutePath, getComponentPath, - isComponentModelType, isComponentNestedDataType, componentPath, componentChildPath, From a92663741d07e5e646c185023ec4bfa1aa03a3f9 Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Mon, 9 Sep 2024 12:53:29 -0500 Subject: [PATCH 2/5] update to core dependency --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5d5d18d22d..34cf47a473 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "dependencies": { "@formio/bootstrap": "3.0.0-dev.98.17ba6ea", "@formio/choices.js": "^10.2.1", - "@formio/core": "2.1.0-dev.145.4491833", + "@formio/core": "2.1.0-dev.146.076f050", "@formio/text-mask-addons": "^3.8.0-formio.2", "@formio/vanilla-text-mask": "^5.1.1-formio.1", "abortcontroller-polyfill": "^1.7.5", diff --git a/yarn.lock b/yarn.lock index 874e83a78a..1b4c746b1d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -275,10 +275,10 @@ fuse.js "^6.6.2" redux "^4.2.0" -"@formio/core@2.1.0-dev.145.4491833": - version "2.1.0-dev.145.4491833" - resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.145.4491833.tgz#f8bb24949e71d865a2ec354a3f58dc5bf56dcde6" - integrity sha512-mN9tymogT+6qWbwlVTNB3xGI+sk9sz4WYxe7BW66fuPpsUsYwxuUtSrnGbM8WInkqmj7aNZrXd5WQ2SAV93wjQ== +"@formio/core@2.1.0-dev.146.076f050": + version "2.1.0-dev.146.076f050" + resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.146.076f050.tgz#068ec42380df561ba85126fb34f25d48ca670e55" + integrity sha512-8zkHEW1+zBLjtYLazJudecwNBlpOTh8e1OuVVc6stNxFrrfMB+OHQKhERl9kqJO5vu9Y/x1sSdelrBBEAvy3pA== dependencies: "@types/json-logic-js" "^2.0.7" browser-cookies "^1.2.0" From 651313724d5c2b3f86f29e91b5f82cbee27bb6c5 Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Mon, 9 Sep 2024 13:02:09 -0500 Subject: [PATCH 3/5] add tests --- src/components/file/File.unit.js | 36 ++++++++++++++++++++++++++++ src/components/hidden/Hidden.unit.js | 19 +++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/components/file/File.unit.js b/src/components/file/File.unit.js index 0affe77b13..f90b14a996 100644 --- a/src/components/file/File.unit.js +++ b/src/components/file/File.unit.js @@ -197,6 +197,42 @@ describe('File Component', () => { }).catch(done); }); + it('Should not incorrectly validate a non-multiple File component', () => { + comp1.multiple = false; + return Harness.testCreate(FileComponent, comp1).then((component) => { + assert(component.checkValidity(), 'Item should be valid'); + component.setValue([ + { + storage: 'base64', + name: 'IMG_5235-ce0abe18-5d3e-4ab4-84ca-b3e06684bc86.jpg', + url: 'data:image/jpg;base64,AAAAIGZ0eXBoZWljAAAAAG1pZjF', + size: 1159732, + type: 'image/jpeg', + originalName: 'IMG_5235.jpg', + } + ]); + assert(component.checkValidity(), 'Item should be valid'); + }); + }) + + it('Should not incorrectly validate a multiple File Component', () => { + comp1.multiple = true; + return Harness.testCreate(FileComponent, comp1).then((component) => { + assert(component.checkValidity(), 'Item should be valid'); + component.setValue([ + { + storage: 'base64', + name: 'IMG_5235-ce0abe18-5d3e-4ab4-84ca-b3e06684bc86.jpg', + url: 'data:image/jpg;base64,AAAAIGZ0eXBoZWljAAAAAG1pZjF', + size: 1159732, + type: 'image/jpeg', + originalName: 'IMG_5235.jpg', + } + ]); + assert(component.checkValidity(), 'Item should be valid'); + }); + }); + it('Should abort the correct file when user clicks the file remove button', (done) => { const cmp = _.cloneDeep(comp1); const abortedFiles = []; diff --git a/src/components/hidden/Hidden.unit.js b/src/components/hidden/Hidden.unit.js index ba3e6b7805..eb75a1987b 100644 --- a/src/components/hidden/Hidden.unit.js +++ b/src/components/hidden/Hidden.unit.js @@ -1,3 +1,5 @@ +const assert = require('power-assert'); + import Harness from '../../../test/harness'; import HiddenComponent from './Hidden'; @@ -9,4 +11,21 @@ describe('Hidden Component', () => { it('Should build a hidden component', () => { return Harness.testCreate(HiddenComponent, comp1); }); + + it('Should not incorrectly validate multiple when hidden component has an array value', () => { + return Harness.testCreate(HiddenComponent, comp1).then((component) => { + assert(component.checkValidity(), 'Item should be valid'); + component.setValue([ + { + key: 'foo', + value: 'bar' + }, + { + key: 'hello', + value: 'world' + } + ]); + assert(component.checkValidity(), 'Item should be valid after setting value'); + }); + }); }); From 938c0024bbae1a2762ff55e94eccf0ad677cfe55 Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Mon, 9 Sep 2024 13:02:45 -0500 Subject: [PATCH 4/5] minor update to tests --- src/components/hidden/Hidden.unit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/hidden/Hidden.unit.js b/src/components/hidden/Hidden.unit.js index eb75a1987b..b901ea9e31 100644 --- a/src/components/hidden/Hidden.unit.js +++ b/src/components/hidden/Hidden.unit.js @@ -1,4 +1,4 @@ -const assert = require('power-assert'); + import Harness from '../../../test/harness'; import HiddenComponent from './Hidden'; From 896e9df14b38bdfc62e6e2c8d9f1c9d89ac33e83 Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Mon, 9 Sep 2024 14:22:23 -0500 Subject: [PATCH 5/5] update deps, fix tests --- package.json | 2 +- src/components/hidden/Hidden.unit.js | 1 + yarn.lock | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 34cf47a473..a8d2c7664d 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "dependencies": { "@formio/bootstrap": "3.0.0-dev.98.17ba6ea", "@formio/choices.js": "^10.2.1", - "@formio/core": "2.1.0-dev.146.076f050", + "@formio/core": "2.1.0-dev.146.e57530c", "@formio/text-mask-addons": "^3.8.0-formio.2", "@formio/vanilla-text-mask": "^5.1.1-formio.1", "abortcontroller-polyfill": "^1.7.5", diff --git a/src/components/hidden/Hidden.unit.js b/src/components/hidden/Hidden.unit.js index b901ea9e31..466f3ab528 100644 --- a/src/components/hidden/Hidden.unit.js +++ b/src/components/hidden/Hidden.unit.js @@ -2,6 +2,7 @@ import Harness from '../../../test/harness'; import HiddenComponent from './Hidden'; +import assert from 'power-assert'; import { comp1 diff --git a/yarn.lock b/yarn.lock index 1b4c746b1d..af073e7a9c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -275,10 +275,10 @@ fuse.js "^6.6.2" redux "^4.2.0" -"@formio/core@2.1.0-dev.146.076f050": - version "2.1.0-dev.146.076f050" - resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.146.076f050.tgz#068ec42380df561ba85126fb34f25d48ca670e55" - integrity sha512-8zkHEW1+zBLjtYLazJudecwNBlpOTh8e1OuVVc6stNxFrrfMB+OHQKhERl9kqJO5vu9Y/x1sSdelrBBEAvy3pA== +"@formio/core@2.1.0-dev.146.e57530c": + version "2.1.0-dev.146.e57530c" + resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.146.e57530c.tgz#def115ff5d61e4d6833c4c807c246a3ca29afe92" + integrity sha512-oRaTKy1eAFVjZclH/Alx6TrdRFNTwlnGgDt5wQ9Nz9L2lGXxuoRv+KPtpBzst9hFZat9JnkZToDOXAQGj5hkHQ== dependencies: "@types/json-logic-js" "^2.0.7" browser-cookies "^1.2.0"