3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-18 15:45:03 +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 9a7530cf48
commit 28b4541e79
5 changed files with 14 additions and 6 deletions

View file

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