@remoteoss/json-schema-form 0.11.11-dev.20250220174843 → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,588 +0,0 @@
1
- import {
2
- createSchemaWithRulesOnFieldA,
3
- createSchemaWithThreePropertiesWithRuleOnFieldA,
4
- multiRuleSchema,
5
- schemaWhereValidationAndComputedValueIsAppliedOnNormalThenStatement,
6
- schemaInlineComputedAttrForMaximumMinimumValues,
7
- schemaInlineComputedAttrForTitle,
8
- schemaWithBadOperation,
9
- schemaWithChecksAndThenValidationsOnThen,
10
- schemaWithComputedAttributeThatDoesntExist,
11
- schemaWithComputedAttributeThatDoesntExistDescription,
12
- schemaWithComputedAttributeThatDoesntExistTitle,
13
- schemaWithComputedAttributes,
14
- schemaWithComputedAttributesAndErrorMessages,
15
- schemaWithComputedValueChecksInIf,
16
- schemaWithDeepVarThatDoesNotExist,
17
- schemaWithDeepVarThatDoesNotExistOnFieldset,
18
- schemaWithGreaterThanChecksForThreeFields,
19
- schemaWithIfStatementWithComputedValuesAndValidationChecks,
20
- schemaWithInlineMultipleRulesForComputedAttributes,
21
- schemaWithInlineRuleForComputedAttributeWithCopy,
22
- schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
23
- schemaWithJSFLogicAndInlineRule,
24
- schemaWithMissingComputedValue,
25
- schemaWithMissingRule,
26
- schemaWithMultipleComputedValueChecks,
27
- schemaWithNativeAndJSONLogicChecks,
28
- schemaWithNonRequiredField,
29
- schemaWithPropertiesCheckAndValidationsInAIf,
30
- schemaWithPropertyThatDoesNotExistInThatLevelButDoesInFieldset,
31
- schemaWithTwoRules,
32
- schemaWithTwoValidationsWhereOneOfThemIsAppliedConditionally,
33
- schemaWithUnknownVariableInComputedValues,
34
- schemaWithUnknownVariableInValidations,
35
- schemaWithValidationThatDoesNotExistOnProperty,
36
- badSchemaThatWillNotSetAForcedValue,
37
- } from './jsonLogic.fixtures';
38
- import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';
39
- import { createHeadlessForm } from '@/createHeadlessForm';
40
-
41
- beforeEach(mockConsole);
42
- afterEach(restoreConsoleAndEnsureItWasNotCalled);
43
-
44
- describe('jsonLogic: cross-values validations', () => {
45
- describe('Does not conflict with native JSON schema', () => {
46
- it('Given an optional field and empty value, jsonLogic validations are ignored', () => {
47
- const { handleValidation } = createHeadlessForm(schemaWithNonRequiredField, {
48
- strictInputType: false,
49
- });
50
- expect(handleValidation({}).formErrors).toBeUndefined();
51
- expect(handleValidation({ field_a: 0, field_b: 10 }).formErrors).toEqual({
52
- field_b: 'Must be greater than field_a',
53
- });
54
- expect(handleValidation({ field_a: 'incorrect value' }).formErrors).toEqual({
55
- field_a: 'The value must be a number',
56
- });
57
- expect(handleValidation({ field_a: 11 }).formErrors).toBeUndefined();
58
- });
59
-
60
- it('Native validations have higher precedence than jsonLogic validations', () => {
61
- const { handleValidation } = createHeadlessForm(schemaWithNativeAndJSONLogicChecks, {
62
- strictInputType: false,
63
- });
64
- expect(handleValidation({}).formErrors).toEqual({ field_a: 'Required field' });
65
- expect(handleValidation({ field_a: 0 }).formErrors).toEqual({
66
- field_a: 'Must be greater or equal to 100',
67
- });
68
- expect(handleValidation({ field_a: 101 }).formErrors).toEqual({
69
- field_a: 'Must be a multiple of 10',
70
- });
71
- expect(handleValidation({ field_a: 110 }).formErrors).toBeUndefined();
72
- });
73
- });
74
-
75
- describe('Relative: <, >, =', () => {
76
- it('bigger: field_a > field_b', () => {
77
- const schema = createSchemaWithRulesOnFieldA({
78
- a_greater_than_b: {
79
- errorMessage: 'Field A must be bigger than field B',
80
- rule: { '>': [{ var: 'field_a' }, { var: 'field_b' }] },
81
- },
82
- });
83
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
84
- const { formErrors } = handleValidation({ field_a: 1, field_b: 2 });
85
- expect(formErrors.field_a).toEqual('Field A must be bigger than field B');
86
- expect(handleValidation({ field_a: 2, field_b: 0 }).formErrors).toEqual(undefined);
87
- });
88
-
89
- it('smaller: field_a < field_b', () => {
90
- const schema = createSchemaWithRulesOnFieldA({
91
- a_less_than_b: {
92
- errorMessage: 'Field A must be smaller than field B',
93
- rule: { '<': [{ var: 'field_a' }, { var: 'field_b' }] },
94
- },
95
- });
96
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
97
- const { formErrors } = handleValidation({ field_a: 2, field_b: 2 });
98
- expect(formErrors.field_a).toEqual('Field A must be smaller than field B');
99
- expect(handleValidation({ field_a: 0, field_b: 2 }).formErrors).toEqual(undefined);
100
- });
101
-
102
- it('equal: field_a = field_b', () => {
103
- const schema = createSchemaWithRulesOnFieldA({
104
- a_equals_b: {
105
- errorMessage: 'Field A must equal field B',
106
- rule: { '==': [{ var: 'field_a' }, { var: 'field_b' }] },
107
- },
108
- });
109
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
110
- const { formErrors } = handleValidation({ field_a: 3, field_b: 2 });
111
- expect(formErrors.field_a).toEqual('Field A must equal field B');
112
- expect(handleValidation({ field_a: 2, field_b: 2 }).formErrors).toEqual(undefined);
113
- });
114
- });
115
-
116
- describe('Incorrectly written schemas', () => {
117
- afterEach(() => console.error.mockClear());
118
-
119
- const cases = [
120
- [
121
- 'x-jsf-logic.validations: throw when theres a missing rule',
122
- schemaWithMissingRule,
123
- '[json-schema-form] json-logic error: Validation "a_greater_than_ten" has missing rule.',
124
- ],
125
- [
126
- 'x-jsf-logic.validations: throw when theres a value that does not exist in a rule',
127
- schemaWithUnknownVariableInValidations,
128
- '[json-schema-form] json-logic error: rule "a_equals_ten" has no variable "field_a".',
129
- ],
130
- [
131
- 'x-jsf-logic.computedValues: throw when theres a value that does not exist in a rule',
132
- schemaWithUnknownVariableInComputedValues,
133
- '[json-schema-form] json-logic error: rule "a_times_ten" has no variable "field_a".',
134
- ],
135
- [
136
- 'x-jsf-logic.computedValues: throw when theres a missing computed value',
137
- schemaWithMissingComputedValue,
138
- '[json-schema-form] json-logic error: Computed value "a_plus_ten" has missing rule.',
139
- ],
140
- [
141
- 'x-jsf-logic-computedAttrs: error if theres a value that does not exist on an attribute.',
142
- schemaWithComputedAttributeThatDoesntExist,
143
- `[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
144
- ],
145
- [
146
- 'x-jsf-logic-computedAttrs: error if theres a value that does not exist on a template string (title).',
147
- schemaWithComputedAttributeThatDoesntExistTitle,
148
- `[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
149
- ],
150
- [
151
- 'x-jsf-logic-computedAttrs: error if theres a value that does not exist on a template string (description).',
152
- schemaWithComputedAttributeThatDoesntExistDescription,
153
- `[json-schema-form] json-logic error: Computed value "iDontExist" doesn't exist in field "field_a".`,
154
- ],
155
- [
156
- 'x-jsf-logic-computedAttrs:, error if theres a value referenced that does not exist on an inline rule.',
157
- schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
158
- `[json-schema-form] json-logic error: fieldName "IdontExist" doesn't exist in field "field_a.x-jsf-logic-computedAttrs.title".`,
159
- ],
160
- [
161
- 'x-jsf-logic.validations: error if a field does not exist in a deeply nested rule',
162
- schemaWithDeepVarThatDoesNotExist,
163
- '[json-schema-form] json-logic error: rule "dummy_rule" has no variable "field_b".',
164
- ],
165
- [
166
- 'x-jsf-logic.validations: error if rule does not exist on a fieldset property',
167
- schemaWithDeepVarThatDoesNotExistOnFieldset,
168
- '[json-schema-form] json-logic error: rule "dummy_rule" has no variable "field_a".',
169
- ],
170
- [
171
- 'x-jsf-validations: error if a validation name does not exist',
172
- schemaWithValidationThatDoesNotExistOnProperty,
173
- `[json-schema-form] json-logic error: "field_a" required validation "iDontExist" doesn't exist.`,
174
- ],
175
- [
176
- 'x-jsf-logic.validations: A top level logic keyword will not be able to reference fieldset properties',
177
- schemaWithPropertyThatDoesNotExistInThatLevelButDoesInFieldset,
178
- '[json-schema-form] json-logic error: rule "validation_parent" has no variable "child".',
179
- ],
180
- [
181
- 'x-jsf-logic.validations: error if unknown operation',
182
- schemaWithBadOperation,
183
- '[json-schema-form] json-logic error: in "badOperator" rule there is an unknown operator "++".',
184
- ],
185
- ];
186
-
187
- test.each(cases)('%p', (_, schema, expectedErrorString) => {
188
- const { error } = createHeadlessForm(schema, { strictInputType: false });
189
- const expectedError = new Error(expectedErrorString);
190
- expect(console.error).toHaveBeenCalledWith('JSON Schema invalid!', expectedError);
191
- expect(error).toEqual(expectedError);
192
- });
193
- });
194
-
195
- describe('Arithmetic: +, -, *, /', () => {
196
- it('multiple: field_a > field_b * 2', () => {
197
- const schema = createSchemaWithRulesOnFieldA({
198
- a_greater_than_b_multiplied_by_2: {
199
- errorMessage: 'Field A must be at least twice as big as field b',
200
- rule: { '>': [{ var: 'field_a' }, { '*': [{ var: 'field_b' }, 2] }] },
201
- },
202
- });
203
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
204
-
205
- const { formErrors } = handleValidation({ field_a: 1, field_b: 4 });
206
- expect(formErrors.field_a).toEqual('Field A must be at least twice as big as field b');
207
- expect(handleValidation({ field_a: 3, field_b: 1 }).formErrors).toEqual(undefined);
208
- });
209
-
210
- it('divide: field_a > field_b / 2', () => {
211
- const { handleValidation } = createHeadlessForm(
212
- createSchemaWithRulesOnFieldA({
213
- a_greater_than_b_divided_by_2: {
214
- errorMessage: 'Field A must be greater than field_b / 2',
215
- rule: { '>': [{ var: 'field_a' }, { '/': [{ var: 'field_b' }, 2] }] },
216
- },
217
- }),
218
- { strictInputType: false }
219
- );
220
- expect(handleValidation({ field_a: 2, field_b: 4 }).formErrors).toEqual({
221
- field_a: 'Field A must be greater than field_b / 2',
222
- });
223
- expect(handleValidation({ field_a: 3, field_b: 5 }).formErrors).toEqual(undefined);
224
- });
225
-
226
- it('sum: field_a > field_b + field_c', () => {
227
- const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
228
- a_is_greater_than_b_plus_c: {
229
- errorMessage: 'Field A must be greater than field_b and field_b added together',
230
- rule: {
231
- '>': [{ var: 'field_a' }, { '+': [{ var: 'field_b' }, { var: 'field_c' }] }],
232
- },
233
- },
234
- });
235
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
236
- const { formErrors } = handleValidation({ field_a: 0, field_b: 1, field_c: 2 });
237
- expect(formErrors.field_a).toEqual(
238
- 'Field A must be greater than field_b and field_b added together'
239
- );
240
- expect(handleValidation({ field_a: 4, field_b: 1, field_c: 2 }).formErrors).toEqual(
241
- undefined
242
- );
243
- });
244
- });
245
-
246
- describe('Logical: ||, &&', () => {
247
- it('AND: field_a > field_b && field_a > field_c (implicit with multiple rules in a single field)', () => {
248
- const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
249
- a_is_greater_than_b: {
250
- errorMessage: 'Field A must be greater than field_b',
251
- rule: {
252
- '>': [{ var: 'field_a' }, { var: 'field_b' }],
253
- },
254
- },
255
- a_is_greater_than_c: {
256
- errorMessage: 'Field A must be greater than field_c',
257
- rule: {
258
- '>': [{ var: 'field_a' }, { var: 'field_c' }],
259
- },
260
- },
261
- });
262
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
263
- expect(handleValidation({ field_a: 1, field_b: 10, field_c: 0 }).formErrors.field_a).toEqual(
264
- 'Field A must be greater than field_b'
265
- );
266
- expect(handleValidation({ field_a: 1, field_b: 0, field_c: 10 }).formErrors.field_a).toEqual(
267
- 'Field A must be greater than field_c'
268
- );
269
- expect(handleValidation({ field_a: 10, field_b: 5, field_c: 5 }).formErrors).toEqual(
270
- undefined
271
- );
272
- });
273
-
274
- it('OR: field_a > field_b or field_a > field_c', () => {
275
- const schema = createSchemaWithThreePropertiesWithRuleOnFieldA({
276
- field_a_is_greater_than_b_or_c: {
277
- errorMessage: 'Field A must be greater than field_b or field_c',
278
- rule: {
279
- or: [
280
- { '>': [{ var: 'field_a' }, { var: 'field_b' }] },
281
- { '>': [{ var: 'field_a' }, { var: 'field_c' }] },
282
- ],
283
- },
284
- },
285
- });
286
- const { handleValidation } = createHeadlessForm(schema, { strictInputType: false });
287
- expect(handleValidation({ field_a: 0, field_b: 10, field_c: 10 }).formErrors.field_a).toEqual(
288
- 'Field A must be greater than field_b or field_c'
289
- );
290
- expect(handleValidation({ field_a: 1, field_b: 0, field_c: 10 }).formErrors).toEqual(
291
- undefined
292
- );
293
- expect(handleValidation({ field_a: 10, field_b: 5, field_c: 5 }).formErrors).toEqual(
294
- undefined
295
- );
296
- });
297
- });
298
-
299
- describe('Multiple validations', () => {
300
- it('two rules: A > B; A is even', () => {
301
- const { handleValidation } = createHeadlessForm(multiRuleSchema, { strictInputType: false });
302
- expect(handleValidation({ field_a: 1 }).formErrors).toEqual({
303
- field_a: 'A must be even',
304
- field_b: 'Required field',
305
- });
306
- expect(handleValidation({ field_a: 1, field_b: 2 }).formErrors).toEqual({
307
- field_a: 'A must be bigger than B',
308
- });
309
- expect(handleValidation({ field_a: 3, field_b: 2 }).formErrors).toEqual({
310
- field_a: 'A must be even',
311
- });
312
- expect(handleValidation({ field_a: 4, field_b: 2 }).formErrors).toEqual(undefined);
313
- });
314
-
315
- it('2 seperate fields with rules failing', () => {
316
- const { handleValidation } = createHeadlessForm(schemaWithTwoRules, {
317
- strictInputType: false,
318
- });
319
- expect(handleValidation({ field_a: 1, field_b: 3 }).formErrors).toEqual({
320
- field_a: 'A must be bigger than B',
321
- field_b: 'B must be even',
322
- });
323
- expect(handleValidation({ field_a: 4, field_b: 2 }).formErrors).toEqual(undefined);
324
- });
325
- });
326
-
327
- describe('Derive values', () => {
328
- it('field_b is field_a * 2', () => {
329
- const { fields, handleValidation } = createHeadlessForm(schemaWithComputedAttributes, {
330
- strictInputType: false,
331
- initialValues: { field_a: 2 },
332
- });
333
- const fieldB = fields.find((i) => i.name === 'field_b');
334
- expect(fieldB.description).toEqual(
335
- 'This field is 2 times bigger than field_a with value of 4.'
336
- );
337
- expect(fieldB.default).toEqual(4);
338
- expect(fieldB.forcedValue).toEqual(4);
339
- handleValidation({ field_a: 4 });
340
- expect(fieldB.default).toEqual(8);
341
- expect(fieldB.label).toEqual('This is 8!');
342
- });
343
-
344
- it('A forced value will not be set when const and default are not equal', () => {
345
- const { fields } = createHeadlessForm(badSchemaThatWillNotSetAForcedValue, {
346
- strictInputType: false,
347
- initialValues: { field_a: 2 },
348
- });
349
- expect(fields[1]).toMatchObject({ const: 6, default: 4 });
350
- expect(fields[1]).not.toMatchObject({ forcedValue: expect.any(Number) });
351
- });
352
-
353
- it('Derived errorMessages and statements work', () => {
354
- const { fields, handleValidation } = createHeadlessForm(
355
- schemaWithComputedAttributesAndErrorMessages,
356
- { strictInputType: false }
357
- );
358
- const fieldB = fields.find((i) => i.name === 'field_b');
359
- expect(handleValidation({ field_a: 2, field_b: 0 }).formErrors).toEqual({
360
- field_b: 'Must be bigger than 4',
361
- });
362
- expect(handleValidation({ field_a: 2, field_b: 100 }).formErrors).toEqual({
363
- field_b: 'Must be smaller than 8',
364
- });
365
- expect(fieldB.minimum).toEqual(4);
366
- expect(fieldB.maximum).toEqual(8);
367
- expect(fieldB.statement).toEqual({ description: 'Must be bigger than 4 and smaller than 8' });
368
- });
369
-
370
- it('Use a inline-rule in a schema for a title attribute', () => {
371
- const { fields, handleValidation } = createHeadlessForm(
372
- schemaWithInlineRuleForComputedAttributeWithCopy,
373
- {
374
- strictInputType: false,
375
- }
376
- );
377
- const [, fieldB] = fields;
378
- expect(handleValidation({ field_a: 0, field_b: null }).formErrors).toEqual(undefined);
379
- expect(fieldB.label).toEqual('I need this to work using the 10.');
380
- expect(handleValidation({ field_a: 10 }).formErrors).toEqual(undefined);
381
- expect(fieldB.label).toEqual('I need this to work using the 20.');
382
- });
383
-
384
- it('Use multiple inline rules with different identifiers', () => {
385
- const { fields, handleValidation } = createHeadlessForm(
386
- schemaWithInlineMultipleRulesForComputedAttributes,
387
- {
388
- strictInputType: false,
389
- }
390
- );
391
- const [, fieldB] = fields;
392
- expect(handleValidation({ field_a: 10, field_b: null }).formErrors).toEqual(undefined);
393
- expect(fieldB.description).toEqual('Must be between 5 and 20.');
394
- });
395
-
396
- it('Use an inline rule in a schema for a title but it just uses the value', () => {
397
- const { fields, handleValidation } = createHeadlessForm(schemaInlineComputedAttrForTitle, {
398
- strictInputType: false,
399
- });
400
- const [, fieldB] = fields;
401
- expect(handleValidation({ field_a: 10, field_b: null }).formErrors).toEqual(undefined);
402
- expect(fieldB.label).toEqual('20');
403
- });
404
-
405
- it('Use an inline rule for a minimum, maximum value', () => {
406
- const { fields, handleValidation } = createHeadlessForm(
407
- schemaInlineComputedAttrForMaximumMinimumValues,
408
- {
409
- strictInputType: false,
410
- }
411
- );
412
- const [, fieldB] = fields;
413
-
414
- // FIXME: We are currently setting NaN here because of how the data clean up works for json-logic
415
- // We should probably set this as undefined when theres no values set?
416
- // tracked in INF-53.
417
- expect(fieldB).toMatchObject({ minimum: NaN, maximum: NaN });
418
- expect(handleValidation({ field_a: 10, field_b: null }).formErrors).toBeUndefined();
419
- expect(fieldB).toMatchObject({ minimum: 0, maximum: 20 });
420
- expect(handleValidation({ field_a: 50, field_b: 20 }).formErrors).toEqual({
421
- field_b: 'Must be greater or equal to 40',
422
- });
423
- expect(fieldB).toMatchObject({ minimum: 40, maximum: 60 });
424
- expect(handleValidation({ field_a: 50, field_b: 70 }).formErrors).toEqual({
425
- field_b: 'Must be smaller or equal to 60',
426
- });
427
- expect(fieldB).toMatchObject({ minimum: 40, maximum: 60 });
428
- expect(handleValidation({ field_a: 50, field_b: 50 }).formErrors).toBeUndefined();
429
- expect(fieldB).toMatchObject({ minimum: 40, maximum: 60 });
430
- });
431
-
432
- it('Mix use of multiple inline rules and an external rule', () => {
433
- const { fields, handleValidation } = createHeadlessForm(schemaWithJSFLogicAndInlineRule, {
434
- strictInputType: false,
435
- });
436
- handleValidation({ field_a: 10 });
437
- const [, fieldB] = fields;
438
- expect(fieldB.label).toEqual('Going to use 20 and 4');
439
- });
440
- });
441
-
442
- describe('Conditionals', () => {
443
- it('when field_a > field_b, show field_c', () => {
444
- const { fields, handleValidation } = createHeadlessForm(
445
- schemaWithGreaterThanChecksForThreeFields,
446
- { strictInputType: false }
447
- );
448
- const fieldC = fields.find((i) => i.name === 'field_c');
449
- expect(fieldC.isVisible).toEqual(false);
450
-
451
- expect(handleValidation({ field_a: 1, field_b: 3 }).formErrors).toEqual(undefined);
452
- expect(handleValidation({ field_a: 1 }).formErrors).toEqual({
453
- field_b: 'Required field',
454
- });
455
- expect(handleValidation({ field_a: 1, field_b: undefined }).formErrors).toEqual({
456
- field_b: 'Required field',
457
- });
458
- expect(handleValidation({ field_a: 10, field_b: 3 }).formErrors).toEqual({
459
- field_c: 'Required field',
460
- });
461
- expect(fieldC.isVisible).toEqual(true);
462
- expect(handleValidation({ field_a: 10, field_b: 3, field_c: 0 }).formErrors).toEqual(
463
- undefined
464
- );
465
- });
466
-
467
- it('A schema with both a `x-jsf-validations` and `properties` check', () => {
468
- const { fields, handleValidation } = createHeadlessForm(
469
- schemaWithPropertiesCheckAndValidationsInAIf,
470
- { strictInputType: false }
471
- );
472
- const fieldC = fields.find((i) => i.name === 'field_c');
473
- expect(handleValidation({ field_a: 1, field_b: 3 }).formErrors).toEqual(undefined);
474
- expect(fieldC.isVisible).toEqual(false);
475
- expect(handleValidation({ field_a: 10, field_b: 3 }).formErrors).toEqual({
476
- field_c: 'Required field',
477
- });
478
- expect(fieldC.isVisible).toEqual(true);
479
- expect(handleValidation({ field_a: 5, field_b: 3 }).formErrors).toEqual(undefined);
480
- });
481
-
482
- it('Conditionally apply a validation on a property depending on values', () => {
483
- const { fields, handleValidation } = createHeadlessForm(
484
- schemaWithChecksAndThenValidationsOnThen,
485
- { strictInputType: false }
486
- );
487
- const cField = fields.find((i) => i.name === 'field_c');
488
- expect(cField.isVisible).toEqual(false);
489
- expect(cField.description).toEqual(undefined);
490
- expect(handleValidation({ field_a: 10, field_b: 5 }).formErrors).toEqual({
491
- field_c: 'Required field',
492
- });
493
- expect(handleValidation({ field_a: 10, field_b: 5, field_c: 0 }).formErrors).toEqual({
494
- field_c: 'Needs more numbers',
495
- });
496
- expect(cField.description).toBe('I am a description!');
497
- expect(handleValidation({ field_a: 10, field_b: 5, field_c: 201 }).formErrors).toEqual(
498
- undefined
499
- );
500
- expect(handleValidation({ field_a: 5, field_b: 10 }).formErrors).toBeUndefined();
501
- expect(cField).toMatchObject({
502
- isVisible: false,
503
- // description: null, the description will currently be `I am a description!`, how do we change it back to null from here? Needs to be fixed by RMT-58
504
- });
505
- });
506
-
507
- it('Should apply a conditional based on a true computedValue', () => {
508
- const { fields, handleValidation } = createHeadlessForm(schemaWithComputedValueChecksInIf, {
509
- strictInputType: false,
510
- });
511
- const cField = fields.find((i) => i.name === 'field_c');
512
- expect(cField.isVisible).toEqual(false);
513
- expect(cField.description).toEqual(undefined);
514
- expect(handleValidation({ field_a: 10, field_b: 5 }).formErrors).toEqual({
515
- field_c: 'Required field',
516
- });
517
- expect(handleValidation({ field_a: 10, field_b: 5, field_c: 201 }).formErrors).toEqual(
518
- undefined
519
- );
520
- });
521
-
522
- it('Handle multiple computedValue checks by ANDing them together', () => {
523
- const { handleValidation } = createHeadlessForm(schemaWithMultipleComputedValueChecks, {
524
- strictInputType: false,
525
- });
526
- expect(handleValidation({}).formErrors).toEqual({
527
- field_a: 'Required field',
528
- field_b: 'Required field',
529
- });
530
- expect(handleValidation({ field_a: 10, field_b: 8 }).formErrors).toEqual({
531
- field_c: 'Required field',
532
- });
533
- expect(handleValidation({ field_a: 10, field_b: 8, field_c: 0 }).formErrors).toEqual({
534
- field_c: 'Must be two times B',
535
- });
536
- expect(handleValidation({ field_a: 10, field_b: 8, field_c: 17 }).formErrors).toEqual(
537
- undefined
538
- );
539
- });
540
-
541
- it('Handle having a true condition with both validations and computedValue checks', () => {
542
- const { handleValidation } = createHeadlessForm(
543
- schemaWithIfStatementWithComputedValuesAndValidationChecks,
544
- { strictInputType: false }
545
- );
546
- expect(handleValidation({ field_a: 1, field_b: 1 }).formErrors).toEqual(undefined);
547
- expect(handleValidation({ field_a: 10, field_b: 20 }).formErrors).toEqual(undefined);
548
- expect(handleValidation({ field_a: 10, field_b: 9 }).formErrors).toEqual({
549
- field_c: 'Required field',
550
- });
551
- expect(handleValidation({ field_a: 10, field_b: 9, field_c: 10 }).formErrors).toEqual(
552
- undefined
553
- );
554
- });
555
-
556
- it('Apply validations and computed values on normal if statement.', () => {
557
- const { fields, handleValidation } = createHeadlessForm(
558
- schemaWhereValidationAndComputedValueIsAppliedOnNormalThenStatement,
559
- { strictInputType: false }
560
- );
561
- expect(handleValidation({ field_a: 0, field_b: 0 }).formErrors).toEqual(undefined);
562
- expect(handleValidation({ field_a: 20, field_b: 0 }).formErrors).toEqual({
563
- field_b: 'Must be greater than Field A + 10',
564
- });
565
- const [, fieldB] = fields;
566
- expect(fieldB.label).toEqual('Must be greater than 30.');
567
- expect(handleValidation({ field_a: 20, field_b: 31 }).formErrors).toEqual(undefined);
568
- });
569
-
570
- it('When we have a required validation on a top level property and another validation is added, both should be accounted for.', () => {
571
- const { handleValidation } = createHeadlessForm(
572
- schemaWithTwoValidationsWhereOneOfThemIsAppliedConditionally,
573
- { strictInputType: false }
574
- );
575
- expect(handleValidation({ field_a: 10, field_b: 0 }).formErrors).toEqual({
576
- field_b: 'Must be greater than A',
577
- });
578
- expect(handleValidation({ field_a: 10, field_b: 20 }).formErrors).toEqual(undefined);
579
- expect(handleValidation({ field_a: 20, field_b: 10 }).formErrors).toEqual({
580
- field_b: 'Must be greater than A',
581
- });
582
- expect(handleValidation({ field_a: 20, field_b: 21 }).formErrors).toEqual({
583
- field_b: 'Must be greater than two times A',
584
- });
585
- expect(handleValidation({ field_a: 20, field_b: 41 }).formErrors).toEqual();
586
- });
587
- });
588
- });