forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation on the env variables config
- Loading branch information
Showing
3 changed files
with
37 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
PORT=3000 | ||
MONGODB_URL=mongodb://127.0.0.1:27017/node-boilerplate | ||
MONGODB_URL=mongodb://localhost:27017/node-boilerplate | ||
JWT_SECRET=thisisasamplesecret | ||
JWT_ACCESS_EXPIRATION_MINUTES=30 | ||
JWT_REFRESH_EXPIRATION_DAYS=30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
const dotenv = require('dotenv'); | ||
const path = require('path'); | ||
const Joi = require('@hapi/joi'); | ||
|
||
dotenv.config({ | ||
path: path.join(__dirname, '../../.env'), | ||
}); | ||
dotenv.config({ path: path.join(__dirname, '../../.env') }); | ||
|
||
const envVarsSchema = Joi.object() | ||
.keys({ | ||
NODE_ENV: Joi.string() | ||
.valid('production', 'development', 'test') | ||
.required(), | ||
PORT: Joi.number().default(3000), | ||
MONGODB_URL: Joi.string() | ||
.required() | ||
.description('Mongo DB url'), | ||
JWT_SECRET: Joi.string() | ||
.required() | ||
.description('JWT secret key'), | ||
JWT_ACCESS_EXPIRATION_MINUTES: Joi.number() | ||
.default(30) | ||
.description('minutes after which access tokens expire'), | ||
JWT_REFRESH_EXPIRATION_DAYS: Joi.number() | ||
.default(30) | ||
.description('days after which refresh tokens expire'), | ||
}) | ||
.unknown(); | ||
|
||
const { value: envVars, error } = envVarsSchema.prefs({ errors: { label: 'key' } }).validate(process.env); | ||
|
||
if (error) { | ||
throw new Error(`Config validation error: ${error.message}`); | ||
} | ||
|
||
module.exports = { | ||
env: process.env.NODE_ENV, | ||
port: process.env.PORT, | ||
env: envVars.NODE_ENV, | ||
port: envVars.PORT, | ||
mongoose: { | ||
url: process.env.MONGODB_URL, | ||
url: envVars.MONGODB_URL, | ||
options: { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true }, | ||
}, | ||
jwt: { | ||
secret: process.env.JWT_SECRET, | ||
accessExpirationMinutes: parseInt(process.env.JWT_ACCESS_EXPIRATION_MINUTES, 10), | ||
refreshExpirationDays: parseInt(process.env.JWT_REFRESH_EXPIRATION_DAYS, 10), | ||
secret: envVars.JWT_SECRET, | ||
accessExpirationMinutes: envVars.JWT_ACCESS_EXPIRATION_MINUTES, | ||
refreshExpirationDays: envVars.JWT_REFRESH_EXPIRATION_DAYS, | ||
}, | ||
}; |