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

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.4.0-beta.0
5
- Generated: Thu, 22 Jun 2023 10:03:23 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.4.1-dev.20230702181843
5
+ Generated: Sun, 02 Jul 2023 18:19:05 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -383,8 +383,9 @@ 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().oneOf(options, ({ value }) => {
387
+ return `The option ${JSON.stringify(value)} is not valid.`;
388
+ }),
388
389
  date: (0, import_yup.string)().nullable().trim().matches(
389
390
  /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
390
391
  `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
@@ -419,18 +420,40 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
419
420
  return "Required field";
420
421
  }
421
422
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
423
+ var getOptionsAllowed = (field) => {
424
+ const onlyValues = field.options?.map((option) => option.value);
425
+ const optionsBroken = [...onlyValues, ""];
426
+ if (field.required) {
427
+ return optionsBroken;
428
+ }
429
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null" option
430
+ // but we don't have direct access at this point. Otherwise
431
+ // the JSON Schema validator will fail because setting jsonType is not enough.
432
+ field.jsonType.includes("null");
433
+ if (isOptionalWithNull) {
434
+ return [...optionsBroken, null];
435
+ }
436
+ return optionsBroken;
437
+ };
438
+ var getYupSchema = ({ inputType, ...field }) => {
439
+ const jsonType = getJsonTypeInArray(field.jsonType);
440
+ if (field.options?.length > 0) {
441
+ const optionsAllowed = getOptionsAllowed(field);
442
+ return yupSchemas.radioOrSelect(optionsAllowed);
443
+ }
444
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
445
+ };
422
446
  function buildYupSchema(field, config) {
423
447
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
424
448
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
425
449
  let baseSchema;
426
- const jsonType = getJsonTypeInArray(jsonTypeValue);
427
450
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
428
451
  if (propertyFields.multiple) {
429
452
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
430
453
  } else if (isCheckboxBoolean) {
431
454
  baseSchema = yupSchemas.checkboxBool;
432
455
  } else {
433
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
456
+ baseSchema = getYupSchema(field);
434
457
  }
435
458
  if (!baseSchema) {
436
459
  return import_noop.default;
@@ -939,30 +962,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
939
962
  };
940
963
 
941
964
  // src/calculateConditionalProperties.js
942
- function isFieldRequired(node, inputName) {
943
- if (node?.required) {
944
- return node.required.includes(inputName);
945
- }
946
- return false;
965
+ function isFieldRequired(node, field) {
966
+ return (
967
+ // Check base root required
968
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
969
+ node?.required?.includes(field.name)
970
+ );
947
971
  }
948
- function rebuildInnerFieldsRequiredProperty(fields, property) {
972
+ function rebuildFieldset(fields, property) {
949
973
  if (property?.properties) {
950
974
  return fields.map((field) => {
975
+ const propertyConditionals = property.properties[field.name];
976
+ if (!propertyConditionals) {
977
+ return field;
978
+ }
979
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
951
980
  if (field.fields) {
952
981
  return {
953
982
  ...field,
954
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
983
+ ...newFieldParams,
984
+ fields: rebuildFieldset(field.fields, propertyConditionals)
955
985
  };
956
986
  }
957
987
  return {
958
988
  ...field,
959
- required: isFieldRequired(property, field.name)
989
+ ...newFieldParams,
990
+ required: isFieldRequired(property, field)
960
991
  };
961
992
  });
962
993
  }
963
994
  return fields.map((field) => ({
964
995
  ...field,
965
- required: isFieldRequired(property, field.name)
996
+ required: isFieldRequired(property, field)
966
997
  }));
967
998
  }
968
999
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -977,10 +1008,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
977
1008
  });
978
1009
  let fieldSetFields;
979
1010
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
980
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
981
- fieldParams.fields,
982
- conditionalProperty
983
- );
1011
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
984
1012
  newFieldParams.fields = fieldSetFields;
985
1013
  }
986
1014
  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-dev.20230702181843
5
+ Generated: Sun, 02 Jul 2023 18:19:05 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -347,8 +347,9 @@ 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().oneOf(options, ({ value }) => {
351
+ return `The option ${JSON.stringify(value)} is not valid.`;
352
+ }),
352
353
  date: string().nullable().trim().matches(
353
354
  /(?:\d){4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[0-1])/,
354
355
  `Must be a valid date in ${DEFAULT_DATE_FORMAT.toLocaleLowerCase()} format. e.g. ${todayDateHint}`
@@ -383,18 +384,40 @@ function getRequiredErrorMessage(inputType, { inlineError, configError }) {
383
384
  return "Required field";
384
385
  }
385
386
  var getJsonTypeInArray = (jsonType) => Array.isArray(jsonType) ? jsonType.find((val) => val !== "null") : jsonType;
387
+ var getOptionsAllowed = (field) => {
388
+ const onlyValues = field.options?.map((option) => option.value);
389
+ const optionsBroken = [...onlyValues, ""];
390
+ if (field.required) {
391
+ return optionsBroken;
392
+ }
393
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null" option
394
+ // but we don't have direct access at this point. Otherwise
395
+ // the JSON Schema validator will fail because setting jsonType is not enough.
396
+ field.jsonType.includes("null");
397
+ if (isOptionalWithNull) {
398
+ return [...optionsBroken, null];
399
+ }
400
+ return optionsBroken;
401
+ };
402
+ var getYupSchema = ({ inputType, ...field }) => {
403
+ const jsonType = getJsonTypeInArray(field.jsonType);
404
+ if (field.options?.length > 0) {
405
+ const optionsAllowed = getOptionsAllowed(field);
406
+ return yupSchemas.radioOrSelect(optionsAllowed);
407
+ }
408
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
409
+ };
386
410
  function buildYupSchema(field, config) {
387
411
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
388
412
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
389
413
  let baseSchema;
390
- const jsonType = getJsonTypeInArray(jsonTypeValue);
391
414
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
392
415
  if (propertyFields.multiple) {
393
416
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
394
417
  } else if (isCheckboxBoolean) {
395
418
  baseSchema = yupSchemas.checkboxBool;
396
419
  } else {
397
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
420
+ baseSchema = getYupSchema(field);
398
421
  }
399
422
  if (!baseSchema) {
400
423
  return noop;
@@ -903,30 +926,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
903
926
  };
904
927
 
905
928
  // src/calculateConditionalProperties.js
906
- function isFieldRequired(node, inputName) {
907
- if (node?.required) {
908
- return node.required.includes(inputName);
909
- }
910
- return false;
929
+ function isFieldRequired(node, field) {
930
+ return (
931
+ // Check base root required
932
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
933
+ node?.required?.includes(field.name)
934
+ );
911
935
  }
912
- function rebuildInnerFieldsRequiredProperty(fields, property) {
936
+ function rebuildFieldset(fields, property) {
913
937
  if (property?.properties) {
914
938
  return fields.map((field) => {
939
+ const propertyConditionals = property.properties[field.name];
940
+ if (!propertyConditionals) {
941
+ return field;
942
+ }
943
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
915
944
  if (field.fields) {
916
945
  return {
917
946
  ...field,
918
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
947
+ ...newFieldParams,
948
+ fields: rebuildFieldset(field.fields, propertyConditionals)
919
949
  };
920
950
  }
921
951
  return {
922
952
  ...field,
923
- required: isFieldRequired(property, field.name)
953
+ ...newFieldParams,
954
+ required: isFieldRequired(property, field)
924
955
  };
925
956
  });
926
957
  }
927
958
  return fields.map((field) => ({
928
959
  ...field,
929
- required: isFieldRequired(property, field.name)
960
+ required: isFieldRequired(property, field)
930
961
  }));
931
962
  }
932
963
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -941,10 +972,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
941
972
  });
942
973
  let fieldSetFields;
943
974
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
944
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
945
- fieldParams.fields,
946
- conditionalProperty
947
- );
975
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
948
976
  newFieldParams.fields = fieldSetFields;
949
977
  }
950
978
  const base = {