From d72d4489ccf66ef22128906c8aa3f178b28bb4f3 Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Fri, 10 Feb 2023 10:33:34 +0100 Subject: [PATCH 1/5] Fix non required json input setting a value when empty --- .../helper-plugin/lib/src/components/GenericInput/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js index b7d5b491cd..e63cada7a8 100644 --- a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js +++ b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js @@ -146,8 +146,7 @@ const GenericInput = ({ hint={hint} required={required} onChange={(json) => { - // Default to null when the field is not required and there is no input value - const value = !attribute.required && !json.length ? 'null' : json; + const value = !attribute.required && !json.length ? null : json; onChange({ target: { name, value } }); }} minHeight={pxToRem(252)} From 1945fd3748fc580cefe260ecb3b77a682a0f349f Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Fri, 10 Feb 2023 11:59:39 +0100 Subject: [PATCH 2/5] Keep comment --- .../core/helper-plugin/lib/src/components/GenericInput/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js index e63cada7a8..daded2ea94 100644 --- a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js +++ b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js @@ -146,6 +146,7 @@ const GenericInput = ({ hint={hint} required={required} onChange={(json) => { + // Default to null when the field is not required and there is no input value const value = !attribute.required && !json.length ? null : json; onChange({ target: { name, value } }); }} From 849f16eec6cb83991e49ee964c60d5b7dac17724 Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Tue, 14 Feb 2023 11:55:35 +0530 Subject: [PATCH 3/5] Remove JSON default values being set by the content manager --- .../EditViewDataManagerProvider/utils/schema.js | 8 +++++++- .../admin/src/content-manager/utils/createDefaultForm.js | 8 -------- .../content-manager/utils/tests/createDefaultForm.test.js | 5 ----- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/core/admin/admin/src/content-manager/components/EditViewDataManagerProvider/utils/schema.js b/packages/core/admin/admin/src/content-manager/components/EditViewDataManagerProvider/utils/schema.js index 52bb5bb2f4..2cb2204fe9 100644 --- a/packages/core/admin/admin/src/content-manager/components/EditViewDataManagerProvider/utils/schema.js +++ b/packages/core/admin/admin/src/content-manager/components/EditViewDataManagerProvider/utils/schema.js @@ -216,6 +216,10 @@ const createYupSchemaAttribute = (type, validations, options) => { schema = yup .mixed(errorsTrads.json) .test('isJSON', errorsTrads.json, (value) => { + if (!value || !value.length) { + return true; + } + try { JSON.parse(value); @@ -226,7 +230,9 @@ const createYupSchemaAttribute = (type, validations, options) => { }) .nullable() .test('required', errorsTrads.required, (value) => { - if (validations.required && !value.length) return false; + if (validations.required && (!value || !value.length)) { + return false; + } return true; }); diff --git a/packages/core/admin/admin/src/content-manager/utils/createDefaultForm.js b/packages/core/admin/admin/src/content-manager/utils/createDefaultForm.js index 3b50f33ae8..65ee4315b3 100644 --- a/packages/core/admin/admin/src/content-manager/utils/createDefaultForm.js +++ b/packages/core/admin/admin/src/content-manager/utils/createDefaultForm.js @@ -5,14 +5,6 @@ const createDefaultForm = (attributes, allComponentsSchema) => { const attribute = get(attributes, [current], {}); const { default: defaultValue, component, type, required, min, repeatable } = attribute; - if (type === 'json') { - acc[current] = null; - } - - if (type === 'json' && required === true) { - acc[current] = {}; - } - if (defaultValue !== undefined) { acc[current] = defaultValue; } diff --git a/packages/core/admin/admin/src/content-manager/utils/tests/createDefaultForm.test.js b/packages/core/admin/admin/src/content-manager/utils/tests/createDefaultForm.test.js index f5431024d6..b14a4da65f 100644 --- a/packages/core/admin/admin/src/content-manager/utils/tests/createDefaultForm.test.js +++ b/packages/core/admin/admin/src/content-manager/utils/tests/createDefaultForm.test.js @@ -11,11 +11,6 @@ describe('CONTENT MANAGER | utils | createDefaultForm', () => { expect(createDefaultForm(attributes, {})).toEqual({}); }); - it('should set the json type with the correct value', () => { - expect(createDefaultForm({ test: { type: 'json' } }, {})).toEqual({ test: null }); - expect(createDefaultForm({ test: { type: 'json', required: true } }, {})).toEqual({ test: {} }); - }); - it('should init the requide dynamic zone type with an empty array', () => { expect(createDefaultForm({ test: { type: 'dynamiczone', required: true } })).toEqual({ test: [], From f6da34d1e71ac6681663121f397a3c4c6c7f790f Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Wed, 15 Feb 2023 17:11:37 +0530 Subject: [PATCH 4/5] Fix null being set on save --- .../core/helper-plugin/lib/src/components/GenericInput/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js index daded2ea94..1f0fdb5b46 100644 --- a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js +++ b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js @@ -141,7 +141,7 @@ const GenericInput = ({ Date: Wed, 15 Feb 2023 18:30:28 +0530 Subject: [PATCH 5/5] Revert change disallowing null as a value --- .../core/helper-plugin/lib/src/components/GenericInput/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js index 1f0fdb5b46..daded2ea94 100644 --- a/packages/core/helper-plugin/lib/src/components/GenericInput/index.js +++ b/packages/core/helper-plugin/lib/src/components/GenericInput/index.js @@ -141,7 +141,7 @@ const GenericInput = ({