mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-11 20:55:01 +00:00
Switch from ajv to io-ts for config validation; validate configs on save in the API/dashboard; start work on creating io-ts schemas for all plugins
This commit is contained in:
parent
b230a73a6f
commit
da114c0e60
14 changed files with 256 additions and 41 deletions
|
@ -3,9 +3,11 @@ import passport from "passport";
|
|||
import { AllowedGuilds } from "../data/AllowedGuilds";
|
||||
import { requireAPIToken } from "./auth";
|
||||
import { ApiPermissions } from "../data/ApiPermissions";
|
||||
import { clientError, ok, unauthorized } from "./responses";
|
||||
import { clientError, error, ok, serverError, unauthorized } from "./responses";
|
||||
import { Configs } from "../data/Configs";
|
||||
import { ApiRoles } from "../data/ApiRoles";
|
||||
import { validateGuildConfig } from "../configValidator";
|
||||
import yaml, { YAMLException } from "js-yaml";
|
||||
|
||||
export function initGuildsAPI(app: express.Express) {
|
||||
const guildAPIRouter = express.Router();
|
||||
|
@ -35,6 +37,28 @@ export function initGuildsAPI(app: express.Express) {
|
|||
const config = req.body.config;
|
||||
if (config == null) return clientError(res, "No config supplied");
|
||||
|
||||
// Validate config
|
||||
let parsedConfig;
|
||||
try {
|
||||
parsedConfig = yaml.safeLoad(config);
|
||||
} catch (e) {
|
||||
if (e instanceof YAMLException) {
|
||||
return error(res, e.message, 400);
|
||||
}
|
||||
|
||||
console.error("Error when loading YAML: " + e.message);
|
||||
return serverError(res, "Server error");
|
||||
}
|
||||
|
||||
if (parsedConfig == null) {
|
||||
parsedConfig = {};
|
||||
}
|
||||
|
||||
const errors = validateGuildConfig(parsedConfig);
|
||||
if (errors) {
|
||||
return res.status(422).json({ errors });
|
||||
}
|
||||
|
||||
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
||||
ok(res);
|
||||
});
|
||||
|
|
|
@ -8,7 +8,7 @@ export function error(res: Response, message: string, statusCode: number = 500)
|
|||
res.status(statusCode).json({ error: message });
|
||||
}
|
||||
|
||||
export function serverError(res: Response, message: string) {
|
||||
export function serverError(res: Response, message = "Server error") {
|
||||
error(res, message, 500);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue