Skip to content

Commit aaf2ecd

Browse files
author
Simone Erba
committed
fix test
1 parent a214abe commit aaf2ecd

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

next/src/validation/json-logic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RulesLogic } from 'json-logic-js'
22
import type { ValidationError, ValidationErrorPath } from '../errors'
33
import type { JsfObjectSchema, JsfSchema, JsonLogicContext, JsonLogicRules, JsonLogicSchema, NonBooleanJsfSchema, ObjectValue, SchemaValue } from '../types'
4-
import jsonLogic from 'json-logic-js';
4+
import jsonLogic from 'json-logic-js'
55

66
/**
77
* Register user defined functions to be used in JSON Logic rules.

next/test/validation/json-logic-v0.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,20 @@ describe('jsonLogic: cross-values validations', () => {
452452

453453
describe('custom operators', () => {
454454
it('custom function', () => {
455-
const { handleValidation } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, validationOptions: { customJsonLogicOps: { is_hello: (a, b) => a === 'hello' && b === 'w' } } })
456-
const { formErrors } = handleValidation({ field_a: 'hello', field_b: 'w' })
455+
const { handleValidation } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, validationOptions: { customJsonLogicOps: { is_hello: a => a === 'hello world' } } })
456+
expect(handleValidation({ field_a: 'hello world' }).formErrors).toEqual(undefined)
457+
const { formErrors } = handleValidation({ field_a: 'wrong text' })
457458
expect(formErrors?.field_a).toEqual('Invalid hello world')
458-
expect(handleValidation({ field_a: 'hello', field_b: 'world' }).formErrors).toEqual(undefined)
459+
})
460+
461+
it('custom function are form specific', () => {
462+
const { handleValidation } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, validationOptions: { customJsonLogicOps: { is_hello: a => a === 'hello world' } } })
463+
expect(handleValidation({ field_a: 'hello world' }).formErrors).toEqual(undefined)
464+
const { formErrors } = handleValidation({ field_a: 'wrong text' })
465+
expect(formErrors?.field_a).toEqual('Invalid hello world')
466+
467+
const { handleValidation: handleValidation2 } = createHeadlessForm(schemaWithCustomValidationFunction, { strictInputType: false, validationOptions: { customJsonLogicOps: { is_hello: a => a === 'hello world!' } } })
468+
expect(handleValidation2({ field_a: 'hello world!' }).formErrors).toEqual(undefined)
459469
})
460470
})
461471
})

next/test/validation/json-logic.fixtures.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,16 +751,13 @@ export const schemaWithCustomValidationFunction = {
751751
'type': 'string',
752752
'x-jsf-logic-validations': ['hello_world'],
753753
},
754-
field_b: {
755-
type: 'string',
756-
},
757754
},
758755
'x-jsf-logic': {
759756
validations: {
760757
hello_world: {
761758
errorMessage: 'Invalid hello world',
762759
rule: {
763-
'!': { is_hello: [{ var: 'field_a' }, { var: 'field_b' }] },
760+
is_hello: { var: 'field_a' },
764761
},
765762
},
766763
},

0 commit comments

Comments
 (0)