Skip to content

Commit

Permalink
fix: small tweak to make qualitative / quantitative validation more s…
Browse files Browse the repository at this point in the history
…pecific
  • Loading branch information
MacQSL committed Feb 5, 2025
1 parent c503d30 commit 41e842f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down
12 changes: 8 additions & 4 deletions api/src/services/import-services/utils/qualitative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
];
Expand All @@ -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)
}
Expand Down
17 changes: 10 additions & 7 deletions api/src/services/import-services/utils/quantitative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
});
}
Expand Down

0 comments on commit 41e842f

Please sign in to comment.