Skip to content

Commit a2e8f4d

Browse files
fix: properly handle function as custom field properties (#213)
* fix: properly handle function as custom field properties * fix tests
1 parent 3cd3c3c commit a2e8f4d

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

src/field/schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,12 @@ export function buildFieldSchema({
373373
return null
374374
}
375375

376-
const presentation = schema['x-jsf-presentation'] || {}
376+
const presentation = {
377+
// We need to use the original schema's presentation as a basis as there are some properties that can't be
378+
// serialized (so they were not cloned)
379+
...(originalSchema['x-jsf-presentation'] || {}),
380+
...(schema['x-jsf-presentation'] || {}),
381+
}
377382
const errorMessage = schema['x-jsf-errorMessage']
378383

379384
// Get input type from presentation or fallback to schema type

src/form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ function buildFields(params: { schema: JsfObjectSchema, originalSchema: JsfObjec
245245
/**
246246
* Ensures that no forbidden options are given
247247
* @param options - The options to validate
248-
* @throws An error if any forbidden options are found
248+
* Alerts to the console that the option is deprecated and not being considered
249249
*/
250250
function validateOptions(options: CreateHeadlessFormOptions) {
251251
if (Object.prototype.hasOwnProperty.call(options, 'customProperties')) {
252-
throw new Error('`customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
252+
console.error('[json-schema-form] `customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
253253
}
254254
}
255255

src/validation/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function deepEqual(a: SchemaValue, b: SchemaValue): boolean {
7171

7272
/**
7373
* Deep clones an object using structuredClone if available, otherwise falls back to JSON.parse/stringify approach.
74+
* Please note: some properties might not be serializable (e.g. functions), so they will be lost in the clone.
7475
*
7576
* @param obj - The object to clone
7677
* @returns deep clone of the original object

test/fields.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ describe('fields', () => {
163163
})
164164
})
165165

166-
it('should handle custom x-jsf-presentation properties', () => {
166+
it('should handle custom x-jsf-presentation properties, including functions', () => {
167+
const customComponent = () => {
168+
return null
169+
}
167170
const schema: JsfSchema = {
168171
type: 'object',
169172
properties: {
@@ -174,6 +177,7 @@ describe('fields', () => {
174177
inputType: 'file',
175178
accept: '.pdf,.doc',
176179
maxFileSize: 5000000,
180+
Component: customComponent,
177181
},
178182
},
179183
},
@@ -192,6 +196,7 @@ describe('fields', () => {
192196
required: false,
193197
accept: '.pdf,.doc',
194198
maxFileSize: 5000000,
199+
Component: customComponent,
195200
},
196201
])
197202
})

test/fields/array.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ describe('buildFieldArray', () => {
217217
isVisible: true,
218218
nameKey: 'title',
219219
required: false,
220+
foo: 'bar',
221+
bar: 'baz',
220222
},
221223
],
222224
items: expect.any(Object),

test/form.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JsfObjectSchema } from '../src/types'
2-
import { describe, expect, it } from '@jest/globals'
2+
import { describe, expect, it, jest } from '@jest/globals'
33
import { createHeadlessForm } from '../src'
44

55
describe('createHeadlessForm', () => {
@@ -16,9 +16,10 @@ describe('createHeadlessForm', () => {
1616
}
1717

1818
it('should throw error when customProperties option is provided', () => {
19-
expect(() => {
20-
createHeadlessForm(basicSchema, { customProperties: {} } as any)
21-
}).toThrow('`customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
19+
// spy on console.error
20+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
21+
createHeadlessForm(basicSchema, { customProperties: {} } as any)
22+
expect(consoleErrorSpy).toHaveBeenCalledWith('[json-schema-form] `customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
2223
})
2324

2425
it('should not throw error when modifyConfig option is not provided', () => {

0 commit comments

Comments
 (0)