@remoteoss/json-schema-form 0.9.0-beta.0 → 0.9.1-dev.20240320153711

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/dist/index.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2024 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.9.0-beta.0
5
- Generated: Mon, 11 Mar 2024 14:55:00 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.9.1-dev.20240320153711
5
+ Generated: Wed, 20 Mar 2024 15:37:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -457,9 +457,18 @@ var validateMaxDate = (value, minDate) => {
457
457
  const compare = compareDates(value, minDate);
458
458
  return compare === "LESSER" || compare === "EQUAL" ? true : false;
459
459
  };
460
+ var validateRadioOrSelectOptions = (value, options) => {
461
+ if (value === void 0)
462
+ return true;
463
+ const exactMatch = options.some((option) => option.value === value);
464
+ if (exactMatch)
465
+ return true;
466
+ const patternMatch = options.some((option) => option.pattern?.test(value));
467
+ return !!patternMatch;
468
+ };
460
469
  var yupSchemas = {
461
470
  text: validateOnlyStrings,
462
- radioOrSelect: (options) => {
471
+ radioOrSelectString: (options) => {
463
472
  return (0, import_yup.string)().nullable().transform((value) => {
464
473
  if (value === "") {
465
474
  return void 0;
@@ -469,28 +478,9 @@ var yupSchemas = {
469
478
  }
470
479
  return value === null ? void 0 : value;
471
480
  }).test(
472
- /*
473
- Custom test determines if the value either:
474
- - Matches a specific option by value
475
- - Matches a pattern
476
- If the option is undefined do not test, to allow for optional fields.
477
- */
478
481
  "matchesOptionOrPattern",
479
482
  ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
480
- (value) => {
481
- if (value === void 0) {
482
- return true;
483
- }
484
- const exactMatch = options.some((option) => option.value === value);
485
- if (exactMatch) {
486
- return true;
487
- }
488
- const patternMatch = options.some((option) => option.pattern?.test(value));
489
- if (patternMatch) {
490
- return true;
491
- }
492
- return false;
493
- }
483
+ (value) => validateRadioOrSelectOptions(value, options)
494
484
  );
495
485
  },
496
486
  date: ({ minDate, maxDate }) => {
@@ -519,6 +509,13 @@ var yupSchemas = {
519
509
  }
520
510
  return dateString;
521
511
  },
512
+ radioOrSelectNumber: (options) => (0, import_yup.number)().strict().typeError("The value must be a number").test(
513
+ "matchesOptionOrPattern",
514
+ ({ value }) => {
515
+ return `The option ${JSON.stringify(value)} is not valid.`;
516
+ },
517
+ (value) => validateRadioOrSelectOptions(value, options)
518
+ ).nullable(),
522
519
  number: (0, import_yup.number)().typeError("The value must be a number").nullable(),
523
520
  file: (0, import_yup.array)().nullable(),
524
521
  email: (0, import_yup.string)().trim().email("Please enter a valid email address").nullable(),
@@ -548,7 +545,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
548
545
  return "Please acknowledge this field";
549
546
  return "Required field";
550
547
  }
551
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
548
+ var getJsonTypeInArray = (jsonType) => {
549
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
550
+ };
552
551
  var getOptions = (field) => {
553
552
  const allValues = field.options?.map((option) => ({
554
553
  value: option.value,
@@ -562,9 +561,16 @@ var getOptions = (field) => {
562
561
  };
563
562
  var getYupSchema = ({ inputType, ...field }) => {
564
563
  const jsonType = getJsonTypeInArray(field.jsonType);
565
- if (field.options?.length > 0) {
564
+ const hasOptions = field.options?.length > 0;
565
+ const generateOptionSchema = (type) => {
566
566
  const optionValues = getOptions(field);
567
- return yupSchemas.radioOrSelect(optionValues);
567
+ return type === "number" ? yupSchemas.radioOrSelectNumber(optionValues) : yupSchemas.radioOrSelectString(optionValues);
568
+ };
569
+ if (hasOptions) {
570
+ if (Array.isArray(field.jsonType)) {
571
+ return field.jsonType.includes("number") ? generateOptionSchema("number") : generateOptionSchema("string");
572
+ }
573
+ return generateOptionSchema(field.jsonType);
568
574
  }
569
575
  if (field.format === "date") {
570
576
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2024 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.9.0-beta.0
5
- Generated: Mon, 11 Mar 2024 14:55:00 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.9.1-dev.20240320153711
5
+ Generated: Wed, 20 Mar 2024 15:37:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -421,9 +421,18 @@ var validateMaxDate = (value, minDate) => {
421
421
  const compare = compareDates(value, minDate);
422
422
  return compare === "LESSER" || compare === "EQUAL" ? true : false;
423
423
  };
424
+ var validateRadioOrSelectOptions = (value, options) => {
425
+ if (value === void 0)
426
+ return true;
427
+ const exactMatch = options.some((option) => option.value === value);
428
+ if (exactMatch)
429
+ return true;
430
+ const patternMatch = options.some((option) => option.pattern?.test(value));
431
+ return !!patternMatch;
432
+ };
424
433
  var yupSchemas = {
425
434
  text: validateOnlyStrings,
426
- radioOrSelect: (options) => {
435
+ radioOrSelectString: (options) => {
427
436
  return string().nullable().transform((value) => {
428
437
  if (value === "") {
429
438
  return void 0;
@@ -433,28 +442,9 @@ var yupSchemas = {
433
442
  }
434
443
  return value === null ? void 0 : value;
435
444
  }).test(
436
- /*
437
- Custom test determines if the value either:
438
- - Matches a specific option by value
439
- - Matches a pattern
440
- If the option is undefined do not test, to allow for optional fields.
441
- */
442
445
  "matchesOptionOrPattern",
443
446
  ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
444
- (value) => {
445
- if (value === void 0) {
446
- return true;
447
- }
448
- const exactMatch = options.some((option) => option.value === value);
449
- if (exactMatch) {
450
- return true;
451
- }
452
- const patternMatch = options.some((option) => option.pattern?.test(value));
453
- if (patternMatch) {
454
- return true;
455
- }
456
- return false;
457
- }
447
+ (value) => validateRadioOrSelectOptions(value, options)
458
448
  );
459
449
  },
460
450
  date: ({ minDate, maxDate }) => {
@@ -483,6 +473,13 @@ var yupSchemas = {
483
473
  }
484
474
  return dateString;
485
475
  },
476
+ radioOrSelectNumber: (options) => number().strict().typeError("The value must be a number").test(
477
+ "matchesOptionOrPattern",
478
+ ({ value }) => {
479
+ return `The option ${JSON.stringify(value)} is not valid.`;
480
+ },
481
+ (value) => validateRadioOrSelectOptions(value, options)
482
+ ).nullable(),
486
483
  number: number().typeError("The value must be a number").nullable(),
487
484
  file: array().nullable(),
488
485
  email: string().trim().email("Please enter a valid email address").nullable(),
@@ -512,7 +509,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
512
509
  return "Please acknowledge this field";
513
510
  return "Required field";
514
511
  }
515
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
512
+ var getJsonTypeInArray = (jsonType) => {
513
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
514
+ };
516
515
  var getOptions = (field) => {
517
516
  const allValues = field.options?.map((option) => ({
518
517
  value: option.value,
@@ -526,9 +525,16 @@ var getOptions = (field) => {
526
525
  };
527
526
  var getYupSchema = ({ inputType, ...field }) => {
528
527
  const jsonType = getJsonTypeInArray(field.jsonType);
529
- if (field.options?.length > 0) {
528
+ const hasOptions = field.options?.length > 0;
529
+ const generateOptionSchema = (type) => {
530
530
  const optionValues = getOptions(field);
531
- return yupSchemas.radioOrSelect(optionValues);
531
+ return type === "number" ? yupSchemas.radioOrSelectNumber(optionValues) : yupSchemas.radioOrSelectString(optionValues);
532
+ };
533
+ if (hasOptions) {
534
+ if (Array.isArray(field.jsonType)) {
535
+ return field.jsonType.includes("number") ? generateOptionSchema("number") : generateOptionSchema("string");
536
+ }
537
+ return generateOptionSchema(field.jsonType);
532
538
  }
533
539
  if (field.format === "date") {
534
540
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2024 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.9.0-beta.0
5
- Generated: Mon, 11 Mar 2024 14:55:00 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.9.1-dev.20240320153711
5
+ Generated: Wed, 20 Mar 2024 15:37:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -11775,9 +11775,18 @@ var validateMaxDate = (value, minDate) => {
11775
11775
  const compare = compareDates(value, minDate);
11776
11776
  return compare === "LESSER" || compare === "EQUAL" ? true : false;
11777
11777
  };
11778
+ var validateRadioOrSelectOptions = (value, options) => {
11779
+ if (value === void 0)
11780
+ return true;
11781
+ const exactMatch = options.some((option) => option.value === value);
11782
+ if (exactMatch)
11783
+ return true;
11784
+ const patternMatch = options.some((option) => option.pattern?.test(value));
11785
+ return !!patternMatch;
11786
+ };
11778
11787
  var yupSchemas = {
11779
11788
  text: validateOnlyStrings,
11780
- radioOrSelect: (options) => {
11789
+ radioOrSelectString: (options) => {
11781
11790
  return StringSchema().nullable().transform((value) => {
11782
11791
  if (value === "") {
11783
11792
  return void 0;
@@ -11787,28 +11796,9 @@ var yupSchemas = {
11787
11796
  }
11788
11797
  return value === null ? void 0 : value;
11789
11798
  }).test(
11790
- /*
11791
- Custom test determines if the value either:
11792
- - Matches a specific option by value
11793
- - Matches a pattern
11794
- If the option is undefined do not test, to allow for optional fields.
11795
- */
11796
11799
  "matchesOptionOrPattern",
11797
11800
  ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
11798
- (value) => {
11799
- if (value === void 0) {
11800
- return true;
11801
- }
11802
- const exactMatch = options.some((option) => option.value === value);
11803
- if (exactMatch) {
11804
- return true;
11805
- }
11806
- const patternMatch = options.some((option) => option.pattern?.test(value));
11807
- if (patternMatch) {
11808
- return true;
11809
- }
11810
- return false;
11811
- }
11801
+ (value) => validateRadioOrSelectOptions(value, options)
11812
11802
  );
11813
11803
  },
11814
11804
  date: ({ minDate, maxDate }) => {
@@ -11837,6 +11827,13 @@ var yupSchemas = {
11837
11827
  }
11838
11828
  return dateString;
11839
11829
  },
11830
+ radioOrSelectNumber: (options) => NumberSchema().strict().typeError("The value must be a number").test(
11831
+ "matchesOptionOrPattern",
11832
+ ({ value }) => {
11833
+ return `The option ${JSON.stringify(value)} is not valid.`;
11834
+ },
11835
+ (value) => validateRadioOrSelectOptions(value, options)
11836
+ ).nullable(),
11840
11837
  number: NumberSchema().typeError("The value must be a number").nullable(),
11841
11838
  file: array_default().nullable(),
11842
11839
  email: StringSchema().trim().email("Please enter a valid email address").nullable(),
@@ -11866,7 +11863,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
11866
11863
  return "Please acknowledge this field";
11867
11864
  return "Required field";
11868
11865
  }
11869
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
11866
+ var getJsonTypeInArray = (jsonType) => {
11867
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
11868
+ };
11870
11869
  var getOptions = (field) => {
11871
11870
  const allValues = field.options?.map((option) => ({
11872
11871
  value: option.value,
@@ -11880,9 +11879,16 @@ var getOptions = (field) => {
11880
11879
  };
11881
11880
  var getYupSchema = ({ inputType, ...field }) => {
11882
11881
  const jsonType = getJsonTypeInArray(field.jsonType);
11883
- if (field.options?.length > 0) {
11882
+ const hasOptions = field.options?.length > 0;
11883
+ const generateOptionSchema = (type) => {
11884
11884
  const optionValues = getOptions(field);
11885
- return yupSchemas.radioOrSelect(optionValues);
11885
+ return type === "number" ? yupSchemas.radioOrSelectNumber(optionValues) : yupSchemas.radioOrSelectString(optionValues);
11886
+ };
11887
+ if (hasOptions) {
11888
+ if (Array.isArray(field.jsonType)) {
11889
+ return field.jsonType.includes("number") ? generateOptionSchema("number") : generateOptionSchema("string");
11890
+ }
11891
+ return generateOptionSchema(field.jsonType);
11886
11892
  }
11887
11893
  if (field.format === "date") {
11888
11894
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.9.0-beta.0",
3
+ "version": "0.9.1-dev.20240320153711",
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",
@@ -193,3 +193,43 @@ describe('const/default with forced values', () => {
193
193
  expect(fields[0]).toMatchObject({ forcedValue: 300 });
194
194
  });
195
195
  });
196
+
197
+ describe('OneOf const', () => {
198
+ it('Validates numbers or strings correctly', () => {
199
+ const { handleValidation } = createHeadlessForm(
200
+ {
201
+ properties: {
202
+ number: { type: 'number', oneOf: [{ const: 0 }, { const: 1 }, { const: 2 }] },
203
+ },
204
+ },
205
+ { strictInputType: false }
206
+ );
207
+ expect(handleValidation({}).formErrors).toEqual(undefined);
208
+ expect(handleValidation({ number: 3 }).formErrors).toEqual({
209
+ number: 'The option 3 is not valid.',
210
+ });
211
+ expect(handleValidation({ number: 2 }).formErrors).toBeUndefined();
212
+ expect(handleValidation({ number: '2' }).formErrors).toEqual({
213
+ number: 'The value must be a number',
214
+ });
215
+ });
216
+
217
+ it('Validates numbers or strings when type is an array with null', () => {
218
+ const { handleValidation } = createHeadlessForm(
219
+ {
220
+ properties: {
221
+ number: { type: ['number', 'null'], oneOf: [{ const: 0 }, { const: 1 }, { const: 2 }] },
222
+ },
223
+ },
224
+ { strictInputType: false }
225
+ );
226
+ expect(handleValidation({}).formErrors).toEqual(undefined);
227
+ expect(handleValidation({ number: 3 }).formErrors).toEqual({
228
+ number: 'The option 3 is not valid.',
229
+ });
230
+ expect(handleValidation({ number: 2 }).formErrors).toBeUndefined();
231
+ expect(handleValidation({ number: '2' }).formErrors).toEqual({
232
+ number: 'The value must be a number',
233
+ });
234
+ });
235
+ });
@@ -93,6 +93,7 @@ export const mockTextMaxLengthInput = {
93
93
  export const mockRadioInputDeprecated = {
94
94
  title: 'Has siblings',
95
95
  description: 'Do you have any siblings?',
96
+ type: 'string',
96
97
  enum: ['yes', 'no'],
97
98
  'x-jsf-presentation': {
98
99
  inputType: 'radio',
@@ -386,6 +387,7 @@ export const mockGroupArrayInput = {
386
387
  ],
387
388
  },
388
389
  title: 'Child Sex',
390
+ type: 'string',
389
391
  },
390
392
  },
391
393
  'x-jsf-order': ['full_name', 'birthdate', 'sex'],
@@ -842,7 +844,7 @@ export const mockRadioInputOptionalNull = {
842
844
  { const: 'yes', title: 'Yes' },
843
845
  { const: 'no', title: 'No' },
844
846
  // JSF excludes the null option from the field output
845
- // But keepts null as an accepted value
847
+ // But keeps null as an accepted value
846
848
  { const: null, title: 'N/A' },
847
849
  ],
848
850
  'x-jsf-presentation': { inputType: 'radio' },
@@ -1612,7 +1614,6 @@ export const schemaFieldsetScopedCondition = {
1612
1614
  properties: {
1613
1615
  has_child: {
1614
1616
  description: 'If yes, it will show its age.',
1615
- maximum: 100,
1616
1617
  'x-jsf-presentation': {
1617
1618
  inputType: 'radio',
1618
1619
  options: [
@@ -1627,7 +1628,7 @@ export const schemaFieldsetScopedCondition = {
1627
1628
  ],
1628
1629
  },
1629
1630
  title: 'Do you have a child?',
1630
- type: 'number',
1631
+ type: 'string',
1631
1632
  },
1632
1633
  age: {
1633
1634
  description: 'This age is required, but the "age" at the root level is still optional.',