Skip to content

Commit

Permalink
feat(Joi + Jest): Add joi configuration and jest elements
Browse files Browse the repository at this point in the history
  • Loading branch information
carrilloapps committed Sep 16, 2024
1 parent d9ffa3e commit ae149e9
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 26 deletions.
203 changes: 202 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "rimraf dist && tsc",
"start": "npm run build && node dist/app.js",
"dev": "nodemon src/app.ts",
"dev": "nodemon --exec ts-node src/app.ts",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand Down Expand Up @@ -62,6 +62,7 @@
"!**/*.util.(t|j)s",
"!**/*.gateway.(t|j)s",
"!**/*.schema.(t|j)s",
"!**/*.env.(t|j)s",
"!**/*.dto.(t|j)s",
"!**/*.module.(t|j)s"
],
Expand All @@ -70,7 +71,8 @@
},
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.17.2"
"express": "^4.17.2",
"joi": "^17.13.3"
},
"devDependencies": {
"@eslint/js": "^9.10.0",
Expand All @@ -89,6 +91,7 @@
"nodemon": "^3.0.1",
"rimraf": "^4.0.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.0.0",
"typescript-eslint": "^8.5.0"
}
Expand Down
23 changes: 23 additions & 0 deletions src/app.env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as dotenv from 'dotenv';
import schema from './app.schema';

dotenv.config();

const { error, value: env } = schema.validate(process.env,
{ abortEarly: false }
);

if (error) {
console.error('Config validation error(s):');
error.details.forEach(detail => {
console.error(`- ${detail.message}`);
});
throw new Error('Environment variables validation failed.');
}

export default {
nodeEnv: env.NODE_ENV,
port: env.PORT,
databaseUrl: env.DATABASE_URL,
apiKey: env.API_KEY,
}
10 changes: 10 additions & 0 deletions src/app.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Joi from 'joi';

const schema = Joi.object({
NODE_ENV: Joi.string().valid('development', 'production', 'test').default('development'),
PORT: Joi.number().default(3000),
DATABASE_URL: Joi.string().uri().optional().default('db://localhost:5432'),
API_KEY: Joi.string().optional().default('sfn-notification-hub'),
}).unknown();

export default schema;
14 changes: 0 additions & 14 deletions src/app.spec.ts

This file was deleted.

Loading

0 comments on commit ae149e9

Please sign in to comment.