Skip to content

Commit 3f0ebc0

Browse files
committed
fix(renderer): Check if the field has name set, not title or key
1 parent 4b30bad commit 3f0ebc0

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

packages/react-form-renderer/src/demo-schemas/miq-schemas/output.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const output = {
1010
{
1111
title: 'Tab 1',
1212
description: 'Text boxes and text areas',
13-
key: '553',
13+
name: '553',
1414
fields: [
1515
{
1616
title: 'Text boxes',
17-
key: '637',
17+
name: '637',
1818
fields: [
1919
{
2020
name: 'text_box_1',
@@ -91,7 +91,7 @@ const output = {
9191
},
9292
{
9393
title: 'Text areas',
94-
key: '638',
94+
name: '638',
9595
fields: [
9696
{
9797
name: 'textarea_box_1',
@@ -108,11 +108,11 @@ const output = {
108108
{
109109
title: 'Tab 2',
110110
description: 'Checks',
111-
key: '554',
111+
name: '554',
112112
fields: [
113113
{
114114
title: 'Check boxes',
115-
key: '639',
115+
name: '639',
116116
fields: [
117117
{
118118
name: 'check_box_1',
@@ -131,7 +131,7 @@ const output = {
131131
},
132132
{
133133
title: 'Radios',
134-
key: '640',
134+
name: '640',
135135
fields: [
136136
{
137137
name: 'radio_button_1',
@@ -205,11 +205,11 @@ const output = {
205205
{
206206
title: 'Tab 3',
207207
description: '',
208-
key: '555',
208+
name: '555',
209209
fields: [
210210
{
211211
title: 'Dropdowns',
212-
key: '641',
212+
name: '641',
213213
fields: [
214214
{
215215
name: 'dropdown_list_1',
@@ -321,11 +321,11 @@ const output = {
321321
{
322322
title: 'Tab 4',
323323
description: '',
324-
key: '556',
324+
name: '556',
325325
fields: [
326326
{
327327
title: 'Datepickers',
328-
key: '642',
328+
name: '642',
329329
fields: [
330330
{
331331
name: 'date_control_1',
@@ -344,7 +344,7 @@ const output = {
344344
},
345345
{
346346
title: 'Timepickers',
347-
key: '643',
347+
name: '643',
348348
fields: [
349349
{
350350
name: 'date_time_control_1',
@@ -367,11 +367,11 @@ const output = {
367367
{
368368
title: 'Mixed',
369369
description: '',
370-
key: '558',
370+
name: '558',
371371
fields: [
372372
{
373373
title: 'New Section',
374-
key: '645',
374+
name: '645',
375375
fields: [
376376
{
377377
name: 'text_box_10',
@@ -457,7 +457,7 @@ const output = {
457457
},
458458
],
459459
component: components.TABS,
460-
key: '57',
460+
name: '57',
461461
},
462462
],
463463
};

packages/react-form-renderer/src/parsers/default-schema-validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ const iterateOverFields = (fields, formFieldsMapper, layoutMapper, parent = {})
149149

150150
}
151151

152-
if (!field.hasOwnProperty('name') && !field.hasOwnProperty('title') && !field.hasOwnProperty('key') && parent.component !== 'field-array') {
153-
throw new DefaultSchemaError(`Each fields item must have "name" or "key" property! Name is used as a unique identifier of form fields.`);
152+
if (!field.hasOwnProperty('name') && parent.component !== 'field-array') {
153+
throw new DefaultSchemaError(`Each fields item must have "name" property! Name is used as a unique identifier of form fields.`);
154154
}
155155

156156
if (field.hasOwnProperty('condition')) {

packages/react-form-renderer/src/tests/parsers/__snapshots__/default-schema-validator.test.js.snap

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ exports[`Default schema validator should fail if field condition when property i
5454

5555
exports[`Default schema validator should fail if field item does not have component property 1`] = `"Each fields item must have \\"component\\" property!"`;
5656

57-
exports[`Default schema validator should fail if field item does not have name property 1`] = `"Each fields item must have \\"name\\" or \\"key\\" property! Name is used as a unique identifier of form fields."`;
57+
exports[`Default schema validator should fail if field item does not have name property 1`] = `"Each fields item must have \\"name\\" property! Name is used as a unique identifier of form fields."`;
58+
59+
exports[`Default schema validator should fail if field item does not have name property but have key 1`] = `"Each fields item must have \\"name\\" property! Name is used as a unique identifier of form fields."`;
5860

5961
exports[`Default schema validator should fail if field validate is not an array. 1`] = `
6062
"
@@ -87,9 +89,9 @@ exports[`Default schema validator should fail if field validate item is not an o
8789

8890
exports[`Default schema validator should fail if input is not a object 1`] = `"Form Schema must be an object, received array!"`;
8991

90-
exports[`Default schema validator should fail if input object does fields key that is not array 1`] = `"Component of type schema must contain \\"fields\\" property of type array, received type: object!"`;
92+
exports[`Default schema validator should fail if input object does fields names that is not array 1`] = `"Component of type schema must contain \\"fields\\" property of type array, received type: object!"`;
9193

92-
exports[`Default schema validator should fail if input object does not have fields key 1`] = `"Component of type schema must contain \\"fields\\" property of type array, received undefined!"`;
94+
exports[`Default schema validator should fail if input object does not have fields names 1`] = `"Component of type schema must contain \\"fields\\" property of type array, received undefined!"`;
9395

9496
exports[`Default schema validator should fail validation when using wrong data type 1`] = `
9597
"

packages/react-form-renderer/src/tests/parsers/default-schema-validator.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ describe('Default schema validator', () => {
1616
expect(() => defaultSchemaValidator([])).toThrowErrorMatchingSnapshot();
1717
});
1818

19-
it('should fail if input object does not have fields key', () => {
19+
it('should fail if input object does not have fields names', () => {
2020
expect(() => defaultSchemaValidator({})).toThrowErrorMatchingSnapshot();
2121
});
2222

23-
it('should fail if input object does fields key that is not array', () => {
23+
it('should fail if input object does fields names that is not array', () => {
2424
expect(() => defaultSchemaValidator({ fields: {}})).toThrowErrorMatchingSnapshot();
2525
});
2626

@@ -34,6 +34,12 @@ describe('Default schema validator', () => {
3434
}]}, formFieldsMapper)).toThrowErrorMatchingSnapshot();
3535
});
3636

37+
it('should fail if field item does not have name property but have key', () => {
38+
expect(() => defaultSchemaValidator({ fields: [{
39+
component: 'foo', key: 'some key',
40+
}]}, formFieldsMapper)).toThrowErrorMatchingSnapshot();
41+
});
42+
3743
it('should fail if field component property is not in form fields mapper.', () => {
3844
expect(() => defaultSchemaValidator({ fields: [{
3945
component: 'blarghs',

0 commit comments

Comments
 (0)