Skip to content

Commit

Permalink
Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea committed Nov 18, 2024
1 parent a6c36a6 commit 08b10cc
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
export class RestorationProjectParamsDto {}
import { SEQUESTRATION_RATE_TIER_TYPES } from '@shared/entities/carbon-inputs/sequestration-rate.entity';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';
import { IsNotEmpty, IsNumber, IsEnum, Validate } from 'class-validator';
import { ValidateEcosystemForTier2SequestrationRate } from '@api/modules/custom-projects/validation/sequestration-rate-ecosystem.validator';
import { ValidateCountryForActivityType } from '../validation/ecosystem-country.validator';

export class RestorationProjectParamsDto {
@Validate(ValidateCountryForActivityType)
countryCode: string;

@Validate(ValidateEcosystemForTier2SequestrationRate)
ecosystem: ECOSYSTEM;
@IsEnum(SEQUESTRATION_RATE_TIER_TYPES)
sequestrationRateUsed: SEQUESTRATION_RATE_TIER_TYPES;

@IsNotEmpty({
message: 'Planting success rate is required for restoration projects',
})
@IsNumber()
plantingSuccessRate: number;

@IsNotEmpty({
message:
'Project Specific Sequestration Rate is required for restoration projects',
})
@IsNumber()
projectSpecificSequestrationRate: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';
import { RESTORATION_ACTIVITY_SUBTYPE } from '@shared/entities/activity.enum';

@ValidatorConstraint({
name: 'ValidateCountryForActivityType',
async: false,
})
export class ValidateCountryForActivityType
implements ValidatorConstraintInterface
{
validate(country: string, args: ValidationArguments): boolean {
const obj = args.object as any;

return !(
(obj.ecosystem === ECOSYSTEM.MANGROVE &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYBRID &&
country.toUpperCase() in
['IDN', 'AUS', 'BHS', 'KEN', 'COL', 'IND', 'CHN', 'ECU']) ||
(obj.ecosystem === ECOSYSTEM.MANGROVE &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY &&
country.toUpperCase() === 'ECU') ||
(obj.ecosystem === ECOSYSTEM.SEAGRASS &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.PLANTING &&
country.toUpperCase() === 'ECU') ||
(obj.ecosystem === ECOSYSTEM.SEAGRASS &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYBRID) ||
(obj.ecosystem === ECOSYSTEM.SEAGRASS &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY) ||
(obj.ecosystem === ECOSYSTEM.SALT_MARSH &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.PLANTING &&
country.toUpperCase() === 'ECU') ||
(obj.ecosystem === ECOSYSTEM.SALT_MARSH &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYBRID &&
country.toUpperCase() in
['IDN', 'AUS', 'BHS', 'KEN', 'COL', 'IND', 'CHN', 'ECU']) ||
(obj.ecosystem === ECOSYSTEM.SALT_MARSH &&
obj.restorationActivitySubtype ===
RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY &&
country.toUpperCase() === 'ECU')
);
}

defaultMessage(): string {
return 'No data available for the selected country and activity subtype type.';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';
import { SEQUESTRATION_RATE_TIER_TYPES } from '@shared/entities/carbon-inputs/sequestration-rate.entity';

@ValidatorConstraint({
name: 'ValidateEcosystemForTier2SequestrationRate',
async: false,
})
export class ValidateEcosystemForTier2SequestrationRate
implements ValidatorConstraintInterface
{
validate(value: ECOSYSTEM, args: ValidationArguments): boolean {
const obj = args.object as any;

return !(
obj.emissionFactorUsed === SEQUESTRATION_RATE_TIER_TYPES.TIER_2 &&
value !== ECOSYSTEM.MANGROVE
);
}

defaultMessage(): string {
return 'No default tier 2 data available for seagrass and salt marsh.';
}
}

0 comments on commit 08b10cc

Please sign in to comment.