Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator #107

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.';
}
}
Loading