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

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.20230705093926
5
- Generated: Wed, 05 Jul 2023 09:39:42 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230717090149
5
+ Generated: Mon, 17 Jul 2023 09:02:11 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -381,6 +381,25 @@ var validateOnlyStrings = (0, import_yup.string)().trim().nullable().test(
381
381
  return true;
382
382
  }
383
383
  );
384
+ var compareDates = (d1, d2) => {
385
+ let date1 = new Date(d1).getTime();
386
+ let date2 = new Date(d2).getTime();
387
+ if (date1 < date2) {
388
+ return "LESSER";
389
+ } else if (date1 > date2) {
390
+ return "GREATER";
391
+ } else {
392
+ return "EQUAL";
393
+ }
394
+ };
395
+ var validateMinDate = (value, minDate) => {
396
+ const compare = compareDates(value, minDate);
397
+ return compare === "GREATER" || compare === "EQUAL" ? true : false;
398
+ };
399
+ var validateMaxDate = (value, minDate) => {
400
+ const compare = compareDates(value, minDate);
401
+ return compare === "LESSER" || compare === "EQUAL" ? true : false;
402
+ };
384
403
  var yupSchemas = {
385
404
  text: validateOnlyStrings,
386
405
  radioOrSelect: (options) => (0, import_yup.string)().nullable().transform((value) => {
@@ -394,10 +413,46 @@ var yupSchemas = {
394
413
  }).oneOf(options, ({ value }) => {
395
414
  return `The option ${JSON.stringify(value)} is not valid.`;
396
415
  }),
397
- date: (0, import_yup.string)().nullable().trim().matches(
398
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
399
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
400
- ),
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
+ }
431
+ 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(
436
+ "minDate",
437
+ `The date must be ${minDate} or after`,
438
+ (value) => validateMinDate(value, minDate)
439
+ );
440
+ }
441
+ 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(
446
+ "maxDate",
447
+ `The date must be ${maxDate} or before`,
448
+ (value) => validateMaxDate(value, maxDate)
449
+ );
450
+ }
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
+ );
455
+ },
401
456
  number: (0, import_yup.number)().typeError("The value must be a number").nullable(),
402
457
  file: (0, import_yup.array)().nullable(),
403
458
  email: (0, import_yup.string)().trim().email("Please enter a valid email address").nullable(),
@@ -442,6 +497,9 @@ var getYupSchema = ({ inputType, ...field }) => {
442
497
  const optionValues = getOptions(field);
443
498
  return yupSchemas.radioOrSelect(optionValues);
444
499
  }
500
+ if (inputType === "date") {
501
+ return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
502
+ }
445
503
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
446
504
  };
447
505
  function buildYupSchema(field, config) {
@@ -914,8 +972,10 @@ function extractParametersFromNode(schemaNode) {
914
972
  },
915
973
  // Handle [name].presentation
916
974
  ...presentation,
917
- description,
918
- extra: presentation.extra,
975
+ description: containsHTML(description) ? wrapWithSpan(description, {
976
+ class: "jsf-description"
977
+ }) : description,
978
+ extra: containsHTML(presentation.extra) ? wrapWithSpan(presentation.extra, { class: "jsf-extra" }) : presentation.extra,
919
979
  statement: presentation.statement && {
920
980
  ...presentation.statement,
921
981
  description: statementDescription
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.20230705093926
5
- Generated: Wed, 05 Jul 2023 09:39:42 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230717090149
5
+ Generated: Mon, 17 Jul 2023 09:02:11 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -345,6 +345,25 @@ var validateOnlyStrings = string().trim().nullable().test(
345
345
  return true;
346
346
  }
347
347
  );
348
+ var compareDates = (d1, d2) => {
349
+ let date1 = new Date(d1).getTime();
350
+ let date2 = new Date(d2).getTime();
351
+ if (date1 < date2) {
352
+ return "LESSER";
353
+ } else if (date1 > date2) {
354
+ return "GREATER";
355
+ } else {
356
+ return "EQUAL";
357
+ }
358
+ };
359
+ var validateMinDate = (value, minDate) => {
360
+ const compare = compareDates(value, minDate);
361
+ return compare === "GREATER" || compare === "EQUAL" ? true : false;
362
+ };
363
+ var validateMaxDate = (value, minDate) => {
364
+ const compare = compareDates(value, minDate);
365
+ return compare === "LESSER" || compare === "EQUAL" ? true : false;
366
+ };
348
367
  var yupSchemas = {
349
368
  text: validateOnlyStrings,
350
369
  radioOrSelect: (options) => string().nullable().transform((value) => {
@@ -358,10 +377,46 @@ var yupSchemas = {
358
377
  }).oneOf(options, ({ value }) => {
359
378
  return `The option ${JSON.stringify(value)} is not valid.`;
360
379
  }),
361
- date: string().nullable().trim().matches(
362
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
363
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
364
- ),
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
+ }
395
+ 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(
400
+ "minDate",
401
+ `The date must be ${minDate} or after`,
402
+ (value) => validateMinDate(value, minDate)
403
+ );
404
+ }
405
+ 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(
410
+ "maxDate",
411
+ `The date must be ${maxDate} or before`,
412
+ (value) => validateMaxDate(value, maxDate)
413
+ );
414
+ }
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
+ );
419
+ },
365
420
  number: number().typeError("The value must be a number").nullable(),
366
421
  file: array().nullable(),
367
422
  email: string().trim().email("Please enter a valid email address").nullable(),
@@ -406,6 +461,9 @@ var getYupSchema = ({ inputType, ...field }) => {
406
461
  const optionValues = getOptions(field);
407
462
  return yupSchemas.radioOrSelect(optionValues);
408
463
  }
464
+ if (inputType === "date") {
465
+ return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
466
+ }
409
467
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
410
468
  };
411
469
  function buildYupSchema(field, config) {
@@ -878,8 +936,10 @@ function extractParametersFromNode(schemaNode) {
878
936
  },
879
937
  // Handle [name].presentation
880
938
  ...presentation,
881
- description,
882
- extra: presentation.extra,
939
+ description: containsHTML(description) ? wrapWithSpan(description, {
940
+ class: "jsf-description"
941
+ }) : description,
942
+ extra: containsHTML(presentation.extra) ? wrapWithSpan(presentation.extra, { class: "jsf-extra" }) : presentation.extra,
883
943
  statement: presentation.statement && {
884
944
  ...presentation.statement,
885
945
  description: statementDescription
@@ -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.20230705093926
5
- Generated: Wed, 05 Jul 2023 09:39:42 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.5.0-dev.20230717090149
5
+ Generated: Mon, 17 Jul 2023 09:02:11 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -11321,6 +11321,25 @@ var validateOnlyStrings = StringSchema().trim().nullable().test(
11321
11321
  return true;
11322
11322
  }
11323
11323
  );
11324
+ var compareDates = (d1, d2) => {
11325
+ let date1 = new Date(d1).getTime();
11326
+ let date2 = new Date(d2).getTime();
11327
+ if (date1 < date2) {
11328
+ return "LESSER";
11329
+ } else if (date1 > date2) {
11330
+ return "GREATER";
11331
+ } else {
11332
+ return "EQUAL";
11333
+ }
11334
+ };
11335
+ var validateMinDate = (value, minDate) => {
11336
+ const compare = compareDates(value, minDate);
11337
+ return compare === "GREATER" || compare === "EQUAL" ? true : false;
11338
+ };
11339
+ var validateMaxDate = (value, minDate) => {
11340
+ const compare = compareDates(value, minDate);
11341
+ return compare === "LESSER" || compare === "EQUAL" ? true : false;
11342
+ };
11324
11343
  var yupSchemas = {
11325
11344
  text: validateOnlyStrings,
11326
11345
  radioOrSelect: (options) => StringSchema().nullable().transform((value) => {
@@ -11334,10 +11353,46 @@ var yupSchemas = {
11334
11353
  }).oneOf(options, ({ value }) => {
11335
11354
  return `The option ${JSON.stringify(value)} is not valid.`;
11336
11355
  }),
11337
- date: StringSchema().nullable().trim().matches(
11338
- /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
11339
- `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
11340
- ),
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
+ }
11371
+ 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(
11376
+ "minDate",
11377
+ `The date must be ${minDate} or after`,
11378
+ (value) => validateMinDate(value, minDate)
11379
+ );
11380
+ }
11381
+ 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(
11386
+ "maxDate",
11387
+ `The date must be ${maxDate} or before`,
11388
+ (value) => validateMaxDate(value, maxDate)
11389
+ );
11390
+ }
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
+ );
11395
+ },
11341
11396
  number: NumberSchema().typeError("The value must be a number").nullable(),
11342
11397
  file: array_default().nullable(),
11343
11398
  email: StringSchema().trim().email("Please enter a valid email address").nullable(),
@@ -11382,6 +11437,9 @@ var getYupSchema = ({ inputType, ...field }) => {
11382
11437
  const optionValues = getOptions(field);
11383
11438
  return yupSchemas.radioOrSelect(optionValues);
11384
11439
  }
11440
+ if (inputType === "date") {
11441
+ return yupSchemas.date({ minDate: field.minDate, maxDate: field.maxDate });
11442
+ }
11385
11443
  return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
11386
11444
  };
11387
11445
  function buildYupSchema(field, config) {
@@ -11854,8 +11912,10 @@ function extractParametersFromNode(schemaNode) {
11854
11912
  },
11855
11913
  // Handle [name].presentation
11856
11914
  ...presentation,
11857
- description,
11858
- extra: presentation.extra,
11915
+ description: containsHTML(description) ? wrapWithSpan(description, {
11916
+ class: "jsf-description"
11917
+ }) : description,
11918
+ extra: containsHTML(presentation.extra) ? wrapWithSpan(presentation.extra, { class: "jsf-extra" }) : presentation.extra,
11859
11919
  statement: presentation.statement && {
11860
11920
  ...presentation.statement,
11861
11921
  description: statementDescription
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.5.0-dev.20230705093926",
3
+ "version": "0.5.0-dev.20230717090149",
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",
@@ -1035,6 +1035,62 @@ describe('createHeadlessForm', () => {
1035
1035
  );
1036
1036
  });
1037
1037
 
1038
+ it('support "date" field type with a minDate', () => {
1039
+ const result = createHeadlessForm(schemaInputTypeDate);
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
+ ],
1054
+ });
1055
+
1056
+ const fieldValidator = result.fields[0].schema;
1057
+ 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
+ );
1064
+ });
1065
+
1066
+ it('support "date" field type with a maxDate', () => {
1067
+ const result = createHeadlessForm(schemaInputTypeDate);
1068
+
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
+ ],
1082
+ });
1083
+
1084
+ const fieldValidator = result.fields[0].schema;
1085
+ 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
+ );
1092
+ });
1093
+
1038
1094
  it('supports "file" field type', () => {
1039
1095
  const result = createHeadlessForm(
1040
1096
  JSONSchemaBuilder()