@remoteoss/json-schema-form 0.4.0-beta.0 → 0.4.1-beta.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ #### 0.4.1-beta.0 (2023-07-03)
2
+
3
+ ##### Bug Fixes
4
+
5
+ * **fieldset:** support root conditionals for fieldsets ([#23](https://github.com/remoteoss/json-schema-form/pull/23)) ([65d87b3a](https://github.com/remoteoss/json-schema-form/commit/65d87b3a93018f0729aed565000eb2a2ce1f2ce7))
6
+ * **select/radio:** Accept just the values in options (plus `''` and `null` for backward-compatibility) ([#18](https://github.com/remoteoss/json-schema-form/pull/18)) ([37501d2d](https://github.com/remoteoss/json-schema-form/commit/37501d2ddafdd5e207b34d2ca3f6b7b7a1006e9d))
7
+
1
8
  #### 0.4.0-beta.0 (2023-06-22)
2
9
 
3
10
  ##### New Features
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.4.0-beta.0
5
- Generated: Thu, 22 Jun 2023 10:03:23 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.4.1-beta.0
5
+ Generated: Mon, 03 Jul 2023 22:21:10 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -383,8 +383,17 @@ var validateOnlyStrings = (0, import_yup.string)().trim().nullable().test(
383
383
  );
384
384
  var yupSchemas = {
385
385
  text: validateOnlyStrings,
386
- select: (0, import_yup.string)().trim().nullable(),
387
- radio: (0, import_yup.string)().trim().nullable(),
386
+ radioOrSelect: (options) => (0, import_yup.string)().nullable().transform((value) => {
387
+ if (value === "") {
388
+ return void 0;
389
+ }
390
+ if (options?.includes(null)) {
391
+ return value;
392
+ }
393
+ return value === null ? void 0 : value;
394
+ }).oneOf(options, ({ value }) => {
395
+ return `The option ${JSON.stringify(value)} is not valid.`;
396
+ }),
388
397
  date: (0, import_yup.string)().nullable().trim().matches(
389
398
  /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
390
399
  `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
@@ -419,18 +428,33 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
419
428
  return "Required field";
420
429
  }
421
430
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
431
+ var getOptions = (field) => {
432
+ const allValues = field.options?.map((option) => option.value);
433
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
434
+ // option but we don't have direct access at this point.
435
+ // Otherwise the JSON Schema validator will fail as explained in PR#18
436
+ field.jsonType.includes("null");
437
+ return isOptionalWithNull ? [...allValues, null] : allValues;
438
+ };
439
+ var getYupSchema = ({ inputType, ...field }) => {
440
+ const jsonType = getJsonTypeInArray(field.jsonType);
441
+ if (field.options?.length > 0) {
442
+ const optionValues = getOptions(field);
443
+ return yupSchemas.radioOrSelect(optionValues);
444
+ }
445
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
446
+ };
422
447
  function buildYupSchema(field, config) {
423
448
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
424
449
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
425
450
  let baseSchema;
426
- const jsonType = getJsonTypeInArray(jsonTypeValue);
427
451
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
428
452
  if (propertyFields.multiple) {
429
453
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
430
454
  } else if (isCheckboxBoolean) {
431
455
  baseSchema = yupSchemas.checkboxBool;
432
456
  } else {
433
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
457
+ baseSchema = getYupSchema(field);
434
458
  }
435
459
  if (!baseSchema) {
436
460
  return import_noop.default;
@@ -630,8 +654,17 @@ function checkIfConditionMatches(node, formValues, formFields) {
630
654
  if (currentProperty.enum) {
631
655
  return currentProperty.enum.includes(value);
632
656
  }
633
- const { inputType } = getField(name, formFields);
634
- return validateFieldSchema({ ...currentProperty, inputType, required: true }, value);
657
+ const field = getField(name, formFields);
658
+ return validateFieldSchema(
659
+ {
660
+ options: field.options,
661
+ // @TODO/CODE SMELL. We are passing the property (raw field), but buildYupSchema() expected the output field.
662
+ ...currentProperty,
663
+ inputType: field.inputType,
664
+ required: true
665
+ },
666
+ value
667
+ );
635
668
  });
636
669
  }
637
670
  function isFieldFilled(fieldValue) {
@@ -939,30 +972,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
939
972
  };
940
973
 
941
974
  // src/calculateConditionalProperties.js
942
- function isFieldRequired(node, inputName) {
943
- if (node?.required) {
944
- return node.required.includes(inputName);
945
- }
946
- return false;
975
+ function isFieldRequired(node, field) {
976
+ return (
977
+ // Check base root required
978
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
979
+ node?.required?.includes(field.name)
980
+ );
947
981
  }
948
- function rebuildInnerFieldsRequiredProperty(fields, property) {
982
+ function rebuildFieldset(fields, property) {
949
983
  if (property?.properties) {
950
984
  return fields.map((field) => {
985
+ const propertyConditionals = property.properties[field.name];
986
+ if (!propertyConditionals) {
987
+ return field;
988
+ }
989
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
951
990
  if (field.fields) {
952
991
  return {
953
992
  ...field,
954
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
993
+ ...newFieldParams,
994
+ fields: rebuildFieldset(field.fields, propertyConditionals)
955
995
  };
956
996
  }
957
997
  return {
958
998
  ...field,
959
- required: isFieldRequired(property, field.name)
999
+ ...newFieldParams,
1000
+ required: isFieldRequired(property, field)
960
1001
  };
961
1002
  });
962
1003
  }
963
1004
  return fields.map((field) => ({
964
1005
  ...field,
965
- required: isFieldRequired(property, field.name)
1006
+ required: isFieldRequired(property, field)
966
1007
  }));
967
1008
  }
968
1009
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -977,10 +1018,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
977
1018
  });
978
1019
  let fieldSetFields;
979
1020
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
980
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
981
- fieldParams.fields,
982
- conditionalProperty
983
- );
1021
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
984
1022
  newFieldParams.fields = fieldSetFields;
985
1023
  }
986
1024
  const base = {
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.4.0-beta.0
5
- Generated: Thu, 22 Jun 2023 10:03:23 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.4.1-beta.0
5
+ Generated: Mon, 03 Jul 2023 22:21:10 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -347,8 +347,17 @@ var validateOnlyStrings = string().trim().nullable().test(
347
347
  );
348
348
  var yupSchemas = {
349
349
  text: validateOnlyStrings,
350
- select: string().trim().nullable(),
351
- radio: string().trim().nullable(),
350
+ radioOrSelect: (options) => string().nullable().transform((value) => {
351
+ if (value === "") {
352
+ return void 0;
353
+ }
354
+ if (options?.includes(null)) {
355
+ return value;
356
+ }
357
+ return value === null ? void 0 : value;
358
+ }).oneOf(options, ({ value }) => {
359
+ return `The option ${JSON.stringify(value)} is not valid.`;
360
+ }),
352
361
  date: string().nullable().trim().matches(
353
362
  /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
354
363
  `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
@@ -383,18 +392,33 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
383
392
  return "Required field";
384
393
  }
385
394
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
395
+ var getOptions = (field) => {
396
+ const allValues = field.options?.map((option) => option.value);
397
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
398
+ // option but we don't have direct access at this point.
399
+ // Otherwise the JSON Schema validator will fail as explained in PR#18
400
+ field.jsonType.includes("null");
401
+ return isOptionalWithNull ? [...allValues, null] : allValues;
402
+ };
403
+ var getYupSchema = ({ inputType, ...field }) => {
404
+ const jsonType = getJsonTypeInArray(field.jsonType);
405
+ if (field.options?.length > 0) {
406
+ const optionValues = getOptions(field);
407
+ return yupSchemas.radioOrSelect(optionValues);
408
+ }
409
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
410
+ };
386
411
  function buildYupSchema(field, config) {
387
412
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
388
413
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
389
414
  let baseSchema;
390
- const jsonType = getJsonTypeInArray(jsonTypeValue);
391
415
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
392
416
  if (propertyFields.multiple) {
393
417
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
394
418
  } else if (isCheckboxBoolean) {
395
419
  baseSchema = yupSchemas.checkboxBool;
396
420
  } else {
397
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
421
+ baseSchema = getYupSchema(field);
398
422
  }
399
423
  if (!baseSchema) {
400
424
  return noop;
@@ -594,8 +618,17 @@ function checkIfConditionMatches(node, formValues, formFields) {
594
618
  if (currentProperty.enum) {
595
619
  return currentProperty.enum.includes(value);
596
620
  }
597
- const { inputType } = getField(name, formFields);
598
- return validateFieldSchema({ ...currentProperty, inputType, required: true }, value);
621
+ const field = getField(name, formFields);
622
+ return validateFieldSchema(
623
+ {
624
+ options: field.options,
625
+ // @TODO/CODE SMELL. We are passing the property (raw field), but buildYupSchema() expected the output field.
626
+ ...currentProperty,
627
+ inputType: field.inputType,
628
+ required: true
629
+ },
630
+ value
631
+ );
599
632
  });
600
633
  }
601
634
  function isFieldFilled(fieldValue) {
@@ -903,30 +936,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
903
936
  };
904
937
 
905
938
  // src/calculateConditionalProperties.js
906
- function isFieldRequired(node, inputName) {
907
- if (node?.required) {
908
- return node.required.includes(inputName);
909
- }
910
- return false;
939
+ function isFieldRequired(node, field) {
940
+ return (
941
+ // Check base root required
942
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
943
+ node?.required?.includes(field.name)
944
+ );
911
945
  }
912
- function rebuildInnerFieldsRequiredProperty(fields, property) {
946
+ function rebuildFieldset(fields, property) {
913
947
  if (property?.properties) {
914
948
  return fields.map((field) => {
949
+ const propertyConditionals = property.properties[field.name];
950
+ if (!propertyConditionals) {
951
+ return field;
952
+ }
953
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
915
954
  if (field.fields) {
916
955
  return {
917
956
  ...field,
918
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
957
+ ...newFieldParams,
958
+ fields: rebuildFieldset(field.fields, propertyConditionals)
919
959
  };
920
960
  }
921
961
  return {
922
962
  ...field,
923
- required: isFieldRequired(property, field.name)
963
+ ...newFieldParams,
964
+ required: isFieldRequired(property, field)
924
965
  };
925
966
  });
926
967
  }
927
968
  return fields.map((field) => ({
928
969
  ...field,
929
- required: isFieldRequired(property, field.name)
970
+ required: isFieldRequired(property, field)
930
971
  }));
931
972
  }
932
973
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -941,10 +982,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
941
982
  });
942
983
  let fieldSetFields;
943
984
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
944
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
945
- fieldParams.fields,
946
- conditionalProperty
947
- );
985
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
948
986
  newFieldParams.fields = fieldSetFields;
949
987
  }
950
988
  const base = {