Skip to content

Commit ff90548

Browse files
author
Simone Erba
committed
move code to new functions
1 parent 3c22c38 commit ff90548

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

next/src/form.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ function applyCustomErrorMessages(errors: ValidationErrorWithMessage[], schema:
193193
})
194194
}
195195

196+
/**
197+
* Register custom used defined JSON Logic operations to the jsonLogic instance
198+
* @param customJsonLogicOps - The custom JSON Logic operations
199+
*/
200+
function addCustomJsonLogicOperations(customJsonLogicOps: Record<string, (...args: any[]) => any> | undefined) {
201+
if (customJsonLogicOps) {
202+
for (const [name, func] of Object.entries(customJsonLogicOps)) {
203+
jsonLogic.add_operation(name, func)
204+
}
205+
}
206+
}
207+
208+
/**
209+
* Remove custom JSON Logic operations from the jsonLogic instance
210+
* @param customJsonLogicOps - The custom JSON Logic operations
211+
*/
212+
function removeJsonLogicCustomOperations(customJsonLogicOps: Record<string, (...args: any[]) => any> | undefined) {
213+
if (customJsonLogicOps) {
214+
for (const name of Object.keys(customJsonLogicOps)) {
215+
jsonLogic.rm_operation(name)
216+
}
217+
}
218+
}
219+
196220
/**
197221
* Validate a value against a schema
198222
* @param value - The value to validate
@@ -252,15 +276,7 @@ function validateOptions(options: CreateHeadlessFormOptions) {
252276
if (Object.prototype.hasOwnProperty.call(options, 'modifyConfig')) {
253277
throw new Error('`modifyConfig` is a deprecated option and it\'s not supported on json-schema-form v1')
254278
}
255-
}
256279

257-
export function createHeadlessForm(
258-
schema: JsfObjectSchema,
259-
options: CreateHeadlessFormOptions = {},
260-
): FormResult {
261-
validateOptions(options)
262-
const initialValues = options.initialValues || {}
263-
const strictInputType = options.strictInputType || false
264280
const customJsonLogicOps = options?.validationOptions?.customJsonLogicOps
265281

266282
if (customJsonLogicOps) {
@@ -276,6 +292,16 @@ export function createHeadlessForm(
276292
}
277293
}
278294
}
295+
}
296+
297+
export function createHeadlessForm(
298+
schema: JsfObjectSchema,
299+
options: CreateHeadlessFormOptions = {},
300+
): FormResult {
301+
validateOptions(options)
302+
const initialValues = options.initialValues || {}
303+
const strictInputType = options.strictInputType || false
304+
279305
// 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)
280306
const updatedSchema = calculateFinalSchema({
281307
schema,
@@ -287,16 +313,11 @@ export function createHeadlessForm(
287313

288314
// TODO: check if we need this isError variable exposed
289315
const isError = false
316+
const customJsonLogicOps = options?.validationOptions?.customJsonLogicOps
290317

291318
const handleValidation = (value: SchemaValue) => {
292-
const customJsonLogicOps = options?.validationOptions?.customJsonLogicOps
293-
294319
try {
295-
if (customJsonLogicOps) {
296-
for (const [name, func] of Object.entries(customJsonLogicOps)) {
297-
jsonLogic.add_operation(name, func)
298-
}
299-
}
320+
addCustomJsonLogicOperations(customJsonLogicOps)
300321

301322
const updatedSchema = calculateFinalSchema({
302323
schema,
@@ -311,11 +332,7 @@ export function createHeadlessForm(
311332
return result
312333
}
313334
finally {
314-
if (customJsonLogicOps) {
315-
for (const name of Object.keys(customJsonLogicOps)) {
316-
jsonLogic.rm_operation(name)
317-
}
318-
}
335+
removeJsonLogicCustomOperations(customJsonLogicOps)
319336
}
320337
}
321338

next/src/validation/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export interface ValidationOptions {
2727
* @default false
2828
*/
2929
allowForbiddenValues?: boolean
30+
31+
/**
32+
* Custom jsonLogic operations to register (only applies once at setup)
33+
* Format: { [operationName]: (...args: any[]) => any }
34+
*/
35+
customJsonLogicOps?: Record<string, (...args: any[]) => any>
3036
}
3137

3238
/**

0 commit comments

Comments
 (0)