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:
parent
73f0b7e30b
commit
7e40308a8a
1 changed files with 18 additions and 0 deletions
|
@ -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>
|
||||
|
|
Loading…
Add table
Reference in a new issue