Skip to content

Commit 2dbdd82

Browse files
fix(schema): keep custom functions on nested fields (#232)
* fix(schema): fullPath not being constructed properly for nested fields * fix(schema): functions in modify config get lost during validation * test: add unit test to assert deep nested field modifications * fix: move assert to the correct test file * test: update unit test description Co-authored-by: Sandrina Pereira <[email protected]> --------- Co-authored-by: Sandrina Pereira <[email protected]>
1 parent aebb39e commit 2dbdd82

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/modify-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ interface ModifyResult {
5151
* shortToFullPath('foo.bar') // 'foo.properties.bar'
5252
*/
5353
function shortToFullPath(path: string) {
54-
return path.replace('.', '.properties.')
54+
return path.replaceAll('.', '.properties.')
5555
}
5656

5757
/**

src/mutations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,11 @@ export function updateFieldProperties(fields: Field[], schema: JsfObjectSchema,
181181
deepMergeSchemas(field, newField)
182182

183183
const fieldSchema = schema.properties?.[field.name]
184+
const originalFieldSchema = originalSchema.properties?.[field.name]
184185

185186
if (fieldSchema && typeof fieldSchema === 'object') {
186187
if (field.fields && fieldSchema.type === 'object') {
187-
updateFieldProperties(field.fields, fieldSchema as JsfObjectSchema, originalSchema)
188+
updateFieldProperties(field.fields, fieldSchema as JsfObjectSchema, originalFieldSchema as JsfObjectSchema)
188189
}
189190
}
190191
}

test/fields/mutation.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,32 @@ describe('field mutation', () => {
439439
hint: 'Optional contact method',
440440
})
441441
})
442+
443+
it('should be able to preserve x-jsf-presentation properties in a nested field', () => {
444+
const customComponent = () => {
445+
return null
446+
}
447+
const schema: JsfObjectSchema = {
448+
type: 'object',
449+
properties: {
450+
apartment: {
451+
type: 'object',
452+
properties: {
453+
number: {
454+
'type': 'string',
455+
'x-jsf-presentation': {
456+
Component: customComponent,
457+
},
458+
},
459+
},
460+
},
461+
},
462+
}
463+
const form = createHeadlessForm(schema)
464+
form.handleValidation({ formType: 'advanced' })
465+
466+
expect(getField(form.fields, 'apartment', 'number')).toHaveProperty('Component', customComponent)
467+
})
442468
})
443469

444470
it('correctly updates required on fields', () => {

test/modify-schema.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ describe('modifySchema', () => {
107107
city: {
108108
title: 'City',
109109
},
110+
apartment: {
111+
title: 'House',
112+
properties: {
113+
floor: {
114+
title: 'Floor',
115+
},
116+
number: {
117+
title: 'Number',
118+
},
119+
},
120+
},
110121
},
111122
},
112123
},
@@ -237,6 +248,10 @@ describe('modifySchema', () => {
237248
'address.city': () => ({
238249
title: 'City name',
239250
}),
251+
// should be able to handle deep nested fields
252+
'address.apartment.number': {
253+
title: 'Apartment Number',
254+
},
240255
// Or pass the native object
241256
'address': (fieldAttrs) => {
242257
return {
@@ -262,11 +277,21 @@ describe('modifySchema', () => {
262277
},
263278
number: {
264279
'title': 'Door Number',
265-
'x-test-siblings': ['street', 'number', 'city'],
280+
'x-test-siblings': ['street', 'number', 'city', 'apartment'],
266281
},
267282
city: {
268283
title: 'City name',
269284
},
285+
apartment: {
286+
properties: {
287+
floor: {
288+
title: 'Floor',
289+
},
290+
number: {
291+
title: 'Apartment Number',
292+
},
293+
},
294+
},
270295
},
271296
},
272297
},

0 commit comments

Comments
 (0)