zappyzep/backend/src/validation.test.ts
Dragory ba2873a29a 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.
2019-11-28 02:34:41 +02:00

41 lines
884 B
TypeScript

import { tDeepPartial } from "./utils";
import * as t from "io-ts";
import * as validatorUtils from "./validatorUtils";
import test from "ava";
import util from "util";
test("tDeepPartial works", ava => {
const originalSchema = t.type({
listOfThings: t.record(
t.string,
t.type({
enabled: t.boolean,
someValue: t.number,
}),
),
});
const deepPartialSchema = tDeepPartial(originalSchema);
const partialValidValue = {
listOfThings: {
myThing: {
someValue: 5,
},
},
};
const partialErrorValue = {
listOfThings: {
myThing: {
someValue: "test",
},
},
};
const result1 = validatorUtils.validate(deepPartialSchema, partialValidValue);
ava.is(result1, null);
const result2 = validatorUtils.validate(deepPartialSchema, partialErrorValue);
ava.not(result2, null);
});