2019-07-11 12:23:57 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { pipe } from "fp-ts/lib/pipeable";
|
|
|
|
import { fold } from "fp-ts/lib/Either";
|
2019-07-22 00:09:45 +03:00
|
|
|
import { noop } from "./utils";
|
|
|
|
|
|
|
|
// From io-ts/lib/PathReporter
|
|
|
|
function stringify(v) {
|
|
|
|
if (typeof v === "function") {
|
|
|
|
return t.getFunctionName(v);
|
|
|
|
}
|
|
|
|
if (typeof v === "number" && !isFinite(v)) {
|
|
|
|
if (isNaN(v)) {
|
|
|
|
return "NaN";
|
|
|
|
}
|
|
|
|
return v > 0 ? "Infinity" : "-Infinity";
|
|
|
|
}
|
|
|
|
return JSON.stringify(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// From io-ts/lib/PathReporter
|
|
|
|
// tslint:disable
|
|
|
|
function getContextPath(context) {
|
|
|
|
return context
|
|
|
|
.map(function(_a) {
|
|
|
|
var key = _a.key,
|
|
|
|
type = _a.type;
|
|
|
|
return key + ": " + type.name;
|
|
|
|
})
|
|
|
|
.join("/");
|
|
|
|
}
|
|
|
|
// tslint:enable
|
|
|
|
|
|
|
|
const report = fold((errors: any) => {
|
|
|
|
return errors.map(err => {
|
|
|
|
if (err.message) return err.message;
|
|
|
|
const context = err.context.map(c => c.key).filter(k => k && !k.startsWith("{"));
|
|
|
|
return `Invalid value <${stringify(err.value)}> supplied to <${context.join("/")}>`;
|
|
|
|
});
|
|
|
|
}, noop);
|
2019-07-11 12:23:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
return pipe(
|
|
|
|
validationResult,
|
|
|
|
fold(
|
2019-07-22 00:09:45 +03:00
|
|
|
err => report(validationResult),
|
2019-07-11 12:23:57 +03:00
|
|
|
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"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|