Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/field/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,12 @@ export function buildFieldSchema({
return null
}

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

// Get input type from presentation or fallback to schema type
Expand Down
4 changes: 2 additions & 2 deletions src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ function buildFields(params: { schema: JsfObjectSchema, originalSchema: JsfObjec
/**
* Ensures that no forbidden options are given
* @param options - The options to validate
* @throws An error if any forbidden options are found
* Alerts to the console that the option is deprecated and not being considered
*/
function validateOptions(options: CreateHeadlessFormOptions) {
if (Object.prototype.hasOwnProperty.call(options, 'customProperties')) {
throw new Error('`customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
console.error('[json-schema-form] `customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
}
}

Expand Down
1 change: 1 addition & 0 deletions src/validation/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function deepEqual(a: SchemaValue, b: SchemaValue): boolean {

/**
* Deep clones an object using structuredClone if available, otherwise falls back to JSON.parse/stringify approach.
* Please note: some properties might not be serializable (e.g. functions), so they will be lost in the clone.
*
* @param obj - The object to clone
* @returns deep clone of the original object
Expand Down
7 changes: 6 additions & 1 deletion test/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ describe('fields', () => {
})
})

it('should handle custom x-jsf-presentation properties', () => {
it('should handle custom x-jsf-presentation properties, including functions', () => {
const customComponent = () => {
return null
}
const schema: JsfSchema = {
type: 'object',
properties: {
Expand All @@ -174,6 +177,7 @@ describe('fields', () => {
inputType: 'file',
accept: '.pdf,.doc',
maxFileSize: 5000000,
Component: customComponent,
},
},
},
Expand All @@ -192,6 +196,7 @@ describe('fields', () => {
required: false,
accept: '.pdf,.doc',
maxFileSize: 5000000,
Component: customComponent,
},
])
})
Expand Down
2 changes: 2 additions & 0 deletions test/fields/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ describe('buildFieldArray', () => {
isVisible: true,
nameKey: 'title',
required: false,
foo: 'bar',
bar: 'baz',
},
],
items: expect.any(Object),
Expand Down
9 changes: 5 additions & 4 deletions test/form.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JsfObjectSchema } from '../src/types'
import { describe, expect, it } from '@jest/globals'
import { describe, expect, it, jest } from '@jest/globals'
import { createHeadlessForm } from '../src'

describe('createHeadlessForm', () => {
Expand All @@ -16,9 +16,10 @@ describe('createHeadlessForm', () => {
}

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

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