@remoteoss/json-schema-form 0.9.1-dev.20240320135936 → 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.1-dev.20240320135936
5
- Generated: Wed, 20 Mar 2024 13:59:53 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,6 +457,15 @@ 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
471
  radioOrSelectString: (options) => {
@@ -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,9 +509,13 @@ var yupSchemas = {
519
509
  }
520
510
  return dateString;
521
511
  },
522
- radioOrSelectNumber: (options) => (0, import_yup.mixed)().typeError("The value must be a number").oneOf(options, ({ value }) => {
523
- return `The option ${JSON.stringify(value)} is not valid.`;
524
- }).nullable(),
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(),
525
519
  number: (0, import_yup.number)().typeError("The value must be a number").nullable(),
526
520
  file: (0, import_yup.array)().nullable(),
527
521
  email: (0, import_yup.string)().trim().email("Please enter a valid email address").nullable(),
@@ -567,13 +561,16 @@ var getOptions = (field) => {
567
561
  };
568
562
  var getYupSchema = ({ inputType, ...field }) => {
569
563
  const jsonType = getJsonTypeInArray(field.jsonType);
570
- if (field.options?.length > 0 && field.jsonType !== "number") {
564
+ const hasOptions = field.options?.length > 0;
565
+ const generateOptionSchema = (type) => {
571
566
  const optionValues = getOptions(field);
572
- return yupSchemas.radioOrSelectString(optionValues);
573
- }
574
- if (field.options?.length > 0 && field.jsonType === "number") {
575
- const optionValues = getOptions(field).map((i) => i.value);
576
- return yupSchemas.radioOrSelectNumber(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);
577
574
  }
578
575
  if (field.format === "date") {
579
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.1-dev.20240320135936
5
- Generated: Wed, 20 Mar 2024 13:59:53 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
 
@@ -386,7 +386,7 @@ import jsonLogic from "json-logic-js";
386
386
  import flow from "lodash/flow";
387
387
  import noop from "lodash/noop";
388
388
  import { randexp } from "randexp";
389
- import { string, number, boolean, object, array, mixed } from "yup";
389
+ import { string, number, boolean, object, array } from "yup";
390
390
  var DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
391
391
  var baseString = string().trim();
392
392
  var todayDateHint = (/* @__PURE__ */ new Date()).toISOString().substring(0, 10);
@@ -421,6 +421,15 @@ 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
435
  radioOrSelectString: (options) => {
@@ -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,9 +473,13 @@ var yupSchemas = {
483
473
  }
484
474
  return dateString;
485
475
  },
486
- radioOrSelectNumber: (options) => mixed().typeError("The value must be a number").oneOf(options, ({ value }) => {
487
- return `The option ${JSON.stringify(value)} is not valid.`;
488
- }).nullable(),
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(),
489
483
  number: number().typeError("The value must be a number").nullable(),
490
484
  file: array().nullable(),
491
485
  email: string().trim().email("Please enter a valid email address").nullable(),
@@ -531,13 +525,16 @@ var getOptions = (field) => {
531
525
  };
532
526
  var getYupSchema = ({ inputType, ...field }) => {
533
527
  const jsonType = getJsonTypeInArray(field.jsonType);
534
- if (field.options?.length > 0 && field.jsonType !== "number") {
528
+ const hasOptions = field.options?.length > 0;
529
+ const generateOptionSchema = (type) => {
535
530
  const optionValues = getOptions(field);
536
- return yupSchemas.radioOrSelectString(optionValues);
537
- }
538
- if (field.options?.length > 0 && field.jsonType === "number") {
539
- const optionValues = getOptions(field).map((i) => i.value);
540
- return yupSchemas.radioOrSelectNumber(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);
541
538
  }
542
539
  if (field.format === "date") {
543
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.1-dev.20240320135936
5
- Generated: Wed, 20 Mar 2024 13:59:53 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,6 +11775,15 @@ 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
11789
  radioOrSelectString: (options) => {
@@ -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,9 +11827,13 @@ var yupSchemas = {
11837
11827
  }
11838
11828
  return dateString;
11839
11829
  },
11840
- radioOrSelectNumber: (options) => SchemaType().typeError("The value must be a number").oneOf(options, ({ value }) => {
11841
- return `The option ${JSON.stringify(value)} is not valid.`;
11842
- }).nullable(),
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(),
11843
11837
  number: NumberSchema().typeError("The value must be a number").nullable(),
11844
11838
  file: array_default().nullable(),
11845
11839
  email: StringSchema().trim().email("Please enter a valid email address").nullable(),
@@ -11885,13 +11879,16 @@ var getOptions = (field) => {
11885
11879
  };
11886
11880
  var getYupSchema = ({ inputType, ...field }) => {
11887
11881
  const jsonType = getJsonTypeInArray(field.jsonType);
11888
- if (field.options?.length > 0 && field.jsonType !== "number") {
11882
+ const hasOptions = field.options?.length > 0;
11883
+ const generateOptionSchema = (type) => {
11889
11884
  const optionValues = getOptions(field);
11890
- return yupSchemas.radioOrSelectString(optionValues);
11891
- }
11892
- if (field.options?.length > 0 && field.jsonType === "number") {
11893
- const optionValues = getOptions(field).map((i) => i.value);
11894
- return yupSchemas.radioOrSelectNumber(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);
11895
11892
  }
11896
11893
  if (field.format === "date") {
11897
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.1-dev.20240320135936",
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",
@@ -195,7 +195,7 @@ describe('const/default with forced values', () => {
195
195
  });
196
196
 
197
197
  describe('OneOf const', () => {
198
- it('Validates numbers correctly', () => {
198
+ it('Validates numbers or strings correctly', () => {
199
199
  const { handleValidation } = createHeadlessForm(
200
200
  {
201
201
  properties: {
@@ -210,26 +210,26 @@ describe('OneOf const', () => {
210
210
  });
211
211
  expect(handleValidation({ number: 2 }).formErrors).toBeUndefined();
212
212
  expect(handleValidation({ number: '2' }).formErrors).toEqual({
213
- number: 'The option "2" is not valid.',
213
+ number: 'The value must be a number',
214
214
  });
215
215
  });
216
216
 
217
- it('Validates strings', () => {
217
+ it('Validates numbers or strings when type is an array with null', () => {
218
218
  const { handleValidation } = createHeadlessForm(
219
219
  {
220
220
  properties: {
221
- number: { type: 'number', oneOf: [{ const: 'a' }, { const: 'b' }, { const: 'c' }] },
221
+ number: { type: ['number', 'null'], oneOf: [{ const: 0 }, { const: 1 }, { const: 2 }] },
222
222
  },
223
223
  },
224
224
  { strictInputType: false }
225
225
  );
226
226
  expect(handleValidation({}).formErrors).toEqual(undefined);
227
- expect(handleValidation({ number: 'd' }).formErrors).toEqual({
228
- number: 'The option "d" is not valid.',
227
+ expect(handleValidation({ number: 3 }).formErrors).toEqual({
228
+ number: 'The option 3 is not valid.',
229
229
  });
230
- expect(handleValidation({ number: 'a' }).formErrors).toBeUndefined();
230
+ expect(handleValidation({ number: 2 }).formErrors).toBeUndefined();
231
231
  expect(handleValidation({ number: '2' }).formErrors).toEqual({
232
- number: 'The option "2" is not valid.',
232
+ number: 'The value must be a number',
233
233
  });
234
234
  });
235
235
  });