-
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.
feat: rework the remaining assertions
- Loading branch information
Showing
19 changed files
with
108 additions
and
43 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
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ import type { Validate } from "./types"; | |
* email("[email protected]"); // passes | ||
* ``` | ||
*/ | ||
export const email: Validate.Regex.Email = regex(/^\w+@.+\..+$/); | ||
export const email = regex(/^\w+@.+\..+$/) as Validate.Regex.Email; |
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
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,14 @@ | ||
import type { Brand, None } from "@the-minimal/types"; | ||
|
||
export namespace Validate { | ||
export namespace Type { | ||
export type Array = Brand< | ||
"array", | ||
None, | ||
{ | ||
input: unknown[]; | ||
output: None; | ||
} | ||
>; | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,16 @@ | ||
import type { Brand, None } from "@the-minimal/types"; | ||
|
||
export namespace Validate { | ||
export namespace Type { | ||
export type Object = Brand< | ||
"object", | ||
None, | ||
{ | ||
input: ObjectUnknown; | ||
output: None; | ||
} | ||
>; | ||
} | ||
} | ||
|
||
export type ObjectUnknown = Record<string, unknown>; |
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import type {Brand} from "@the-minimal/types"; | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export namespace Validate { | ||
export namespace Type { | ||
export type Any = Brand<"type", any, { | ||
input: any; | ||
output: any; | ||
}>; | ||
export namespace Type { | ||
export type Any = Brand< | ||
"type", | ||
any, | ||
{ | ||
input: any; | ||
output: any; | ||
} | ||
>; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export const error = (assertion: unknown) => { | ||
throw assertion; | ||
export const error = (cause: unknown, message = "") => { | ||
throw { | ||
name: "Validation", | ||
message, | ||
cause, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
import type { AnyBrand } from "@the-minimal/types"; | ||
import type { InferInput } from "@types"; | ||
import type { InferOutput } from "@types"; | ||
|
||
export function assert<$Brand extends AnyBrand>( | ||
brand: $Brand, | ||
value: unknown, | ||
): asserts value is InferInput<$Brand> { | ||
): asserts value is InferOutput<$Brand> { | ||
(brand as any)(value); | ||
} | ||
|
||
export function parse<$Brand extends AnyBrand>(brand: $Brand, value: unknown) { | ||
(brand as any)(value); | ||
|
||
return value as InferOutput<$Brand>; | ||
} | ||
|
||
export function is<$Brand extends AnyBrand>( | ||
brand: $Brand, | ||
value: unknown, | ||
): value is InferOutput<$Brand> { | ||
try { | ||
(brand as any)(value); | ||
|
||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |