3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Run a loose pre-check before preprocessStaticConfig

This loose pre-check checks the config schema by treating every object
as partial. This means that if a property exists, it's guaranteed to be
the correct type (e.g. object). However, there's no guarantee that all
or any properties exist.

This allows preprocessStaticConfig implementations to be much less
defensive and thus reduce boilerplate.
This commit is contained in:
Dragory 2019-11-28 02:34:41 +02:00
parent 279a8fe7ae
commit ba2873a29a
4 changed files with 124 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import {
resolveMember,
resolveUser,
resolveUserId,
tDeepPartial,
trimEmptyStartEndLines,
trimIndents,
UnknownUser,
@ -19,7 +20,7 @@ import {
import { Invite, Member, User } from "eris";
import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line
import { performance } from "perf_hooks";
import { decodeAndValidateStrict, StrictValidationError } from "../validatorUtils";
import { decodeAndValidateStrict, StrictValidationError, validate } from "../validatorUtils";
import { SimpleCache } from "../SimpleCache";
const SLOW_RESOLVE_THRESHOLD = 1500;
@ -121,6 +122,13 @@ export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plug
? options.overrides
: (defaultOptions.overrides || []).concat(options.overrides || []);
// Before preprocessing the static config, do a loose check by checking the schema as deeply partial.
// This way the preprocessing function can trust that if a property exists, its value will be the correct (partial) type.
const initialLooseCheck = this.configSchema ? validate(tDeepPartial(this.configSchema), mergedConfig) : null;
if (initialLooseCheck) {
throw initialLooseCheck;
}
mergedConfig = this.preprocessStaticConfig(mergedConfig);
const decodedConfig = this.configSchema ? decodeAndValidateStrict(this.configSchema, mergedConfig) : mergedConfig;