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

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.20240320135936
5
+ Generated: Wed, 20 Mar 2024 13:59:53 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -459,7 +459,7 @@ var validateMaxDate = (value, minDate) => {
459
459
  };
460
460
  var yupSchemas = {
461
461
  text: validateOnlyStrings,
462
- radioOrSelect: (options) => {
462
+ radioOrSelectString: (options) => {
463
463
  return (0, import_yup.string)().nullable().transform((value) => {
464
464
  if (value === "") {
465
465
  return void 0;
@@ -519,6 +519,9 @@ var yupSchemas = {
519
519
  }
520
520
  return dateString;
521
521
  },
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(),
522
525
  number: (0, import_yup.number)().typeError("The value must be a number").nullable(),
523
526
  file: (0, import_yup.array)().nullable(),
524
527
  email: (0, import_yup.string)().trim().email("Please enter a valid email address").nullable(),
@@ -548,7 +551,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
548
551
  return "Please acknowledge this field";
549
552
  return "Required field";
550
553
  }
551
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
554
+ var getJsonTypeInArray = (jsonType) => {
555
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
556
+ };
552
557
  var getOptions = (field) => {
553
558
  const allValues = field.options?.map((option) => ({
554
559
  value: option.value,
@@ -562,9 +567,13 @@ var getOptions = (field) => {
562
567
  };
563
568
  var getYupSchema = ({ inputType, ...field }) => {
564
569
  const jsonType = getJsonTypeInArray(field.jsonType);
565
- if (field.options?.length > 0) {
570
+ if (field.options?.length > 0 && field.jsonType !== "number") {
566
571
  const optionValues = getOptions(field);
567
- return yupSchemas.radioOrSelect(optionValues);
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);
568
577
  }
569
578
  if (field.format === "date") {
570
579
  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.20240320135936
5
+ Generated: Wed, 20 Mar 2024 13:59:53 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 } from "yup";
389
+ import { string, number, boolean, object, array, mixed } 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);
@@ -423,7 +423,7 @@ var validateMaxDate = (value, minDate) => {
423
423
  };
424
424
  var yupSchemas = {
425
425
  text: validateOnlyStrings,
426
- radioOrSelect: (options) => {
426
+ radioOrSelectString: (options) => {
427
427
  return string().nullable().transform((value) => {
428
428
  if (value === "") {
429
429
  return void 0;
@@ -483,6 +483,9 @@ var yupSchemas = {
483
483
  }
484
484
  return dateString;
485
485
  },
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(),
486
489
  number: number().typeError("The value must be a number").nullable(),
487
490
  file: array().nullable(),
488
491
  email: string().trim().email("Please enter a valid email address").nullable(),
@@ -512,7 +515,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
512
515
  return "Please acknowledge this field";
513
516
  return "Required field";
514
517
  }
515
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
518
+ var getJsonTypeInArray = (jsonType) => {
519
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
520
+ };
516
521
  var getOptions = (field) => {
517
522
  const allValues = field.options?.map((option) => ({
518
523
  value: option.value,
@@ -526,9 +531,13 @@ var getOptions = (field) => {
526
531
  };
527
532
  var getYupSchema = ({ inputType, ...field }) => {
528
533
  const jsonType = getJsonTypeInArray(field.jsonType);
529
- if (field.options?.length > 0) {
534
+ if (field.options?.length > 0 && field.jsonType !== "number") {
530
535
  const optionValues = getOptions(field);
531
- return yupSchemas.radioOrSelect(optionValues);
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);
532
541
  }
533
542
  if (field.format === "date") {
534
543
  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.20240320135936
5
+ Generated: Wed, 20 Mar 2024 13:59:53 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -11777,7 +11777,7 @@ var validateMaxDate = (value, minDate) => {
11777
11777
  };
11778
11778
  var yupSchemas = {
11779
11779
  text: validateOnlyStrings,
11780
- radioOrSelect: (options) => {
11780
+ radioOrSelectString: (options) => {
11781
11781
  return StringSchema().nullable().transform((value) => {
11782
11782
  if (value === "") {
11783
11783
  return void 0;
@@ -11837,6 +11837,9 @@ var yupSchemas = {
11837
11837
  }
11838
11838
  return dateString;
11839
11839
  },
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(),
11840
11843
  number: NumberSchema().typeError("The value must be a number").nullable(),
11841
11844
  file: array_default().nullable(),
11842
11845
  email: StringSchema().trim().email("Please enter a valid email address").nullable(),
@@ -11866,7 +11869,9 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
11866
11869
  return "Please acknowledge this field";
11867
11870
  return "Required field";
11868
11871
  }
11869
- var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
11872
+ var getJsonTypeInArray = (jsonType) => {
11873
+ return Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
11874
+ };
11870
11875
  var getOptions = (field) => {
11871
11876
  const allValues = field.options?.map((option) => ({
11872
11877
  value: option.value,
@@ -11880,9 +11885,13 @@ var getOptions = (field) => {
11880
11885
  };
11881
11886
  var getYupSchema = ({ inputType, ...field }) => {
11882
11887
  const jsonType = getJsonTypeInArray(field.jsonType);
11883
- if (field.options?.length > 0) {
11888
+ if (field.options?.length > 0 && field.jsonType !== "number") {
11884
11889
  const optionValues = getOptions(field);
11885
- return yupSchemas.radioOrSelect(optionValues);
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);
11886
11895
  }
11887
11896
  if (field.format === "date") {
11888
11897
  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.20240320135936",
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 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 option "2" is not valid.',
214
+ });
215
+ });
216
+
217
+ it('Validates strings', () => {
218
+ const { handleValidation } = createHeadlessForm(
219
+ {
220
+ properties: {
221
+ number: { type: 'number', oneOf: [{ const: 'a' }, { const: 'b' }, { const: 'c' }] },
222
+ },
223
+ },
224
+ { strictInputType: false }
225
+ );
226
+ expect(handleValidation({}).formErrors).toEqual(undefined);
227
+ expect(handleValidation({ number: 'd' }).formErrors).toEqual({
228
+ number: 'The option "d" is not valid.',
229
+ });
230
+ expect(handleValidation({ number: 'a' }).formErrors).toBeUndefined();
231
+ expect(handleValidation({ number: '2' }).formErrors).toEqual({
232
+ number: 'The option "2" is not valid.',
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.',