feat(v3.2.3): new config validation
This commit is contained in:
parent
6063c9efac
commit
c9b0d2664f
5 changed files with 32 additions and 54 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "zip3",
|
"name": "zip3",
|
||||||
"version": "3.2.2",
|
"version": "3.2.3",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "husky install",
|
"prepare": "husky install",
|
||||||
"dev": "NODE_ENV=development node server",
|
"dev": "NODE_ENV=development node server",
|
||||||
|
|
|
@ -34,8 +34,7 @@ function shouldUseYarn() {
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const config = readConfig();
|
const config = await validateConfig(readConfig());
|
||||||
await validateConfig(config);
|
|
||||||
|
|
||||||
const data = await prismaRun(config.core.database_url, ['migrate', 'status'], true);
|
const data = await prismaRun(config.core.database_url, ['migrate', 'status'], true);
|
||||||
if (data.includes('Following migration have not yet been applied:')) {
|
if (data.includes('Following migration have not yet been applied:')) {
|
||||||
|
|
|
@ -1,45 +1,29 @@
|
||||||
const Logger = require('../src/lib/logger');
|
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 => {
|
module.exports = async config => {
|
||||||
const paths = [
|
try {
|
||||||
path('core.secure', 'boolean'),
|
return await validator.validate(config, { abortEarly: false });
|
||||||
path('core.secret', 'string'),
|
} catch (e) {
|
||||||
path('core.host', 'string'),
|
throw `${e.errors.length} errors occured\n${e.errors.map(x => '\t' + x).join('\n')}`;
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
|
@ -19,7 +19,7 @@ class Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
error(error) {
|
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) {
|
formatMessage(level, name, message) {
|
||||||
|
|
|
@ -2,7 +2,7 @@ const { existsSync, readFileSync } = require('fs');
|
||||||
const { join } = require('path');
|
const { join } = require('path');
|
||||||
const Logger = require('./logger');
|
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 = [
|
const envValues = [
|
||||||
e('SECURE', 'boolean', (c, v) => c.core.secure = v),
|
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_DIRECTORY', 'string', (c, v) => c.uploader.directory = v),
|
||||||
e('UPLOADER_ADMIN_LIMIT', 'number', (c, v) => c.uploader.admin_limit = 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_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 = () => {
|
module.exports = () => {
|
||||||
|
@ -54,21 +54,16 @@ function tryReadEnv() {
|
||||||
const envValue = envValues[i];
|
const envValue = envValues[i];
|
||||||
let value = process.env[envValue.val];
|
let value = process.env[envValue.val];
|
||||||
|
|
||||||
if (envValue.required && !value) {
|
if (!value) {
|
||||||
Logger.get('config').error(`there is no config file or required environment variables (${envValue.val})... exiting...`);
|
envValues[i].fn(config, undefined);
|
||||||
|
} else {
|
||||||
process.exit(1);
|
envValues[i].fn(config, value);
|
||||||
}
|
|
||||||
|
|
||||||
envValues[i].fn(config, value);
|
|
||||||
if (envValue.required) {
|
|
||||||
if (envValue.type === 'number') value = parseToNumber(value);
|
if (envValue.type === 'number') value = parseToNumber(value);
|
||||||
else if (envValue.type === 'boolean') value = parseToBoolean(value);
|
else if (envValue.type === 'boolean') value = parseToBoolean(value);
|
||||||
else if (envValue.type === 'array') value = parseToArray(value);
|
else if (envValue.type === 'array') value = parseToArray(value);
|
||||||
envValues[i].fn(config, value);
|
envValues[i].fn(config, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue