diff --git a/api/src/services/import-services/measurement/utils/measurement-dynamic-headers-config.ts b/api/src/services/import-services/measurement/utils/measurement-dynamic-headers-config.ts index 8f393c0f68..8cfdbae782 100644 --- a/api/src/services/import-services/measurement/utils/measurement-dynamic-headers-config.ts +++ b/api/src/services/import-services/measurement/utils/measurement-dynamic-headers-config.ts @@ -88,13 +88,13 @@ export const validateQualitativeMeasurementCell = ( params: CSVParams, measurement: CBQualitativeMeasurementTypeDefinition ): CSVError[] => { + const options = measurement.options.map((option) => ({ + option_id: option.qualitative_option_id, + option_name: option.option_label + })); + // Normalize the measurement type definition and validate - const result = validateQualitativeValue(params.cell, { - options: measurement.options.map((option) => ({ - option_id: option.qualitative_option_id, - option_name: option.option_label - })) - }); + const result = validateQualitativeValue(params.cell, { options: options }, 'measurement'); // If the result is list of CSV errors if (typeof result !== 'string') { @@ -124,10 +124,14 @@ export const validateQuantitativeMeasurementCell = ( measurement: CBQuantitativeMeasurementTypeDefinition ): CSVError[] => { // Normalize the measurement type definition and validate - const result = validateQuantitativeValue(params.cell, { - min: measurement.min_value, - max: measurement.max_value - }); + const result = validateQuantitativeValue( + params.cell, + { + min: measurement.min_value, + max: measurement.max_value + }, + 'measurement' + ); // If the result is list of CSV errors if (typeof result !== 'number') { diff --git a/api/src/services/import-services/observation/utils/environment-dynamic-headers-config.ts b/api/src/services/import-services/observation/utils/environment-dynamic-headers-config.ts index b0035d49d9..e9dc222465 100644 --- a/api/src/services/import-services/observation/utils/environment-dynamic-headers-config.ts +++ b/api/src/services/import-services/observation/utils/environment-dynamic-headers-config.ts @@ -74,13 +74,13 @@ export const validateQualitativeEnvironmentCell = ( params: CSVParams, environment: QualitativeEnvironmentTypeDefinition ): CSVError[] => { + const options = environment.options.map((option) => ({ + option_id: option.environment_qualitative_option_id, + option_name: option.name + })); + // Normalize the environment type definition and validate the cell - const result = validateQualitativeValue(params.cell, { - options: environment.options.map((option) => ({ - option_id: option.environment_qualitative_option_id, - option_name: option.name - })) - }); + const result = validateQualitativeValue(params.cell, { options: options }, 'environment'); // If the result is not a qualitative value it is a list of CSV errors if (typeof result !== 'string') { @@ -113,10 +113,14 @@ export const validateQuantitativeEnvironmentCell = ( environment: QuantitativeEnvironmentTypeDefinition ): CSVError[] => { // Normalize the environment type definition and validate the cell - const result = validateQuantitativeValue(params.cell, { - min: environment.min, - max: environment.max - }); + const result = validateQuantitativeValue( + params.cell, + { + min: environment.min, + max: environment.max + }, + 'environment' + ); // If the result is not a quantitative value it is a list of CSV errors if (typeof result !== 'number') { diff --git a/api/src/services/import-services/utils/qualitative.ts b/api/src/services/import-services/utils/qualitative.ts index 2b94fcf4d6..bf616cae88 100644 --- a/api/src/services/import-services/utils/qualitative.ts +++ b/api/src/services/import-services/utils/qualitative.ts @@ -32,16 +32,20 @@ interface IQualitativeTypeDefinitionStub { * * @param {unknown} value - The value to validate * @param {IQualitativeTypeDefinitionStub} qualitativeTypeDefinition - The qualitative type definition + * @param {string} [qualitativeTag] - The tag to inject into the error message ie: 'measurement' or 'environment' * @returns {CSVError[] | string} - The list of errors or the qualitative value */ export const validateQualitativeValue = ( value: unknown, - qualitativeTypeDefinition: IQualitativeTypeDefinitionStub + qualitativeTypeDefinition: IQualitativeTypeDefinitionStub, + qualitativeTag?: string ): CSVError[] | string => { + const qualitativeTagString = qualitativeTag ? ` ${qualitativeTag}` : ''; + if (typeof value !== 'string') { return [ { - error: 'Qualitative value must be a string', + error: `Qualitative${qualitativeTagString} value must be a string`, solution: 'Update the value to match the expected type' } ]; @@ -51,11 +55,11 @@ export const validateQualitativeValue = ( (option) => option.option_name.toLowerCase() === value.toLowerCase() ); - // Validate value is an alowed qualitative environment option + // Validate value is an alowed qualitative option if (!matchingQualitativeOption) { return [ { - error: `Invalid qualitative option`, + error: `Invalid qualitative${qualitativeTagString} option`, solution: `Use a valid qualitative option`, values: qualitativeTypeDefinition.options.map((option) => option.option_name) } diff --git a/api/src/services/import-services/utils/quantitative.ts b/api/src/services/import-services/utils/quantitative.ts index 311895a6c9..7cfe4359e5 100644 --- a/api/src/services/import-services/utils/quantitative.ts +++ b/api/src/services/import-services/utils/quantitative.ts @@ -24,36 +24,39 @@ interface IQuantitativeTypeDefinitionStub { * * @param {unknown} value - The value to validate * @param {IQuantitativeTypeDefinitionStub} quantitativeTypeDefinition - The quantitative type definition + * @param {string} [quantitativeTag] - The tag to inject into the error message ie: 'measurement' or 'environment' * @returns {CSVError[] | number} - The list of errors or the quantitative value */ export const validateQuantitativeValue = ( value: unknown, - quantitativeTypeDefinition: IQuantitativeTypeDefinitionStub + quantitativeTypeDefinition: IQuantitativeTypeDefinitionStub, + quantitativeTag?: string ): CSVError[] | number => { const errors: CSVError[] = []; + const quantitativeTagString = quantitativeTag ? ` ${quantitativeTag}` : ''; - // Quantitative environments are numbers ie: antler count: 2 + // Quantitative are numbers ie: antler count: 2 if (typeof value !== 'number') { return [ { - error: 'Quantitative value must be a number', + error: `Quantitative${quantitativeTagString} value must be a number`, solution: 'Update the value to match the expected type' } ]; } - // Validate value is withing the environment min max bounds + // Validate value is within min max bounds if (quantitativeTypeDefinition.max != null && value > quantitativeTypeDefinition.max) { errors.push({ - error: 'Quantitative value too large', + error: `Quantitative${quantitativeTagString} value too large`, solution: `Value must be less than or equal to ${quantitativeTypeDefinition.max}` }); } - // Validate value is withing the environment min max bounds + // Validate value is within the min max bounds if (quantitativeTypeDefinition.min != null && value < quantitativeTypeDefinition.min) { errors.push({ - error: 'Quantitative value too small', + error: `Quantitative${quantitativeTagString} value too small`, solution: `Value must be greater than or equal to ${quantitativeTypeDefinition.min}` }); }