parent
d493e74bf3
commit
d291cf0d1b
57
src/errors/AddressErrors.ts
Normal file
57
src/errors/AddressErrors.ts
Normal file
@ -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"
|
||||||
|
}
|
80
src/models/Address.ts
Normal file
80
src/models/Address.ts
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
import {
|
||||||
|
IsPostalCode,
|
||||||
|
IsString
|
||||||
|
} from "class-validator";
|
||||||
|
import ValidatorJS from 'validator';
|
||||||
|
import { config } from '../../config';
|
||||||
|
import { AddressCityEmptyError, AddressCountryEmptyError, AddressFirstLineEmptyError, AddressPostalCodeEmptyError, AddressPostalCodeInvalidError } from '../../errors/AddressErrors';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the Address class.
|
||||||
|
* Implemented this way to prevent any formatting differences.
|
||||||
|
*/
|
||||||
|
export class Address {
|
||||||
|
/**
|
||||||
|
* The address's first line.
|
||||||
|
* Containing the street and house number.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
address1?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The address's second line.
|
||||||
|
* Containing optional information.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
address2?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The address's postal code.
|
||||||
|
* This will get checked against the postal code syntax for the configured country.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsPostalCode(config.postalcode_validation_countrycode)
|
||||||
|
postalcode: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The address's city.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
city: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The address's country.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
country: string;
|
||||||
|
|
||||||
|
public reset() {
|
||||||
|
this.address1 = null;
|
||||||
|
this.address2 = null;
|
||||||
|
this.city = null;
|
||||||
|
this.country = null;
|
||||||
|
this.postalcode = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this is a valid address
|
||||||
|
*/
|
||||||
|
public static isValidAddress(address: Address): Boolean {
|
||||||
|
if (address == null) { return false; }
|
||||||
|
if (address.address1 == null || address.city == null || address.country == null || address.postalcode == null) { return false; }
|
||||||
|
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(); }
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user