@remoteoss/json-schema-form 0.5.0-dev.20230810172154 → 0.5.1-dev.20230913083845

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.5.0-dev.20230810172154",
3
+ "version": "0.5.1-dev.20230913083845",
4
4
  "description": "Headless UI form powered by JSON Schemas",
5
5
  "author": "Remote.com <engineering@remote.com> (https://remote.com/)",
6
6
  "license": "MIT",
@@ -47,6 +47,7 @@
47
47
  ]
48
48
  },
49
49
  "dependencies": {
50
+ "json-logic-js": "^2.0.2",
50
51
  "lodash": "^4.17.21",
51
52
  "randexp": "^0.5.3",
52
53
  "yup": "^0.30.0"
@@ -0,0 +1,98 @@
1
+ import { createHeadlessForm } from '../createHeadlessForm';
2
+
3
+ describe('validations: const', () => {
4
+ it('Should work for number', () => {
5
+ const { handleValidation } = createHeadlessForm(
6
+ {
7
+ properties: {
8
+ ten_only: { type: 'number', const: 10 },
9
+ },
10
+ },
11
+ { strictInputType: false }
12
+ );
13
+ expect(handleValidation({}).formErrors).toEqual(undefined);
14
+ expect(handleValidation({ ten_only: 1 }).formErrors).toEqual({
15
+ ten_only: 'The only accepted value is 10.',
16
+ });
17
+ expect(handleValidation({ ten_only: 10 }).formErrors).toBeUndefined();
18
+ // null is also considered valid until we fix @BUG RMT-518
19
+ // Expectation: To fail with error "The only accepted value is 10."
20
+ expect(handleValidation({ ten_only: null }).formErrors).toBeUndefined();
21
+ });
22
+
23
+ it('Should work for text', () => {
24
+ const { handleValidation } = createHeadlessForm(
25
+ {
26
+ properties: {
27
+ hello_only: { type: 'string', const: 'hello' },
28
+ },
29
+ },
30
+ { strictInputType: false }
31
+ );
32
+ expect(handleValidation({}).formErrors).toEqual(undefined);
33
+ expect(handleValidation({ hello_only: 'what' }).formErrors).toEqual({
34
+ hello_only: 'The only accepted value is hello.',
35
+ });
36
+ expect(handleValidation({ hello_only: 'hello' }).formErrors).toEqual(undefined);
37
+ });
38
+
39
+ it('Should work for a conditionally applied const', () => {
40
+ const { handleValidation } = createHeadlessForm(
41
+ {
42
+ properties: {
43
+ answer: { type: 'string', oneOf: [{ const: 'yes' }, { const: 'no' }] },
44
+ amount: {
45
+ description: 'If you select yes, this needs to be exactly 10.',
46
+ type: 'number',
47
+ },
48
+ },
49
+ allOf: [
50
+ {
51
+ if: { properties: { answer: { const: 'yes' } }, required: ['answer'] },
52
+ then: { properties: { amount: { const: 10 } }, required: ['amount'] },
53
+ },
54
+ ],
55
+ },
56
+ { strictInputType: false }
57
+ );
58
+ expect(handleValidation({}).formErrors).toEqual(undefined);
59
+ expect(handleValidation({ answer: 'no' }).formErrors).toEqual(undefined);
60
+ expect(handleValidation({ answer: 'yes' }).formErrors).toEqual({ amount: 'Required field' });
61
+ expect(handleValidation({ answer: 'yes', amount: 1 }).formErrors).toEqual({
62
+ amount: 'The only accepted value is 10.',
63
+ });
64
+ expect(handleValidation({ answer: 'yes', amount: 10 }).formErrors).toEqual(undefined);
65
+ });
66
+
67
+ it('Should show the custom error message', () => {
68
+ const { handleValidation } = createHeadlessForm(
69
+ {
70
+ properties: {
71
+ string: {
72
+ type: 'string',
73
+ const: 'hello',
74
+ 'x-jsf-errorMessage': { const: 'You must say hello!!!' },
75
+ },
76
+ },
77
+ },
78
+ { strictInputType: false }
79
+ );
80
+ expect(handleValidation({}).formErrors).toEqual(undefined);
81
+ expect(handleValidation({ string: 'hi' }).formErrors).toEqual({
82
+ string: 'You must say hello!!!',
83
+ });
84
+ expect(handleValidation({ string: 'hello' }).formErrors).toEqual(undefined);
85
+ });
86
+
87
+ it('Should have value attribute for when const & default is present', () => {
88
+ const { fields } = createHeadlessForm(
89
+ {
90
+ properties: {
91
+ ten_only: { type: 'number', const: 10, default: 10 },
92
+ },
93
+ },
94
+ { strictInputType: false }
95
+ );
96
+ expect(fields[0]).toMatchObject({ value: 10, const: 10, default: 10 });
97
+ });
98
+ });
@@ -18,6 +18,7 @@ import {
18
18
  schemaInputTypeSelectMultipleDeprecated,
19
19
  schemaInputTypeSelectMultiple,
20
20
  schemaInputTypeSelectMultipleOptional,
21
+ schemaInputTypeFieldset,
21
22
  schemaInputTypeNumber,
22
23
  schemaInputTypeNumberZeroMaximum,
23
24
  schemaInputTypeDate,
@@ -56,6 +57,7 @@ import {
56
57
  schemaForErrorMessageSpecificity,
57
58
  jsfConfigForErrorMessageSpecificity,
58
59
  } from './helpers';
60
+ import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';
59
61
 
60
62
  function buildJSONSchemaInput({ presentationFields, inputFields = {}, required }) {
61
63
  return {
@@ -91,14 +93,8 @@ const getField = (fields, name, ...subNames) => {
91
93
  return field;
92
94
  };
93
95
 
94
- beforeEach(() => {
95
- jest.spyOn(console, 'error').mockImplementation(() => {});
96
- });
97
-
98
- afterEach(() => {
99
- expect(console.error).not.toHaveBeenCalled();
100
- console.error.mockRestore();
101
- });
96
+ beforeEach(mockConsole);
97
+ afterEach(restoreConsoleAndEnsureItWasNotCalled);
102
98
 
103
99
  describe('createHeadlessForm', () => {
104
100
  it('returns empty result given no schema', () => {
@@ -2044,7 +2040,10 @@ describe('createHeadlessForm', () => {
2044
2040
  });
2045
2041
 
2046
2042
  expect(result).toMatchObject({
2047
- fields: [{ description: 'I am regular' }, { description: 'I am <b>bold</b>.' }],
2043
+ fields: [
2044
+ { description: 'I am regular' },
2045
+ { description: '<span class="jsf-description">I am <b>bold</b>.</span>' },
2046
+ ],
2048
2047
  });
2049
2048
  });
2050
2049
 
@@ -2503,7 +2502,7 @@ describe('createHeadlessForm', () => {
2503
2502
  });
2504
2503
 
2505
2504
  describe('when a field has conditional presentation properties', () => {
2506
- it('adds the html to nested statement markup when visible', () => {
2505
+ it('adds .jsf-statement to nested statement markup when visible', () => {
2507
2506
  const { fields } = createHeadlessForm(schemaWithConditionalPresentationProperties, {
2508
2507
  initialValues: {
2509
2508
  // show the hidden statement
@@ -2511,7 +2510,9 @@ describe('createHeadlessForm', () => {
2511
2510
  },
2512
2511
  });
2513
2512
 
2514
- expect(fields[0].statement.description).toBe(`<a href="">conditional statement markup</a>`);
2513
+ expect(fields[0].statement.description).toBe(
2514
+ `<span class="jsf-statement"><a href="">conditional statement markup</a></span>`
2515
+ );
2515
2516
  });
2516
2517
  });
2517
2518
 
@@ -3441,6 +3442,27 @@ describe('createHeadlessForm', () => {
3441
3442
  ],
3442
3443
  });
3443
3444
  });
3445
+
3446
+ it('should ignore initial values that do not match the field type (eg string vs object)', () => {
3447
+ const result = createHeadlessForm(schemaInputTypeFieldset, {
3448
+ initialValues: {
3449
+ a_fieldset: 'foo', // should be an object instead of string
3450
+ },
3451
+ });
3452
+
3453
+ // It returns fields without errors
3454
+ expect(result.fields).toBeDefined();
3455
+ expect(result.fields[0].fields[0].name).toBe('id_number');
3456
+ expect(result.fields[0].fields[1].name).toBe('tabs');
3457
+
3458
+ // Warn about those missmatched values
3459
+ expect(console.warn).toHaveBeenCalledWith(
3460
+ `Field "a_fieldset"'s value is "foo", but should be type object.`
3461
+ );
3462
+ console.warn.mockClear();
3463
+
3464
+ expect(console.error).not.toHaveBeenCalled();
3465
+ });
3444
3466
  });
3445
3467
  });
3446
3468
 
@@ -3688,7 +3710,7 @@ describe('createHeadlessForm', () => {
3688
3710
  expect(fields).toMatchObject([
3689
3711
  {
3690
3712
  name: 'time',
3691
- description: 'Write in <b>hh:ss</b> format', // from presentation
3713
+ description: '<span class="jsf-description">Write in <b>hh:ss</b> format</span>', // from presentation
3692
3714
  inputType: 'clock', // arbitrary type from presentation
3693
3715
  deprecated: {
3694
3716
  description: 'In favor of X', // from presentation
@@ -207,7 +207,6 @@ export const schemaInputTypeHidden = {
207
207
  title: 'Select multi hidden',
208
208
  default: ['Albania, Algeria'],
209
209
  'x-jsf-presentation': { inputType: 'hidden' },
210
- const: ['Albania, Algeria'],
211
210
  type: 'array',
212
211
  },
213
212
  },
@@ -1020,12 +1020,12 @@ export const schemaInputTypeFileWithSkippable = JSONSchemaBuilder()
1020
1020
  })
1021
1021
  .build();
1022
1022
 
1023
- export const schemaInputTypeFieldset = JSONSchemaBuilder()
1024
- .addInput({
1023
+ export const schemaInputTypeFieldset = {
1024
+ properties: {
1025
1025
  a_fieldset: mockFieldset,
1026
- })
1027
- .setRequiredFields(['a_fieldset'])
1028
- .build();
1026
+ },
1027
+ required: ['a_fieldset'],
1028
+ };
1029
1029
 
1030
1030
  export const schemaInputTypeFocusedFieldset = JSONSchemaBuilder()
1031
1031
  .addInput({
@@ -0,0 +1,441 @@
1
+ export function createSchemaWithRulesOnFieldA(rules) {
2
+ return {
3
+ properties: {
4
+ field_a: {
5
+ type: 'number',
6
+ 'x-jsf-logic-validations': Object.keys(rules),
7
+ },
8
+ field_b: {
9
+ type: 'number',
10
+ },
11
+ },
12
+ required: ['field_a', 'field_b'],
13
+ 'x-jsf-logic': { validations: rules },
14
+ };
15
+ }
16
+
17
+ export function createSchemaWithThreePropertiesWithRuleOnFieldA(rules) {
18
+ return {
19
+ properties: {
20
+ field_a: {
21
+ type: 'number',
22
+ 'x-jsf-logic-validations': Object.keys(rules),
23
+ },
24
+ field_b: {
25
+ type: 'number',
26
+ },
27
+ field_c: {
28
+ type: 'number',
29
+ },
30
+ },
31
+ 'x-jsf-logic': { validations: rules },
32
+ required: ['field_a', 'field_b', 'field_c'],
33
+ };
34
+ }
35
+
36
+ export const schemaWithNonRequiredField = {
37
+ properties: {
38
+ field_a: {
39
+ type: 'number',
40
+ },
41
+ field_b: {
42
+ type: 'number',
43
+ 'x-jsf-logic-validations': ['a_greater_than_field_b'],
44
+ },
45
+ },
46
+ 'x-jsf-logic': {
47
+ validations: {
48
+ a_greater_than_field_b: {
49
+ errorMessage: 'Must be greater than field_a',
50
+ rule: {
51
+ '>': [{ var: 'field_a' }, { var: 'field_b' }],
52
+ },
53
+ },
54
+ },
55
+ },
56
+ required: [],
57
+ };
58
+
59
+ export const schemaWithNativeAndJSONLogicChecks = {
60
+ properties: {
61
+ field_a: {
62
+ type: 'number',
63
+ minimum: 100,
64
+ 'x-jsf-logic-validations': ['a_multiple_of_ten'],
65
+ },
66
+ },
67
+ 'x-jsf-logic': {
68
+ validations: {
69
+ a_multiple_of_ten: {
70
+ errorMessage: 'Must be a multiple of 10',
71
+ rule: {
72
+ '===': [{ '%': [{ var: 'field_a' }, 10] }, 0],
73
+ },
74
+ },
75
+ },
76
+ },
77
+ required: ['field_a'],
78
+ };
79
+
80
+ export const schemaWithMissingRule = {
81
+ properties: {
82
+ field_a: {
83
+ type: 'number',
84
+ 'x-jsf-logic-validations': ['a_greater_than_ten'],
85
+ },
86
+ },
87
+ 'x-jsf-logic': {
88
+ validations: {
89
+ a_greater_than_ten: {
90
+ errorMessage: 'Must be greater than 10',
91
+ // rule: { '>': [{ var: 'field_a' }, 10] }, this missing causes test to fail.
92
+ },
93
+ },
94
+ },
95
+ required: [],
96
+ };
97
+
98
+ export const schemaWithUnknownVariableInValidations = {
99
+ properties: {
100
+ // field_a: { type: 'number' }, this missing causes test to fail.
101
+ },
102
+ 'x-jsf-logic': {
103
+ validations: {
104
+ a_equals_ten: {
105
+ errorMessage: 'Must equal 10',
106
+ rule: { '===': [{ var: 'field_a' }, 10] },
107
+ },
108
+ },
109
+ },
110
+ };
111
+
112
+ export const schemaWithUnknownVariableInComputedValues = {
113
+ properties: {
114
+ // field_a: { type: 'number' }, this missing causes test to fail.
115
+ },
116
+ 'x-jsf-logic': {
117
+ computedValues: {
118
+ a_times_ten: {
119
+ rule: { '*': [{ var: 'field_a' }, 10] },
120
+ },
121
+ },
122
+ },
123
+ };
124
+
125
+ export const schemaWithMissingComputedValue = {
126
+ properties: {
127
+ field_a: {
128
+ type: 'number',
129
+ 'x-jsf-logic-computedAttrs': {
130
+ title: '{{a_plus_ten}}',
131
+ },
132
+ },
133
+ },
134
+ 'x-jsf-logic': {
135
+ computedValues: {
136
+ a_plus_ten: {
137
+ // rule: { '+': [{ var: 'field_a' }, 10 ]} this missing causes test to fail.
138
+ },
139
+ },
140
+ },
141
+ required: [],
142
+ };
143
+
144
+ export const multiRuleSchema = {
145
+ properties: {
146
+ field_a: {
147
+ type: 'number',
148
+ 'x-jsf-logic-validations': ['a_bigger_than_b', 'is_even_number'],
149
+ },
150
+ field_b: {
151
+ type: 'number',
152
+ },
153
+ },
154
+ required: ['field_a', 'field_b'],
155
+ 'x-jsf-logic': {
156
+ validations: {
157
+ a_bigger_than_b: {
158
+ errorMessage: 'A must be bigger than B',
159
+ rule: {
160
+ '>': [{ var: 'field_a' }, { var: 'field_b' }],
161
+ },
162
+ },
163
+ is_even_number: {
164
+ errorMessage: 'A must be even',
165
+ rule: {
166
+ '===': [{ '%': [{ var: 'field_a' }, 2] }, 0],
167
+ },
168
+ },
169
+ },
170
+ },
171
+ };
172
+
173
+ export const schemaWithTwoRules = {
174
+ properties: {
175
+ field_a: {
176
+ type: 'number',
177
+ 'x-jsf-logic-validations': ['a_bigger_than_b'],
178
+ },
179
+ field_b: {
180
+ type: 'number',
181
+ 'x-jsf-logic-validations': ['is_even_number'],
182
+ },
183
+ },
184
+ required: ['field_a', 'field_b'],
185
+ 'x-jsf-logic': {
186
+ validations: {
187
+ a_bigger_than_b: {
188
+ errorMessage: 'A must be bigger than B',
189
+ rule: {
190
+ '>': [{ var: 'field_a' }, { var: 'field_b' }],
191
+ },
192
+ },
193
+ is_even_number: {
194
+ errorMessage: 'B must be even',
195
+ rule: {
196
+ '===': [{ '%': [{ var: 'field_b' }, 2] }, 0],
197
+ },
198
+ },
199
+ },
200
+ },
201
+ };
202
+
203
+ export const schemaWithComputedAttributes = {
204
+ properties: {
205
+ field_a: {
206
+ type: 'number',
207
+ },
208
+ field_b: {
209
+ type: 'number',
210
+ 'x-jsf-logic-computedAttrs': {
211
+ title: 'This is {{a_times_two}}!',
212
+ const: 'a_times_two',
213
+ default: 'a_times_two',
214
+ description: 'This field is 2 times bigger than field_a with value of {{a_times_two}}.',
215
+ },
216
+ },
217
+ },
218
+ required: ['field_a', 'field_b'],
219
+ 'x-jsf-logic': {
220
+ computedValues: {
221
+ a_times_two: {
222
+ rule: {
223
+ '*': [{ var: 'field_a' }, 2],
224
+ },
225
+ },
226
+ },
227
+ },
228
+ };
229
+
230
+ export const schemaWithInlineRuleForComputedAttributeWithoutCopy = {
231
+ properties: {
232
+ field_a: {
233
+ type: 'number',
234
+ },
235
+ field_b: {
236
+ type: 'number',
237
+ 'x-jsf-logic-computedAttrs': {
238
+ title: {
239
+ rule: {
240
+ '+': [{ var: 'field_a' }, 10],
241
+ },
242
+ },
243
+ },
244
+ },
245
+ },
246
+ };
247
+
248
+ export const schemaWithComputedAttributeThatDoesntExist = {
249
+ properties: {
250
+ field_a: {
251
+ type: 'number',
252
+ 'x-jsf-logic-computedAttrs': {
253
+ default: 'iDontExist',
254
+ },
255
+ },
256
+ },
257
+ // x-jsf-logic: { computedValues: { iDontExist: { rule: 10 }} this missing causes test to fail.
258
+ };
259
+
260
+ export const schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar = {
261
+ properties: {
262
+ // iDontExist: { type: 'number' } this missing causes test to fail.
263
+ field_a: {
264
+ type: 'number',
265
+ 'x-jsf-logic-computedAttrs': {
266
+ title: {
267
+ rule: {
268
+ '+': [{ var: 'IdontExist' }, 10],
269
+ },
270
+ },
271
+ },
272
+ },
273
+ },
274
+ };
275
+
276
+ export const schemaWithComputedAttributeThatDoesntExistTitle = {
277
+ properties: {
278
+ field_a: {
279
+ type: 'number',
280
+ 'x-jsf-logic-computedAttrs': {
281
+ title: `this doesn't exist {{iDontExist}}`,
282
+ },
283
+ },
284
+ },
285
+ };
286
+
287
+ export const schemaWithComputedAttributeThatDoesntExistDescription = {
288
+ properties: {
289
+ // iDontExist: { type: 'number'}, this missing causes test to fail
290
+ field_a: {
291
+ type: 'number',
292
+ 'x-jsf-logic-computedAttrs': {
293
+ description: `this doesn't exist {{iDontExist}}`,
294
+ },
295
+ },
296
+ },
297
+ };
298
+
299
+ export const schemaWithComputedAttributesAndErrorMessages = {
300
+ properties: {
301
+ field_a: {
302
+ type: 'number',
303
+ },
304
+ field_b: {
305
+ type: 'number',
306
+ 'x-jsf-logic-computedAttrs': {
307
+ minimum: 'a_times_two',
308
+ maximum: 'a_times_four',
309
+ 'x-jsf-errorMessage': {
310
+ minimum: 'Must be bigger than {{a_times_two}}',
311
+ maximum: 'Must be smaller than {{a_times_four}}',
312
+ },
313
+ 'x-jsf-presentation': {
314
+ statement: {
315
+ description: 'Must be bigger than {{a_times_two}} and smaller than {{a_times_four}}',
316
+ },
317
+ },
318
+ },
319
+ },
320
+ },
321
+ required: ['field_a', 'field_b'],
322
+ 'x-jsf-logic': {
323
+ computedValues: {
324
+ a_times_two: {
325
+ rule: {
326
+ '*': [{ var: 'field_a' }, 2],
327
+ },
328
+ },
329
+ a_times_four: {
330
+ rule: {
331
+ '*': [{ var: 'field_a' }, 4],
332
+ },
333
+ },
334
+ },
335
+ },
336
+ };
337
+
338
+ export const schemaWithDeepVarThatDoesNotExist = {
339
+ properties: {
340
+ field_a: {
341
+ type: 'number',
342
+ },
343
+ },
344
+ 'x-jsf-logic': {
345
+ validations: {
346
+ dummy_rule: {
347
+ errorMessage: 'Random stuff to illustrate a deeply nested rule.',
348
+ rule: {
349
+ '>': [{ var: 'field_a' }, { '*': [2, { '/': [2, { '*': [1, { var: 'field_b' }] }] }] }],
350
+ },
351
+ },
352
+ },
353
+ },
354
+ required: [],
355
+ };
356
+
357
+ export const schemaWithDeepVarThatDoesNotExistOnFieldset = {
358
+ properties: {
359
+ field_a: {
360
+ type: 'object',
361
+ properties: {
362
+ child: {
363
+ type: 'number',
364
+ },
365
+ },
366
+ 'x-jsf-logic': {
367
+ validations: {
368
+ dummy_rule: {
369
+ errorMessage: 'Must be greater than 10',
370
+ rule: {
371
+ '>': [{ var: 'child' }, { '*': [2, { '/': [2, { '*': [1, { var: 'field_a' }] }] }] }],
372
+ },
373
+ },
374
+ },
375
+ },
376
+ },
377
+ },
378
+ required: [],
379
+ };
380
+
381
+ export const schemaWithValidationThatDoesNotExistOnProperty = {
382
+ properties: {
383
+ field_a: {
384
+ type: 'number',
385
+ 'x-jsf-logic-validations': ['iDontExist'],
386
+ },
387
+ },
388
+ };
389
+
390
+ export const schemaWithPropertyThatDoesNotExistInThatLevelButDoesInFieldset = {
391
+ properties: {
392
+ field_a: {
393
+ type: 'object',
394
+ 'x-jsf-presentation': {
395
+ inputType: 'fieldset',
396
+ },
397
+ properties: {
398
+ child: {
399
+ type: 'number',
400
+ 'x-jsf-logic-validations': ['child_greater_than_10'],
401
+ },
402
+ other_child: {
403
+ type: 'number',
404
+ 'x-jsf-logic-validations': ['greater_than_child'],
405
+ },
406
+ },
407
+ required: ['child', 'other_child'],
408
+ },
409
+ },
410
+ // the issue here is that this should be nested inside `field_a` in order to not fail.
411
+ 'x-jsf-logic': {
412
+ validations: {
413
+ validation_parent: {
414
+ errorMessage: 'Must be greater than 10!',
415
+ rule: {
416
+ '>': [{ var: 'child' }, 10],
417
+ },
418
+ },
419
+ greater_than_child: {
420
+ errorMessage: 'Must be greater than child',
421
+ rule: {
422
+ '>': [{ var: 'other_child' }, { var: 'child' }],
423
+ },
424
+ },
425
+ },
426
+ },
427
+ required: ['field_a'],
428
+ };
429
+
430
+ export const schemaWithBadOperation = {
431
+ properties: {},
432
+ 'x-jsf-logic': {
433
+ validations: {
434
+ badOperator: {
435
+ rule: {
436
+ '++': [10, 2],
437
+ },
438
+ },
439
+ },
440
+ },
441
+ };