diff --git a/src/errors/AddressErrors.ts b/src/errors/AddressErrors.ts new file mode 100644 index 0000000..300bb31 --- /dev/null +++ b/src/errors/AddressErrors.ts @@ -0,0 +1,57 @@ +import { IsString } from 'class-validator'; +import { BadRequestError } from 'routing-controllers'; + +/** + * Error to throw when an address's postal code fails validation. + */ +export class AddressPostalCodeInvalidError extends BadRequestError { + @IsString() + name = "AddressPostalCodeInvalidError" + + @IsString() + message = "The postal code you provided is invalid. \n Please check if your postal code follows the postal code validation guidelines." +} + +/** + * Error to throw when an non-empty address's first line isn't set. + */ +export class AddressFirstLineEmptyError extends BadRequestError { + @IsString() + name = "AddressFirstLineEmptyError" + + @IsString() + message = "You provided a empty first address line. \n If you want an empty address please set all propertys to null. \n For non-empty addresses the following fields have to be set: address1, postalcode, city, country" +} + +/** + * Error to throw when an non-empty address's postal code isn't set. + */ +export class AddressPostalCodeEmptyError extends BadRequestError { + @IsString() + name = "AddressPostalCodeEmptyError" + + @IsString() + message = "You provided a empty postal code. \n If you want an empty address please set all propertys to null. \n For non-empty addresses the following fields have to be set: address1, postalcode, city, country" +} + +/** + * Error to throw when an non-empty address's city isn't set. + */ +export class AddressCityEmptyError extends BadRequestError { + @IsString() + name = "AddressCityEmptyError" + + @IsString() + message = "You provided a empty city. \n If you want an empty address please set all propertys to null. \n For non-empty addresses the following fields have to be set: address1, postalcode, city, country" +} + +/** + * Error to throw when an non-empty address's country isn't set. + */ +export class AddressCountryEmptyError extends BadRequestError { + @IsString() + name = "AddressCountryEmptyError" + + @IsString() + message = "You provided a empty country. \n If you want an empty address please set all propertys to null. \n For non-empty addresses the following fields have to be set: address1, postalcode, city, country" +} \ No newline at end of file diff --git a/src/models/actions/create/CreateDonor.ts b/src/models/actions/create/CreateDonor.ts index 2a9051f..397a8d7 100644 --- a/src/models/actions/create/CreateDonor.ts +++ b/src/models/actions/create/CreateDonor.ts @@ -29,7 +29,7 @@ export class CreateDonor extends CreateParticipant { newDonor.email = this.email; newDonor.receiptNeeded = this.receiptNeeded; newDonor.address = this.address; - + Address.validate(newDonor.address); if (this.receiptNeeded == true && Address.isValidAddress(newDonor.address) == false) { throw new DonorReceiptAddressNeededError() } diff --git a/src/models/actions/create/CreateGroupContact.ts b/src/models/actions/create/CreateGroupContact.ts index c38682a..c6538e8 100644 --- a/src/models/actions/create/CreateGroupContact.ts +++ b/src/models/actions/create/CreateGroupContact.ts @@ -62,6 +62,7 @@ export class CreateGroupContact { contact.email = this.email; contact.phone = this.phone; contact.address = this.address; - return null; + Address.validate(contact.address); + return contact; } } \ No newline at end of file diff --git a/src/models/actions/create/CreateRunner.ts b/src/models/actions/create/CreateRunner.ts index 83d9ca9..9916ba8 100644 --- a/src/models/actions/create/CreateRunner.ts +++ b/src/models/actions/create/CreateRunner.ts @@ -3,6 +3,7 @@ import { getConnectionManager } from 'typeorm'; import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors'; import { RunnerOrganisationWrongTypeError } from '../../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors'; +import { Address } from '../../entities/Address'; import { Runner } from '../../entities/Runner'; import { RunnerGroup } from '../../entities/RunnerGroup'; import { CreateParticipant } from './CreateParticipant'; @@ -31,6 +32,7 @@ export class CreateRunner extends CreateParticipant { newRunner.email = this.email; newRunner.group = await this.getGroup(); newRunner.address = this.address; + Address.validate(newRunner.address); return newRunner; } diff --git a/src/models/actions/create/CreateRunnerOrganisation.ts b/src/models/actions/create/CreateRunnerOrganisation.ts index 8edcebb..e99d851 100644 --- a/src/models/actions/create/CreateRunnerOrganisation.ts +++ b/src/models/actions/create/CreateRunnerOrganisation.ts @@ -23,6 +23,7 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup { newRunnerOrganisation.name = this.name; newRunnerOrganisation.contact = await this.getContact(); newRunnerOrganisation.address = this.address; + Address.validate(newRunnerOrganisation.address); return newRunnerOrganisation; } diff --git a/src/models/actions/update/UpdateDonor.ts b/src/models/actions/update/UpdateDonor.ts index 3896309..db6a745 100644 --- a/src/models/actions/update/UpdateDonor.ts +++ b/src/models/actions/update/UpdateDonor.ts @@ -35,6 +35,7 @@ export class UpdateDonor extends CreateParticipant { donor.email = this.email; donor.receiptNeeded = this.receiptNeeded; donor.address = this.address; + Address.validate(donor.address); if (this.receiptNeeded == true && Address.isValidAddress(donor.address) == false) { throw new DonorReceiptAddressNeededError() } diff --git a/src/models/actions/update/UpdateRunner.ts b/src/models/actions/update/UpdateRunner.ts index cb24f1f..1202ad0 100644 --- a/src/models/actions/update/UpdateRunner.ts +++ b/src/models/actions/update/UpdateRunner.ts @@ -2,6 +2,7 @@ import { IsInt, IsPositive } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors'; import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors'; +import { Address } from '../../entities/Address'; import { Runner } from '../../entities/Runner'; import { RunnerGroup } from '../../entities/RunnerGroup'; import { CreateParticipant } from '../create/CreateParticipant'; @@ -36,6 +37,7 @@ export class UpdateRunner extends CreateParticipant { runner.email = this.email; runner.group = await this.getGroup(); runner.address = this.address; + Address.validate(runner.address); return runner; } diff --git a/src/models/actions/update/UpdateRunnerOrganisation.ts b/src/models/actions/update/UpdateRunnerOrganisation.ts index ef52c2c..e53ade1 100644 --- a/src/models/actions/update/UpdateRunnerOrganisation.ts +++ b/src/models/actions/update/UpdateRunnerOrganisation.ts @@ -30,6 +30,7 @@ export class UpdateRunnerOrganisation extends CreateRunnerGroup { organisation.name = this.name; organisation.contact = await this.getContact(); organisation.address = this.address; + Address.validate(organisation.address); return organisation; } diff --git a/src/models/entities/Address.ts b/src/models/entities/Address.ts index 038f5d6..ea35f7c 100644 --- a/src/models/entities/Address.ts +++ b/src/models/entities/Address.ts @@ -7,6 +7,7 @@ import { import { Column } from "typeorm"; import ValidatorJS from 'validator'; import { config } from '../../config'; +import { AddressCityEmptyError, AddressCountryEmptyError, AddressFirstLineEmptyError, AddressPostalCodeEmptyError, AddressPostalCodeInvalidError } from '../../errors/AddressErrors'; /** * Defines the Address class. @@ -66,4 +67,19 @@ export class Address { if (ValidatorJS.isPostalCode(address.postalcode, config.postalcode_validation_countrycode) == false) { return false; } return true; } + + /** + * This function validates addresses. + * This is a workaround for non-existant class validation for embedded entities. + * @param address The address that shall get validated. + */ + public static validate(address: Address) { + if (address == null) { return; } + if (address.address1 == null && address.city == null && address.country == null && address.postalcode == null) { return; } + if (address.address1 == null) { throw new AddressFirstLineEmptyError(); } + if (address.postalcode == null) { throw new AddressPostalCodeEmptyError(); } + if (address.city == null) { throw new AddressCityEmptyError(); } + if (address.country == null) { throw new AddressCountryEmptyError(); } + if (ValidatorJS.isPostalCode(address.postalcode.toString(), config.postalcode_validation_countrycode) == false) { throw new AddressPostalCodeInvalidError(); } + } }