-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.validation.ts
More file actions
86 lines (67 loc) · 1.61 KB
/
Copy pathenv.validation.ts
File metadata and controls
86 lines (67 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { plainToInstance } from 'class-transformer';
import {
IsEnum,
IsNumber,
IsString,
validateSync,
// ValidationError,
} from 'class-validator';
enum Environment {
Development = 'development',
Production = 'production',
Test = 'test',
Provision = 'provision',
}
class EnvironmentVariables {
@IsEnum(Environment)
NODE_ENV: Environment;
@IsNumber()
PORT: number;
@IsNumber()
DB_PORT: number;
@IsString()
DB_HOST: string;
@IsString()
DB_USENAME: string;
@IsString()
DB_PWD: string;
@IsString()
DB_NAME: string;
@IsString()
EMAIL_HOST: string;
@IsNumber()
EMAIL_PORT: number;
@IsString()
EMAIL_USER: string;
@IsString()
EMAIL_PASS: string;
@IsString()
FRONT_END_URL: string;
@IsString()
SECRET: string;
}
export function validate(config: Record<string, unknown>) {
// console.log('config: ', config);
const validatedConfig = plainToInstance(EnvironmentVariables, config, {
enableImplicitConversion: true,
});
// console.log(validatedConfig);
const errors = validateSync(validatedConfig, {
skipMissingProperties: false,
});
if (errors.length > 0) {
throw new Error(errors.toString());
}
// if (errors.length > 0) {
// const errorMessages = errors
// .map((err: ValidationError) => {
// const constraints = err.constraints
// ? Object.values(err.constraints).join(', ')
// : 'Unknown error';
// return `${err.property}: ${constraints}`;
// })
// .join(' | ');
// throw new Error(`Environment validation error(s): ${errorMessages}`);
// }
return validatedConfig;
}