Skip to content

Commit 5be36cd

Browse files
lukadantoniocapelo
andauthored
chore(next): throw error when deprecated modifyConfig option is passed to createHeadlessForm (#199)
* chore(next): throw error when deprecated modifyConfig option is passed to createHeadlessForm * Update next/src/form.ts Co-authored-by: Capelo <[email protected]> * update test text --------- Co-authored-by: Capelo <[email protected]>
1 parent 8366bf7 commit 5be36cd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

next/src/form.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,22 @@ function buildFields(params: { schema: JsfObjectSchema, originalSchema: JsfObjec
242242
return fields
243243
}
244244

245+
/**
246+
* Ensures that no forbidden options are given
247+
* @param options - The options to validate
248+
* @throws An error if any forbidden options are found
249+
*/
250+
function validateOptions(options: CreateHeadlessFormOptions) {
251+
if (Object.prototype.hasOwnProperty.call(options, 'modifyConfig')) {
252+
throw new Error('`modifyConfig` is a deprecated option and it\'s not supported on json-schema-form v1')
253+
}
254+
}
255+
245256
export function createHeadlessForm(
246257
schema: JsfObjectSchema,
247258
options: CreateHeadlessFormOptions = {},
248259
): FormResult {
260+
validateOptions(options)
249261
const initialValues = options.initialValues || {}
250262
const strictInputType = options.strictInputType || false
251263
// Make a new version of the schema with all the computed attrs applied, as well as the final version of each property (taking into account conditional rules)

next/test/form.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1+
import type { JsfObjectSchema } from '../src/types'
12
import { describe, expect, it } from '@jest/globals'
23
import { createHeadlessForm } from '../src'
34

45
describe('createHeadlessForm', () => {
56
it('should be a function', () => {
67
expect(createHeadlessForm).toBeInstanceOf(Function)
78
})
9+
10+
describe('options validation', () => {
11+
const basicSchema: JsfObjectSchema = {
12+
type: 'object',
13+
properties: {
14+
name: { type: 'string' },
15+
},
16+
}
17+
18+
it('should throw error when modifyConfig option is provided', () => {
19+
expect(() => {
20+
createHeadlessForm(basicSchema, { modifyConfig: {} } as any)
21+
}).toThrow('`modifyConfig` is a deprecated option and it\'s not supported on json-schema-form v1')
22+
})
23+
24+
it('should not throw error when modifyConfig option is not provided', () => {
25+
expect(() => {
26+
createHeadlessForm(basicSchema, {})
27+
}).not.toThrow()
28+
})
29+
30+
it('should not throw error when other valid options are provided', () => {
31+
expect(() => {
32+
createHeadlessForm(basicSchema, {
33+
initialValues: { name: 'test' },
34+
validationOptions: {},
35+
strictInputType: true,
36+
})
37+
}).not.toThrow()
38+
})
39+
})
840
})

0 commit comments

Comments
 (0)