Implemented the getter for loading the postalcodelocale from env

ref #43
This commit is contained in:
Nicolai Ort 2020-12-22 12:38:53 +01:00
parent 1bf6d3d564
commit b8c93bf476
3 changed files with 17 additions and 4 deletions

View File

@ -1,10 +1,13 @@
import { config as configDotenv } from 'dotenv';
import ValidatorJS from 'validator';
configDotenv();
export const config = {
internal_port: parseInt(process.env.APP_PORT) || 4010,
development: process.env.NODE_ENV === "production",
jwt_secret: process.env.JWT_SECRET || "secretjwtsecret",
phone_validation_countrycode: process.env.PHONE_COUNTRYCODE || "ZZ"
phone_validation_countrycode: process.env.PHONE_COUNTRYCODE || "ZZ",
postalcode_validation_countrycode: getPostalCodeLocale()
}
let errors = 0
if (typeof config.internal_port !== "number") {
@ -19,4 +22,13 @@ if (config.phone_validation_countrycode.length !== 2) {
if (typeof config.development !== "boolean") {
errors++
}
function getPostalCodeLocale(): any {
try {
const stringArray: String[] = ValidatorJS.isPostalCodeLocales;
let index = stringArray.indexOf(process.env.POSTALCODE_COUNTRYCODE);
return ValidatorJS.isPostalCodeLocales[index];
} catch (error) {
return null;
}
}
export let e = errors

View File

@ -1,4 +1,5 @@
import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator';
import { config } from '../../config';
import { Address } from '../entities/Address';
/**
@ -35,7 +36,7 @@ export class CreateAddress {
*/
@IsString()
@IsNotEmpty()
@IsPostalCode("DE")
@IsPostalCode(config.postalcode_validation_countrycode)
postalcode: string;
/**

View File

@ -6,6 +6,7 @@ import {
IsString
} from "class-validator";
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { config } from '../../config';
import { Participant } from "./Participant";
import { RunnerOrganisation } from "./RunnerOrganisation";
@ -52,12 +53,11 @@ export class Address {
/**
* The address's postal code.
* This will get checked against the postal code syntax for the configured country.
* TODO: Implement the config option.
*/
@Column()
@IsString()
@IsNotEmpty()
@IsPostalCode("DE")
@IsPostalCode(config.postalcode_validation_countrycode)
postalcode: string;
/**