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
1 change: 1 addition & 0 deletions next/json-schema-test-suite
Submodule json-schema-test-suite added at e52450
46 changes: 37 additions & 9 deletions src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { LegacyOptions } from './validation/schema'
import { getErrorMessage } from './errors/messages'
import { buildFieldSchema } from './field/schema'
import { calculateFinalSchema, updateFieldProperties } from './mutations'
import { addCustomJsonLogicOperations, removeCustomJsonLogicOperations } from './validation/json-logic'
import { validateSchema } from './validation/schema'

export { LegacyOptions } from './validation/schema'
Expand Down Expand Up @@ -228,6 +229,11 @@ export interface CreateHeadlessFormOptions {
* @default false
*/
strictInputType?: boolean

/**
* Custom user defined functions. A dictionary of name and function
*/
customJsonLogicOps?: Record<string, (...args: any[]) => any>
}

function buildFields(params: { schema: JsfObjectSchema, originalSchema: JsfObjectSchema, strictInputType?: boolean }): Field[] {
Expand All @@ -251,6 +257,20 @@ function validateOptions(options: CreateHeadlessFormOptions) {
if (Object.prototype.hasOwnProperty.call(options, 'customProperties')) {
console.error('[json-schema-form] `customProperties` is a deprecated option and it\'s not supported on json-schema-form v1')
}

if (options.customJsonLogicOps) {
if (typeof options.customJsonLogicOps !== 'object' || options.customJsonLogicOps === null) {
throw new TypeError('validationOptions.customJsonLogicOps must be an object.')
}

for (const [name, func] of Object.entries(options.customJsonLogicOps)) {
if (typeof func !== 'function') {
throw new TypeError(
`Custom JSON Logic operator '${name}' must be a function, but received type '${typeof func}'.`,
)
}
}
}
}

export function createHeadlessForm(
Expand All @@ -273,18 +293,26 @@ export function createHeadlessForm(
const isError = false

const handleValidation = (value: SchemaValue) => {
const updatedSchema = calculateFinalSchema({
schema,
values: value,
options: options.legacyOptions,
})
const customJsonLogicOps = options?.customJsonLogicOps

try {
addCustomJsonLogicOperations(customJsonLogicOps)

const result = validate(value, updatedSchema, options.legacyOptions)
const updatedSchema = calculateFinalSchema({
schema,
values: value,
options: options.legacyOptions,
})

// Fields properties might have changed, so we need to reset the fields by updating them in place
updateFieldProperties(fields, updatedSchema, schema)
const result = validate(value, updatedSchema, options.legacyOptions)

return result
updateFieldProperties(fields, updatedSchema, schema)

return result
}
finally {
removeCustomJsonLogicOperations(customJsonLogicOps)
}
}

return {
Expand Down
16 changes: 16 additions & 0 deletions src/validation/json-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,19 @@ function cycleThroughAttrsAndApplyValues(propertySchema: JsfSchema, computedValu
}
}
}

export function addCustomJsonLogicOperations(ops?: Record<string, (...args: any[]) => any>) {
if (ops) {
for (const [name, func] of Object.entries(ops)) {
jsonLogic.add_operation(name, func)
}
}
}

export function removeCustomJsonLogicOperations(ops?: Record<string, (...args: any[]) => any>) {
if (ops) {
for (const name of Object.keys(ops)) {
jsonLogic.rm_operation(name)
}
}
}
35 changes: 35 additions & 0 deletions test/validation/json-logic-v0.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
schemaWithComputedAttributeThatDoesntExist,
schemaWithComputedAttributeThatDoesntExistDescription,
schemaWithComputedAttributeThatDoesntExistTitle,
schemaWithCustomValidationFunction,
schemaWithDeepVarThatDoesNotExist,
schemaWithDeepVarThatDoesNotExistOnFieldset,
schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
Expand Down Expand Up @@ -446,4 +447,38 @@ describe('jsonLogic: cross-values validations', () => {
expect(handleValidation({ field_a: 20, field_b: 41 }).formErrors).toEqual()
})
})

describe('custom operators', () => {
it('custom function', () => {
const { handleValidation } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, customJsonLogicOps: { is_hello: a => a === 'hello world' } })
expect(handleValidation({ field_a: 'hello world' }).formErrors).toEqual(undefined)
const { formErrors } = handleValidation({ field_a: 'wrong text' })
expect(formErrors?.field_a).toEqual('Invalid hello world')
})

it('custom function are form specific', () => {
const { handleValidation } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, customJsonLogicOps: { is_hello: a => a === 'hello world' } })
expect(handleValidation({ field_a: 'hello world' }).formErrors).toEqual(undefined)
const { formErrors } = handleValidation({ field_a: 'wrong text' })
expect(formErrors?.field_a).toEqual('Invalid hello world')

const { handleValidation: handleValidation2 } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, customJsonLogicOps: { is_hello: a => a === 'hello world!' } })
expect(handleValidation2({ field_a: 'hello world!' }).formErrors).toEqual(undefined)

const { handleValidation: handleValidation3 } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false })
const actionThatWillThrow = () => {
handleValidation3({ field_a: 'hello world' })
}

expect(actionThatWillThrow).toThrow('Unrecognized operation is_hello')
})

it('validation on custom functions', () => {
const actionThatWillThrow = () => {
createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, customJsonLogicOps: { is_hello: 'not a funcion' } })
}

expect(actionThatWillThrow).toThrow('Custom JSON Logic operator \'is_hello\' must be a function, but received type \'string\'.')
})
})
})
19 changes: 19 additions & 0 deletions test/validation/json-logic.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,22 @@ export const schemaWithReduceAccumulator = {
},
},
}

export const schemaWithCustomValidationFunction = {
'properties': {
field_a: {
'type': 'string',
'x-jsf-logic-validations': ['hello_world'],
},
},
'x-jsf-logic': {
validations: {
hello_world: {
errorMessage: 'Invalid hello world',
rule: {
is_hello: { var: 'field_a' },
},
},
},
},
}
Loading