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

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.20230703082439
5
+ Generated: Mon, 03 Jul 2023 08:24:57 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,48 @@ 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 = [
426
+ ...onlyValues,
427
+ ""
428
+ // [1]
429
+ ];
430
+ if (field.required) {
431
+ return [
432
+ ...optionsBroken,
433
+ null
434
+ // [2]
435
+ ];
436
+ }
437
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
438
+ // option but we don't have direct access at this point.
439
+ // Otherwise the JSON Schema validator will fail as explained in PR#18
440
+ field.jsonType.includes("null");
441
+ if (isOptionalWithNull) {
442
+ return [...optionsBroken, null];
443
+ }
444
+ return optionsBroken;
445
+ };
446
+ var getYupSchema = ({ inputType, ...field }) => {
447
+ const jsonType = getJsonTypeInArray(field.jsonType);
448
+ if (field.options?.length > 0) {
449
+ const optionsAllowed = getOptionsAllowed(field);
450
+ return yupSchemas.radioOrSelect(optionsAllowed);
451
+ }
452
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
453
+ };
422
454
  function buildYupSchema(field, config) {
423
455
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
424
456
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
425
457
  let baseSchema;
426
- const jsonType = getJsonTypeInArray(jsonTypeValue);
427
458
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
428
459
  if (propertyFields.multiple) {
429
460
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
430
461
  } else if (isCheckboxBoolean) {
431
462
  baseSchema = yupSchemas.checkboxBool;
432
463
  } else {
433
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
464
+ baseSchema = getYupSchema(field);
434
465
  }
435
466
  if (!baseSchema) {
436
467
  return import_noop.default;
@@ -939,30 +970,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
939
970
  };
940
971
 
941
972
  // src/calculateConditionalProperties.js
942
- function isFieldRequired(node, inputName) {
943
- if (node?.required) {
944
- return node.required.includes(inputName);
945
- }
946
- return false;
973
+ function isFieldRequired(node, field) {
974
+ return (
975
+ // Check base root required
976
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
977
+ node?.required?.includes(field.name)
978
+ );
947
979
  }
948
- function rebuildInnerFieldsRequiredProperty(fields, property) {
980
+ function rebuildFieldset(fields, property) {
949
981
  if (property?.properties) {
950
982
  return fields.map((field) => {
983
+ const propertyConditionals = property.properties[field.name];
984
+ if (!propertyConditionals) {
985
+ return field;
986
+ }
987
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
951
988
  if (field.fields) {
952
989
  return {
953
990
  ...field,
954
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
991
+ ...newFieldParams,
992
+ fields: rebuildFieldset(field.fields, propertyConditionals)
955
993
  };
956
994
  }
957
995
  return {
958
996
  ...field,
959
- required: isFieldRequired(property, field.name)
997
+ ...newFieldParams,
998
+ required: isFieldRequired(property, field)
960
999
  };
961
1000
  });
962
1001
  }
963
1002
  return fields.map((field) => ({
964
1003
  ...field,
965
- required: isFieldRequired(property, field.name)
1004
+ required: isFieldRequired(property, field)
966
1005
  }));
967
1006
  }
968
1007
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -977,10 +1016,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
977
1016
  });
978
1017
  let fieldSetFields;
979
1018
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
980
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
981
- fieldParams.fields,
982
- conditionalProperty
983
- );
1019
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
984
1020
  newFieldParams.fields = fieldSetFields;
985
1021
  }
986
1022
  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.20230703082439
5
+ Generated: Mon, 03 Jul 2023 08:24:57 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,48 @@ 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 = [
390
+ ...onlyValues,
391
+ ""
392
+ // [1]
393
+ ];
394
+ if (field.required) {
395
+ return [
396
+ ...optionsBroken,
397
+ null
398
+ // [2]
399
+ ];
400
+ }
401
+ const isOptionalWithNull = Array.isArray(field.jsonType) && // @TODO should also check the "oneOf" directly looking for "null"
402
+ // option but we don't have direct access at this point.
403
+ // Otherwise the JSON Schema validator will fail as explained in PR#18
404
+ field.jsonType.includes("null");
405
+ if (isOptionalWithNull) {
406
+ return [...optionsBroken, null];
407
+ }
408
+ return optionsBroken;
409
+ };
410
+ var getYupSchema = ({ inputType, ...field }) => {
411
+ const jsonType = getJsonTypeInArray(field.jsonType);
412
+ if (field.options?.length > 0) {
413
+ const optionsAllowed = getOptionsAllowed(field);
414
+ return yupSchemas.radioOrSelect(optionsAllowed);
415
+ }
416
+ return yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
417
+ };
386
418
  function buildYupSchema(field, config) {
387
419
  const { inputType, jsonType: jsonTypeValue, errorMessage = {}, ...propertyFields } = field;
388
420
  const isCheckboxBoolean = typeof propertyFields.checkboxValue === "boolean";
389
421
  let baseSchema;
390
- const jsonType = getJsonTypeInArray(jsonTypeValue);
391
422
  const errorMessageFromConfig = config?.inputTypes?.[inputType]?.errorMessage || {};
392
423
  if (propertyFields.multiple) {
393
424
  baseSchema = yupSchemas.multiple[inputType] || yupSchemasToJsonTypes.array;
394
425
  } else if (isCheckboxBoolean) {
395
426
  baseSchema = yupSchemas.checkboxBool;
396
427
  } else {
397
- baseSchema = yupSchemas[inputType] || yupSchemasToJsonTypes[jsonType];
428
+ baseSchema = getYupSchema(field);
398
429
  }
399
430
  if (!baseSchema) {
400
431
  return noop;
@@ -903,30 +934,38 @@ var handleValuesChange = (fields, jsonSchema, config) => (values) => {
903
934
  };
904
935
 
905
936
  // src/calculateConditionalProperties.js
906
- function isFieldRequired(node, inputName) {
907
- if (node?.required) {
908
- return node.required.includes(inputName);
909
- }
910
- return false;
937
+ function isFieldRequired(node, field) {
938
+ return (
939
+ // Check base root required
940
+ field.scopedJsonSchema?.required?.includes(field.name) || // Check conditional required
941
+ node?.required?.includes(field.name)
942
+ );
911
943
  }
912
- function rebuildInnerFieldsRequiredProperty(fields, property) {
944
+ function rebuildFieldset(fields, property) {
913
945
  if (property?.properties) {
914
946
  return fields.map((field) => {
947
+ const propertyConditionals = property.properties[field.name];
948
+ if (!propertyConditionals) {
949
+ return field;
950
+ }
951
+ const newFieldParams = extractParametersFromNode(propertyConditionals);
915
952
  if (field.fields) {
916
953
  return {
917
954
  ...field,
918
- fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name])
955
+ ...newFieldParams,
956
+ fields: rebuildFieldset(field.fields, propertyConditionals)
919
957
  };
920
958
  }
921
959
  return {
922
960
  ...field,
923
- required: isFieldRequired(property, field.name)
961
+ ...newFieldParams,
962
+ required: isFieldRequired(property, field)
924
963
  };
925
964
  });
926
965
  }
927
966
  return fields.map((field) => ({
928
967
  ...field,
929
- required: isFieldRequired(property, field.name)
968
+ required: isFieldRequired(property, field)
930
969
  }));
931
970
  }
932
971
  function calculateConditionalProperties(fieldParams, customProperties) {
@@ -941,10 +980,7 @@ function calculateConditionalProperties(fieldParams, customProperties) {
941
980
  });
942
981
  let fieldSetFields;
943
982
  if (fieldParams.inputType === supportedTypes.FIELDSET) {
944
- fieldSetFields = rebuildInnerFieldsRequiredProperty(
945
- fieldParams.fields,
946
- conditionalProperty
947
- );
983
+ fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
948
984
  newFieldParams.fields = fieldSetFields;
949
985
  }
950
986
  const base = {