-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {MayNullable, Promisify} from "@typesafeunit/util"; | ||
import {MayInput} from "../interfaces"; | ||
import {TypeAbstract} from "../TypeAbstract"; | ||
|
||
export type UnionSelector<TInput extends MayInput> = | ||
(input: MayNullable<TInput>) => TypeAbstract<unknown> | undefined; | ||
|
||
export class Union<TValue, TInput extends MayInput = MayInput> extends TypeAbstract<TValue, TInput> { | ||
readonly #selector: UnionSelector<TInput>; | ||
readonly #name: string; | ||
|
||
constructor(selector: UnionSelector<TInput>, name = "Union") { | ||
super(); | ||
this.#selector = selector; | ||
this.#name = name; | ||
} | ||
|
||
public get name(): string { | ||
return this.#name; | ||
} | ||
|
||
public validate(input: MayNullable<TInput>): Promisify<TValue> { | ||
const type = this.#selector(input); | ||
this.assert(!!type, `${this.name} detection was failed`, input); | ||
return type.validate(input) as Promisify<TValue>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters