forked from remoteoss/json-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-logic-v0.test.js
More file actions
484 lines (450 loc) · 21.2 KB
/
Copy pathjson-logic-v0.test.js
File metadata and controls
484 lines (450 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import { createHeadlessForm } from '@/createHeadlessForm'
import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'
import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from '../test-utils'
import {
badSchemaThatWillNotSetAForcedValue,
createSchemaWithRulesOnFieldA,
createSchemaWithThreePropertiesWithRuleOnFieldA,
multiRuleSchema,
schemaValidationForMaximumAndMinimumValues,
schemaValidationForMaximumAndMinimumValuesWithDynamicErrorMessage,
schemaWhereValidationAndComputedValueIsAppliedOnNormalThenStatement,
schemaWithBadOperation,
schemaWithComputedAttributes,
schemaWithComputedAttributesAndErrorMessages,
schemaWithComputedAttributeThatDoesntExist,
schemaWithComputedAttributeThatDoesntExistDescription,
schemaWithComputedAttributeThatDoesntExistTitle,
schemaWithCustomValidationFunction,
schemaWithDeepVarThatDoesNotExist,
schemaWithDeepVarThatDoesNotExistOnFieldset,
schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
schemaWithMissingComputedValue,
schemaWithMissingRule,
schemaWithNativeAndJSONLogicChecks,
schemaWithNonRequiredField,
schemaWithPropertyThatDoesNotExistInThatLevelButDoesInFieldset,
schemaWithReduceAccumulator,
schemaWithTwoRules,
schemaWithTwoValidationsWhereOneOfThemIsAppliedConditionally,
schemaWithUnknownVariableInComputedValues,
schemaWithUnknownVariableInValidations,
schemaWithValidationThatDoesNotExistOnProperty,
} from './json-logic.fixtures'
beforeEach(mockConsole)
afterEach(restoreConsoleAndEnsureItWasNotCalled)
describe('jsonLogic: cross-values validations', () => {
describe('does not conflict with native JSON schema', () => {
it('given an optional field and empty value, jsonLogic validations are ignored', () => {
const { handleValidation } = createHeadlessForm(schemaWithNonRequiredField, {
strictInputType: false,
})
expect(handleValidation({}).formErrors).toBeUndefined()
expect(handleValidation({ field_a: 0, field_b: 10 }).formErrors).toEqual({
field_b: 'Must be greater than field_a',
})
expect(handleValidation({ field_a: 'incorrect value' }).formErrors).toEqual({
field_a: 'The value must be a number',
})
expect(handleValidation({ field_a: 11 }).formErrors).toBeUndefined()
})
it('native validations have higher precedence than jsonLogic validations', () => {
const { handleValidation } = createHeadlessForm(schemaWithNativeAndJSONLogicChecks, {
strictInputType: false,
})
expect(handleValidation({}).formErrors).toEqual({ field_a: 'Required field' })
expect(handleValidation({ field_a: 0 }).formErrors).toEqual({
field_a: 'Must be greater or equal to 100',
})
expect(handleValidation({ field_a: 101 }).formErrors).toEqual({
field_a: 'Must be a multiple of 10',
})
expect(handleValidation({ field_a: 110 }).formErrors).toBeUndefined()
})
})
describe('relative: <, >, =', () => {
it('bigger: field_a > field_b', () => {
const schema = createSchemaWithRulesOnFieldA({
a_greater_than_b: {
errorMessage: 'Field A must be bigger than field B',
rule: { '>': [{ var: 'field_a' }, { var: 'field_b' }] },
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
const { formErrors } = handleValidation({ field_a: 1, field_b: 2 })
expect(formErrors.field_a).toEqual('Field A must be bigger than field B')
expect(handleValidation({ field_a: 2, field_b: 0 }).formErrors).toEqual(undefined)
})
it('smaller: field_a < field_b', () => {
const schema = createSchemaWithRulesOnFieldA({
a_less_than_b: {
errorMessage: 'Field A must be smaller than field B',
rule: { '<': [{ var: 'field_a' }, { var: 'field_b' }] },
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
const { formErrors } = handleValidation({ field_a: 2, field_b: 2 })
expect(formErrors.field_a).toEqual('Field A must be smaller than field B')
expect(handleValidation({ field_a: 0, field_b: 2 }).formErrors).toEqual(undefined)
})
it('equal: field_a = field_b', () => {
const schema = createSchemaWithRulesOnFieldA({
a_equals_b: {
errorMessage: 'Field A must equal field B',
rule: { '==': [{ var: 'field_a' }, { var: 'field_b' }] },
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
const { formErrors } = handleValidation({ field_a: 3, field_b: 2 })
expect(formErrors.field_a).toEqual('Field A must equal field B')
expect(handleValidation({ field_a: 2, field_b: 2 }).formErrors).toEqual(undefined)
})
})
// TODO: These suites are skipped for now because v1 does not throw as many errors as v0.
describe.skip('incorrectly written schemas', () => {
afterEach(() => console.error.mockClear())
const cases = [
[
'x-jsf-logic.validations: throw when theres a missing rule',
schemaWithMissingRule,
'[json-schema-form] json-logic error: Validation "a_greater_than_ten" has missing rule.',
],
[
'x-jsf-logic.validations: throw when theres a value that does not exist in a rule',
schemaWithUnknownVariableInValidations,
'[json-schema-form] json-logic error: rule "a_equals_ten" has no variable "field_a".',
],
[
'x-jsf-logic.computedValues: throw when theres a value that does not exist in a rule',
schemaWithUnknownVariableInComputedValues,
'[json-schema-form] json-logic error: rule "a_times_ten" has no variable "field_a".',
],
[
'x-jsf-logic.computedValues: throw when theres a missing computed value',
schemaWithMissingComputedValue,
'[json-schema-form] json-logic error: Computed value "a_plus_ten" has missing rule.',
],
[
'x-jsf-logic-computedAttrs: error if theres a value that does not exist on an attribute.',
schemaWithComputedAttributeThatDoesntExist,
`[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
],
[
'x-jsf-logic-computedAttrs: error if theres a value that does not exist on a template string (title).',
schemaWithComputedAttributeThatDoesntExistTitle,
`[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
],
[
'x-jsf-logic-computedAttrs: error if theres a value that does not exist on a template string (description).',
schemaWithComputedAttributeThatDoesntExistDescription,
`[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
],
[
'x-jsf-logic-computedAttrs:, error if theres a value referenced that does not exist on an inline rule.',
schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
`[json-schema-form] json-logic error: fieldName "IdontExist" doesn't exist in field "field_a.x-jsf-logic-computedAttrs.title".`,
],
[
'x-jsf-logic.validations: error if a field does not exist in a deeply nested rule',
schemaWithDeepVarThatDoesNotExist,
'[json-schema-form] json-logic error: rule "dummy_rule" has no variable "field_b".',
],
[
'x-jsf-logic.validations: error if rule does not exist on a fieldset property',
schemaWithDeepVarThatDoesNotExistOnFieldset,
'[json-schema-form] json-logic error: rule "dummy_rule" has no variable "field_a".',
],
[
'x-jsf-validations: error if a validation name does not exist',
schemaWithValidationThatDoesNotExistOnProperty,
`[json-schema-form] json-logic error: "field_a" required validation "iDontExist" doesn't exist.`,
],
[
'x-jsf-logic.validations: A top level logic keyword will not be able to reference fieldset properties',
schemaWithPropertyThatDoesNotExistInThatLevelButDoesInFieldset,
'[json-schema-form] json-logic error: rule "validation_parent" has no variable "child".',
],
[
'x-jsf-logic.validations: error if unknown operation',
schemaWithBadOperation,
'[json-schema-form] json-logic error: in "badOperator" rule there is an unknown operator "++".',
],
]
it.each(cases)('%p', (_, schema, expectedErrorString) => {
const { error } = createHeadlessForm(schema, { strictInputType: false })
const expectedError = new Error(expectedErrorString)
expect(console.error).toHaveBeenCalledWith('JSON Schema invalid!', expectedError)
expect(error).toEqual(expectedError)
})
})
describe('arithmetic: +, -, *, /', () => {
it('multiple: field_a > field_b * 2', () => {
const schema = createSchemaWithRulesOnFieldA({
a_greater_than_b_multiplied_by_2: {
errorMessage: 'Field A must be at least twice as big as field b',
rule: { '>': [{ var: 'field_a' }, { '*': [{ var: 'field_b' }, 2] }] },
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
const { formErrors } = handleValidation({ field_a: 1, field_b: 4 })
expect(formErrors.field_a).toEqual('Field A must be at least twice as big as field b')
expect(handleValidation({ field_a: 3, field_b: 1 }).formErrors).toEqual(undefined)
})
it('divide: field_a > field_b / 2', () => {
const { handleValidation } = createHeadlessForm(
createSchemaWithRulesOnFieldA({
a_greater_than_b_divided_by_2: {
errorMessage: 'Field A must be greater than field_b / 2',
rule: { '>': [{ var: 'field_a' }, { '/': [{ var: 'field_b' }, 2] }] },
},
}),
{ strictInputType: false },
)
expect(handleValidation({ field_a: 2, field_b: 4 }).formErrors).toEqual({
field_a: 'Field A must be greater than field_b / 2',
})
expect(handleValidation({ field_a: 3, field_b: 5 }).formErrors).toEqual(undefined)
})
it('sum: field_a > field_b + field_c', () => {
const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
a_is_greater_than_b_plus_c: {
errorMessage: 'Field A must be greater than field_b and field_b added together',
rule: {
'>': [{ var: 'field_a' }, { '+': [{ var: 'field_b' }, { var: 'field_c' }] }],
},
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
const { formErrors } = handleValidation({ field_a: 0, field_b: 1, field_c: 2 })
expect(formErrors.field_a).toEqual(
'Field A must be greater than field_b and field_b added together',
)
expect(handleValidation({ field_a: 4, field_b: 1, field_c: 2 }).formErrors).toEqual(
undefined,
)
})
})
describe('reduce', () => {
it('reduce: working_hours_per_day * work_days', () => {
const { fields, handleValidation } = createHeadlessForm(schemaWithReduceAccumulator, {
strictInputType: false,
})
handleValidation({
work_days: ['monday', 'tuesday'],
working_hours_per_day: 8,
})
const field = fields.find(i => i.name === 'working_hours_per_week')
expect(field.const).toEqual(16)
expect(field.default).toEqual(16)
expect(field.label).toEqual('16 hours per week')
})
})
describe('logical: ||, &&', () => {
it('aND: field_a > field_b && field_a > field_c (implicit with multiple rules in a single field)', () => {
const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
a_is_greater_than_b: {
errorMessage: 'Field A must be greater than field_b',
rule: {
'>': [{ var: 'field_a' }, { var: 'field_b' }],
},
},
a_is_greater_than_c: {
errorMessage: 'Field A must be greater than field_c',
rule: {
'>': [{ var: 'field_a' }, { var: 'field_c' }],
},
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
expect(handleValidation({ field_a: 1, field_b: 10, field_c: 0 }).formErrors.field_a).toEqual(
'Field A must be greater than field_b',
)
expect(handleValidation({ field_a: 1, field_b: 0, field_c: 10 }).formErrors.field_a).toEqual(
'Field A must be greater than field_c',
)
expect(handleValidation({ field_a: 10, field_b: 5, field_c: 5 }).formErrors).toEqual(
undefined,
)
})
it('oR: field_a > field_b or field_a > field_c', () => {
const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
field_a_is_greater_than_b_or_c: {
errorMessage: 'Field A must be greater than field_b or field_c',
rule: {
or: [
{ '>': [{ var: 'field_a' }, { var: 'field_b' }] },
{ '>': [{ var: 'field_a' }, { var: 'field_c' }] },
],
},
},
})
const { handleValidation } = createHeadlessForm(schema, { strictInputType: false })
expect(handleValidation({ field_a: 0, field_b: 10, field_c: 10 }).formErrors.field_a).toEqual(
'Field A must be greater than field_b or field_c',
)
expect(handleValidation({ field_a: 1, field_b: 0, field_c: 10 }).formErrors).toEqual(
undefined,
)
expect(handleValidation({ field_a: 10, field_b: 5, field_c: 5 }).formErrors).toEqual(
undefined,
)
})
})
describe('multiple validations', () => {
it('two rules: A > B; A is even', () => {
const { handleValidation } = createHeadlessForm(multiRuleSchema, { strictInputType: false })
expect(handleValidation({ field_a: 1 }).formErrors).toEqual({
field_a: 'A must be even',
field_b: 'Required field',
})
expect(handleValidation({ field_a: 1, field_b: 2 }).formErrors).toEqual({
field_a: 'A must be even',
})
expect(handleValidation({ field_a: 3, field_b: 2 }).formErrors).toEqual({
field_a: 'A must be even',
})
expect(handleValidation({ field_a: 4, field_b: 2 }).formErrors).toEqual(undefined)
})
it('2 seperate fields with rules failing', () => {
const { handleValidation } = createHeadlessForm(schemaWithTwoRules, {
strictInputType: false,
})
expect(handleValidation({ field_a: 1, field_b: 3 }).formErrors).toEqual({
field_a: 'A must be bigger than B',
field_b: 'B must be even',
})
expect(handleValidation({ field_a: 4, field_b: 2 }).formErrors).toEqual(undefined)
})
})
describe('derive values', () => {
it('field_b is field_a * 2', () => {
const { fields, handleValidation } = createHeadlessForm(schemaWithComputedAttributes, {
strictInputType: false,
initialValues: { field_a: 2 },
})
let fieldB = fields.find(i => i.name === 'field_b')
expect(fieldB.description).toEqual(
'This field is 2 times bigger than field_a with value of 4.',
)
expect(fieldB.default).toEqual(4)
handleValidation({ field_a: 4 })
fieldB = fields.find(i => i.name === 'field_b')
expect(fieldB.default).toEqual(8)
expect(fieldB.label).toEqual('This is 8!')
})
it('a forced value will not be set when const and default are not equal', () => {
const { fields } = createHeadlessForm(badSchemaThatWillNotSetAForcedValue, {
strictInputType: false,
initialValues: { field_a: 2 },
})
expect(fields[1]).toMatchObject({ const: 6, default: 4 })
expect(fields[1]).not.toMatchObject({ forcedValue: expect.any(Number) })
})
it('derived errorMessages and statements work', () => {
const { fields, handleValidation } = createHeadlessForm(
schemaWithComputedAttributesAndErrorMessages,
{ strictInputType: false },
)
expect(handleValidation({ field_a: 2, field_b: 0 }).formErrors).toEqual({
field_b: 'Must be bigger than 4',
})
expect(handleValidation({ field_a: 2, field_b: 100 }).formErrors).toEqual({
field_b: 'Must be smaller than 8',
})
const fieldB = fields.find(i => i.name === 'field_b')
expect(fieldB.minimum).toEqual(4)
expect(fieldB.maximum).toEqual(8)
expect(fieldB.statement).toEqual({ description: 'Must be bigger than 4 and smaller than 8' })
})
it('use x-jsf-logic for setting dynamic minimum and maximum values', () => {
const { handleValidation } = createHeadlessForm(
schemaValidationForMaximumAndMinimumValues,
{
strictInputType: false,
},
)
expect(handleValidation({ field_a: 20, field_b: 17 }).formErrors).toEqual({
field_b: 'Field B must be greater than or equal to 20 - 2',
})
expect(handleValidation({ field_a: 20, field_b: 23 }).formErrors).toEqual({
field_b: 'Field B must be smaller than or equal to 20 + 2',
})
expect(handleValidation({ field_a: 20, field_b: 21 }).formErrors).toBeUndefined()
expect(handleValidation({ field_a: 20, field_b: 19 }).formErrors).toBeUndefined()
})
it('use x-jsf-logic for setting dynamic minimum and maximum values, with a dynamic error message', () => {
const { handleValidation } = createHeadlessForm(
schemaValidationForMaximumAndMinimumValuesWithDynamicErrorMessage,
{
strictInputType: false,
},
)
expect(handleValidation({ field_a: 20, field_b: 17 }).formErrors).toEqual({
field_b: 'Field B must be greater than or equal to 18',
})
expect(handleValidation({ field_a: 20, field_b: 23 }).formErrors).toEqual({
field_b: 'Field B must be smaller than or equal to 22',
})
expect(handleValidation({ field_a: 20, field_b: 21 }).formErrors).toBeUndefined()
})
})
describe('conditionals', () => {
it('apply validations and computed values on normal if statement.', () => {
const { fields, handleValidation } = createHeadlessForm(
schemaWhereValidationAndComputedValueIsAppliedOnNormalThenStatement,
{ strictInputType: false },
)
expect(handleValidation({ field_a: 0, field_b: 0 }).formErrors).toEqual(undefined)
expect(handleValidation({ field_a: 20, field_b: 0 }).formErrors).toEqual({
field_b: 'Must be greater than Field A + 10',
})
const [, fieldB] = fields
expect(fieldB.label).toEqual('Must be greater than 30.')
expect(handleValidation({ field_a: 20, field_b: 31 }).formErrors).toEqual(undefined)
})
it('when we have a required validation on a top level property and another validation is added, both should be accounted for.', () => {
const { handleValidation } = createHeadlessForm(
schemaWithTwoValidationsWhereOneOfThemIsAppliedConditionally,
{ strictInputType: false },
)
expect(handleValidation({ field_a: 10, field_b: 0 }).formErrors).toEqual({
field_b: 'Must be greater than A',
})
expect(handleValidation({ field_a: 10, field_b: 20 }).formErrors).toEqual(undefined)
expect(handleValidation({ field_a: 20, field_b: 10 }).formErrors).toEqual({
field_b: 'Must be greater than two times A',
})
expect(handleValidation({ field_a: 20, field_b: 21 }).formErrors).toEqual({
field_b: 'Must be greater than two times A',
})
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\'.')
})
})
})