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
12 changes: 12 additions & 0 deletions next/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,22 @@ function buildFields(params: { schema: JsfObjectSchema, originalSchema: JsfObjec
return fields
}

/**
* Ensures that no forbidden options are given
* @param options - The options to validate
* @throws An error if any forbidden options are found
*/
function validateOptions(options: CreateHeadlessFormOptions) {
if (Object.prototype.hasOwnProperty.call(options, 'modifyConfig')) {
throw new Error('`modifyConfig` is a deprecated option and it\'s not supported on json-schema-form v1')
}
}

export function createHeadlessForm(
schema: JsfObjectSchema,
options: CreateHeadlessFormOptions = {},
): FormResult {
validateOptions(options)
const initialValues = options.initialValues || {}
const strictInputType = options.strictInputType || false
// 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)
Expand Down
32 changes: 32 additions & 0 deletions next/test/form.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
import type { JsfObjectSchema } from '../src/types'
import { describe, expect, it } from '@jest/globals'
import { createHeadlessForm } from '../src'

describe('createHeadlessForm', () => {
it('should be a function', () => {
expect(createHeadlessForm).toBeInstanceOf(Function)
})

describe('options validation', () => {
const basicSchema: JsfObjectSchema = {
type: 'object',
properties: {
name: { type: 'string' },
},
}

it('should throw error when modifyConfig option is provided', () => {
expect(() => {
createHeadlessForm(basicSchema, { modifyConfig: {} } as any)
}).toThrow('`modifyConfig` 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', () => {
expect(() => {
createHeadlessForm(basicSchema, {})
}).not.toThrow()
})

it('should not throw error when other valid options are provided', () => {
expect(() => {
createHeadlessForm(basicSchema, {
initialValues: { name: 'test' },
validationOptions: {},
strictInputType: true,
})
}).not.toThrow()
})
})
})