@remoteoss/json-schema-form 0.5.0-dev.20230717090149 → 0.5.0-dev.20230719114157

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.5.0-dev.20230717090149
5
- Generated: Mon, 17 Jul 2023 09:02:11 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230719114157
5
+ Generated: Wed, 19 Jul 2023 11:42:22 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -414,44 +414,25 @@ var yupSchemas = {
414
414
  return `The option ${JSON.stringify(value)} is not valid.`;
415
415
  }),
416
416
  date: ({ minDate, maxDate }) => {
417
- if (minDate && maxDate) {
418
- return (0, import_yup.string)().nullable().trim().matches(
419
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
420
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
421
- ).test(
422
- "minDate",
423
- `The date must be ${minDate} or after`,
424
- (value) => validateMinDate(value, minDate)
425
- ).test(
426
- "maxDate",
427
- `The date must be ${maxDate} or before`,
428
- (value) => validateMaxDate(value, maxDate)
429
- );
430
- }
417
+ let dateString = (0, import_yup.string)().nullable().trim().matches(
418
+ /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
419
+ `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
420
+ );
431
421
  if (minDate) {
432
- return (0, import_yup.string)().nullable().trim().matches(
433
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
434
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
435
- ).test(
422
+ dateString = dateString.test(
436
423
  "minDate",
437
- `The date must be ${minDate} or after`,
424
+ `The date must be ${minDate} or after.`,
438
425
  (value) => validateMinDate(value, minDate)
439
426
  );
440
427
  }
441
428
  if (maxDate) {
442
- return (0, import_yup.string)().nullable().trim().matches(
443
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
444
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
445
- ).test(
429
+ dateString = dateString.test(
446
430
  "maxDate",
447
- `The date must be ${maxDate} or before`,
431
+ `The date must be ${maxDate} or before.`,
448
432
  (value) => validateMaxDate(value, maxDate)
449
433
  );
450
434
  }
451
- return (0, import_yup.string)().nullable().trim().matches(
452
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
453
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
454
- );
435
+ return dateString;
455
436
  },
456
437
  number: (0, import_yup.number)().typeError("The value must be a number").nullable(),
457
438
  file: (0, import_yup.array)().nullable(),
@@ -497,7 +478,7 @@ var getYupSchema = ({ inputType, ...field }) => {
497
478
  const optionValues = getOptions(field);
498
479
  return yupSchemas.radioOrSelect(optionValues);
499
480
  }
500
- if (inputType === "date") {
481
+ if (field.format === "date") {
501
482
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
502
483
  }
503
484
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
@@ -959,6 +940,7 @@ function extractParametersFromNode(schemaNode) {
959
940
  maxFileSize: node.maxFileSize,
960
941
  // @deprecated in favor of presentation.maxFileSize
961
942
  default: node.default,
943
+ format: node.format,
962
944
  // Checkboxes conditions
963
945
  // — For checkboxes that only accept one value (string)
964
946
  ...presentation?.inputType === "checkbox" && { checkboxValue: node.const },
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.5.0-dev.20230717090149
5
- Generated: Mon, 17 Jul 2023 09:02:11 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230719114157
5
+ Generated: Wed, 19 Jul 2023 11:42:22 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -378,44 +378,25 @@ var yupSchemas = {
378
378
  return `The option ${JSON.stringify(value)} is not valid.`;
379
379
  }),
380
380
  date: ({ minDate, maxDate }) => {
381
- if (minDate && maxDate) {
382
- return string().nullable().trim().matches(
383
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
384
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
385
- ).test(
386
- "minDate",
387
- `The date must be ${minDate} or after`,
388
- (value) => validateMinDate(value, minDate)
389
- ).test(
390
- "maxDate",
391
- `The date must be ${maxDate} or before`,
392
- (value) => validateMaxDate(value, maxDate)
393
- );
394
- }
381
+ let dateString = string().nullable().trim().matches(
382
+ /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
383
+ `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
384
+ );
395
385
  if (minDate) {
396
- return string().nullable().trim().matches(
397
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
398
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
399
- ).test(
386
+ dateString = dateString.test(
400
387
  "minDate",
401
- `The date must be ${minDate} or after`,
388
+ `The date must be ${minDate} or after.`,
402
389
  (value) => validateMinDate(value, minDate)
403
390
  );
404
391
  }
405
392
  if (maxDate) {
406
- return string().nullable().trim().matches(
407
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
408
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
409
- ).test(
393
+ dateString = dateString.test(
410
394
  "maxDate",
411
- `The date must be ${maxDate} or before`,
395
+ `The date must be ${maxDate} or before.`,
412
396
  (value) => validateMaxDate(value, maxDate)
413
397
  );
414
398
  }
415
- return string().nullable().trim().matches(
416
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
417
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
418
- );
399
+ return dateString;
419
400
  },
420
401
  number: number().typeError("The value must be a number").nullable(),
421
402
  file: array().nullable(),
@@ -461,7 +442,7 @@ var getYupSchema = ({ inputType, ...field }) => {
461
442
  const optionValues = getOptions(field);
462
443
  return yupSchemas.radioOrSelect(optionValues);
463
444
  }
464
- if (inputType === "date") {
445
+ if (field.format === "date") {
465
446
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
466
447
  }
467
448
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
@@ -923,6 +904,7 @@ function extractParametersFromNode(schemaNode) {
923
904
  maxFileSize: node.maxFileSize,
924
905
  // @deprecated in favor of presentation.maxFileSize
925
906
  default: node.default,
907
+ format: node.format,
926
908
  // Checkboxes conditions
927
909
  // — For checkboxes that only accept one value (string)
928
910
  ...presentation?.inputType === "checkbox" && { checkboxValue: node.const },
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2023 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230717090149
5
- Generated: Mon, 17 Jul 2023 09:02:11 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230719114157
5
+ Generated: Wed, 19 Jul 2023 11:42:22 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -11354,44 +11354,25 @@ var yupSchemas = {
11354
11354
  return `The option ${JSON.stringify(value)} is not valid.`;
11355
11355
  }),
11356
11356
  date: ({ minDate, maxDate }) => {
11357
- if (minDate && maxDate) {
11358
- return StringSchema().nullable().trim().matches(
11359
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11360
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11361
- ).test(
11362
- "minDate",
11363
- `The date must be ${minDate} or after`,
11364
- (value) => validateMinDate(value, minDate)
11365
- ).test(
11366
- "maxDate",
11367
- `The date must be ${maxDate} or before`,
11368
- (value) => validateMaxDate(value, maxDate)
11369
- );
11370
- }
11357
+ let dateString = StringSchema().nullable().trim().matches(
11358
+ /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11359
+ `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11360
+ );
11371
11361
  if (minDate) {
11372
- return StringSchema().nullable().trim().matches(
11373
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11374
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11375
- ).test(
11362
+ dateString = dateString.test(
11376
11363
  "minDate",
11377
- `The date must be ${minDate} or after`,
11364
+ `The date must be ${minDate} or after.`,
11378
11365
  (value) => validateMinDate(value, minDate)
11379
11366
  );
11380
11367
  }
11381
11368
  if (maxDate) {
11382
- return StringSchema().nullable().trim().matches(
11383
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11384
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11385
- ).test(
11369
+ dateString = dateString.test(
11386
11370
  "maxDate",
11387
- `The date must be ${maxDate} or before`,
11371
+ `The date must be ${maxDate} or before.`,
11388
11372
  (value) => validateMaxDate(value, maxDate)
11389
11373
  );
11390
11374
  }
11391
- return StringSchema().nullable().trim().matches(
11392
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11393
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11394
- );
11375
+ return dateString;
11395
11376
  },
11396
11377
  number: NumberSchema().typeError("The value must be a number").nullable(),
11397
11378
  file: array_default().nullable(),
@@ -11437,7 +11418,7 @@ var getYupSchema = ({ inputType, ...field }) => {
11437
11418
  const optionValues = getOptions(field);
11438
11419
  return yupSchemas.radioOrSelect(optionValues);
11439
11420
  }
11440
- if (inputType === "date") {
11421
+ if (field.format === "date") {
11441
11422
  return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
11442
11423
  }
11443
11424
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
@@ -11899,6 +11880,7 @@ function extractParametersFromNode(schemaNode) {
11899
11880
  maxFileSize: node.maxFileSize,
11900
11881
  // @deprecated in favor of presentation.maxFileSize
11901
11882
  default: node.default,
11883
+ format: node.format,
11902
11884
  // Checkboxes conditions
11903
11885
  // — For checkboxes that only accept one value (string)
11904
11886
  ...presentation?.inputType === "checkbox" && { checkboxValue: node.const },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.5.0-dev.20230717090149",
3
+ "version": "0.5.0-dev.20230719114157",
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",
@@ -1036,59 +1036,91 @@ describe('createHeadlessForm', () => {
1036
1036
  });
1037
1037
 
1038
1038
  it('support "date" field type with a minDate', () => {
1039
- const result = createHeadlessForm(schemaInputTypeDate);
1039
+ const { fields, handleValidation } = createHeadlessForm(schemaInputTypeDate);
1040
1040
 
1041
- expect(result).toMatchObject({
1042
- fields: [
1043
- {
1044
- label: 'Birthdate',
1045
- name: 'birthdate',
1046
- required: true,
1047
- schema: expect.any(Object),
1048
- type: 'date',
1049
- maxLength: 10,
1050
- minDate: '1922-03-01',
1051
- maxDate: '2022-03-01',
1052
- },
1053
- ],
1041
+ const validateForm = (vals) => friendlyError(handleValidation(vals));
1042
+
1043
+ expect(fields[0]).toMatchObject({
1044
+ label: 'Birthdate',
1045
+ name: 'birthdate',
1046
+ required: true,
1047
+ schema: expect.any(Object),
1048
+ type: 'date',
1049
+ maxLength: 10,
1050
+ minDate: '1922-03-01',
1051
+ maxDate: '2022-03-01',
1052
+ });
1053
+
1054
+ expect(validateForm({})).toEqual({
1055
+ birthdate: 'Required field',
1054
1056
  });
1055
1057
 
1056
- const fieldValidator = result.fields[0].schema;
1057
1058
  const todayDateHint = new Date().toISOString().substring(0, 10);
1058
- expect(fieldValidator.isValidSync('1922-02-01')).toBe(false);
1059
- expect(fieldValidator.isValidSync('1922-03-01')).toBe(true);
1060
- expect(fieldValidator.isValidSync('2021-03-01')).toBe(true);
1061
- expect(() => fieldValidator.validateSync('')).toThrowError(
1062
- `Must be a valid date in yyyy-mm-dd format. e.g. ${todayDateHint}`
1063
- );
1059
+
1060
+ expect(validateForm({ birthdate: '' })).toEqual({
1061
+ birthdate: `Must be a valid date in yyyy-mm-dd format. e.g. ${todayDateHint}`,
1062
+ });
1063
+
1064
+ expect(validateForm({ birthdate: '1922-02-01' })).toEqual({
1065
+ birthdate: 'The date must be 1922-03-01 or after.',
1066
+ });
1067
+
1068
+ expect(validateForm({ birthdate: '1922-03-01' })).toEqual(undefined);
1069
+
1070
+ expect(validateForm({ birthdate: '2021-03-01' })).toEqual(undefined);
1064
1071
  });
1065
1072
 
1066
1073
  it('support "date" field type with a maxDate', () => {
1067
- const result = createHeadlessForm(schemaInputTypeDate);
1074
+ const { fields, handleValidation } = createHeadlessForm(schemaInputTypeDate);
1068
1075
 
1069
- expect(result).toMatchObject({
1070
- fields: [
1071
- {
1072
- label: 'Birthdate',
1073
- name: 'birthdate',
1074
- required: true,
1075
- schema: expect.any(Object),
1076
- type: 'date',
1077
- maxLength: 10,
1078
- minDate: '1922-03-01',
1079
- maxDate: '2022-03-01',
1080
- },
1081
- ],
1076
+ const validateForm = (vals) => friendlyError(handleValidation(vals));
1077
+
1078
+ expect(fields[0]).toMatchObject({
1079
+ label: 'Birthdate',
1080
+ name: 'birthdate',
1081
+ required: true,
1082
+ schema: expect.any(Object),
1083
+ type: 'date',
1084
+ maxLength: 10,
1085
+ minDate: '1922-03-01',
1086
+ maxDate: '2022-03-01',
1082
1087
  });
1083
1088
 
1084
- const fieldValidator = result.fields[0].schema;
1085
1089
  const todayDateHint = new Date().toISOString().substring(0, 10);
1086
- expect(fieldValidator.isValidSync('2022-02-01')).toBe(true);
1087
- expect(fieldValidator.isValidSync('2022-03-01')).toBe(true);
1088
- expect(fieldValidator.isValidSync('2022-04-01')).toBe(false);
1089
- expect(() => fieldValidator.validateSync('')).toThrowError(
1090
- `Must be a valid date in yyyy-mm-dd format. e.g. ${todayDateHint}`
1091
- );
1090
+
1091
+ expect(validateForm({ birthdate: '' })).toEqual({
1092
+ birthdate: `Must be a valid date in yyyy-mm-dd format. e.g. ${todayDateHint}`,
1093
+ });
1094
+
1095
+ expect(validateForm({ birthdate: '2022-02-01' })).toEqual(undefined);
1096
+ expect(validateForm({ birthdate: '2022-03-01' })).toEqual(undefined);
1097
+ expect(validateForm({ birthdate: '2022-04-01' })).toEqual({
1098
+ birthdate: 'The date must be 2022-03-01 or before.',
1099
+ });
1100
+ });
1101
+
1102
+ it('support format date with minDate and maxDate', () => {
1103
+ const schemaFormatDate = {
1104
+ properties: {
1105
+ birthdate: {
1106
+ title: 'Birthdate',
1107
+ type: 'string',
1108
+ format: 'date',
1109
+ 'x-jsf-presentation': {
1110
+ inputType: 'myDateType',
1111
+ maxDate: '2022-03-01',
1112
+ minDate: '1922-03-01',
1113
+ },
1114
+ },
1115
+ },
1116
+ };
1117
+
1118
+ const { handleValidation } = createHeadlessForm(schemaFormatDate);
1119
+ const validateForm = (vals) => friendlyError(handleValidation(vals));
1120
+
1121
+ expect(validateForm({ birthdate: '1922-02-01' })).toEqual({
1122
+ birthdate: 'The date must be 1922-03-01 or after.',
1123
+ });
1092
1124
  });
1093
1125
 
1094
1126
  it('supports "file" field type', () => {