-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from bcgov/feature/frontend-validator
Feature / Frontend File-upload/Input-param Validations and Validators
- Loading branch information
Showing
18 changed files
with
392 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,95 @@ | ||
import { ValidationBase } from './validationBase' | ||
import { NUM_INPUT_LIMITS } from '@/constants/constants' | ||
import Papa from 'papaparse' | ||
import { FileUploadValidator } from './fileUploadValidator' | ||
|
||
export class FileUploadValidation extends ValidationBase { | ||
validateRequiredFields( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
ageIncrement: number | null, | ||
): boolean { | ||
return ( | ||
startingAge !== null && finishingAge !== null && ageIncrement !== null | ||
const fileUploadValidator = new FileUploadValidator() | ||
|
||
export const validateComparison = ( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
) => { | ||
if (!fileUploadValidator.validateAgeComparison(startingAge, finishingAge)) { | ||
return { isValid: false } | ||
} | ||
|
||
return { isValid: true } | ||
} | ||
|
||
export const validateRequiredFields = ( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
ageIncrement: number | null, | ||
) => { | ||
if ( | ||
!fileUploadValidator.validateRequiredFields( | ||
startingAge, | ||
finishingAge, | ||
ageIncrement, | ||
) | ||
) { | ||
return { isValid: false } | ||
} | ||
return { isValid: true } | ||
} | ||
|
||
export const validateRange = ( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
ageIncrement: number | null, | ||
) => { | ||
if (!fileUploadValidator.validateStartingAgeRange(startingAge)) { | ||
return { | ||
isValid: false, | ||
errorType: 'startingAge', | ||
} | ||
} | ||
|
||
if (!fileUploadValidator.validateFinishingAgeRange(finishingAge)) { | ||
return { | ||
isValid: false, | ||
errorType: 'finishingAge', | ||
} | ||
} | ||
|
||
validateAgeComparison( | ||
finishingAge: number | null, | ||
startingAge: number | null, | ||
): boolean { | ||
if (finishingAge !== null && startingAge !== null) { | ||
return finishingAge >= startingAge | ||
if (!fileUploadValidator.validateAgeIncrementRange(ageIncrement)) { | ||
return { | ||
isValid: false, | ||
errorType: 'ageIncrement', | ||
} | ||
return true | ||
} | ||
|
||
validateStartingAgeRange(startingAge: number | null): boolean { | ||
if (startingAge !== null) { | ||
return ( | ||
startingAge >= NUM_INPUT_LIMITS.STARTING_AGE_MIN && | ||
startingAge <= NUM_INPUT_LIMITS.STARTING_AGE_MAX | ||
) | ||
return { isValid: true } | ||
} | ||
|
||
export const validateFiles = async ( | ||
layerFile: File | null, | ||
polygonFile: File | null, | ||
) => { | ||
if (!layerFile) { | ||
return { | ||
isValid: false, | ||
errorType: 'layerFileMissing', | ||
} | ||
return true | ||
} | ||
|
||
validateFinishingAgeRange(finishingAge: number | null): boolean { | ||
if (finishingAge !== null) { | ||
return ( | ||
finishingAge >= NUM_INPUT_LIMITS.FINISHING_AGE_MIN && | ||
finishingAge <= NUM_INPUT_LIMITS.FINISHING_AGE_MAX | ||
) | ||
if (!polygonFile) { | ||
return { | ||
isValid: false, | ||
errorType: 'polygonFileMissing', | ||
} | ||
return true | ||
} | ||
|
||
validateAgeIncrementRange(ageIncrement: number | null): boolean { | ||
if (ageIncrement !== null) { | ||
return ( | ||
ageIncrement >= NUM_INPUT_LIMITS.AGE_INC_MIN && | ||
ageIncrement <= NUM_INPUT_LIMITS.AGE_INC_MAX | ||
) | ||
if (!(await fileUploadValidator.isCSVFile(layerFile))) { | ||
return { | ||
isValid: false, | ||
errorType: 'layerFileNotCSVFormat', | ||
} | ||
return true | ||
} | ||
|
||
async isCSVFile(file: File): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
Papa.parse(file, { | ||
complete: (results: any) => { | ||
resolve(results.errors.length === 0) | ||
}, | ||
error: () => resolve(false), | ||
}) | ||
}) | ||
if (!(await fileUploadValidator.isCSVFile(polygonFile))) { | ||
return { | ||
isValid: false, | ||
errorType: 'polygonFileNotCSVFormat', | ||
} | ||
} | ||
|
||
return { isValid: true } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ValidationBase } from './validationBase' | ||
import { NUM_INPUT_LIMITS } from '@/constants/constants' | ||
|
||
export class FileUploadValidator extends ValidationBase { | ||
validateRequiredFields( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
ageIncrement: number | null, | ||
): boolean { | ||
return ( | ||
startingAge !== null && finishingAge !== null && ageIncrement !== null | ||
) | ||
} | ||
|
||
validateAgeComparison( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
): boolean { | ||
if (startingAge !== null && finishingAge !== null) { | ||
return finishingAge >= startingAge | ||
} | ||
return true | ||
} | ||
|
||
validateStartingAgeRange(startingAge: number | null): boolean { | ||
if (startingAge !== null) { | ||
return ( | ||
startingAge >= NUM_INPUT_LIMITS.STARTING_AGE_MIN && | ||
startingAge <= NUM_INPUT_LIMITS.STARTING_AGE_MAX | ||
) | ||
} | ||
return true | ||
} | ||
|
||
validateFinishingAgeRange(finishingAge: number | null): boolean { | ||
if (finishingAge !== null) { | ||
return ( | ||
finishingAge >= NUM_INPUT_LIMITS.FINISHING_AGE_MIN && | ||
finishingAge <= NUM_INPUT_LIMITS.FINISHING_AGE_MAX | ||
) | ||
} | ||
return true | ||
} | ||
|
||
validateAgeIncrementRange(ageIncrement: number | null): boolean { | ||
if (ageIncrement !== null) { | ||
return ( | ||
ageIncrement >= NUM_INPUT_LIMITS.AGE_INC_MIN && | ||
ageIncrement <= NUM_INPUT_LIMITS.AGE_INC_MAX | ||
) | ||
} | ||
return true | ||
} | ||
|
||
async isCSVFile(file: File): Promise<boolean> { | ||
// Check file extension | ||
const fileExtension = file.name.split('.').pop()?.toLowerCase() | ||
if (fileExtension !== 'csv') { | ||
return false | ||
} | ||
|
||
// Check MIME type | ||
const validMimeType = 'text/csv' | ||
if (file.type !== validMimeType) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * as speciesInfoValidation from './speciesInfoValidation' | ||
export * as siteInfoValidation from './siteInfoValidation' | ||
export * as standDensityValidation from './standDensityValidation' | ||
export * as reportInfoValidation from './reportInfoValidation' | ||
export * as fileUploadValidation from './fileUploadValidation' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,43 @@ | ||
import { ValidationBase } from './validationBase' | ||
import { NUM_INPUT_LIMITS } from '@/constants/constants' | ||
import { ReportInfoValidator } from '@/validation/reportInfoValidator' | ||
|
||
export class ReportInfoValidation extends ValidationBase { | ||
validateAgeComparison( | ||
finishingAge: number | null, | ||
startingAge: number | null, | ||
): boolean { | ||
if (finishingAge !== null && startingAge !== null) { | ||
return finishingAge >= startingAge | ||
} | ||
return true | ||
const reportInfoValidator = new ReportInfoValidator() | ||
|
||
export const validateComparison = ( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
) => { | ||
if (!reportInfoValidator.validateAgeComparison(startingAge, finishingAge)) { | ||
return { isValid: false } | ||
} | ||
|
||
validateStartingAgeRange(startingAge: number | null): boolean { | ||
if (startingAge !== null) { | ||
return ( | ||
startingAge >= NUM_INPUT_LIMITS.STARTING_AGE_MIN && | ||
startingAge <= NUM_INPUT_LIMITS.STARTING_AGE_MAX | ||
) | ||
return { isValid: true } | ||
} | ||
|
||
export const validateRange = ( | ||
startingAge: number | null, | ||
finishingAge: number | null, | ||
ageIncrement: number | null, | ||
) => { | ||
if (!reportInfoValidator.validateStartingAgeRange(startingAge)) { | ||
return { | ||
isValid: false, | ||
errorType: 'startingAge', | ||
} | ||
return true | ||
} | ||
|
||
validateFinishingAgeRange(finishingAge: number | null): boolean { | ||
if (finishingAge !== null) { | ||
return ( | ||
finishingAge >= NUM_INPUT_LIMITS.FINISHING_AGE_MIN && | ||
finishingAge <= NUM_INPUT_LIMITS.FINISHING_AGE_MAX | ||
) | ||
if (!reportInfoValidator.validateFinishingAgeRange(finishingAge)) { | ||
return { | ||
isValid: false, | ||
errorType: 'finishingAge', | ||
} | ||
return true | ||
} | ||
|
||
validateAgeIncrementRange(ageIncrement: number | null): boolean { | ||
if (ageIncrement !== null) { | ||
return ( | ||
ageIncrement >= NUM_INPUT_LIMITS.AGE_INC_MIN && | ||
ageIncrement <= NUM_INPUT_LIMITS.AGE_INC_MAX | ||
) | ||
if (!reportInfoValidator.validateAgeIncrementRange(ageIncrement)) { | ||
return { | ||
isValid: false, | ||
errorType: 'ageIncrement', | ||
} | ||
return true | ||
} | ||
|
||
return { isValid: true } | ||
} |
Oops, something went wrong.