3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Fix unknown property validation; fix override validation; be clear about which property is unknown

This commit is contained in:
Dragory 2019-07-22 13:09:05 +03:00
parent 6f75185e74
commit 00e34b322d
5 changed files with 14 additions and 6 deletions

5
package-lock.json generated
View file

@ -3853,6 +3853,11 @@
"integrity": "sha1-JJXduvbrh0q7Dhvp3yLS5aVEMmw=", "integrity": "sha1-JJXduvbrh0q7Dhvp3yLS5aVEMmw=",
"dev": true "dev": true
}, },
"deep-diff": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/deep-diff/-/deep-diff-1.0.2.tgz",
"integrity": "sha512-aWS3UIVH+NPGCD1kki+DCU9Dua032iSsO43LqQpcs4R3+dVv7tX0qBGjiVHJHjplsoUM2XRO/KB92glqc68awg=="
},
"deep-extend": { "deep-extend": {
"version": "0.6.0", "version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",

View file

@ -20,6 +20,7 @@
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",
"cross-env": "^5.2.0", "cross-env": "^5.2.0",
"deep-diff": "^1.0.2",
"dotenv": "^4.0.0", "dotenv": "^4.0.0",
"emoji-regex": "^8.0.0", "emoji-regex": "^8.0.0",
"eris": "^0.10.1", "eris": "^0.10.1",

View file

@ -18,7 +18,7 @@ const guildConfigRootSchema = t.type({
levels: t.record(t.string, t.number), levels: t.record(t.string, t.number),
plugins: t.record(t.string, t.unknown), plugins: t.record(t.string, t.unknown),
}); });
const partialGuildConfigRootSchema = t.exact(t.partial(guildConfigRootSchema.props)); const partialGuildConfigRootSchema = t.partial(guildConfigRootSchema.props);
const globalConfigRootSchema = t.type({ const globalConfigRootSchema = t.type({
url: t.string, url: t.string,

View file

@ -63,7 +63,7 @@ export class ZeppelinPlugin<TConfig extends {} = IBasePluginConfig> extends Plug
{}, {},
(this.getStaticDefaultOptions() as any).config || {}, (this.getStaticDefaultOptions() as any).config || {},
options.config || {}, options.config || {},
...options.overrides.slice(0, i), ...options.overrides.slice(0, i).map(o => o.config || {}),
override.config, override.config,
); );
const errors = validateStrict(this.configSchema, merged); const errors = validateStrict(this.configSchema, merged);

View file

@ -2,6 +2,7 @@ import * as t from "io-ts";
import { pipe } from "fp-ts/lib/pipeable"; import { pipe } from "fp-ts/lib/pipeable";
import { fold } from "fp-ts/lib/Either"; import { fold } from "fp-ts/lib/Either";
import { noop } from "./utils"; import { noop } from "./utils";
import deepDiff from "deep-diff";
// From io-ts/lib/PathReporter // From io-ts/lib/PathReporter
function stringify(v) { function stringify(v) {
@ -42,8 +43,8 @@ const report = fold((errors: any) => {
* Validates the given value against the given schema while also disallowing extra properties * Validates the given value against the given schema while also disallowing extra properties
* See: https://github.com/gcanti/io-ts/issues/322 * See: https://github.com/gcanti/io-ts/issues/322
*/ */
export function validateStrict(schema: t.Type<any, any, any>, value: any): string[] | null { export function validateStrict(schema: t.HasProps, value: any): string[] | null {
const validationResult = schema.decode(value); const validationResult = t.exact(schema).decode(value);
return pipe( return pipe(
validationResult, validationResult,
fold( fold(
@ -51,8 +52,9 @@ export function validateStrict(schema: t.Type<any, any, any>, value: any): strin
result => { result => {
// Make sure there are no extra properties // Make sure there are no extra properties
if (JSON.stringify(value) !== JSON.stringify(result)) { if (JSON.stringify(value) !== JSON.stringify(result)) {
// TODO: Actually mention what the unknown property is const diff = deepDiff(result, value);
return ["Found unknown properties"]; const errors = diff.filter(d => d.kind === "N").map(d => `Unknown property <${d.path.join(".")}>`);
return errors.length ? errors : ["Found unknown properties"];
} }
return null; return null;