This commit is contained in:
parent
9a115e75b1
commit
c07c6aeeab
@ -1,10 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
IsPostalCode,
|
|
||||||
IsString
|
IsString
|
||||||
} from "class-validator";
|
} from "class-validator";
|
||||||
import ValidatorJS from 'validator';
|
|
||||||
import { config } from '../config';
|
|
||||||
import { AddressCityEmptyError, AddressCountryEmptyError, AddressFirstLineEmptyError, AddressPostalCodeEmptyError, AddressPostalCodeInvalidError } from '../errors/AddressErrors';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the Address class.
|
* Defines the Address class.
|
||||||
@ -30,7 +26,6 @@ export class Address {
|
|||||||
* This will get checked against the postal code syntax for the configured country.
|
* This will get checked against the postal code syntax for the configured country.
|
||||||
*/
|
*/
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsPostalCode(config.postalcode_validation_countrycode)
|
|
||||||
postalcode: string;
|
postalcode: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,29 +47,4 @@ export class Address {
|
|||||||
this.country = null;
|
this.country = null;
|
||||||
this.postalcode = 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