more env validations
This commit is contained in:
parent
a582d4786d
commit
bc7cd033f2
2 changed files with 34 additions and 9 deletions
|
|
@ -19,7 +19,6 @@ AWS_S3_SECRET_ACCESS_KEY=
|
||||||
AWS_S3_REGION=
|
AWS_S3_REGION=
|
||||||
AWS_S3_BUCKET=
|
AWS_S3_BUCKET=
|
||||||
AWS_S3_ENDPOINT=
|
AWS_S3_ENDPOINT=
|
||||||
AWS_S3_URL=
|
|
||||||
|
|
||||||
# options: smtp | postmark
|
# options: smtp | postmark
|
||||||
MAIL_DRIVER=smtp
|
MAIL_DRIVER=smtp
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,11 @@
|
||||||
import { IsNotEmpty, IsNotIn, IsUrl, validateSync } from 'class-validator';
|
import {
|
||||||
|
IsIn,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsNotIn,
|
||||||
|
IsOptional,
|
||||||
|
IsUrl,
|
||||||
|
validateSync,
|
||||||
|
} from 'class-validator';
|
||||||
import { plainToInstance } from 'class-transformer';
|
import { plainToInstance } from 'class-transformer';
|
||||||
|
|
||||||
export class EnvironmentVariables {
|
export class EnvironmentVariables {
|
||||||
|
|
@ -9,9 +16,28 @@ export class EnvironmentVariables {
|
||||||
)
|
)
|
||||||
DATABASE_URL: string;
|
DATABASE_URL: string;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsUrl(
|
||||||
|
{ protocols: ['redis', 'rediss'], require_tld: false },
|
||||||
|
{ message: 'REDIS_URL must be a valid redis connection string' },
|
||||||
|
)
|
||||||
|
REDIS_URL: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsUrl({ protocols: ['http', 'https'], require_tld: false })
|
||||||
|
APP_URL: string;
|
||||||
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsNotIn(['REPLACE_WITH_LONG_SECRET'])
|
@IsNotIn(['REPLACE_WITH_LONG_SECRET'])
|
||||||
APP_SECRET: string;
|
APP_SECRET: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsIn(['smtp', 'postmark'])
|
||||||
|
MAIL_DRIVER: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsIn(['local', 's3'])
|
||||||
|
STORAGE_DRIVER: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validate(config: Record<string, any>) {
|
export function validate(config: Record<string, any>) {
|
||||||
|
|
@ -19,17 +45,17 @@ export function validate(config: Record<string, any>) {
|
||||||
|
|
||||||
const errors = validateSync(validatedConfig);
|
const errors = validateSync(validatedConfig);
|
||||||
|
|
||||||
|
if (errors.length > 0) {
|
||||||
console.error(
|
console.error(
|
||||||
'The EnvironmentVariables has failed the following validations:',
|
'The Environment variables has failed the following validations:',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (errors.length > 0) {
|
|
||||||
errors.map((error) => {
|
errors.map((error) => {
|
||||||
console.log(JSON.stringify(error.constraints));
|
console.error(JSON.stringify(error.constraints));
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(
|
console.error(
|
||||||
'Please fix the environment variables and try again. Shutting down...',
|
'Please fix the environment variables and try again. Exiting program...',
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue