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

Add tNormalizedNullOrUndefined and tNormalizedNullOptional

tNormalizedNullOrUndefined:
io-ts type that accepts null and undefined and normalizes both to
undefined

tNormalizedNullOptional:
io-ts type that accepts the specified type or null/undefined.
if null/undefined, the value is normalized to undefined.
this allows creating optional config options that can be "removed" by
setting their value to null.
This commit is contained in:
Dragory 2021-04-29 00:46:21 +03:00
parent 73f0b7e30b
commit 7e40308a8a
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1

View file

@ -95,6 +95,24 @@ function typeIsArray(type: any): type is t.ArrayC<any> {
return type._tag === "ArrayType";
}
export const tNormalizedNullOrUndefined = new t.Type<undefined, null | undefined>(
"tNormalizedNullOrUndefined",
(v): v is undefined => typeof v === "undefined",
(v, c) => (v == null ? t.success(undefined) : t.failure(v, c, "Value must be null or undefined")),
s => undefined,
);
/**
* Similar to `tNullable`, but normalizes both `null` and `undefined` to `undefined`.
* This allows adding optional config options that can be "removed" by setting the value to `null`.
*/
export function tNormalizedNullOptional<T extends t.Type<any, any>>(type: T) {
return t.union(
[type, tNormalizedNullOrUndefined],
`Optional<${type.name}>`, // Simplified name for errors and config schema views
);
}
export type TDeepPartial<T> = T extends t.InterfaceType<any>
? TDeepPartialProps<T["props"]>
: T extends t.DictionaryType<any, any>