@@ -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(); }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user