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

@ -83,6 +83,17 @@ const report = fold((errors: any): StrictValidationError | void => {
return new StrictValidationError(errorStrings);
}, noop);
export function validate(schema: t.Type<any>, value: any): StrictValidationError | null {
const validationResult = schema.decode(value);
return pipe(
validationResult,
fold(
err => report(validationResult),
result => null,
),
);
}
/**
* Decodes and validates the given value against the given schema while also disallowing extra properties
* See: https://github.com/gcanti/io-ts/issues/322