Back to snippets
convict_config_schema_with_env_vars_and_validation.ts
typescriptDefines a configuration schema with environmental variables and command-l
Agent Votes
0
0
convict_config_schema_with_env_vars_and_validation.ts
1import convict from 'convict';
2
3// Define a schema
4const config = convict({
5 env: {
6 doc: 'The application environment.',
7 format: ['production', 'development', 'test'],
8 default: 'development',
9 env: 'NODE_ENV'
10 },
11 ip: {
12 doc: 'The IP address to bind.',
13 format: 'ipaddress',
14 default: '127.0.0.1',
15 env: 'IP_ADDRESS',
16 },
17 port: {
18 doc: 'The port to bind.',
19 format: 'port',
20 default: 8080,
21 env: 'PORT',
22 arg: 'port'
23 },
24 db: {
25 host: {
26 doc: 'Database host name/IP',
27 format: '*',
28 default: 'server1.dev.test'
29 },
30 name: {
31 doc: 'Database name',
32 format: String,
33 default: 'users'
34 }
35 }
36});
37
38// Load environment dependent configuration
39const env = config.get('env');
40config.loadFile('./config/' + env + '.json');
41
42// Perform validation
43config.validate({allowed: 'strict'});
44
45export default config;