Skip to content

Commit

Permalink
feat(v3.2.3): new config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Sep 18, 2021
1 parent 6063c9e commit c9b0d26
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zip3",
"version": "3.2.2",
"version": "3.2.3",
"scripts": {
"prepare": "husky install",
"dev": "NODE_ENV=development node server",
Expand Down
3 changes: 1 addition & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function shouldUseYarn() {

(async () => {
try {
const config = readConfig();
await validateConfig(config);
const config = await validateConfig(readConfig());

const data = await prismaRun(config.core.database_url, ['migrate', 'status'], true);
if (data.includes('Following migration have not yet been applied:')) {
Expand Down
62 changes: 23 additions & 39 deletions server/validateConfig.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
const Logger = require('../src/lib/logger');
const yup = require('yup');

function dot(str, obj) {
return str.split('.').reduce((a,b) => a[b], obj);
}

const path = (path, type) => ({ path, type });
const validator = yup.object({
core: yup.object({
secure: yup.bool().default(false),
secret: yup.string().min(8).required(),
host: yup.string().default('0.0.0.0'),
port: yup.number().default(3000),
database_url: yup.string().required(),
}).required(),
uploader: yup.object({
route: yup.string().required(),
length: yup.number().default(6),
directory: yup.string().required(),
admin_limit: yup.number().default(104900000),
user_limit: yup.number().default(104900000),
disabled_extensions: yup.array().default([]),
}).required(),
});

module.exports = async config => {
const paths = [
path('core.secure', 'boolean'),
path('core.secret', 'string'),
path('core.host', 'string'),
path('core.port', 'number'),
path('core.database_url', 'string'),
path('uploader.route', 'string'),
path('uploader.length', 'number'),
path('uploader.directory', 'string'),
path('uploader.admin_limit', 'number'),
path('uploader.user_limit', 'number'),
path('uploader.disabled_extentions', 'object'),
];

let errors = 0;

for (let i = 0, L = paths.length; i !== L; ++i) {
const path = paths[i];
const value = dot(path.path, config);
if (value === undefined) {
Logger.get('config').error(`there was no ${path.path} in config which was required`);
++errors;
}

const type = typeof value;
if (value !== undefined && type !== path.type) {
Logger.get('config').error(`expected ${path.type} on ${path.path}, but got ${type}`);
++errors;
}
}

if (errors !== 0) {
Logger.get('config').error(`exiting due to ${errors} errors`);
process.exit(1);
}
try {
return await validator.validate(config, { abortEarly: false });
} catch (e) {
throw `${e.errors.length} errors occured\n${e.errors.map(x => '\t' + x).join('\n')}`;
}
};
2 changes: 1 addition & 1 deletion src/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Logger {
}

error(error) {
console.log(this.formatMessage('ERROR', this.name, error.toString()));
console.log(this.formatMessage('ERROR', this.name, error.stack ?? error));
}

formatMessage(level, name, message) {
Expand Down
17 changes: 6 additions & 11 deletions src/lib/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { existsSync, readFileSync } = require('fs');
const { join } = require('path');
const Logger = require('./logger');

const e = (val, type, fn, required = true) => ({ val, type, fn, required });
const e = (val, type, fn) => ({ val, type, fn });

const envValues = [
e('SECURE', 'boolean', (c, v) => c.core.secure = v),
Expand All @@ -15,7 +15,7 @@ const envValues = [
e('UPLOADER_DIRECTORY', 'string', (c, v) => c.uploader.directory = v),
e('UPLOADER_ADMIN_LIMIT', 'number', (c, v) => c.uploader.admin_limit = v),
e('UPLOADER_USER_LIMIT', 'number', (c, v) => c.uploader.user_limit = v),
e('UPLOADER_DISABLED_EXTS', 'array', (c, v) => v ? c.uploader.disabled_extentions = v : c.uploader.disabled_extentions = [], false),
e('UPLOADER_DISABLED_EXTS', 'array', (c, v) => v ? c.uploader.disabled_extentions = v : c.uploader.disabled_extentions = []),
];

module.exports = () => {
Expand Down Expand Up @@ -54,21 +54,16 @@ function tryReadEnv() {
const envValue = envValues[i];
let value = process.env[envValue.val];

if (envValue.required && !value) {
Logger.get('config').error(`there is no config file or required environment variables (${envValue.val})... exiting...`);

process.exit(1);
}

envValues[i].fn(config, value);
if (envValue.required) {
if (!value) {
envValues[i].fn(config, undefined);
} else {
envValues[i].fn(config, value);
if (envValue.type === 'number') value = parseToNumber(value);
else if (envValue.type === 'boolean') value = parseToBoolean(value);
else if (envValue.type === 'array') value = parseToArray(value);
envValues[i].fn(config, value);
}
}

return config;
}

Expand Down

0 comments on commit c9b0d26

Please sign in to comment.