feat(v3.2.3): new config validation

This commit is contained in:
diced 2021-09-17 21:38:24 -07:00
parent 6063c9efac
commit c9b0d2664f
No known key found for this signature in database
GPG key ID: 85AB64C74535D76E
5 changed files with 32 additions and 54 deletions

View file

@ -1,6 +1,6 @@
{
"name": "zip3",
"version": "3.2.2",
"version": "3.2.3",
"scripts": {
"prepare": "husky install",
"dev": "NODE_ENV=development node server",

View file

@ -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:')) {

View file

@ -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')}`;
}
};

View file

@ -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) {

View file

@ -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),
@ -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 = () => {
@ -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);
}
if (!value) {
envValues[i].fn(config, undefined);
} else {
envValues[i].fn(config, value);
if (envValue.required) {
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;
}