@remoteoss/json-schema-form 0.7.1-dev.20231025070834 → 0.7.1-dev.20231026204916

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) 2023 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231025070834
5
- Generated: Wed, 25 Oct 2023 07:08:56 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231026204916
5
+ Generated: Thu, 26 Oct 2023 20:49:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -465,13 +465,34 @@ var yupSchemas = {
465
465
  if (value === "") {
466
466
  return void 0;
467
467
  }
468
- if (options?.includes(null)) {
468
+ if (options?.some((option) => option.value === null)) {
469
469
  return value;
470
470
  }
471
471
  return value === null ? void 0 : value;
472
- }).oneOf(options, ({ value }) => {
473
- return `The option ${JSON.stringify(value)} is not valid.`;
474
- }),
472
+ }).test(
473
+ /*
474
+ Custom test determines if the value either:
475
+ - Matches a specific option by value
476
+ - Matches a pattern
477
+ If the option is undefined do not test, to allow for optional fields.
478
+ */
479
+ "matchesOptionOrPattern",
480
+ ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
481
+ (value) => {
482
+ if (value === void 0) {
483
+ return true;
484
+ }
485
+ const exactMatch = options.some((option) => option.value === value);
486
+ if (exactMatch) {
487
+ return true;
488
+ }
489
+ const patternMatch = options.some((option) => option.pattern?.test(value));
490
+ if (patternMatch) {
491
+ return true;
492
+ }
493
+ return false;
494
+ }
495
+ ),
475
496
  date: ({ minDate, maxDate }) => {
476
497
  let dateString = (0, import_yup.string)().nullable().transform((value) => {
477
498
  if (value === "") {
@@ -529,12 +550,15 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
529
550
  }
530
551
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
531
552
  var getOptions = (field) => {
532
- const allValues = field.options?.map((option) => option.value);
553
+ const allValues = field.options?.map((option) => ({
554
+ value: option.value,
555
+ pattern: option.pattern ? new RegExp(option.pattern) : null
556
+ }));
533
557
  const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
534
558
  // option but we don't have direct access at this point.
535
559
  // Otherwise the JSON Schema validator will fail as explained in PR#18
536
560
  field.jsonType.includes("null");
537
- return isOptionalWithNull ? [...allValues, null] : allValues;
561
+ return isOptionalWithNull ? [...allValues, { option: null }] : allValues;
538
562
  };
539
563
  var getYupSchema = ({ inputType, ...field }) => {
540
564
  const jsonType = getJsonTypeInArray(field.jsonType);
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2023 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231025070834
5
- Generated: Wed, 25 Oct 2023 07:08:56 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231026204916
5
+ Generated: Thu, 26 Oct 2023 20:49:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -429,13 +429,34 @@ var yupSchemas = {
429
429
  if (value === "") {
430
430
  return void 0;
431
431
  }
432
- if (options?.includes(null)) {
432
+ if (options?.some((option) => option.value === null)) {
433
433
  return value;
434
434
  }
435
435
  return value === null ? void 0 : value;
436
- }).oneOf(options, ({ value }) => {
437
- return `The option ${JSON.stringify(value)} is not valid.`;
438
- }),
436
+ }).test(
437
+ /*
438
+ Custom test determines if the value either:
439
+ - Matches a specific option by value
440
+ - Matches a pattern
441
+ If the option is undefined do not test, to allow for optional fields.
442
+ */
443
+ "matchesOptionOrPattern",
444
+ ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
445
+ (value) => {
446
+ if (value === void 0) {
447
+ return true;
448
+ }
449
+ const exactMatch = options.some((option) => option.value === value);
450
+ if (exactMatch) {
451
+ return true;
452
+ }
453
+ const patternMatch = options.some((option) => option.pattern?.test(value));
454
+ if (patternMatch) {
455
+ return true;
456
+ }
457
+ return false;
458
+ }
459
+ ),
439
460
  date: ({ minDate, maxDate }) => {
440
461
  let dateString = string().nullable().transform((value) => {
441
462
  if (value === "") {
@@ -493,12 +514,15 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
493
514
  }
494
515
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
495
516
  var getOptions = (field) => {
496
- const allValues = field.options?.map((option) => option.value);
517
+ const allValues = field.options?.map((option) => ({
518
+ value: option.value,
519
+ pattern: option.pattern ? new RegExp(option.pattern) : null
520
+ }));
497
521
  const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
498
522
  // option but we don't have direct access at this point.
499
523
  // Otherwise the JSON Schema validator will fail as explained in PR#18
500
524
  field.jsonType.includes("null");
501
- return isOptionalWithNull ? [...allValues, null] : allValues;
525
+ return isOptionalWithNull ? [...allValues, { option: null }] : allValues;
502
526
  };
503
527
  var getYupSchema = ({ inputType, ...field }) => {
504
528
  const jsonType = getJsonTypeInArray(field.jsonType);
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2023 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231025070834
5
- Generated: Wed, 25 Oct 2023 07:08:56 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.7.1-dev.20231026204916
5
+ Generated: Thu, 26 Oct 2023 20:49:34 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -11783,13 +11783,34 @@ var yupSchemas = {
11783
11783
  if (value === "") {
11784
11784
  return void 0;
11785
11785
  }
11786
- if (options?.includes(null)) {
11786
+ if (options?.some((option) => option.value === null)) {
11787
11787
  return value;
11788
11788
  }
11789
11789
  return value === null ? void 0 : value;
11790
- }).oneOf(options, ({ value }) => {
11791
- return `The option ${JSON.stringify(value)} is not valid.`;
11792
- }),
11790
+ }).test(
11791
+ /*
11792
+ Custom test determines if the value either:
11793
+ - Matches a specific option by value
11794
+ - Matches a pattern
11795
+ If the option is undefined do not test, to allow for optional fields.
11796
+ */
11797
+ "matchesOptionOrPattern",
11798
+ ({ value }) => `The option ${JSON.stringify(value)} is not valid.`,
11799
+ (value) => {
11800
+ if (value === void 0) {
11801
+ return true;
11802
+ }
11803
+ const exactMatch = options.some((option) => option.value === value);
11804
+ if (exactMatch) {
11805
+ return true;
11806
+ }
11807
+ const patternMatch = options.some((option) => option.pattern?.test(value));
11808
+ if (patternMatch) {
11809
+ return true;
11810
+ }
11811
+ return false;
11812
+ }
11813
+ ),
11793
11814
  date: ({ minDate, maxDate }) => {
11794
11815
  let dateString = StringSchema().nullable().transform((value) => {
11795
11816
  if (value === "") {
@@ -11847,12 +11868,15 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
11847
11868
  }
11848
11869
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
11849
11870
  var getOptions = (field) => {
11850
- const allValues = field.options?.map((option) => option.value);
11871
+ const allValues = field.options?.map((option) => ({
11872
+ value: option.value,
11873
+ pattern: option.pattern ? new RegExp(option.pattern) : null
11874
+ }));
11851
11875
  const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
11852
11876
  // option but we don't have direct access at this point.
11853
11877
  // Otherwise the JSON Schema validator will fail as explained in PR#18
11854
11878
  field.jsonType.includes("null");
11855
- return isOptionalWithNull ? [...allValues, null] : allValues;
11879
+ return isOptionalWithNull ? [...allValues, { option: null }] : allValues;
11856
11880
  };
11857
11881
  var getYupSchema = ({ inputType, ...field }) => {
11858
11882
  const jsonType = getJsonTypeInArray(field.jsonType);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.7.1-dev.20231025070834",
3
+ "version": "0.7.1-dev.20231026204916",
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",
@@ -38,6 +38,7 @@ import {
38
38
  mockFileInput,
39
39
  mockRadioCardInput,
40
40
  mockRadioCardExpandableInput,
41
+ mockTelWithPattern,
41
42
  mockTextInput,
42
43
  mockTextInputDeprecated,
43
44
  mockNumberInput,
@@ -1442,6 +1443,51 @@ describe('createHeadlessForm', () => {
1442
1443
  });
1443
1444
  });
1444
1445
 
1446
+ it('supports oneOf pattern validation', () => {
1447
+ const result = createHeadlessForm(mockTelWithPattern);
1448
+
1449
+ expect(result).toMatchObject({
1450
+ fields: [
1451
+ {
1452
+ label: 'Phone number',
1453
+ name: 'phone_number',
1454
+ type: 'tel',
1455
+ required: false,
1456
+ options: [
1457
+ {
1458
+ label: 'Portugal',
1459
+ pattern: '^(\\+351)[0-9]{9,}$',
1460
+ },
1461
+ {
1462
+ label: 'United Kingdom (UK)',
1463
+ pattern: '^(\\+44)[0-9]{1,}$',
1464
+ },
1465
+ {
1466
+ label: 'Bolivia',
1467
+ pattern: '^(\\+591)[0-9]{9,}$',
1468
+ },
1469
+ {
1470
+ label: 'Canada',
1471
+ pattern: '^(\\+1)(206|224)[0-9]{1,}$',
1472
+ },
1473
+ {
1474
+ label: 'United States',
1475
+ pattern: '^(\\+1)[0-9]{1,}$',
1476
+ },
1477
+ ],
1478
+ },
1479
+ ],
1480
+ });
1481
+
1482
+ const fieldValidator = result.fields[0].schema;
1483
+
1484
+ expect(fieldValidator.isValidSync('+351123123123')).toBe(true);
1485
+ expect(() => fieldValidator.validateSync('+35100')).toThrowError(
1486
+ 'The option "+35100" is not valid.'
1487
+ );
1488
+ expect(fieldValidator.isValidSync(undefined)).toBe(true);
1489
+ });
1490
+
1445
1491
  describe('supports "fieldset" field type', () => {
1446
1492
  it('supports basic case', () => {
1447
1493
  const result = createHeadlessForm({
@@ -1947,9 +1993,9 @@ describe('createHeadlessForm', () => {
1947
1993
  },
1948
1994
  },
1949
1995
  {
1950
- name: 'a_or_b',
1951
- label: 'A dropdown',
1952
- type: 'select',
1996
+ name: 'role',
1997
+ label: 'Role',
1998
+ type: 'text',
1953
1999
  statement: {
1954
2000
  description: 'This is another statement message, but more severe.',
1955
2001
  inputType: 'statement',
@@ -453,6 +453,46 @@ export const mockCheckboxInput = {
453
453
  type: 'string',
454
454
  };
455
455
 
456
+ export const mockTelWithPattern = {
457
+ properties: {
458
+ phone_number: {
459
+ title: 'Phone number',
460
+ description: 'Enter your telephone number',
461
+ type: 'string',
462
+ 'x-jsf-presentation': {
463
+ inputType: 'tel',
464
+ },
465
+ oneOf: [
466
+ {
467
+ title: 'Portugal',
468
+ pattern: '^(\\+351)[0-9]{9,}$',
469
+ 'x-jsf-presentation': { meta: { countryCode: '351' } },
470
+ },
471
+ {
472
+ title: 'United Kingdom (UK)',
473
+ pattern: '^(\\+44)[0-9]{1,}$',
474
+ 'x-jsf-presentation': { meta: { countryCode: '44' } },
475
+ },
476
+ {
477
+ title: 'Bolivia',
478
+ pattern: '^(\\+591)[0-9]{9,}$',
479
+ 'x-jsf-presentation': { meta: { countryCode: '591' } },
480
+ },
481
+ {
482
+ title: 'Canada',
483
+ pattern: '^(\\+1)(206|224)[0-9]{1,}$',
484
+ 'x-jsf-presentation': { meta: { countryCode: '1' } },
485
+ },
486
+ {
487
+ title: 'United States',
488
+ pattern: '^(\\+1)[0-9]{1,}$',
489
+ 'x-jsf-presentation': { meta: { countryCode: '1' } },
490
+ },
491
+ ],
492
+ },
493
+ },
494
+ };
495
+
456
496
  /**
457
497
  * Compose a schema with lower chance of human error
458
498
  * @param {Object} schema version