@evolu/common 5.4.8 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -34
- package/dist/src/Array.d.ts +17 -0
- package/dist/src/Array.d.ts.map +1 -0
- package/dist/src/Array.js +12 -0
- package/dist/src/Assert.d.ts +68 -0
- package/dist/src/Assert.d.ts.map +1 -0
- package/dist/src/Assert.js +77 -0
- package/dist/src/BigInt.d.ts +20 -0
- package/dist/src/BigInt.d.ts.map +1 -0
- package/dist/src/BigInt.js +18 -0
- package/dist/src/Buffer.d.ts +92 -0
- package/dist/src/Buffer.d.ts.map +1 -0
- package/dist/src/Buffer.js +62 -0
- package/dist/src/Callbacks.d.ts +20 -0
- package/dist/src/Callbacks.d.ts.map +1 -0
- package/dist/src/Callbacks.js +18 -0
- package/dist/src/Console.d.ts +78 -0
- package/dist/src/Console.d.ts.map +1 -0
- package/dist/src/Console.js +103 -0
- package/dist/src/Crypto.d.ts +72 -39
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +89 -54
- package/dist/src/Eq.d.ts +97 -0
- package/dist/src/Eq.d.ts.map +1 -0
- package/dist/src/Eq.js +167 -0
- package/dist/src/Error.d.ts +14 -10
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +43 -11
- package/dist/src/Evolu/Config.d.ts +69 -0
- package/dist/src/Evolu/Config.d.ts.map +1 -0
- package/dist/src/Evolu/Config.js +9 -0
- package/dist/src/Evolu/Db.d.ts +126 -0
- package/dist/src/Evolu/Db.d.ts.map +1 -0
- package/dist/src/Evolu/Db.js +774 -0
- package/dist/src/Evolu/Diff.d.ts +43 -0
- package/dist/src/Evolu/Diff.d.ts.map +1 -0
- package/dist/src/Evolu/Diff.js +95 -0
- package/dist/src/Evolu/Evolu.d.ts +334 -0
- package/dist/src/Evolu/Evolu.d.ts.map +1 -0
- package/dist/src/Evolu/Evolu.js +434 -0
- package/dist/src/Evolu/Internal.d.ts +26 -0
- package/dist/src/Evolu/Internal.d.ts.map +1 -0
- package/dist/src/Evolu/Internal.js +25 -0
- package/dist/src/Evolu/Kysely.d.ts +6 -0
- package/dist/src/Evolu/Kysely.d.ts.map +1 -0
- package/dist/src/Evolu/Kysely.js +21 -0
- package/dist/src/Evolu/Owner.d.ts +155 -0
- package/dist/src/Evolu/Owner.d.ts.map +1 -0
- package/dist/src/Evolu/Owner.js +126 -0
- package/dist/src/Evolu/Platform.d.ts +23 -0
- package/dist/src/Evolu/Platform.d.ts.map +1 -0
- package/dist/src/Evolu/Platform.js +1 -0
- package/dist/src/Evolu/Protocol.d.ts +401 -0
- package/dist/src/Evolu/Protocol.d.ts.map +1 -0
- package/dist/src/Evolu/Protocol.js +1151 -0
- package/dist/src/Evolu/Public.d.ts +18 -0
- package/dist/src/Evolu/Public.d.ts.map +1 -0
- package/dist/src/Evolu/Public.js +11 -0
- package/dist/src/Evolu/PublicKysely.d.ts +148 -0
- package/dist/src/Evolu/PublicKysely.d.ts.map +1 -0
- package/dist/src/Evolu/PublicKysely.js +185 -0
- package/dist/src/Evolu/Query.d.ts +63 -0
- package/dist/src/Evolu/Query.d.ts.map +1 -0
- package/dist/src/Evolu/Query.js +61 -0
- package/dist/src/Evolu/Relay.d.ts +13 -0
- package/dist/src/Evolu/Relay.d.ts.map +1 -0
- package/dist/src/Evolu/Relay.js +109 -0
- package/dist/src/Evolu/Schema.d.ts +201 -0
- package/dist/src/Evolu/Schema.d.ts.map +1 -0
- package/dist/src/Evolu/Schema.js +150 -0
- package/dist/src/Evolu/Storage.d.ts +49 -0
- package/dist/src/Evolu/Storage.d.ts.map +1 -0
- package/dist/src/Evolu/Storage.js +1111 -0
- package/dist/src/Evolu/Sync.d.ts +59 -0
- package/dist/src/Evolu/Sync.d.ts.map +1 -0
- package/dist/src/Evolu/Sync.js +29 -0
- package/dist/src/Evolu/Timestamp.d.ts +106 -0
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -0
- package/dist/src/Evolu/Timestamp.js +179 -0
- package/dist/src/Function.d.ts +54 -0
- package/dist/src/Function.d.ts.map +1 -0
- package/dist/src/Function.js +38 -0
- package/dist/src/ManyToManyMap.d.ts +26 -0
- package/dist/src/ManyToManyMap.d.ts.map +1 -0
- package/dist/src/ManyToManyMap.js +92 -0
- package/dist/src/NanoId.d.ts +27 -0
- package/dist/src/NanoId.d.ts.map +1 -0
- package/dist/src/NanoId.js +6 -0
- package/dist/src/Number.d.ts +42 -0
- package/dist/src/Number.d.ts.map +1 -0
- package/dist/src/Number.js +55 -0
- package/dist/src/Object.d.ts +35 -0
- package/dist/src/Object.d.ts.map +1 -0
- package/dist/src/Object.js +36 -0
- package/dist/src/Order.d.ts +90 -0
- package/dist/src/Order.d.ts.map +1 -0
- package/dist/src/Order.js +85 -0
- package/dist/src/Promise.d.ts +180 -0
- package/dist/src/Promise.d.ts.map +1 -0
- package/dist/src/Promise.js +176 -0
- package/dist/src/Random.d.ts +52 -0
- package/dist/src/Random.d.ts.map +1 -0
- package/dist/src/Random.js +29 -0
- package/dist/src/Ref.d.ts +40 -0
- package/dist/src/Ref.d.ts.map +1 -0
- package/dist/src/Ref.js +13 -0
- package/dist/src/Result.d.ts +421 -0
- package/dist/src/Result.d.ts.map +1 -0
- package/dist/src/Result.js +357 -0
- package/dist/src/Skiplist.d.ts +23 -0
- package/dist/src/Skiplist.d.ts.map +1 -0
- package/dist/src/Skiplist.js +58 -0
- package/dist/src/Sqlite.d.ts +116 -52
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +183 -67
- package/dist/src/Store.d.ts +45 -8
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +33 -17
- package/dist/src/String.d.ts +2 -0
- package/dist/src/String.d.ts.map +1 -0
- package/dist/src/String.js +14 -0
- package/dist/src/Time.d.ts +20 -0
- package/dist/src/Time.d.ts.map +1 -0
- package/dist/src/Time.js +25 -0
- package/dist/src/Type.d.ts +1937 -0
- package/dist/src/Type.d.ts.map +1 -0
- package/dist/src/Type.js +2002 -0
- package/dist/src/Types.d.ts +188 -0
- package/dist/src/Types.d.ts.map +1 -0
- package/dist/src/Types.js +6 -0
- package/dist/src/WebSocket.d.ts +112 -0
- package/dist/src/WebSocket.d.ts.map +1 -0
- package/dist/src/WebSocket.js +139 -0
- package/dist/src/Worker.d.ts +44 -0
- package/dist/src/Worker.d.ts.map +1 -0
- package/dist/src/Worker.js +66 -0
- package/dist/src/index.d.ts +24 -11
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +24 -11
- package/package.json +29 -38
- package/src/Array.ts +39 -0
- package/src/Assert.ts +116 -0
- package/src/BigInt.ts +29 -0
- package/src/Buffer.ts +175 -0
- package/src/Callbacks.ts +43 -0
- package/src/Console.ts +159 -0
- package/src/Crypto.ts +169 -115
- package/src/Eq.ts +204 -0
- package/src/Error.ts +57 -20
- package/src/Evolu/Config.ts +83 -0
- package/src/Evolu/Db.ts +1275 -0
- package/src/Evolu/Diff.ts +142 -0
- package/src/Evolu/Evolu.ts +947 -0
- package/src/Evolu/Internal.ts +26 -0
- package/src/Evolu/Kysely.ts +38 -0
- package/src/Evolu/Owner.ts +296 -0
- package/src/Evolu/Platform.ts +27 -0
- package/src/Evolu/Protocol.ts +1857 -0
- package/src/Evolu/Public.ts +43 -0
- package/src/Evolu/PublicKysely.ts +240 -0
- package/src/Evolu/Query.ts +167 -0
- package/src/Evolu/Relay.ts +142 -0
- package/src/Evolu/Schema.ts +417 -0
- package/src/Evolu/Storage.ts +1281 -0
- package/src/Evolu/Sync.ts +105 -0
- package/src/Evolu/Timestamp.ts +311 -0
- package/src/Function.ts +58 -0
- package/src/ManyToManyMap.ts +140 -0
- package/src/NanoId.ts +39 -0
- package/src/Number.ts +90 -0
- package/src/Object.ts +64 -0
- package/src/Order.ts +113 -0
- package/src/Promise.ts +295 -0
- package/src/Random.ts +68 -0
- package/src/Ref.ts +63 -0
- package/src/Result.ts +453 -0
- package/src/Skiplist.ts +102 -0
- package/src/Sqlite.ts +366 -153
- package/src/Store.ts +79 -36
- package/src/String.ts +10 -0
- package/src/Time.ts +36 -0
- package/src/Type.ts +3978 -0
- package/src/Types.ts +209 -0
- package/src/WebSocket.ts +273 -0
- package/src/Worker.ts +129 -0
- package/src/index.ts +24 -11
- package/dist/src/Config.d.ts +0 -56
- package/dist/src/Config.d.ts.map +0 -1
- package/dist/src/Config.js +0 -39
- package/dist/src/Crdt.d.ts +0 -89
- package/dist/src/Crdt.d.ts.map +0 -1
- package/dist/src/Crdt.js +0 -181
- package/dist/src/Db.d.ts +0 -107
- package/dist/src/Db.d.ts.map +0 -1
- package/dist/src/Db.js +0 -443
- package/dist/src/Diff.d.ts +0 -27
- package/dist/src/Diff.d.ts.map +0 -1
- package/dist/src/Diff.js +0 -84
- package/dist/src/Evolu.d.ts +0 -426
- package/dist/src/Evolu.d.ts.map +0 -1
- package/dist/src/Evolu.js +0 -333
- package/dist/src/Model.d.ts +0 -141
- package/dist/src/Model.d.ts.map +0 -1
- package/dist/src/Model.js +0 -125
- package/dist/src/Murmurhash.d.ts +0 -2
- package/dist/src/Murmurhash.d.ts.map +0 -1
- package/dist/src/Murmurhash.js +0 -60
- package/dist/src/Owner.d.ts +0 -33
- package/dist/src/Owner.d.ts.map +0 -1
- package/dist/src/Owner.js +0 -26
- package/dist/src/Platform.d.ts +0 -37
- package/dist/src/Platform.d.ts.map +0 -1
- package/dist/src/Platform.js +0 -11
- package/dist/src/Protobuf.d.ts +0 -81
- package/dist/src/Protobuf.d.ts.map +0 -1
- package/dist/src/Protobuf.js +0 -92
- package/dist/src/Public.d.ts +0 -13
- package/dist/src/Public.d.ts.map +0 -1
- package/dist/src/Public.js +0 -6
- package/dist/src/Socket.d.ts +0 -8
- package/dist/src/Socket.d.ts.map +0 -1
- package/dist/src/Socket.js +0 -51
- package/dist/src/Sql.d.ts +0 -12
- package/dist/src/Sql.d.ts.map +0 -1
- package/dist/src/Sql.js +0 -30
- package/dist/src/Sync.d.ts +0 -70
- package/dist/src/Sync.d.ts.map +0 -1
- package/dist/src/Sync.js +0 -127
- package/src/Config.ts +0 -119
- package/src/Crdt.ts +0 -361
- package/src/Db.ts +0 -955
- package/src/Diff.ts +0 -114
- package/src/Evolu.ts +0 -1016
- package/src/Model.ts +0 -233
- package/src/Murmurhash.ts +0 -70
- package/src/Owner.ts +0 -69
- package/src/Platform.ts +0 -47
- package/src/Protobuf.ts +0 -155
- package/src/Public.ts +0 -12
- package/src/Socket.ts +0 -83
- package/src/Sql.ts +0 -41
- package/src/Sync.ts +0 -315
|
@@ -0,0 +1,1937 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🧩 Validation, Parsing, and Transformation
|
|
3
|
+
*
|
|
4
|
+
* ## Intro
|
|
5
|
+
*
|
|
6
|
+
* You probably know [Zod](https://zod.dev). Evolu has {@link Type}.
|
|
7
|
+
*
|
|
8
|
+
* Evolu Type exists because no existing validation/parsing/transformation
|
|
9
|
+
* library fully met our needs:
|
|
10
|
+
*
|
|
11
|
+
* - **Result-based error handling**: Leveraging {@link Result} instead of throwing
|
|
12
|
+
* exceptions.
|
|
13
|
+
* - **Consistent constraints**: Enforcing {@link Brand} for all constraints.
|
|
14
|
+
* - **Typed errors with decoupled formatters**: Avoiding coupling error messages
|
|
15
|
+
* with validators.
|
|
16
|
+
* - **No user-land chaining**: Designed with ES pipe operator in mind.
|
|
17
|
+
* - **Selective validation/transformation**: Skipping parent Type validations and
|
|
18
|
+
* transformations when TypeScript's type system can be relied upon.
|
|
19
|
+
* - **Bidirectional transformations**: Supporting transformations in both
|
|
20
|
+
* directions.
|
|
21
|
+
* - **Minimal and transparent code**: No runtime dependencies or hidden magic.
|
|
22
|
+
*
|
|
23
|
+
* **Note**: A proper quickstart guide is on the way. In the meantime, each type
|
|
24
|
+
* includes its own usage example, and you can (and should) check the tests for
|
|
25
|
+
* practical demonstrations of the API. Or dang, just read the code. It's
|
|
26
|
+
* simple.
|
|
27
|
+
*
|
|
28
|
+
* - Evolu `Type` is:
|
|
29
|
+
*
|
|
30
|
+
* - A TypeScript type with a {@link Brand} whenever it's possible.
|
|
31
|
+
* - A function to create a value of that type, which may fail.
|
|
32
|
+
* - A function to transform value back to its original representation, which
|
|
33
|
+
* cannot fail.
|
|
34
|
+
*
|
|
35
|
+
* Types are chainable. The chain starts with a Base Type that refines an
|
|
36
|
+
* unknown value into something and can continue with further refinements or
|
|
37
|
+
* transformations. For example, `NonEmptyTrimmedString100` chain looks like
|
|
38
|
+
* this:
|
|
39
|
+
*
|
|
40
|
+
* `Unknown` -> `String` -> `TrimmedString` -> `NonEmptyTrimmedString100`
|
|
41
|
+
*
|
|
42
|
+
* For `NonEmptyTrimmedString100`, the parent Type is `TrimmedString`. For
|
|
43
|
+
* `TrimmedString`, the parent Type is `String`.
|
|
44
|
+
*
|
|
45
|
+
* The parent of the `String` Type is the `String` Type itself. All Base Types
|
|
46
|
+
* `fromParent` functions are just a typed alias to `fromUnknown` to ensure that
|
|
47
|
+
* `fromParent` and `toParent` can be called on any Type.
|
|
48
|
+
*
|
|
49
|
+
* Speaking of `fromParent` and `toParent`, those functions exist to bypass
|
|
50
|
+
* parent Types when we can rely on TypeScript types.
|
|
51
|
+
*
|
|
52
|
+
* `Type` transformations should be reversible. If you need an irreversible
|
|
53
|
+
* transformation, such as `TrimString` (trimming is not reversible as `untrim`
|
|
54
|
+
* can't know what has been trimmed), you can do that, but note in JSDoc that
|
|
55
|
+
* `to` will not restore the original representation. You can also use
|
|
56
|
+
* {@link assert}: `assert(false, "Untrim is not possible")`.
|
|
57
|
+
*
|
|
58
|
+
* ### Tip
|
|
59
|
+
*
|
|
60
|
+
* If necessary, write `globalThis.String` instead of `String` to avoid naming
|
|
61
|
+
* clashes with Base Types.
|
|
62
|
+
*
|
|
63
|
+
* ### Design Decision:
|
|
64
|
+
*
|
|
65
|
+
* While the `from` function can fail, the `to` function cannot. This simplifies
|
|
66
|
+
* the model by ensuring that every valid input has a corresponding valid
|
|
67
|
+
* output, eliminating the risk of edge cases caused by irreversible
|
|
68
|
+
* operations.
|
|
69
|
+
*
|
|
70
|
+
* @module
|
|
71
|
+
*/
|
|
72
|
+
import { NanoIdLibDep } from "./NanoId.js";
|
|
73
|
+
import { Ok, Result } from "./Result.js";
|
|
74
|
+
import type { Brand, Literal, Simplify, WidenLiteral } from "./Types.js";
|
|
75
|
+
export interface Type<Name extends TypeName,
|
|
76
|
+
/** The type this Type resolves to. */
|
|
77
|
+
T,
|
|
78
|
+
/** The type expected by `from` and `fromUnknown`. */
|
|
79
|
+
Input,
|
|
80
|
+
/** The specific error introduced by this Type. */
|
|
81
|
+
Error extends TypeError = never,
|
|
82
|
+
/** The parent type. */
|
|
83
|
+
Parent = T,
|
|
84
|
+
/** The parent's error. */
|
|
85
|
+
ParentError extends TypeError = Error> {
|
|
86
|
+
readonly name: Name;
|
|
87
|
+
/**
|
|
88
|
+
* Creates `T` from an `Input` value.
|
|
89
|
+
*
|
|
90
|
+
* This is useful when we have a typed value.
|
|
91
|
+
*
|
|
92
|
+
* `from` is a typed alias of `fromUnknown`.
|
|
93
|
+
*/
|
|
94
|
+
readonly from: (value: Input) => Result<T, ParentError | Error>;
|
|
95
|
+
/**
|
|
96
|
+
* Creates `T` from an unknown value.
|
|
97
|
+
*
|
|
98
|
+
* This is useful when a value is unknown.
|
|
99
|
+
*/
|
|
100
|
+
readonly fromUnknown: (value: unknown) => Result<T, ParentError | Error>;
|
|
101
|
+
/**
|
|
102
|
+
* The opposite of `from` and `fromUnknown`.
|
|
103
|
+
*
|
|
104
|
+
* This is useful to transform `T` back to its `Input` representation.
|
|
105
|
+
*
|
|
106
|
+
* For `refine`, it only removes the brand. For `transform`, it changes value.
|
|
107
|
+
*/
|
|
108
|
+
readonly to: (value: T) => Input;
|
|
109
|
+
/**
|
|
110
|
+
* Creates `T` from `Parent` type.
|
|
111
|
+
*
|
|
112
|
+
* This function skips parent Types validations/transformations when we have
|
|
113
|
+
* already partially validated/transformed value.
|
|
114
|
+
*
|
|
115
|
+
* For example, `TrimString.from` checks whether a value is a string and trims
|
|
116
|
+
* it. If we only want to trim a string, we can use `fromParent`.
|
|
117
|
+
*
|
|
118
|
+
* ### Example
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* // string & Brand<"Trimmed">
|
|
122
|
+
* const value = TrimString.fromParent("a ").value; // as efficient as foo.trim()
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
readonly fromParent: (value: Parent) => Result<T, Error>;
|
|
126
|
+
/** The opposite of `fromParent`. */
|
|
127
|
+
readonly toParent: (value: T) => Parent;
|
|
128
|
+
/**
|
|
129
|
+
* A **type guard** that checks whether an unknown value satisfies the
|
|
130
|
+
* {@link Type}.
|
|
131
|
+
*
|
|
132
|
+
* ### Example
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* const value: unknown = "hello";
|
|
136
|
+
* if (String.is(value)) {
|
|
137
|
+
* // TypeScript now knows `value` is a `string` here.
|
|
138
|
+
* console.log("This is a valid string!");
|
|
139
|
+
* }
|
|
140
|
+
*
|
|
141
|
+
* const strings: unknown[] = [1, "hello", true, "world"];
|
|
142
|
+
* const filteredStrings = strings.filter(String.is);
|
|
143
|
+
*
|
|
144
|
+
* console.log(filteredStrings); // ["hello", "world"]
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
readonly is: (value: unknown) => value is T;
|
|
148
|
+
readonly [EvoluTypeSymbol]: true;
|
|
149
|
+
/**
|
|
150
|
+
* The type this Type resolves to.
|
|
151
|
+
*
|
|
152
|
+
* ### Example
|
|
153
|
+
*
|
|
154
|
+
* ```ts
|
|
155
|
+
* type String = typeof String.Type;
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
readonly Type: T;
|
|
159
|
+
/**
|
|
160
|
+
* The type expected by `from` and `fromUnknown`.
|
|
161
|
+
*
|
|
162
|
+
* ### Example
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* type StringInput = typeof String.Input;
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
readonly Input: Input;
|
|
169
|
+
/**
|
|
170
|
+
* The specific error introduced by this Type.
|
|
171
|
+
*
|
|
172
|
+
* ### Example
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* type StringError = typeof String.Error;
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
readonly Error: Error;
|
|
179
|
+
/**
|
|
180
|
+
* The parent type.
|
|
181
|
+
*
|
|
182
|
+
* ### Example
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* type StringParent = typeof String.Parent;
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
readonly Parent: Parent;
|
|
189
|
+
/**
|
|
190
|
+
* The parent's error.
|
|
191
|
+
*
|
|
192
|
+
* ### Example
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* type StringParentError = typeof String.ParentError;
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
readonly ParentError: ParentError;
|
|
199
|
+
/**
|
|
200
|
+
* Error | ParentError
|
|
201
|
+
*
|
|
202
|
+
* ### Example
|
|
203
|
+
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* type StringParentErrors = typeof String.Errors;
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
readonly Errors: Error | ParentError;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Unique identifier for a {@link Type}.
|
|
212
|
+
*
|
|
213
|
+
* @category Utilities
|
|
214
|
+
*/
|
|
215
|
+
export type TypeName = Capitalize<string>;
|
|
216
|
+
export interface TypeError<Name extends TypeName = TypeName> {
|
|
217
|
+
readonly type: Name;
|
|
218
|
+
/**
|
|
219
|
+
* The value that was received and caused the error. Provides additional
|
|
220
|
+
* context for debugging and validation feedback.
|
|
221
|
+
*/
|
|
222
|
+
readonly value: unknown;
|
|
223
|
+
}
|
|
224
|
+
export interface TypeErrorWithReason<Name extends TypeName = TypeName, Reason extends {
|
|
225
|
+
readonly kind: Capitalize<string>;
|
|
226
|
+
} = {
|
|
227
|
+
readonly kind: Capitalize<string>;
|
|
228
|
+
}> extends TypeError<Name> {
|
|
229
|
+
/** The detailed reason for the error, represented as a tagged union. */
|
|
230
|
+
readonly reason: Reason;
|
|
231
|
+
}
|
|
232
|
+
export type AnyType = Type<any, any, any, any, any, any>;
|
|
233
|
+
export type InferName<A extends AnyType> = A extends Type<infer Name, any, any, any, any, any> ? Name : never;
|
|
234
|
+
export type InferType<A extends AnyType> = A extends Type<any, infer T, any, any, any, any> ? T : never;
|
|
235
|
+
export type InferInput<A extends AnyType> = A extends Type<any, any, infer Input, any, any, any> ? Input : never;
|
|
236
|
+
export type InferError<A extends AnyType> = A extends Type<any, any, any, infer Error, any, any> ? Error : never;
|
|
237
|
+
export type InferParent<A extends AnyType> = A extends Type<any, any, any, any, infer Parent, any> ? Parent : never;
|
|
238
|
+
export type InferParentError<A extends AnyType> = A extends Type<any, any, any, any, any, infer ParentError> ? ParentError : never;
|
|
239
|
+
export type InferErrors<T extends AnyType> = T extends Type<any, any, any, infer Error, any, infer ParentError> ? Error | ParentError : never;
|
|
240
|
+
declare const EvoluTypeSymbol: unique symbol;
|
|
241
|
+
/**
|
|
242
|
+
* Checks if the given value is an {@link Type}.
|
|
243
|
+
*
|
|
244
|
+
* @category Utilities
|
|
245
|
+
*/
|
|
246
|
+
export declare const isType: (value: unknown) => value is AnyType;
|
|
247
|
+
/**
|
|
248
|
+
* Creates a formatter function for {@link TypeError}.
|
|
249
|
+
*
|
|
250
|
+
* The formatter generates human-readable error messages using a custom
|
|
251
|
+
* formatting function and a safely stringified error value.
|
|
252
|
+
*
|
|
253
|
+
* ### Example
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* const formatStringError = createTypeErrorFormatter<StringError>(
|
|
257
|
+
* (value) => `A value ${value} is not a string.`,
|
|
258
|
+
* );
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* @category Utilities
|
|
262
|
+
*/
|
|
263
|
+
export declare const createTypeErrorFormatter: <Error extends TypeError>(format: (error: Simplify<Omit<Error, "value"> & {
|
|
264
|
+
value: string;
|
|
265
|
+
}>) => string) => TypeErrorFormatter<Error>;
|
|
266
|
+
export type TypeErrorFormatter<Error extends TypeError> = (error: Error) => string;
|
|
267
|
+
/**
|
|
268
|
+
* Base {@link Type}.
|
|
269
|
+
*
|
|
270
|
+
* A Base Type validates that a value conforms to a specific TypeScript type.
|
|
271
|
+
* Unlike refinements or transformations, Base Types establish the fundamental
|
|
272
|
+
* shape of a value before any branding or transformation occurs.
|
|
273
|
+
*
|
|
274
|
+
* - To **refine** a Base Type further, use the {@link brand} Type Factory.
|
|
275
|
+
* - To **transform** a Base Type into a different representation, use the
|
|
276
|
+
* {@link transform} Type Factory.
|
|
277
|
+
*
|
|
278
|
+
* ### Example
|
|
279
|
+
*
|
|
280
|
+
* ```ts
|
|
281
|
+
* const String = base("String", (value) =>
|
|
282
|
+
* typeof value === "string"
|
|
283
|
+
* ? ok(value)
|
|
284
|
+
* : err<StringError>({ type: "String", value }),
|
|
285
|
+
* );
|
|
286
|
+
*
|
|
287
|
+
* interface StringError extends TypeError<"String"> {}
|
|
288
|
+
*
|
|
289
|
+
* const formatStringError = createTypeErrorFormatter<StringError>(
|
|
290
|
+
* (error) => `A value ${error.value} is not a string`,
|
|
291
|
+
* );
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @category Base Factories
|
|
295
|
+
*/
|
|
296
|
+
export declare const base: <Name extends TypeName, T, Error extends TypeError>(name: Name, fromUnknown: (value: unknown) => Result<T, Error>) => Type<Name, T, T, Error>;
|
|
297
|
+
/**
|
|
298
|
+
* Creates a formatter function for a base {@link TypeError}.
|
|
299
|
+
*
|
|
300
|
+
* This formatter is specifically for Base Types that only need a simple error
|
|
301
|
+
* message indicating that the value is not of the expected type.
|
|
302
|
+
*
|
|
303
|
+
* ### Example
|
|
304
|
+
*
|
|
305
|
+
* ```ts
|
|
306
|
+
* export const formatStringError =
|
|
307
|
+
* createBaseTypeErrorFormatter<StringError>();
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* @category Utilities
|
|
311
|
+
*/
|
|
312
|
+
export declare const createBaseTypeErrorFormatter: <Error extends TypeError>() => TypeErrorFormatter<Error>;
|
|
313
|
+
/** @category Base Types */
|
|
314
|
+
export declare const Unknown: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
315
|
+
/**
|
|
316
|
+
* @category Base Types
|
|
317
|
+
* @category String
|
|
318
|
+
*/
|
|
319
|
+
export declare const String: Type<"String", string, string, StringError, string, StringError>;
|
|
320
|
+
export interface StringError extends TypeError<"String"> {
|
|
321
|
+
}
|
|
322
|
+
export declare const formatStringError: TypeErrorFormatter<StringError>;
|
|
323
|
+
/** @category Base Types */
|
|
324
|
+
export declare const Number: Type<"Number", number, number, NumberError, number, NumberError>;
|
|
325
|
+
export interface NumberError extends TypeError<"Number"> {
|
|
326
|
+
}
|
|
327
|
+
export declare const formatNumberError: TypeErrorFormatter<NumberError>;
|
|
328
|
+
/** @category Base Types */
|
|
329
|
+
export declare const BigInt: Type<"BigInt", bigint, bigint, BigIntError, bigint, BigIntError>;
|
|
330
|
+
export interface BigIntError extends TypeError<"BigInt"> {
|
|
331
|
+
}
|
|
332
|
+
export declare const formatBigIntError: TypeErrorFormatter<BigIntError>;
|
|
333
|
+
/** @category Base Types */
|
|
334
|
+
export declare const Boolean: Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>;
|
|
335
|
+
export interface BooleanError extends TypeError<"Boolean"> {
|
|
336
|
+
}
|
|
337
|
+
export declare const formatBooleanError: TypeErrorFormatter<BooleanError>;
|
|
338
|
+
/** @category Base Types */
|
|
339
|
+
export declare const Undefined: Type<"Undefined", undefined, undefined, UndefinedError, undefined, UndefinedError>;
|
|
340
|
+
export interface UndefinedError extends TypeError<"Undefined"> {
|
|
341
|
+
}
|
|
342
|
+
export declare const formatUndefinedError: TypeErrorFormatter<UndefinedError>;
|
|
343
|
+
/** @category Base Types */
|
|
344
|
+
export declare const Null: Type<"Null", null, null, NullError, null, NullError>;
|
|
345
|
+
export interface NullError extends TypeError<"Null"> {
|
|
346
|
+
}
|
|
347
|
+
export declare const formatNullError: TypeErrorFormatter<NullError>;
|
|
348
|
+
/** @category Base Types */
|
|
349
|
+
export declare const Function: Type<"Function", Function, Function, FunctionError, Function, FunctionError>;
|
|
350
|
+
export interface FunctionError extends TypeError<"Function"> {
|
|
351
|
+
}
|
|
352
|
+
export declare const formatFunctionError: TypeErrorFormatter<FunctionError>;
|
|
353
|
+
/** @category Base Types */
|
|
354
|
+
export declare const Uint8Array: Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, Uint8ArrayError, Uint8Array<ArrayBufferLike>, Uint8ArrayError>;
|
|
355
|
+
export interface Uint8ArrayError extends TypeError<"Uint8Array"> {
|
|
356
|
+
}
|
|
357
|
+
export declare const formatUint8ArrayError: TypeErrorFormatter<Uint8ArrayError>;
|
|
358
|
+
/**
|
|
359
|
+
* `instanceof` {@link Type}.
|
|
360
|
+
*
|
|
361
|
+
* Ensures that a value is an instance of the given class constructor.
|
|
362
|
+
*
|
|
363
|
+
* ### Example
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* class User {
|
|
367
|
+
* constructor(public name: string) {}
|
|
368
|
+
* }
|
|
369
|
+
*
|
|
370
|
+
* const UserInstance = instanceOf(User);
|
|
371
|
+
*
|
|
372
|
+
* const result = UserInstance.from(new User("Alice")); // ok
|
|
373
|
+
* const error = UserInstance.from({}); // err
|
|
374
|
+
* ```
|
|
375
|
+
*
|
|
376
|
+
* @category Base Factories
|
|
377
|
+
*/
|
|
378
|
+
export declare const instanceOf: <T extends abstract new (...args: any) => any>(ctor: T) => InstanceOfType<T>;
|
|
379
|
+
export interface InstanceOfError extends TypeError<"InstanceOf"> {
|
|
380
|
+
readonly ctor: string;
|
|
381
|
+
}
|
|
382
|
+
export interface InstanceOfType<T extends abstract new (...args: any) => any> extends Type<"InstanceOf", InstanceType<T>, InstanceType<T>, InstanceOfError> {
|
|
383
|
+
ctor: T;
|
|
384
|
+
}
|
|
385
|
+
export declare const formatInstanceOfError: TypeErrorFormatter<InstanceOfError>;
|
|
386
|
+
/**
|
|
387
|
+
* JavaScript Date.
|
|
388
|
+
*
|
|
389
|
+
* @category Base Types
|
|
390
|
+
*/
|
|
391
|
+
export declare const Date: InstanceOfType<DateConstructor>;
|
|
392
|
+
/**
|
|
393
|
+
* Validates that an unknown value is an Evolu {@link Type} (i.e., satisfies
|
|
394
|
+
* `AnyType`).
|
|
395
|
+
*
|
|
396
|
+
* ### Example
|
|
397
|
+
*
|
|
398
|
+
* ```ts
|
|
399
|
+
* const result = EvoluType.from(String); // ok(String)
|
|
400
|
+
* const error = EvoluType.from("not a Type"); // err
|
|
401
|
+
* ```
|
|
402
|
+
*/
|
|
403
|
+
export declare const EvoluType: Type<"EvoluType", AnyType, AnyType, EvoluTypeError, AnyType, EvoluTypeError>;
|
|
404
|
+
export interface EvoluTypeError extends TypeError<"EvoluType"> {
|
|
405
|
+
}
|
|
406
|
+
export declare const formatIsTypeError: TypeErrorFormatter<EvoluTypeError>;
|
|
407
|
+
/**
|
|
408
|
+
* Branded {@link Type}.
|
|
409
|
+
*
|
|
410
|
+
* The `brand` Type Factory takes the name of a new {@link Brand}, a parent Type
|
|
411
|
+
* to be branded, and the optional `refine` function for additional constraint.
|
|
412
|
+
*
|
|
413
|
+
* If the `refine` function is omited, TODO:
|
|
414
|
+
*
|
|
415
|
+
* ### Examples
|
|
416
|
+
*
|
|
417
|
+
* A simple `CurrencyCode` Type:
|
|
418
|
+
*
|
|
419
|
+
* ```ts
|
|
420
|
+
* const CurrencyCode = brand("CurrencyCode", String, (value) =>
|
|
421
|
+
* /^[A-Z]{3}$/.test(value)
|
|
422
|
+
* ? ok(value)
|
|
423
|
+
* : err<CurrencyCodeError>({ type: "CurrencyCode", value }),
|
|
424
|
+
* );
|
|
425
|
+
*
|
|
426
|
+
* // string & Brand<"CurrencyCode">
|
|
427
|
+
* type CurrencyCode = typeof CurrencyCode.Type;
|
|
428
|
+
*
|
|
429
|
+
* interface CurrencyCodeError extends TypeError<"CurrencyCode"> {}
|
|
430
|
+
*
|
|
431
|
+
* const formatCurrencyCodeError =
|
|
432
|
+
* createTypeErrorFormatter<CurrencyCodeError>(
|
|
433
|
+
* (error) => `Invalid currency code: ${error.value}`,
|
|
434
|
+
* );
|
|
435
|
+
*
|
|
436
|
+
* // Usage
|
|
437
|
+
* const result = CurrencyCode.from("USD");
|
|
438
|
+
* if (result.ok) {
|
|
439
|
+
* console.log("Valid currency code:", result.value);
|
|
440
|
+
* } else {
|
|
441
|
+
* console.error(formatCurrencyCodeError(result.error));
|
|
442
|
+
* }
|
|
443
|
+
* ```
|
|
444
|
+
*
|
|
445
|
+
* Often, we want to make a branded Type reusable. For example, instead of
|
|
446
|
+
* `TrimmedString`, we want the `trimmed` Type Factory:
|
|
447
|
+
*
|
|
448
|
+
* ```ts
|
|
449
|
+
* const trimmed: BrandFactory<"Trimmed", string, TrimmedError> = (
|
|
450
|
+
* parent,
|
|
451
|
+
* ) =>
|
|
452
|
+
* brand("Trimmed", parent, (value) =>
|
|
453
|
+
* value.trim().length === value.length
|
|
454
|
+
* ? ok(value)
|
|
455
|
+
* : err<TrimmedError>({ type: "Trimmed", value }),
|
|
456
|
+
* );
|
|
457
|
+
*
|
|
458
|
+
* interface TrimmedError extends TypeError<"Trimmed"> {}
|
|
459
|
+
*
|
|
460
|
+
* const formatTrimmedError = createTypeErrorFormatter<TrimmedError>(
|
|
461
|
+
* (error) => `A value ${error.value} is not trimmed`,
|
|
462
|
+
* );
|
|
463
|
+
*
|
|
464
|
+
* const TrimmedString = trimmed(String);
|
|
465
|
+
*
|
|
466
|
+
* // string & Brand<"Trimmed">
|
|
467
|
+
* type TrimmedString = typeof TrimmedString.Type;
|
|
468
|
+
*
|
|
469
|
+
* const TrimmedNote = trimmed(Note);
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
472
|
+
* As noted earlier, the `refine` function is optional. That's useful to add
|
|
473
|
+
* semantic meaning to the existing Type without altering its functionality:
|
|
474
|
+
*
|
|
475
|
+
* ```ts
|
|
476
|
+
* const SimplePassword = brand(
|
|
477
|
+
* "SimplePassword",
|
|
478
|
+
* minLength(8)(maxLength(64)(TrimmedString)),
|
|
479
|
+
* );
|
|
480
|
+
* // string & Brand<"Trimmed"> & Brand<"MinLength8"> & Brand<"MaxLength64"> & Brand<"SimplePassword">
|
|
481
|
+
* type SimplePassword = typeof SimplePassword.Type;
|
|
482
|
+
* ```
|
|
483
|
+
*
|
|
484
|
+
* We can use `brand` to enforce valid object as well:
|
|
485
|
+
*
|
|
486
|
+
* ```ts
|
|
487
|
+
* const Form = object({
|
|
488
|
+
* password: SimplePassword,
|
|
489
|
+
* confirmPassword: SimplePassword,
|
|
490
|
+
* });
|
|
491
|
+
*
|
|
492
|
+
* const ValidForm = brand("Valid", Form, (value) => {
|
|
493
|
+
* if (value.password !== value.confirmPassword)
|
|
494
|
+
* return err<ValidFormError>({
|
|
495
|
+
* type: "ValidForm",
|
|
496
|
+
* value,
|
|
497
|
+
* reason: { kind: "PasswordMismatch" },
|
|
498
|
+
* });
|
|
499
|
+
* return ok(value);
|
|
500
|
+
* });
|
|
501
|
+
* type ValidForm = typeof ValidForm.Type;
|
|
502
|
+
*
|
|
503
|
+
* interface ValidFormError extends TypeError<"ValidForm"> {
|
|
504
|
+
* readonly reason: { kind: "PasswordMismatch" };
|
|
505
|
+
* }
|
|
506
|
+
*
|
|
507
|
+
* const result = ValidForm.from({
|
|
508
|
+
* password: "abcde123",
|
|
509
|
+
* confirmPassword: "bbcde123",
|
|
510
|
+
* });
|
|
511
|
+
*
|
|
512
|
+
* const safeForm = (_form: ValidForm) => {
|
|
513
|
+
* //
|
|
514
|
+
* };
|
|
515
|
+
*
|
|
516
|
+
* if (result.ok) {
|
|
517
|
+
* safeForm(result.value);
|
|
518
|
+
* }
|
|
519
|
+
*
|
|
520
|
+
* expect(result).toEqual(
|
|
521
|
+
* err({
|
|
522
|
+
* type: "ValidForm",
|
|
523
|
+
* value: {
|
|
524
|
+
* confirmPassword: "bbcde123",
|
|
525
|
+
* password: "abcde123",
|
|
526
|
+
* },
|
|
527
|
+
* reason: {
|
|
528
|
+
* kind: "PasswordMismatch",
|
|
529
|
+
* },
|
|
530
|
+
* }),
|
|
531
|
+
* );
|
|
532
|
+
* ```
|
|
533
|
+
*
|
|
534
|
+
* @category Base Factories
|
|
535
|
+
*/
|
|
536
|
+
export declare function brand<Name extends TypeName, ParentType extends AnyType, Parent = InferType<ParentType>, RefineError extends TypeError = never>(name: Name, parent: ParentType, refine: (value: Parent) => Result<Parent, RefineError>): BrandType<ParentType, Name, RefineError, InferErrors<ParentType>>;
|
|
537
|
+
export declare function brand<Name extends TypeName, ParentType extends AnyType>(name: Name, parent: ParentType): BrandType<ParentType, Name, BrandWithoutRefineError<Name, InferErrors<ParentType>>>;
|
|
538
|
+
export interface BrandType<ParentType extends AnyType, Name extends TypeName, Error extends TypeError = never, ParentError extends TypeError = never> extends Type<"Brand", InferType<ParentType> & Brand<Name>, InferInput<ParentType>, Error, InferType<ParentType>, ParentError> {
|
|
539
|
+
readonly brand: Name;
|
|
540
|
+
readonly parentType: ParentType;
|
|
541
|
+
}
|
|
542
|
+
export interface BrandWithoutRefineError<Name extends TypeName, ParentError extends TypeError> extends TypeError<Name> {
|
|
543
|
+
readonly parentError: ParentError;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* A three-letter ISO 4217 currency code (e.g., USD, EUR).
|
|
547
|
+
*
|
|
548
|
+
* @category String
|
|
549
|
+
*/
|
|
550
|
+
export declare const CurrencyCode: BrandType<Type<"String", string, string, StringError, string, StringError>, "CurrencyCode", CurrencyCodeError, StringError>;
|
|
551
|
+
export type CurrencyCode = typeof CurrencyCode.Type;
|
|
552
|
+
export interface CurrencyCodeError extends TypeError<"CurrencyCode"> {
|
|
553
|
+
}
|
|
554
|
+
export declare const formatCurrencyCodeError: TypeErrorFormatter<CurrencyCodeError>;
|
|
555
|
+
/**
|
|
556
|
+
* ISO 8601 date-time string.
|
|
557
|
+
*
|
|
558
|
+
* This {@link Type} represents a date-time string that follows the ISO 8601
|
|
559
|
+
* format and is compatible with SQLite, which lacks a native date type and
|
|
560
|
+
* relies on ISO 8601 strings for sorting. Enforcing a 24-character format
|
|
561
|
+
* ensures correct lexicographic ordering.
|
|
562
|
+
*
|
|
563
|
+
* It must be a valid JavaScript Date string that can be parsed.
|
|
564
|
+
*
|
|
565
|
+
* Valid range: `"0000-01-01T00:00:00.000Z"` to `"9999-12-31T23:59:59.999Z"`.
|
|
566
|
+
*
|
|
567
|
+
* ### Example
|
|
568
|
+
*
|
|
569
|
+
* ```ts
|
|
570
|
+
* const result = DateIsoString.from("2023-01-01T12:00:00.000Z"); // ok
|
|
571
|
+
* const error = DateIsoString.from("10000-01-01T00:00:00.000Z"); // err
|
|
572
|
+
* ```
|
|
573
|
+
*
|
|
574
|
+
* @category String
|
|
575
|
+
*/
|
|
576
|
+
export declare const DateIsoString: BrandType<Type<"String", string, string, StringError, string, StringError>, "DateIso", DateIsoStringError, StringError>;
|
|
577
|
+
export type DateIsoString = typeof DateIsoString.Type;
|
|
578
|
+
export interface DateIsoStringError extends TypeError<"DateIsoString"> {
|
|
579
|
+
}
|
|
580
|
+
export declare const formatDateIsoStringError: TypeErrorFormatter<DateIsoStringError>;
|
|
581
|
+
/**
|
|
582
|
+
* Helper type for Type Factory that creates a branded Type.
|
|
583
|
+
*
|
|
584
|
+
* ### Example
|
|
585
|
+
*
|
|
586
|
+
* ```ts
|
|
587
|
+
* const trimmed: BrandFactory<"Trimmed", string, TrimmedError> = (
|
|
588
|
+
* parent,
|
|
589
|
+
* ) =>
|
|
590
|
+
* brand("Trimmed", parent, (value) =>
|
|
591
|
+
* value.trim().length === value.length
|
|
592
|
+
* ? ok(value)
|
|
593
|
+
* : err<TrimmedError>({ type: "Trimmed", value }),
|
|
594
|
+
* );
|
|
595
|
+
* ```
|
|
596
|
+
*
|
|
597
|
+
* @category Utilities
|
|
598
|
+
*/
|
|
599
|
+
export type BrandFactory<Name extends TypeName, Input, RefineError extends TypeError> = <PName extends TypeName, P extends Input, PInput, PParent, PError extends TypeError = never, PParentError extends TypeError = never>(parent: Type<PName, P, PInput, PError, PParent, PParentError>) => BrandType<Type<PName, P, PInput, PError, PParent, PParentError>, Name, RefineError, PError | PParentError>;
|
|
600
|
+
/**
|
|
601
|
+
* Trimmed string.
|
|
602
|
+
*
|
|
603
|
+
* This Type Factory does not transform; it only validates whether a string has
|
|
604
|
+
* no leading or trailing whitespaces. To trim a string, use {@link trim} Type
|
|
605
|
+
* Factory.
|
|
606
|
+
*
|
|
607
|
+
* ### Examples
|
|
608
|
+
*
|
|
609
|
+
* ```ts
|
|
610
|
+
* // this Type already exists
|
|
611
|
+
* const TrimmedString = trimmed(String);
|
|
612
|
+
* type TrimmedString = typeof TrimmedString.Type;
|
|
613
|
+
*
|
|
614
|
+
* // we can make any branded Type trimmed:
|
|
615
|
+
* const TrimmedNonEmptyString = trimmed(minLength(1)(String));
|
|
616
|
+
* // string & Brand<"MinLength1"> & Brand<"Trimmed">
|
|
617
|
+
* type TrimmedNonEmptyString = typeof TrimmedNonEmptyString.Type;
|
|
618
|
+
* ```
|
|
619
|
+
*
|
|
620
|
+
* @category String
|
|
621
|
+
*/
|
|
622
|
+
export declare const trimmed: BrandFactory<"Trimmed", string, TrimmedError>;
|
|
623
|
+
export interface TrimmedError extends TypeError<"Trimmed"> {
|
|
624
|
+
}
|
|
625
|
+
export declare const formatTrimmedError: TypeErrorFormatter<TrimmedError>;
|
|
626
|
+
export type TransformBrandFactory<Name extends TypeName, Input, TransformError extends TypeError = never> = <PName extends TypeName, P extends Input, PInput, PParent, PError extends TypeError = never, PParentError extends TypeError = never>(parent: Type<PName, P, PInput, PError, PParent, PParentError>) => TransformType<Type<PName, P, PInput, PError, PParent, PParentError>, BrandType<Type<PName, P, PInput, PError, PParent, PParentError>, Name, never, PError | PParentError>, TransformError>;
|
|
627
|
+
/**
|
|
628
|
+
* Trims leading and trailing whitespace from a string.
|
|
629
|
+
*
|
|
630
|
+
* This Type Factory **transforms** the input string by removing whitespace from
|
|
631
|
+
* both ends. For validation only, use {@link trimmed} Type Factory.
|
|
632
|
+
*
|
|
633
|
+
* ### Example
|
|
634
|
+
*
|
|
635
|
+
* ```ts
|
|
636
|
+
* const TrimString = trim(String);
|
|
637
|
+
* expect(TrimString.from("a ")).toEqual(ok("a"));
|
|
638
|
+
* expect(TrimString.fromParent("a ").value).toEqual("a");
|
|
639
|
+
*
|
|
640
|
+
* const TrimNonEmptyString = trim(NonEmptyString);
|
|
641
|
+
* expect(TrimNonEmptyString.from("a " as NonEmptyString)).toEqual(ok("a"));
|
|
642
|
+
* expect(
|
|
643
|
+
* TrimNonEmptyString.fromParent("a " as NonEmptyString).value,
|
|
644
|
+
* ).toEqual("a");
|
|
645
|
+
* ```
|
|
646
|
+
*
|
|
647
|
+
* **Note:** This transformation is irreversible. Calling `toParent` will not
|
|
648
|
+
* restore the original representation.
|
|
649
|
+
*
|
|
650
|
+
* @category String
|
|
651
|
+
*/
|
|
652
|
+
export declare const trim: TransformBrandFactory<"Trimmed", string>;
|
|
653
|
+
/**
|
|
654
|
+
* Trimmed string
|
|
655
|
+
*
|
|
656
|
+
* - Use `TrimmedString.is` to check if an unknown value is trimmed.
|
|
657
|
+
* - Use `TrimmedString.from` to check if a string is trimmed.
|
|
658
|
+
*
|
|
659
|
+
* @category String
|
|
660
|
+
*/
|
|
661
|
+
export declare const TrimmedString: BrandType<Type<"String", string, string, StringError, string, StringError>, "Trimmed", TrimmedError, StringError>;
|
|
662
|
+
export type TrimmedString = typeof TrimmedString.Type;
|
|
663
|
+
/**
|
|
664
|
+
* Minimum length.
|
|
665
|
+
*
|
|
666
|
+
* ### Example
|
|
667
|
+
*
|
|
668
|
+
* ```ts
|
|
669
|
+
* // string & Brand<"MinLength1">
|
|
670
|
+
* const NonEmptyString = minLength(1)(String);
|
|
671
|
+
* ```
|
|
672
|
+
*
|
|
673
|
+
* @category String
|
|
674
|
+
* @category Array
|
|
675
|
+
*/
|
|
676
|
+
export declare const minLength: <Min extends number>(min: Min) => BrandFactory<`MinLength${Min}`, {
|
|
677
|
+
length: number;
|
|
678
|
+
}, MinLengthError<Min>>;
|
|
679
|
+
export interface MinLengthError<Min extends number = number> extends TypeError<"MinLength"> {
|
|
680
|
+
readonly min: Min;
|
|
681
|
+
}
|
|
682
|
+
export declare const formatMinLengthError: TypeErrorFormatter<MinLengthError<number>>;
|
|
683
|
+
/**
|
|
684
|
+
* Maximum length.
|
|
685
|
+
*
|
|
686
|
+
* ### Example
|
|
687
|
+
*
|
|
688
|
+
* ```ts
|
|
689
|
+
* // string & Brand<"MaxLength100">
|
|
690
|
+
* const String100 = maxLength(100)(String);
|
|
691
|
+
* ```
|
|
692
|
+
*
|
|
693
|
+
* @category String
|
|
694
|
+
* @category Array
|
|
695
|
+
*/
|
|
696
|
+
export declare const maxLength: <Max extends number>(max: Max) => BrandFactory<`MaxLength${Max}`, {
|
|
697
|
+
length: number;
|
|
698
|
+
}, MaxLengthError<Max>>;
|
|
699
|
+
export interface MaxLengthError<Max extends number = number> extends TypeError<"MaxLength"> {
|
|
700
|
+
readonly max: Max;
|
|
701
|
+
}
|
|
702
|
+
export declare const formatMaxLengthError: TypeErrorFormatter<MaxLengthError<number>>;
|
|
703
|
+
/**
|
|
704
|
+
* Exact length.
|
|
705
|
+
*
|
|
706
|
+
* ### Example
|
|
707
|
+
*
|
|
708
|
+
* ```ts
|
|
709
|
+
* // string & Brand<"Length1">
|
|
710
|
+
* const Length1String = length(1)(String);
|
|
711
|
+
* ```
|
|
712
|
+
*
|
|
713
|
+
* @category String
|
|
714
|
+
* @category Array
|
|
715
|
+
*/
|
|
716
|
+
export declare const length: <Exact extends number>(exact: Exact) => BrandFactory<`Length${Exact}`, {
|
|
717
|
+
length: number;
|
|
718
|
+
}, LengthError<Exact>>;
|
|
719
|
+
export interface LengthError<Exact extends number = number> extends TypeError<"Length"> {
|
|
720
|
+
readonly exact: Exact;
|
|
721
|
+
}
|
|
722
|
+
export declare const formatLengthError: TypeErrorFormatter<LengthError<number>>;
|
|
723
|
+
/** @category String */
|
|
724
|
+
export declare const NonEmptyString: BrandType<Type<"String", string, string, StringError, string, StringError>, "MinLength1", MinLengthError<1>, StringError>;
|
|
725
|
+
export type NonEmptyString = typeof NonEmptyString.Type;
|
|
726
|
+
/** @category String */
|
|
727
|
+
export declare const String100: BrandType<Type<"String", string, string, StringError, string, StringError>, "MaxLength100", MaxLengthError<100>, StringError>;
|
|
728
|
+
export type String100 = typeof String100.Type;
|
|
729
|
+
/** @category String */
|
|
730
|
+
export declare const String1000: BrandType<Type<"String", string, string, StringError, string, StringError>, "MaxLength1000", MaxLengthError<1000>, StringError>;
|
|
731
|
+
export type String1000 = typeof String1000.Type;
|
|
732
|
+
/** @category String */
|
|
733
|
+
export declare const NonEmptyString100: BrandType<Type<"Brand", string & Brand<"MaxLength100">, string, MaxLengthError<100>, string, StringError>, "MinLength1", MinLengthError<1>, StringError | MaxLengthError<100>>;
|
|
734
|
+
export type NonEmptyString100 = typeof NonEmptyString100.Type;
|
|
735
|
+
/** @category String */
|
|
736
|
+
export declare const NonEmptyString1000: BrandType<Type<"Brand", string & Brand<"MaxLength1000">, string, MaxLengthError<1000>, string, StringError>, "MinLength1", MinLengthError<1>, StringError | MaxLengthError<1000>>;
|
|
737
|
+
export type NonEmptyString1000 = typeof NonEmptyString1000.Type;
|
|
738
|
+
/** @category String */
|
|
739
|
+
export declare const NonEmptyTrimmedString: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>;
|
|
740
|
+
export type NonEmptyTrimmedString = typeof NonEmptyTrimmedString.Type;
|
|
741
|
+
/** @category String */
|
|
742
|
+
export declare const TrimmedString100: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MaxLength100", MaxLengthError<100>, StringError | TrimmedError>;
|
|
743
|
+
export type TrimmedString100 = typeof TrimmedString100.Type;
|
|
744
|
+
/** @category String */
|
|
745
|
+
export declare const TrimmedString1000: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MaxLength1000", MaxLengthError<1000>, StringError | TrimmedError>;
|
|
746
|
+
export type TrimmedString1000 = typeof TrimmedString1000.Type;
|
|
747
|
+
/** @category String */
|
|
748
|
+
export declare const NonEmptyTrimmedString100: BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength100">, string, MaxLengthError<100>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError | MaxLengthError<100>>;
|
|
749
|
+
export type NonEmptyTrimmedString100 = typeof NonEmptyTrimmedString100.Type;
|
|
750
|
+
/** @category String */
|
|
751
|
+
export declare const NonEmptyTrimmedString1000: BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength1000">, string, MaxLengthError<1000>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError | MaxLengthError<1000>>;
|
|
752
|
+
export type NonEmptyTrimmedString1000 = typeof NonEmptyTrimmedString1000.Type;
|
|
753
|
+
/**
|
|
754
|
+
* The mnemonic, also known as a "seed phrase," is a set of 12 words in a
|
|
755
|
+
* specific order chosen from a predefined list (BIP39). It provides a
|
|
756
|
+
* human-readable way to store a private key securely. The mnemonic is generated
|
|
757
|
+
* safely on the user's device using cryptographically secure random number
|
|
758
|
+
* generation, ensuring it remains private and unique.
|
|
759
|
+
*
|
|
760
|
+
* @category String
|
|
761
|
+
*/
|
|
762
|
+
export declare const Mnemonic: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>, "Mnemonic", MnemonicError, StringError | TrimmedError | MinLengthError<1>>;
|
|
763
|
+
export type Mnemonic = typeof Mnemonic.Type;
|
|
764
|
+
export interface MnemonicError extends TypeError<"Mnemonic"> {
|
|
765
|
+
}
|
|
766
|
+
export declare const formatMnemonicError: TypeErrorFormatter<MnemonicError>;
|
|
767
|
+
/**
|
|
768
|
+
* String matching a regular expression.
|
|
769
|
+
*
|
|
770
|
+
* ### Example
|
|
771
|
+
*
|
|
772
|
+
* ```ts
|
|
773
|
+
* const Alphanumeric = regex("Alphanumeric", /^[a-z0-9]+$/i)(String);
|
|
774
|
+
* ```
|
|
775
|
+
*
|
|
776
|
+
* @category String
|
|
777
|
+
*/
|
|
778
|
+
export declare const regex: <Name extends TypeName>(name: Name, pattern: RegExp) => BrandFactory<Name, string, RegexError<Name>>;
|
|
779
|
+
export interface RegexError<Name extends TypeName = TypeName> extends TypeError<"Regex"> {
|
|
780
|
+
readonly name: Name;
|
|
781
|
+
readonly pattern: RegExp;
|
|
782
|
+
}
|
|
783
|
+
export declare const formatRegexError: TypeErrorFormatter<RegexError<Capitalize<string>>>;
|
|
784
|
+
/**
|
|
785
|
+
* URL-safe Base64 string.
|
|
786
|
+
*
|
|
787
|
+
* A `Base64Url` string uses a limited alphabet that is URL-safe:
|
|
788
|
+
*
|
|
789
|
+
* - Uppercase letters (`A-Z`)
|
|
790
|
+
* - Lowercase letters (`a-z`)
|
|
791
|
+
* - Digits (`0-9`)
|
|
792
|
+
* - Dash (`-`)
|
|
793
|
+
* - Underscore (`_`)
|
|
794
|
+
*
|
|
795
|
+
* ### Example
|
|
796
|
+
*
|
|
797
|
+
* ```ts
|
|
798
|
+
* const result = Base64Url.from("abc123_-");
|
|
799
|
+
* if (result.ok) {
|
|
800
|
+
* console.log("Valid Base64Url string:", result.value);
|
|
801
|
+
* } else {
|
|
802
|
+
* console.error("Invalid Base64Url string:", result.error);
|
|
803
|
+
* }
|
|
804
|
+
* ```
|
|
805
|
+
*
|
|
806
|
+
* @category String
|
|
807
|
+
*/
|
|
808
|
+
export declare const Base64Url: BrandType<Type<"String", string, string, StringError, string, StringError>, "Base64Url", RegexError<"Base64Url">, StringError>;
|
|
809
|
+
export type Base64Url = typeof Base64Url.Type;
|
|
810
|
+
export type Base64UrlError = typeof Base64Url.Error;
|
|
811
|
+
/**
|
|
812
|
+
* Simple alphanumeric string for naming.
|
|
813
|
+
*
|
|
814
|
+
* A `SimpleName` string uses a limited, safe alphabet for naming purposes:
|
|
815
|
+
*
|
|
816
|
+
* - Uppercase letters (`A-Z`)
|
|
817
|
+
* - Lowercase letters (`a-z`)
|
|
818
|
+
* - Digits (`0-9`)
|
|
819
|
+
* - Dash (`-`)
|
|
820
|
+
*
|
|
821
|
+
* The string must be between 1 and 42 characters.
|
|
822
|
+
*
|
|
823
|
+
* ### Example
|
|
824
|
+
*
|
|
825
|
+
* ```ts
|
|
826
|
+
* const result = SimpleName.from("data-report-123");
|
|
827
|
+
* if (result.ok) {
|
|
828
|
+
* console.log("Valid SimpleName string:", result.value);
|
|
829
|
+
* } else {
|
|
830
|
+
* console.error("Invalid SimpleName string:", result.error);
|
|
831
|
+
* }
|
|
832
|
+
* ```
|
|
833
|
+
*
|
|
834
|
+
* @category String
|
|
835
|
+
*/
|
|
836
|
+
export declare const SimpleName: BrandType<Type<"String", string, string, StringError, string, StringError>, "SimpleName", RegexError<"SimpleName">, StringError>;
|
|
837
|
+
export type SimpleName = typeof SimpleName.Type;
|
|
838
|
+
export type SimpleNameError = typeof SimpleName.Error;
|
|
839
|
+
/**
|
|
840
|
+
* Default NanoId.
|
|
841
|
+
*
|
|
842
|
+
* @category String
|
|
843
|
+
*/
|
|
844
|
+
export declare const NanoId: BrandType<Type<"String", string, string, StringError, string, StringError>, "NanoId", RegexError<"NanoId">, StringError>;
|
|
845
|
+
export type NanoId = typeof NanoId.Type;
|
|
846
|
+
export type NanoIdError = typeof NanoId.Error;
|
|
847
|
+
/**
|
|
848
|
+
* Trimmed string between 8 and 64 characters, branded as `SimplePassword`.
|
|
849
|
+
*
|
|
850
|
+
* @category String
|
|
851
|
+
*/
|
|
852
|
+
export declare const SimplePassword: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength64">, string, MaxLengthError<64>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength8", MinLengthError<8>, StringError | TrimmedError | MaxLengthError<64>>, "SimplePassword", BrandWithoutRefineError<"SimplePassword", StringError | TrimmedError | MinLengthError<8> | MaxLengthError<64>>, never>;
|
|
853
|
+
export type SimplePassword = typeof SimplePassword.Type;
|
|
854
|
+
export type SimplePasswordError = typeof SimplePassword.Error;
|
|
855
|
+
export declare const formatSimplePasswordError: (formatTypeError: TypeErrorFormatter<StringError | MinLengthError<8> | MaxLengthError<64> | TrimmedError>) => TypeErrorFormatter<SimplePasswordError>;
|
|
856
|
+
/**
|
|
857
|
+
* `Id` {@link Type}.
|
|
858
|
+
*
|
|
859
|
+
* Represents a unique identifier with exactly 21 characters, using NanoID's
|
|
860
|
+
* standard format (`A-Za-z0-9_-`).
|
|
861
|
+
*
|
|
862
|
+
* @category String
|
|
863
|
+
*/
|
|
864
|
+
export declare const Id: BrandType<Type<"String", string, string, StringError, string, StringError>, "Id", RegexError<"Id">, StringError>;
|
|
865
|
+
export type Id = typeof Id.Type;
|
|
866
|
+
export declare const idTypeValueLength = 21;
|
|
867
|
+
/**
|
|
868
|
+
* Creates an {@link Id}.
|
|
869
|
+
*
|
|
870
|
+
* ### Example
|
|
871
|
+
*
|
|
872
|
+
* ```ts
|
|
873
|
+
* // string & Brand<"Id">
|
|
874
|
+
* const id = createId(deps);
|
|
875
|
+
* ```
|
|
876
|
+
*/
|
|
877
|
+
export declare const createId: (deps: NanoIdLibDep) => Id;
|
|
878
|
+
/**
|
|
879
|
+
* Type Factory to create branded {@link Id} Type for a specific table.
|
|
880
|
+
*
|
|
881
|
+
* ### Example
|
|
882
|
+
*
|
|
883
|
+
* ```ts
|
|
884
|
+
* const TodoId = id("Todo");
|
|
885
|
+
* // string & Brand<"Id"> & Brand<"Todo">
|
|
886
|
+
* type TodoId = typeof TodoId.Type;
|
|
887
|
+
* ```
|
|
888
|
+
*
|
|
889
|
+
* @category String
|
|
890
|
+
*/
|
|
891
|
+
export declare const id: <Table extends TypeName>(table: Table) => IdType<Table>;
|
|
892
|
+
export interface IdType<Table extends TypeName> extends Type<"Id", string & Brand<"Id"> & Brand<Table>, string, IdError<Table>, string, StringError> {
|
|
893
|
+
table: Table;
|
|
894
|
+
}
|
|
895
|
+
export interface IdError<Table extends TypeName = TypeName> extends TypeError<"Id"> {
|
|
896
|
+
readonly table: Table;
|
|
897
|
+
}
|
|
898
|
+
export declare const formatIdError: TypeErrorFormatter<IdError<Capitalize<string>>>;
|
|
899
|
+
/**
|
|
900
|
+
* Positive number.
|
|
901
|
+
*
|
|
902
|
+
* ### Example
|
|
903
|
+
*
|
|
904
|
+
* ```ts
|
|
905
|
+
* const PositiveNumber = positive(Number);
|
|
906
|
+
*
|
|
907
|
+
* const result = PositiveNumber.from(42); // ok
|
|
908
|
+
* const errorResult = PositiveNumber.from(-5); // err
|
|
909
|
+
* ```
|
|
910
|
+
*
|
|
911
|
+
* @category Number
|
|
912
|
+
*/
|
|
913
|
+
export declare const positive: BrandFactory<"Positive", number, PositiveError>;
|
|
914
|
+
export interface PositiveError extends TypeError<"Positive"> {
|
|
915
|
+
}
|
|
916
|
+
export declare const formatPositiveError: TypeErrorFormatter<PositiveError>;
|
|
917
|
+
/**
|
|
918
|
+
* Negative number.
|
|
919
|
+
*
|
|
920
|
+
* ### Example
|
|
921
|
+
*
|
|
922
|
+
* ```ts
|
|
923
|
+
* const NegativeNumber = negative(Number);
|
|
924
|
+
* ```
|
|
925
|
+
*
|
|
926
|
+
* @category Number
|
|
927
|
+
*/
|
|
928
|
+
export declare const negative: BrandFactory<"Negative", number, NegativeError>;
|
|
929
|
+
export interface NegativeError extends TypeError<"Negative"> {
|
|
930
|
+
}
|
|
931
|
+
export declare const formatNegativeError: TypeErrorFormatter<NegativeError>;
|
|
932
|
+
/**
|
|
933
|
+
* Non-positive number.
|
|
934
|
+
*
|
|
935
|
+
* ### Example
|
|
936
|
+
*
|
|
937
|
+
* ```ts
|
|
938
|
+
* const NonPositiveNumber = nonPositive(Number);
|
|
939
|
+
* ```
|
|
940
|
+
*
|
|
941
|
+
* @category Number
|
|
942
|
+
*/
|
|
943
|
+
export declare const nonPositive: BrandFactory<"NonPositive", number, NonPositiveError>;
|
|
944
|
+
export interface NonPositiveError extends TypeError<"NonPositive"> {
|
|
945
|
+
}
|
|
946
|
+
export declare const formatNonPositiveError: TypeErrorFormatter<NonPositiveError>;
|
|
947
|
+
/**
|
|
948
|
+
* Non-negative number.
|
|
949
|
+
*
|
|
950
|
+
* ### Example
|
|
951
|
+
*
|
|
952
|
+
* ```ts
|
|
953
|
+
* const NonNegativeNumber = nonNegative(Number);
|
|
954
|
+
* ```
|
|
955
|
+
*
|
|
956
|
+
* @category Number
|
|
957
|
+
*/
|
|
958
|
+
export declare const nonNegative: BrandFactory<"NonNegative", number, NonNegativeError>;
|
|
959
|
+
export interface NonNegativeError extends TypeError<"NonNegative"> {
|
|
960
|
+
}
|
|
961
|
+
export declare const formatNonNegativeError: TypeErrorFormatter<NonNegativeError>;
|
|
962
|
+
/** @category Number */
|
|
963
|
+
export declare const NonNegativeNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonNegative", NonNegativeError, NumberError>;
|
|
964
|
+
export type NonNegativeNumber = typeof NonNegativeNumber.Type;
|
|
965
|
+
/** @category Number */
|
|
966
|
+
export declare const PositiveNumber: BrandType<Type<"Brand", number & Brand<"NonNegative">, number, NonNegativeError, number, NumberError>, "Positive", PositiveError, NumberError | NonNegativeError>;
|
|
967
|
+
export type PositiveNumber = typeof PositiveNumber.Type;
|
|
968
|
+
/** @category Number */
|
|
969
|
+
export declare const NonPositiveNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonPositive", NonPositiveError, NumberError>;
|
|
970
|
+
export type NonPositiveNumber = typeof NonPositiveNumber.Type;
|
|
971
|
+
/** @category Number */
|
|
972
|
+
export declare const NegativeNumber: BrandType<Type<"Brand", number & Brand<"NonPositive">, number, NonPositiveError, number, NumberError>, "Negative", NegativeError, NumberError | NonPositiveError>;
|
|
973
|
+
export type NegativeNumber = typeof NegativeNumber.Type;
|
|
974
|
+
/**
|
|
975
|
+
* Integer within the safe range of JavaScript numbers.
|
|
976
|
+
*
|
|
977
|
+
* ### Example
|
|
978
|
+
*
|
|
979
|
+
* ```ts
|
|
980
|
+
* const Int = int(Number);
|
|
981
|
+
* ```
|
|
982
|
+
*
|
|
983
|
+
* @category Number
|
|
984
|
+
*/
|
|
985
|
+
export declare const int: BrandFactory<"Int", number, IntError>;
|
|
986
|
+
export interface IntError extends TypeError<"Int"> {
|
|
987
|
+
}
|
|
988
|
+
export declare const formatIntError: TypeErrorFormatter<IntError>;
|
|
989
|
+
/**
|
|
990
|
+
* Integer within the safe range of JavaScript numbers.
|
|
991
|
+
*
|
|
992
|
+
* @category Number
|
|
993
|
+
*/
|
|
994
|
+
export declare const Int: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Int", IntError, NumberError>;
|
|
995
|
+
export type Int = typeof Int.Type;
|
|
996
|
+
/** @category Number */
|
|
997
|
+
export declare const NonNegativeInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonNegative", NonNegativeError, NumberError | IntError>;
|
|
998
|
+
export type NonNegativeInt = typeof NonNegativeInt.Type;
|
|
999
|
+
/** @category Number */
|
|
1000
|
+
export declare const PositiveInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, NonNegativeError, number & Brand<"Int">, NumberError | IntError>, "Positive", PositiveError, NumberError | NonNegativeError | IntError>;
|
|
1001
|
+
export type PositiveInt = typeof PositiveInt.Type;
|
|
1002
|
+
/** @category Number */
|
|
1003
|
+
export declare const NonPositiveInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonPositive", NonPositiveError, NumberError | IntError>;
|
|
1004
|
+
export type NonPositiveInt = typeof NonPositiveInt.Type;
|
|
1005
|
+
/** @category Number */
|
|
1006
|
+
export declare const NegativeInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonPositive">, number, NonPositiveError, number & Brand<"Int">, NumberError | IntError>, "Negative", NegativeError, NumberError | NonPositiveError | IntError>;
|
|
1007
|
+
export type NegativeInt = typeof NegativeInt.Type;
|
|
1008
|
+
/**
|
|
1009
|
+
* Number greater than a specified value.
|
|
1010
|
+
*
|
|
1011
|
+
* @category Number
|
|
1012
|
+
*/
|
|
1013
|
+
export declare const greaterThan: <Min extends number>(min: Min) => BrandFactory<`GreaterThan${Min}`, number, GreaterThanError<Min>>;
|
|
1014
|
+
export interface GreaterThanError<Min extends number = number> extends TypeError<"GreaterThan"> {
|
|
1015
|
+
readonly min: Min;
|
|
1016
|
+
}
|
|
1017
|
+
export declare const formatGreaterThanError: TypeErrorFormatter<GreaterThanError<number>>;
|
|
1018
|
+
/**
|
|
1019
|
+
* Number less than a specified value.
|
|
1020
|
+
*
|
|
1021
|
+
* @category Number
|
|
1022
|
+
*/
|
|
1023
|
+
export declare const lessThan: <Max extends number>(max: Max) => BrandFactory<`LessThan${Max}`, number, LessThanError<Max>>;
|
|
1024
|
+
export interface LessThanError<Max extends number = number> extends TypeError<"LessThan"> {
|
|
1025
|
+
readonly max: Max;
|
|
1026
|
+
}
|
|
1027
|
+
export declare const formatLessThanError: TypeErrorFormatter<LessThanError<number>>;
|
|
1028
|
+
/**
|
|
1029
|
+
* Number ≥ a specified value.
|
|
1030
|
+
*
|
|
1031
|
+
* @category Number
|
|
1032
|
+
*/
|
|
1033
|
+
export declare const greaterThanOrEqualTo: <Min extends number>(min: Min) => BrandFactory<`GreaterThanOrEqualTo${Min}`, number, GreaterThanOrEqualToError<Min>>;
|
|
1034
|
+
export interface GreaterThanOrEqualToError<Min extends number = number> extends TypeError<"GreaterThanOrEqualTo"> {
|
|
1035
|
+
readonly min: Min;
|
|
1036
|
+
}
|
|
1037
|
+
export declare const formatGreaterThanOrEqualToError: TypeErrorFormatter<GreaterThanOrEqualToError<number>>;
|
|
1038
|
+
/**
|
|
1039
|
+
* Number ≤ a specified value.
|
|
1040
|
+
*
|
|
1041
|
+
* @category Number
|
|
1042
|
+
*/
|
|
1043
|
+
export declare const lessThanOrEqualTo: <Max extends number>(max: Max) => BrandFactory<`LessThanOrEqualTo${Max}`, number, LessThanOrEqualToError<Max>>;
|
|
1044
|
+
export interface LessThanOrEqualToError<Max extends number = number> extends TypeError<"LessThanOrEqualTo"> {
|
|
1045
|
+
readonly max: Max;
|
|
1046
|
+
}
|
|
1047
|
+
export declare const formatLessThanOrEqualToError: TypeErrorFormatter<LessThanOrEqualToError<number>>;
|
|
1048
|
+
/**
|
|
1049
|
+
* Number that is not NaN.
|
|
1050
|
+
*
|
|
1051
|
+
* @category Number
|
|
1052
|
+
*/
|
|
1053
|
+
export declare const nonNaN: BrandFactory<"NonNaN", number, NonNaNError>;
|
|
1054
|
+
export interface NonNaNError extends TypeError<"NonNaN"> {
|
|
1055
|
+
}
|
|
1056
|
+
export declare const formatNonNaNError: TypeErrorFormatter<NonNaNError>;
|
|
1057
|
+
/** @category Number */
|
|
1058
|
+
export declare const NonNaNNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonNaN", NonNaNError, NumberError>;
|
|
1059
|
+
export type NonNaNNumber = typeof NonNaNNumber.Type;
|
|
1060
|
+
/**
|
|
1061
|
+
* Finite number.
|
|
1062
|
+
*
|
|
1063
|
+
* @category Number
|
|
1064
|
+
*/
|
|
1065
|
+
export declare const finite: BrandFactory<"Finite", number, FiniteError>;
|
|
1066
|
+
export interface FiniteError extends TypeError<"Finite"> {
|
|
1067
|
+
}
|
|
1068
|
+
export declare const formatFiniteError: TypeErrorFormatter<FiniteError>;
|
|
1069
|
+
/**
|
|
1070
|
+
* Finite number.
|
|
1071
|
+
*
|
|
1072
|
+
* This Type ensures that a number is finite.
|
|
1073
|
+
*
|
|
1074
|
+
* **Why is this important?**
|
|
1075
|
+
*
|
|
1076
|
+
* `JSON.stringify` serializes JavaScript numbers into `null` if they are not
|
|
1077
|
+
* finite (e.g., `Infinity`, `-Infinity`, or `NaN`). Using `FiniteNumber` helps
|
|
1078
|
+
* prevent these unexpected behaviors when working with JSON serialization.
|
|
1079
|
+
*
|
|
1080
|
+
* @category Number
|
|
1081
|
+
*/
|
|
1082
|
+
export declare const FiniteNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>;
|
|
1083
|
+
export type FiniteNumber = typeof FiniteNumber.Type;
|
|
1084
|
+
/**
|
|
1085
|
+
* Number that is a multiple of a divisor.
|
|
1086
|
+
*
|
|
1087
|
+
* @category Number
|
|
1088
|
+
*/
|
|
1089
|
+
export declare const multipleOf: <Divisor extends number>(divisor: Divisor) => BrandFactory<`MultipleOf${Divisor}`, number, MultipleOfError<Divisor>>;
|
|
1090
|
+
export interface MultipleOfError<Divisor extends number = number> extends TypeError<"MultipleOf"> {
|
|
1091
|
+
readonly divisor: Divisor;
|
|
1092
|
+
}
|
|
1093
|
+
export declare const formatMultipleOfError: TypeErrorFormatter<MultipleOfError<number>>;
|
|
1094
|
+
/**
|
|
1095
|
+
* Number within a range, inclusive.
|
|
1096
|
+
*
|
|
1097
|
+
* ### Example
|
|
1098
|
+
*
|
|
1099
|
+
* ```ts
|
|
1100
|
+
* const Between1And10 = between(1, 10)(PositiveNumber);
|
|
1101
|
+
* const result = Between1And10.from(5); // ok(5)
|
|
1102
|
+
* const errorResult = Between1And10.from(11); // err
|
|
1103
|
+
* ```
|
|
1104
|
+
*
|
|
1105
|
+
* @category Number
|
|
1106
|
+
*/
|
|
1107
|
+
export declare const between: <Min extends number, Max extends number>(min: Min, max: Max) => BrandFactory<`Between${Min}-${Max}`, number, BetweenError<Min, Max>>;
|
|
1108
|
+
export interface BetweenError<Min extends number = number, Max extends number = number> extends TypeError<"Between"> {
|
|
1109
|
+
readonly min: Min;
|
|
1110
|
+
readonly max: Max;
|
|
1111
|
+
}
|
|
1112
|
+
export declare const formatBetweenError: TypeErrorFormatter<BetweenError<number, number>>;
|
|
1113
|
+
/** @category Number */
|
|
1114
|
+
export declare const Between1And10: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Between1-10", BetweenError<1, 10>, NumberError>;
|
|
1115
|
+
export type Between1And10 = typeof Between1And10.Type;
|
|
1116
|
+
/**
|
|
1117
|
+
* Literal {@link Type}.
|
|
1118
|
+
*
|
|
1119
|
+
* https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types
|
|
1120
|
+
*
|
|
1121
|
+
* ### Example
|
|
1122
|
+
*
|
|
1123
|
+
* ```ts
|
|
1124
|
+
* const LiteralHello = literal("Hello");
|
|
1125
|
+
* const result = LiteralHello.from("Hello"); // ok("Hello")
|
|
1126
|
+
* const errorResult = LiteralHello.from("World"); // err
|
|
1127
|
+
* ```
|
|
1128
|
+
*
|
|
1129
|
+
* TODO: Add JsonValue
|
|
1130
|
+
*
|
|
1131
|
+
* @category Base Factories
|
|
1132
|
+
*/
|
|
1133
|
+
export declare const literal: <T extends Literal>(expected: T) => LiteralType<T>;
|
|
1134
|
+
export interface LiteralType<T extends Literal> extends Type<"Literal", T, WidenLiteral<T>, LiteralError<T>> {
|
|
1135
|
+
expected: T;
|
|
1136
|
+
}
|
|
1137
|
+
export interface LiteralError<T extends Literal = Literal> extends TypeError<"Literal"> {
|
|
1138
|
+
readonly expected: T;
|
|
1139
|
+
}
|
|
1140
|
+
export declare const formatLiteralError: TypeErrorFormatter<LiteralError<Literal>>;
|
|
1141
|
+
/**
|
|
1142
|
+
* {@link Type} that transforms values between `FromType` and `ToType`.
|
|
1143
|
+
*
|
|
1144
|
+
* - `fromParent`: Converts `FromType` to `ToType`, may fail.
|
|
1145
|
+
* - `toParent`: Converts `ToType` back to `FromType`, must not fail.
|
|
1146
|
+
*
|
|
1147
|
+
* ### Example
|
|
1148
|
+
*
|
|
1149
|
+
* // TODO: Examples
|
|
1150
|
+
*
|
|
1151
|
+
* @category Base Factories
|
|
1152
|
+
*/
|
|
1153
|
+
export declare const transform: <FromType extends AnyType, ToType extends AnyType, TransformError extends TypeError = never>(fromType: FromType, toType: ToType, fromParent: (parentValue: InferType<FromType>) => Result<InferType<ToType>, TransformError>, toParent: (value: InferType<ToType>) => InferType<FromType>) => TransformType<FromType, ToType, TransformError>;
|
|
1154
|
+
/**
|
|
1155
|
+
* TransformType extends {@link Type} with additional `fromType` and `toType`
|
|
1156
|
+
* properties for reflection.
|
|
1157
|
+
*/
|
|
1158
|
+
export interface TransformType<FromType extends AnyType, ToType extends AnyType, TransformError extends TypeError = never> extends Type<"Transform", InferType<ToType>, InferInput<FromType>, TransformError, InferType<FromType>, InferErrors<FromType>> {
|
|
1159
|
+
readonly fromType: FromType;
|
|
1160
|
+
readonly toType: ToType;
|
|
1161
|
+
readonly fromParent: (value: InferType<FromType>) => [TransformError] extends [never] ? Ok<InferType<ToType>> : Result<InferType<ToType>, TransformError>;
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Trims leading and trailing whitespace from a string.
|
|
1165
|
+
*
|
|
1166
|
+
* ### Example
|
|
1167
|
+
*
|
|
1168
|
+
* ```ts
|
|
1169
|
+
* expect(TrimString.from("a ")).toEqual(ok("a"));
|
|
1170
|
+
* expect(TrimString.fromParent("a ").value).toEqual("a");
|
|
1171
|
+
* ```
|
|
1172
|
+
*
|
|
1173
|
+
* @category String
|
|
1174
|
+
*/
|
|
1175
|
+
export declare const TrimString: TransformType<Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"String", string, string, StringError, string, StringError>, "Trimmed", never, StringError>, never>;
|
|
1176
|
+
/**
|
|
1177
|
+
* Transforms a {@link Date} into a {@link DateIsoString} string and vice versa.
|
|
1178
|
+
*
|
|
1179
|
+
* ### Example
|
|
1180
|
+
*
|
|
1181
|
+
* TODO:
|
|
1182
|
+
*
|
|
1183
|
+
* @category String
|
|
1184
|
+
*/
|
|
1185
|
+
export declare const DateIso: TransformType<InstanceOfType<DateConstructor>, BrandType<Type<"String", string, string, StringError, string, StringError>, "DateIso", DateIsoStringError, StringError>, DateIsoStringError>;
|
|
1186
|
+
/**
|
|
1187
|
+
* Transforms a {@link NonEmptyTrimmedString} into a {@link FiniteNumber}.
|
|
1188
|
+
*
|
|
1189
|
+
* ### Example
|
|
1190
|
+
*
|
|
1191
|
+
* ```ts
|
|
1192
|
+
* NumberFromString.from("42"); // ok(42)
|
|
1193
|
+
* NumberFromString.from("abc"); // err({ type: "NumberFromString", value: "abc" })
|
|
1194
|
+
* ```
|
|
1195
|
+
*
|
|
1196
|
+
* @category Number
|
|
1197
|
+
*/
|
|
1198
|
+
export declare const NumberFromString: TransformType<BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, NumberFromStringError>;
|
|
1199
|
+
export interface NumberFromStringError extends TypeError<"NumberFromString"> {
|
|
1200
|
+
}
|
|
1201
|
+
export declare const formatNumberFromStringError: TypeErrorFormatter<NumberFromStringError>;
|
|
1202
|
+
/**
|
|
1203
|
+
* Array of a specific {@link Type}.
|
|
1204
|
+
*
|
|
1205
|
+
* Usage:
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* const NumberArray = array(Number);
|
|
1209
|
+
*
|
|
1210
|
+
* const result1 = NumberArray.from([1, 2, 3]); // ok([1, 2, 3])
|
|
1211
|
+
* const result2 = NumberArray.from(["a", "b"]); // err(...)
|
|
1212
|
+
* ```
|
|
1213
|
+
*
|
|
1214
|
+
* @category Base Factories
|
|
1215
|
+
* @category Array
|
|
1216
|
+
*/
|
|
1217
|
+
export declare const array: <ElementType extends AnyType>(element: ElementType) => ArrayType<ElementType>;
|
|
1218
|
+
/** ArrayType extends Type with an additional `element` property for reflection. */
|
|
1219
|
+
export interface ArrayType<ElementType extends AnyType> extends Type<"Array", ReadonlyArray<InferType<ElementType>>, ReadonlyArray<InferInput<ElementType>>, ArrayError<InferError<ElementType>>, ReadonlyArray<InferParent<ElementType>>, ArrayError<InferParentError<ElementType>>> {
|
|
1220
|
+
readonly element: ElementType;
|
|
1221
|
+
}
|
|
1222
|
+
export interface ArrayError<Error extends TypeError = TypeError> extends TypeErrorWithReason<"Array", {
|
|
1223
|
+
readonly kind: "NotArray";
|
|
1224
|
+
} | {
|
|
1225
|
+
readonly kind: "Element";
|
|
1226
|
+
readonly index: number;
|
|
1227
|
+
readonly error: Error;
|
|
1228
|
+
}> {
|
|
1229
|
+
}
|
|
1230
|
+
export declare const formatArrayError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<ArrayError<Error>>;
|
|
1231
|
+
/**
|
|
1232
|
+
* Record of a key {@link Type} and value {@link Type}.
|
|
1233
|
+
*
|
|
1234
|
+
* - The input must be a plain object (validated by {@link isPlainObject}).
|
|
1235
|
+
* - Each key is validated/transformed by the `key` Type.
|
|
1236
|
+
* - Each value is validated/transformed by the `value` Type.
|
|
1237
|
+
*
|
|
1238
|
+
* The resulting type is `Readonly<Record<KeyT, ValueT>>`.
|
|
1239
|
+
*
|
|
1240
|
+
* ### Example
|
|
1241
|
+
*
|
|
1242
|
+
* ```ts
|
|
1243
|
+
* const StringToNumberRecord = record(String, Number);
|
|
1244
|
+
*
|
|
1245
|
+
* // ok({ "a": 1, "b": 2 })
|
|
1246
|
+
* StringToNumberRecord.from({ a: 1, b: 2 });
|
|
1247
|
+
*
|
|
1248
|
+
* // err => "Key" because 42 is not a string key
|
|
1249
|
+
* StringToNumberRecord.from({ 42: 1, b: 2 });
|
|
1250
|
+
*
|
|
1251
|
+
* // err => "Value" because "x" is not a number
|
|
1252
|
+
* StringToNumberRecord.from({ a: "x", b: 2 });
|
|
1253
|
+
* ```
|
|
1254
|
+
*
|
|
1255
|
+
* @category Base Factories
|
|
1256
|
+
* @category Object
|
|
1257
|
+
*/
|
|
1258
|
+
export declare const record: <KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType>(keyType: Type<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError>, valueType: Value) => RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>;
|
|
1259
|
+
/**
|
|
1260
|
+
* RecordType extends {@link Type} with additional `key` and `value` properties
|
|
1261
|
+
* for reflection.
|
|
1262
|
+
*/
|
|
1263
|
+
export interface RecordType<KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType = AnyType> extends Type<"Record", Readonly<Record<KeyT, InferType<Value>>>, Readonly<Record<KeyInput, InferInput<Value>>>, RecordError<KeyError, InferError<Value>>, Readonly<Record<KeyParent, InferParent<Value>>>, RecordError<KeyParentError, InferParentError<Value>>> {
|
|
1264
|
+
readonly key: Type<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError>;
|
|
1265
|
+
readonly value: Value;
|
|
1266
|
+
}
|
|
1267
|
+
export interface RecordError<KeyError extends TypeError = TypeError, ValueError extends TypeError = TypeError> extends TypeErrorWithReason<"Record", {
|
|
1268
|
+
readonly kind: "NotRecord";
|
|
1269
|
+
} | {
|
|
1270
|
+
readonly kind: "Key";
|
|
1271
|
+
readonly key: unknown;
|
|
1272
|
+
readonly error: KeyError;
|
|
1273
|
+
} | {
|
|
1274
|
+
readonly kind: "Value";
|
|
1275
|
+
readonly key: unknown;
|
|
1276
|
+
readonly error: ValueError;
|
|
1277
|
+
}> {
|
|
1278
|
+
}
|
|
1279
|
+
export declare const formatRecordError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<RecordError<Error, Error>>;
|
|
1280
|
+
/**
|
|
1281
|
+
* Object {@link Type}.
|
|
1282
|
+
*
|
|
1283
|
+
* This validates that:
|
|
1284
|
+
*
|
|
1285
|
+
* - The value is a plain object (checked with {@link isPlainObject}).
|
|
1286
|
+
* - The object has no extra properties beyond the specified keys unless an index
|
|
1287
|
+
* signature is provided.
|
|
1288
|
+
* - Each property's value matches the specified Type.
|
|
1289
|
+
*
|
|
1290
|
+
* When an index signature is included, the object can have additional keys that
|
|
1291
|
+
* conform to the specified key and value Types.
|
|
1292
|
+
*
|
|
1293
|
+
* The resulting `ObjectType` includes `props` for reflection, which defines the
|
|
1294
|
+
* expected structure, and optionally an `record` for flexible key/value pairs.
|
|
1295
|
+
*
|
|
1296
|
+
* https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures
|
|
1297
|
+
*
|
|
1298
|
+
* ### Examples
|
|
1299
|
+
*
|
|
1300
|
+
* #### Basic Object Validation
|
|
1301
|
+
*
|
|
1302
|
+
* ```ts
|
|
1303
|
+
* const User = object({
|
|
1304
|
+
* name: NonEmptyTrimmedString,
|
|
1305
|
+
* age: PositiveNumber,
|
|
1306
|
+
* });
|
|
1307
|
+
* type User = typeof User.Type;
|
|
1308
|
+
*
|
|
1309
|
+
* const result = User.from({ name: "John", age: 30 }); // ok({ name: "John", age: 30 })
|
|
1310
|
+
* const error = User.from({ name: "John", age: -5 }); // err
|
|
1311
|
+
* ```
|
|
1312
|
+
*
|
|
1313
|
+
* #### Optional Properties
|
|
1314
|
+
*
|
|
1315
|
+
* In this example the `age` property is marked as optional using
|
|
1316
|
+
* {@link optional}.
|
|
1317
|
+
*
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* const User = object({
|
|
1320
|
+
* name: NonEmptyString, // Required
|
|
1321
|
+
* age: optional(PositiveNumber), // Optional
|
|
1322
|
+
* });
|
|
1323
|
+
* type User = typeof User.Type;
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* #### Allowing Additional Properties
|
|
1327
|
+
*
|
|
1328
|
+
* ```ts
|
|
1329
|
+
* const UserWithAnyExtraProperties = object(
|
|
1330
|
+
* {
|
|
1331
|
+
* name: NonEmptyString,
|
|
1332
|
+
* age: PositiveNumber,
|
|
1333
|
+
* },
|
|
1334
|
+
* record(String, Unknown),
|
|
1335
|
+
* );
|
|
1336
|
+
*
|
|
1337
|
+
* expect(
|
|
1338
|
+
* UserWithAnyExtraProperties.from({ name: "a", age: 1, foo: 1 }),
|
|
1339
|
+
* ).toEqual({
|
|
1340
|
+
* ok: true,
|
|
1341
|
+
* value: { age: 1, foo: 1, name: "a" },
|
|
1342
|
+
* });
|
|
1343
|
+
* ```
|
|
1344
|
+
*
|
|
1345
|
+
* #### Combining Fixed and Flexible Properties
|
|
1346
|
+
*
|
|
1347
|
+
* ```ts
|
|
1348
|
+
* const NumberDictionary = object(
|
|
1349
|
+
* { length: Number },
|
|
1350
|
+
* record(String, Number),
|
|
1351
|
+
* );
|
|
1352
|
+
*
|
|
1353
|
+
* const validInput = {
|
|
1354
|
+
* length: 5,
|
|
1355
|
+
* extraKey1: 10,
|
|
1356
|
+
* extraKey2: 15,
|
|
1357
|
+
* };
|
|
1358
|
+
*
|
|
1359
|
+
* const fromResult = NumberDictionary.from(validInput);
|
|
1360
|
+
* expect(fromResult).toEqual(ok(validInput));
|
|
1361
|
+
*
|
|
1362
|
+
* const invalidInput = {
|
|
1363
|
+
* length: 5,
|
|
1364
|
+
* extraKey1: "not a number",
|
|
1365
|
+
* extraKey2: 15,
|
|
1366
|
+
* };
|
|
1367
|
+
*
|
|
1368
|
+
* const invalidFromResult = NumberDictionary.fromUnknown(invalidInput);
|
|
1369
|
+
* expect(invalidFromResult).toEqual(
|
|
1370
|
+
* err({
|
|
1371
|
+
* type: "Object",
|
|
1372
|
+
* value: invalidInput,
|
|
1373
|
+
* reason: {
|
|
1374
|
+
* kind: "IndexValue",
|
|
1375
|
+
* key: "extraKey1",
|
|
1376
|
+
* error: { type: "Number", value: "not a number" },
|
|
1377
|
+
* },
|
|
1378
|
+
* }),
|
|
1379
|
+
* );
|
|
1380
|
+
* ```
|
|
1381
|
+
*
|
|
1382
|
+
* @category Base Factories
|
|
1383
|
+
* @category Object
|
|
1384
|
+
*/
|
|
1385
|
+
export declare function object<Props extends Record<string, AnyType>>(props: Props): ObjectType<Props>;
|
|
1386
|
+
export declare function object<Props extends Record<string, AnyType>, KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType>(props: Props, record: RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>): ObjectWithRecordType<Props, KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>;
|
|
1387
|
+
/**
|
|
1388
|
+
* ObjectType extends {@link Type} with an additional `props` property for
|
|
1389
|
+
* reflection.
|
|
1390
|
+
*/
|
|
1391
|
+
export interface ObjectType<Props extends Record<string, AnyType>> extends Type<"Object", Readonly<ObjectT<Props>>, Readonly<ObjectInput<Props>>, ObjectError<{
|
|
1392
|
+
[K in keyof Props]: InferError<Props[K]>;
|
|
1393
|
+
}>, Readonly<ObjectParent<Props>>, ObjectError<{
|
|
1394
|
+
[K in keyof Props]: InferParentError<Props[K]>;
|
|
1395
|
+
}>> {
|
|
1396
|
+
readonly props: Props;
|
|
1397
|
+
}
|
|
1398
|
+
type ObjectT<Props extends Record<string, AnyType>> = Simplify<{
|
|
1399
|
+
[K in RequiredKeys<Props>]: InferType<Props[K]>;
|
|
1400
|
+
} & {
|
|
1401
|
+
[K in OptionalKeys<Props>]?: Props[K] extends OptionalType<infer U> ? InferType<U> : never;
|
|
1402
|
+
}>;
|
|
1403
|
+
type ObjectInput<Props extends Record<string, AnyType>> = Simplify<{
|
|
1404
|
+
[K in RequiredKeys<Props>]: InferInput<Props[K]>;
|
|
1405
|
+
} & {
|
|
1406
|
+
[K in OptionalKeys<Props>]?: Props[K] extends OptionalType<infer U> ? InferInput<U> : never;
|
|
1407
|
+
}>;
|
|
1408
|
+
type ObjectParent<Props extends Record<string, AnyType>> = Simplify<{
|
|
1409
|
+
[K in RequiredKeys<Props>]: InferParent<Props[K]>;
|
|
1410
|
+
} & {
|
|
1411
|
+
[K in OptionalKeys<Props>]?: Props[K] extends OptionalType<infer U> ? InferParent<U> : never;
|
|
1412
|
+
}>;
|
|
1413
|
+
type RequiredKeys<Props extends Record<string, AnyType>> = Exclude<keyof Props, OptionalKeys<Props>>;
|
|
1414
|
+
type OptionalKeys<Props extends Record<string, AnyType>> = {
|
|
1415
|
+
[K in keyof Props]: Props[K] extends OptionalType<any> ? K : never;
|
|
1416
|
+
}[keyof Props];
|
|
1417
|
+
export interface ObjectError<PropsErrors extends Record<string, TypeError> = Record<string, TypeError>> extends TypeErrorWithReason<"Object", {
|
|
1418
|
+
readonly kind: "NotObject";
|
|
1419
|
+
} | {
|
|
1420
|
+
readonly kind: "Props";
|
|
1421
|
+
readonly errors: Partial<PropsErrors>;
|
|
1422
|
+
} | {
|
|
1423
|
+
readonly kind: "ExtraKeys";
|
|
1424
|
+
readonly extraKeys: ReadonlyArray<string>;
|
|
1425
|
+
}> {
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* Merge Error and ParentError into one ObjectError so tooltips and error
|
|
1429
|
+
* messages are easier to read.
|
|
1430
|
+
*
|
|
1431
|
+
* @category Utilities
|
|
1432
|
+
*/
|
|
1433
|
+
export type MergeObjectTypeErrors<T extends ObjectType<any>> = T extends ObjectType<infer Props> ? ObjectError<{
|
|
1434
|
+
[K in keyof Props]: InferErrors<Props[K]>;
|
|
1435
|
+
}> : never;
|
|
1436
|
+
export declare const formatObjectError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<ObjectError<Record<string, Error>>>;
|
|
1437
|
+
/**
|
|
1438
|
+
* ObjectWithRecordType extends {@link Type} with additional `props` and `record`
|
|
1439
|
+
* properties for reflection.
|
|
1440
|
+
*/
|
|
1441
|
+
export interface ObjectWithRecordType<Props extends Record<string, AnyType>, KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType> extends Type<"ObjectWithRecord", Readonly<ObjectT<Props>> & Readonly<Record<KeyT, InferType<Value>>>, Readonly<ObjectInput<Props>> & Readonly<Record<KeyInput, InferInput<Value>>>, ObjectWithRecordError<{
|
|
1442
|
+
[K in keyof Props]: InferError<Props[K]>;
|
|
1443
|
+
}, KeyError, InferError<Value>>, Readonly<ObjectParent<Props>> & Readonly<Record<KeyParent, InferParent<Value>>>, ObjectWithRecordError<{
|
|
1444
|
+
[K in keyof Props]: InferParentError<Props[K]>;
|
|
1445
|
+
}, KeyParentError, InferParentError<Value>>> {
|
|
1446
|
+
readonly props: Props;
|
|
1447
|
+
readonly record: RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>;
|
|
1448
|
+
}
|
|
1449
|
+
export interface ObjectWithRecordError<PropsErrors extends Record<string, TypeError> = Record<string, TypeError>, KeyError extends TypeError = TypeError, ValueError extends TypeError = TypeError> extends TypeErrorWithReason<"ObjectWithRecord", {
|
|
1450
|
+
readonly kind: "NotObject";
|
|
1451
|
+
} | {
|
|
1452
|
+
readonly kind: "Props";
|
|
1453
|
+
readonly errors: Partial<PropsErrors>;
|
|
1454
|
+
} | {
|
|
1455
|
+
readonly kind: "IndexKey";
|
|
1456
|
+
readonly key: unknown;
|
|
1457
|
+
readonly error: KeyError;
|
|
1458
|
+
} | {
|
|
1459
|
+
readonly kind: "IndexValue";
|
|
1460
|
+
readonly key: string;
|
|
1461
|
+
readonly error: ValueError;
|
|
1462
|
+
}> {
|
|
1463
|
+
}
|
|
1464
|
+
export declare const formatObjectWithRecordError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<ObjectWithRecordError<Record<string, Error>, Error, Error>>;
|
|
1465
|
+
/**
|
|
1466
|
+
* Union {@link Type}.
|
|
1467
|
+
*
|
|
1468
|
+
* `UnionType` represents a union of multiple member Types. Accepts both
|
|
1469
|
+
* {@link Type} and literal values as arguments.
|
|
1470
|
+
*
|
|
1471
|
+
* Note that the `union` Type Factory delegates `fromParent` to `fromUnknown`.
|
|
1472
|
+
* That's because the union members can have different `Parent` types, and at
|
|
1473
|
+
* runtime, it is impossible to determine which member should process a given
|
|
1474
|
+
* `Parent` value.
|
|
1475
|
+
*
|
|
1476
|
+
* ### Example
|
|
1477
|
+
*
|
|
1478
|
+
* ```ts
|
|
1479
|
+
* const AorB = union("a", "b");
|
|
1480
|
+
* const result1 = AorB.from("a"); // ok("a")
|
|
1481
|
+
* const result2 = AorB.from("c"); // err
|
|
1482
|
+
*
|
|
1483
|
+
* const StringOrNumber = union(String, Number);
|
|
1484
|
+
* const result3 = StringOrNumber.from(42); // ok(42)
|
|
1485
|
+
* ```
|
|
1486
|
+
*
|
|
1487
|
+
* @category Base Factories
|
|
1488
|
+
*/
|
|
1489
|
+
export declare function union<Members extends [AnyType, AnyType, ...ReadonlyArray<AnyType>]>(...members: Members): UnionType<Members>;
|
|
1490
|
+
export declare function union<Literals extends [Literal, Literal, ...ReadonlyArray<Literal>]>(...literals: Literals): UnionType<{
|
|
1491
|
+
[K in keyof Literals]: LiteralType<Literals[K]>;
|
|
1492
|
+
}>;
|
|
1493
|
+
/**
|
|
1494
|
+
* UnionType extends {@link Type} with an additional `members` property for
|
|
1495
|
+
* reflection.
|
|
1496
|
+
*/
|
|
1497
|
+
export interface UnionType<Members extends [AnyType, ...ReadonlyArray<AnyType>]> extends Type<"Union", InferType<Members[number]>, InferInput<Members[number]>, UnionError<InferErrors<Members[number]>>, InferInput<Members[number]>, never> {
|
|
1498
|
+
readonly members: Members;
|
|
1499
|
+
}
|
|
1500
|
+
export interface UnionError<E extends TypeError = TypeError> extends TypeError<"Union"> {
|
|
1501
|
+
readonly errors: Array<E>;
|
|
1502
|
+
}
|
|
1503
|
+
export declare const formatUnionError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<UnionError<Error>>;
|
|
1504
|
+
export declare const isUnionType: (t: AnyType) => t is UnionType<[AnyType, ...ReadonlyArray<AnyType>]>;
|
|
1505
|
+
/**
|
|
1506
|
+
* Recursive {@link Type}.
|
|
1507
|
+
*
|
|
1508
|
+
* Recursive types can't be inferred, so we must define them using an interface
|
|
1509
|
+
* and `recursive` Type Factory that returns a {@link Type}.
|
|
1510
|
+
*
|
|
1511
|
+
* ### Example
|
|
1512
|
+
*
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* interface Category {
|
|
1515
|
+
* readonly name: string;
|
|
1516
|
+
* readonly subcategories: ReadonlyArray<Category>;
|
|
1517
|
+
* }
|
|
1518
|
+
*
|
|
1519
|
+
* interface CategoryInput {
|
|
1520
|
+
* readonly name: string;
|
|
1521
|
+
* readonly subcategories: ReadonlyArray<CategoryInput>;
|
|
1522
|
+
* }
|
|
1523
|
+
*
|
|
1524
|
+
* type CategoryError = ObjectError<{
|
|
1525
|
+
* readonly name: typeof String.Error;
|
|
1526
|
+
* readonly subcategories: ArrayError<CategoryError>;
|
|
1527
|
+
* }>;
|
|
1528
|
+
*
|
|
1529
|
+
* const Category = recursive(
|
|
1530
|
+
* (): Type<"Object", Category, CategoryInput, CategoryError> =>
|
|
1531
|
+
* object({
|
|
1532
|
+
* name: String,
|
|
1533
|
+
* subcategories: array(Category),
|
|
1534
|
+
* }),
|
|
1535
|
+
* );
|
|
1536
|
+
* ```
|
|
1537
|
+
*
|
|
1538
|
+
* @category Base Factories
|
|
1539
|
+
*/
|
|
1540
|
+
export declare const recursive: <ParentType extends AnyType>(create: () => ParentType) => RecursiveType<ParentType>;
|
|
1541
|
+
export interface RecursiveType<ParentType extends AnyType> extends Type<"Recursive", InferType<ParentType>, InferInput<ParentType>, InferError<ParentType>, InferParent<ParentType>, InferParentError<ParentType>> {
|
|
1542
|
+
getParentType(): ParentType;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* `union(null, T)` {@link Type}.
|
|
1546
|
+
*
|
|
1547
|
+
* ### Example
|
|
1548
|
+
*
|
|
1549
|
+
* ```ts
|
|
1550
|
+
* const NullOrString = nullOr(String);
|
|
1551
|
+
* NullOrString.from("hello"); // ok("hello")
|
|
1552
|
+
* NullOrString.from(null); // ok(null)
|
|
1553
|
+
* NullOrString.from(42); // err(...)
|
|
1554
|
+
* ```
|
|
1555
|
+
*
|
|
1556
|
+
* @category Base Factories
|
|
1557
|
+
*/
|
|
1558
|
+
export declare const nullOr: <T extends AnyType>(type: T) => UnionType<[typeof Null, T]>;
|
|
1559
|
+
/**
|
|
1560
|
+
* `union(undefined, T)` {@link Type}.
|
|
1561
|
+
*
|
|
1562
|
+
* ### Example
|
|
1563
|
+
*
|
|
1564
|
+
* ```ts
|
|
1565
|
+
* const UndefinedOrString = undefinedOr(String);
|
|
1566
|
+
* UndefinedOrString.from("world"); // ok("world")
|
|
1567
|
+
* UndefinedOrString.from(undefined); // ok()
|
|
1568
|
+
* UndefinedOrString.from(42); // err(...)
|
|
1569
|
+
* ```
|
|
1570
|
+
*
|
|
1571
|
+
* @category Base Factories
|
|
1572
|
+
*/
|
|
1573
|
+
export declare const undefinedOr: <T extends AnyType>(type: T) => UnionType<[typeof Undefined, T]>;
|
|
1574
|
+
/**
|
|
1575
|
+
* `union(undefined, null, T)` {@link Type}.
|
|
1576
|
+
*
|
|
1577
|
+
* Learn more:
|
|
1578
|
+
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing
|
|
1579
|
+
*
|
|
1580
|
+
* ### Example
|
|
1581
|
+
*
|
|
1582
|
+
* ```ts
|
|
1583
|
+
* const NullishOrString = nullishOr(String);
|
|
1584
|
+
* NullishOrString.from("test"); // ok("test")
|
|
1585
|
+
* NullishOrString.from(null); // ok(null)
|
|
1586
|
+
* NullishOrString.from(undefined); // ok()
|
|
1587
|
+
* NullishOrString.from(42); // err(...)
|
|
1588
|
+
* ```
|
|
1589
|
+
*
|
|
1590
|
+
* @category Base Factories
|
|
1591
|
+
*/
|
|
1592
|
+
export declare const nullishOr: <T extends AnyType>(type: T) => UnionType<[typeof Undefined, typeof Null, T]>;
|
|
1593
|
+
/**
|
|
1594
|
+
* Tuple {@link Type}.
|
|
1595
|
+
*
|
|
1596
|
+
* Represents a tuple of specific Types.
|
|
1597
|
+
*
|
|
1598
|
+
* ### Example
|
|
1599
|
+
*
|
|
1600
|
+
* ```ts
|
|
1601
|
+
* const NameAndAge = tuple(NonEmptyTrimmedString, PositiveNumber);
|
|
1602
|
+
*
|
|
1603
|
+
* const result = NameAndAge.from(["Alice", 25]); // ok(["Alice", 25])
|
|
1604
|
+
* const error = NameAndAge.from(["Alice", -10]); // err
|
|
1605
|
+
* ```
|
|
1606
|
+
*
|
|
1607
|
+
* @category Base Factories
|
|
1608
|
+
*/
|
|
1609
|
+
export declare const tuple: <Elements extends [AnyType, ...ReadonlyArray<AnyType>]>(...elements: Elements) => TupleType<Elements>;
|
|
1610
|
+
/**
|
|
1611
|
+
* TupleType extends {@link Type} with an additional `elements` property for
|
|
1612
|
+
* reflection.
|
|
1613
|
+
*/
|
|
1614
|
+
export interface TupleType<Elements extends readonly [AnyType, ...ReadonlyArray<AnyType>]> extends Type<"Tuple", readonly [...{
|
|
1615
|
+
[K in keyof Elements]: InferType<Elements[K]>;
|
|
1616
|
+
}], readonly [...{
|
|
1617
|
+
[K in keyof Elements]: InferInput<Elements[K]>;
|
|
1618
|
+
}], TupleError<{
|
|
1619
|
+
[K in keyof Elements]: InferError<Elements[K]>;
|
|
1620
|
+
}[number]>, readonly [...{
|
|
1621
|
+
[K in keyof Elements]: InferParent<Elements[K]>;
|
|
1622
|
+
}], TupleError<{
|
|
1623
|
+
[K in keyof Elements]: InferParentError<Elements[K]>;
|
|
1624
|
+
}[number]>> {
|
|
1625
|
+
readonly elements: Elements;
|
|
1626
|
+
}
|
|
1627
|
+
export interface TupleError<ElementError extends TypeError = TypeError> extends TypeErrorWithReason<"Tuple", {
|
|
1628
|
+
readonly kind: "InvalidLength";
|
|
1629
|
+
readonly expected: number;
|
|
1630
|
+
} | {
|
|
1631
|
+
readonly kind: "Element";
|
|
1632
|
+
readonly index: number;
|
|
1633
|
+
readonly error: ElementError;
|
|
1634
|
+
}> {
|
|
1635
|
+
}
|
|
1636
|
+
export declare const formatTupleError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<TupleError<Error>>;
|
|
1637
|
+
/**
|
|
1638
|
+
* 64-bit signed integer.
|
|
1639
|
+
*
|
|
1640
|
+
* `Int64` represents a `BigInt` constrained to a 64-bit signed integer range,
|
|
1641
|
+
* which is useful for platforms that do not support the `bigint` type, such as
|
|
1642
|
+
* SQLite.
|
|
1643
|
+
*
|
|
1644
|
+
* Because SQLite lacks a dedicated `bigint` type, it may return `number` or
|
|
1645
|
+
* 'Int64` depending on the stored value or even a wrong value if a platform
|
|
1646
|
+
* wrapper does not support it. A workaround for SQLite is to insert 'Int64`
|
|
1647
|
+
* serialized as a string (SQLite will convert it to int) and manually cast the
|
|
1648
|
+
* result to a string in SQL query and then to `Int64` in JS.
|
|
1649
|
+
*
|
|
1650
|
+
* https://www.sqlite.org/c3ref/int64.html
|
|
1651
|
+
*
|
|
1652
|
+
* @category Number
|
|
1653
|
+
*/
|
|
1654
|
+
export declare const Int64: BrandType<Type<"BigInt", bigint, bigint, BigIntError, bigint, BigIntError>, "Int64", Int64Error, BigIntError>;
|
|
1655
|
+
export type Int64 = typeof Int64.Type;
|
|
1656
|
+
export interface Int64Error extends TypeError<"Int64"> {
|
|
1657
|
+
}
|
|
1658
|
+
export declare const formatInt64Error: TypeErrorFormatter<Int64Error>;
|
|
1659
|
+
export declare const BigIntFromString: TransformType<Type<"String", string, string, StringError, string, StringError>, Type<"BigInt", bigint, bigint, BigIntError, bigint, BigIntError>, BigIntFromStringError>;
|
|
1660
|
+
export interface BigIntFromStringError extends TypeError<"BigIntFromString"> {
|
|
1661
|
+
}
|
|
1662
|
+
export declare const formatBigIntFromStringError: TypeErrorFormatter<BigIntFromStringError>;
|
|
1663
|
+
/**
|
|
1664
|
+
* Stringified {@link Int64}.
|
|
1665
|
+
*
|
|
1666
|
+
* @category Number
|
|
1667
|
+
*/
|
|
1668
|
+
export declare const Int64String: BrandType<Type<"String", string, string, StringError, string, StringError>, "Int64", Int64StringError, StringError>;
|
|
1669
|
+
export type Int64String = typeof Int64String.Type;
|
|
1670
|
+
export interface Int64StringError extends TypeError<"Int64String"> {
|
|
1671
|
+
}
|
|
1672
|
+
export declare const formatInt64StringError: TypeErrorFormatter<Int64StringError>;
|
|
1673
|
+
export type JsonValue = string | FiniteNumber | boolean | null | JsonArray | JsonObject;
|
|
1674
|
+
export type JsonValueInput = string | number | boolean | null | JsonArrayInput | JsonObjectInput;
|
|
1675
|
+
export type JsonValueError = UnionError<StringError | BooleanError | NullError | FiniteError | NumberError | ArrayError<JsonValueError> | RecordError<StringError, JsonValueError>>;
|
|
1676
|
+
export interface JsonObject {
|
|
1677
|
+
readonly [key: string]: JsonValue;
|
|
1678
|
+
}
|
|
1679
|
+
export interface JsonObjectInput {
|
|
1680
|
+
readonly [key: string]: JsonValueInput;
|
|
1681
|
+
}
|
|
1682
|
+
export type JsonArray = ReadonlyArray<JsonValue>;
|
|
1683
|
+
export type JsonArrayInput = ReadonlyArray<JsonValueInput>;
|
|
1684
|
+
/**
|
|
1685
|
+
* JSON-compatible value: string, {@link FiniteNumber}, boolean, null,
|
|
1686
|
+
* {@link JsonArray}, or {@link JsonObject}.
|
|
1687
|
+
*
|
|
1688
|
+
* @category Base Types
|
|
1689
|
+
*/
|
|
1690
|
+
export declare const JsonValue: RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>;
|
|
1691
|
+
/**
|
|
1692
|
+
* JSON-compatible array of {@link JsonValue} elements.
|
|
1693
|
+
*
|
|
1694
|
+
* @category Array
|
|
1695
|
+
*/
|
|
1696
|
+
export declare const JsonArray: ArrayType<RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>>;
|
|
1697
|
+
/**
|
|
1698
|
+
* JSON-compatible object with string keys and {@link JsonValue} values.
|
|
1699
|
+
*
|
|
1700
|
+
* @category Object
|
|
1701
|
+
*/
|
|
1702
|
+
export declare const JsonObject: RecordType<"String", string, string, StringError, string, StringError, RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>>;
|
|
1703
|
+
/**
|
|
1704
|
+
* Transform Type that parses a JSON into a {@link JsonValue} and serializes a
|
|
1705
|
+
* JsonValue back into a JSON string.
|
|
1706
|
+
*
|
|
1707
|
+
* ### Example
|
|
1708
|
+
*
|
|
1709
|
+
* ```ts
|
|
1710
|
+
* JsonValueFromString.from(`{"key":"value"}`); // -> ok({ key: "value" })
|
|
1711
|
+
* JsonValueFromString.to({ key: "value" }); // -> '{"key":"value"}'
|
|
1712
|
+
* ```
|
|
1713
|
+
*
|
|
1714
|
+
* @category String
|
|
1715
|
+
*/
|
|
1716
|
+
export declare const JsonValueFromString: TransformType<Type<"String", string, string, StringError, string, StringError>, RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>, JsonValueFromStringError>;
|
|
1717
|
+
export interface JsonValueFromStringError extends TypeError<"JsonValueFromString"> {
|
|
1718
|
+
readonly message: string;
|
|
1719
|
+
}
|
|
1720
|
+
export declare const formatJsonValueFromStringError: TypeErrorFormatter<JsonValueFromStringError>;
|
|
1721
|
+
/**
|
|
1722
|
+
* JSON-string {@link Type}.
|
|
1723
|
+
*
|
|
1724
|
+
* ### Example
|
|
1725
|
+
*
|
|
1726
|
+
* ```ts
|
|
1727
|
+
* const result = Json.from('{"key":"value"}'); // -> ok('{"key":"value"}')
|
|
1728
|
+
* const error = Json.from("invalid json"); // -> err({ type: "Json", value: "invalid json", message: "Unexpected token i in JSON at position 0" })
|
|
1729
|
+
* ```
|
|
1730
|
+
*
|
|
1731
|
+
* @category String
|
|
1732
|
+
*/
|
|
1733
|
+
export declare const Json: BrandType<Type<"String", string, string, StringError, string, StringError>, "Json", JsonError, StringError>;
|
|
1734
|
+
export type Json = typeof Json.Type;
|
|
1735
|
+
export interface JsonError extends TypeError<"Json"> {
|
|
1736
|
+
readonly message: string;
|
|
1737
|
+
}
|
|
1738
|
+
export declare const formatJsonError: TypeErrorFormatter<JsonError>;
|
|
1739
|
+
/**
|
|
1740
|
+
* Optional {@link Type}.
|
|
1741
|
+
*
|
|
1742
|
+
* Marks a `Type` as **optional**, meaning:
|
|
1743
|
+
*
|
|
1744
|
+
* - If the value is **present**, it must match the given `Type`.
|
|
1745
|
+
* - If the value is **absent**, it is **not included** in the final object.
|
|
1746
|
+
*
|
|
1747
|
+
* This is different from {@link undefinedOr}, which allows explicit `undefined`
|
|
1748
|
+
* but **still requires the key to exist**.
|
|
1749
|
+
*
|
|
1750
|
+
* ### Example:
|
|
1751
|
+
*
|
|
1752
|
+
* ```ts
|
|
1753
|
+
* const Todo = object({
|
|
1754
|
+
* id: TodoId,
|
|
1755
|
+
* title: NonEmptyString1000,
|
|
1756
|
+
* isCompleted: optional(SqliteBoolean),
|
|
1757
|
+
* });
|
|
1758
|
+
* ```
|
|
1759
|
+
*/
|
|
1760
|
+
export declare const optional: <T extends AnyType>(type: T) => OptionalType<T>;
|
|
1761
|
+
export interface OptionalType<T extends AnyType> extends Type<"Optional", InferType<T>, InferInput<T>, InferError<T>, InferParent<T>, InferParentError<T>> {
|
|
1762
|
+
readonly parent: T;
|
|
1763
|
+
}
|
|
1764
|
+
/** Determines if a given type is an {@link OptionalType}. */
|
|
1765
|
+
export declare const isOptionalType: (x: unknown) => x is OptionalType<any>;
|
|
1766
|
+
/**
|
|
1767
|
+
* Creates a partial object type where all properties are optional.
|
|
1768
|
+
*
|
|
1769
|
+
* This is useful when you want to validate an object in which none of the keys
|
|
1770
|
+
* are required, but if they are present they must conform to their
|
|
1771
|
+
* corresponding Types.
|
|
1772
|
+
*
|
|
1773
|
+
* ### Example
|
|
1774
|
+
*
|
|
1775
|
+
* ```ts
|
|
1776
|
+
* const PartialUser = partial({
|
|
1777
|
+
* name: NonEmptyString,
|
|
1778
|
+
* age: PositiveNumber,
|
|
1779
|
+
* });
|
|
1780
|
+
*
|
|
1781
|
+
* // Valid: an empty object is accepted
|
|
1782
|
+
* PartialUser.from({});
|
|
1783
|
+
*
|
|
1784
|
+
* // Valid: when provided, the properties must validate correctly
|
|
1785
|
+
* PartialUser.from({ name: "Alice" });
|
|
1786
|
+
*
|
|
1787
|
+
* // Invalid: if a property is present but fails validation it returns an error
|
|
1788
|
+
* PartialUser.from({ age: -5 });
|
|
1789
|
+
* ```
|
|
1790
|
+
*
|
|
1791
|
+
* @category Object
|
|
1792
|
+
*/
|
|
1793
|
+
export declare const partial: <Props extends Record<string, AnyType>>(props: Props) => ObjectType<{ [K in keyof Props]: OptionalType<Props[K]>; }>;
|
|
1794
|
+
/**
|
|
1795
|
+
* Converts each “nullable” property (a union that includes {@link Null}) into an
|
|
1796
|
+
* {@link optional} property. This means consumers can omit the property
|
|
1797
|
+
* entirely, or set it to `null`, or set it to the non-null member of the
|
|
1798
|
+
* union.
|
|
1799
|
+
*
|
|
1800
|
+
* @category Object
|
|
1801
|
+
*/
|
|
1802
|
+
export declare const nullableToOptional: <Props extends Record<string, AnyType>>(props: Props) => ObjectType<NullableToOptionalProps<Props>>;
|
|
1803
|
+
export type NullableToOptionalProps<Props extends Record<string, AnyType>> = {
|
|
1804
|
+
[K in keyof Props]: TransformNullable<Props[K]>;
|
|
1805
|
+
};
|
|
1806
|
+
export type TransformNullable<P extends AnyType> = IsUnionWithNull<P> extends true ? OptionalType<P> : P;
|
|
1807
|
+
export type IsUnionWithNull<U extends AnyType> = U extends UnionType<infer Members> ? Members extends [AnyType, ...Array<AnyType>] ? NullTypeInMembers<Members> : false : false;
|
|
1808
|
+
export type NullTypeInMembers<Members extends [AnyType, ...Array<AnyType>]> = Members extends [infer Head, ...infer Tail] ? Head extends typeof Null ? true : Tail extends [AnyType, ...Array<AnyType>] ? NullTypeInMembers<Tail> : false : false;
|
|
1809
|
+
/**
|
|
1810
|
+
* Create a new `object` {@link Type} by omitting some keys.
|
|
1811
|
+
*
|
|
1812
|
+
* @category Object
|
|
1813
|
+
*/
|
|
1814
|
+
export declare function omit<T extends ObjectType<any>, Keys extends keyof T["props"]>(objectType: T, ...keys: ReadonlyArray<Keys>): ObjectType<Omit<T["props"], Keys>>;
|
|
1815
|
+
/**
|
|
1816
|
+
* Creates a transform Type that serializes a given `Type` into a branded JSON
|
|
1817
|
+
* string. The transformation is reversible, ensuring that we can safely parse
|
|
1818
|
+
* it back.
|
|
1819
|
+
*
|
|
1820
|
+
* ### Example
|
|
1821
|
+
*
|
|
1822
|
+
* ```ts
|
|
1823
|
+
* const Person = object({
|
|
1824
|
+
* name: NonEmptyString50,
|
|
1825
|
+
* age: FiniteNumber,
|
|
1826
|
+
* });
|
|
1827
|
+
* type Person = typeof Person.Type;
|
|
1828
|
+
*
|
|
1829
|
+
* const PersonJson = json(Person, "PersonJson");
|
|
1830
|
+
* // string & Brand<"PersonJson">
|
|
1831
|
+
* type PersonJson = typeof PersonJson.Type;
|
|
1832
|
+
*
|
|
1833
|
+
* // Person -> string & Brand<"PersonJson">
|
|
1834
|
+
* const personJson = PersonJson.from({ name: "Alice", age: 30 });
|
|
1835
|
+
* expect(personJson).toEqual(ok('{"name":"Alice","age":30}'));
|
|
1836
|
+
*
|
|
1837
|
+
* // string & Brand<"PersonJson"> -> Person
|
|
1838
|
+
* const person = PersonJson.to(personJson);
|
|
1839
|
+
*
|
|
1840
|
+
* // serialize/parse any JSON value
|
|
1841
|
+
* const AnyJson = json(JsonValue, "AnyJson");
|
|
1842
|
+
* ```
|
|
1843
|
+
*/
|
|
1844
|
+
export declare const json: <T extends AnyType, Name extends TypeName>(type: T, name: Name) => TransformType<T, BrandType<typeof String, Name, JsonValueFromStringError | T["Errors"], StringError>>;
|
|
1845
|
+
/**
|
|
1846
|
+
* Union of all `TypeError`s defined in the `Type.ts` file, including base type
|
|
1847
|
+
* errors (e.g., `StringError`, `NumberError`), composite type errors
|
|
1848
|
+
* (`ArrayError`, `ObjectError`), and optionally, user-defined extra errors.
|
|
1849
|
+
*
|
|
1850
|
+
* This type is **recursive**, meaning errors can be nested within composite
|
|
1851
|
+
* structures like arrays, objects, records, unions, and tuples.
|
|
1852
|
+
*
|
|
1853
|
+
* Used by {@link createFormatTypeError} to generate human-readable error
|
|
1854
|
+
* messages.
|
|
1855
|
+
*
|
|
1856
|
+
* @category Utilities
|
|
1857
|
+
*/
|
|
1858
|
+
export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | NumberError | BigIntError | BooleanError | UndefinedError | NullError | FunctionError | Uint8ArrayError | InstanceOfError | EvoluTypeError | CurrencyCodeError | DateIsoStringError | TrimmedError | MinLengthError | MaxLengthError | LengthError | MnemonicError | RegexError | NanoIdError | SimplePasswordError | IdError | PositiveError | NegativeError | NonPositiveError | NonNegativeError | IntError | GreaterThanError | LessThanError | GreaterThanOrEqualToError | LessThanOrEqualToError | NonNaNError | FiniteError | MultipleOfError | BetweenError | LiteralError | Int64Error | BigIntFromStringError | Int64StringError | JsonValueFromStringError | JsonError | ExtraErrors | ArrayError<TypeErrors<ExtraErrors>> | RecordError<TypeErrors<ExtraErrors>, TypeErrors<ExtraErrors>> | ObjectError<Record<string, TypeErrors<ExtraErrors>>> | ObjectWithRecordError<Record<string, TypeErrors<ExtraErrors>>, TypeErrors<ExtraErrors>, TypeErrors<ExtraErrors>> | UnionError<TypeErrors<ExtraErrors>> | TupleError<TypeErrors<ExtraErrors>>;
|
|
1859
|
+
/**
|
|
1860
|
+
* Creates a unified error formatter that handles both Evolu Type's built-in
|
|
1861
|
+
* {@link TypeErrors} and custom errors. It also lets us override the default
|
|
1862
|
+
* formatting for specific errors.
|
|
1863
|
+
*
|
|
1864
|
+
* If you prefer not to reuse any built-in error formatters, you can write your
|
|
1865
|
+
* own `formatTypeError` function from scratch.
|
|
1866
|
+
*
|
|
1867
|
+
* ### Examples
|
|
1868
|
+
*
|
|
1869
|
+
* ```ts
|
|
1870
|
+
* const formatError = createFormatTypeError();
|
|
1871
|
+
* console.log(formatError({ type: "String", value: 42 }));
|
|
1872
|
+
* // "A value 42 is not a string."
|
|
1873
|
+
* ```
|
|
1874
|
+
*
|
|
1875
|
+
* A custom `formatTypeError` function:
|
|
1876
|
+
*
|
|
1877
|
+
* ```ts
|
|
1878
|
+
* type AppErrors =
|
|
1879
|
+
* | ValidMutationSizeError
|
|
1880
|
+
* | StringError
|
|
1881
|
+
* | MinLengthError
|
|
1882
|
+
* | MaxLengthError
|
|
1883
|
+
* | NullError
|
|
1884
|
+
* | IdError
|
|
1885
|
+
* | TrimmedError
|
|
1886
|
+
* | MnemonicError
|
|
1887
|
+
* | LiteralError
|
|
1888
|
+
* // Composite errors
|
|
1889
|
+
* | ObjectError<Record<string, AppErrors>>
|
|
1890
|
+
* | UnionError<AppErrors>;
|
|
1891
|
+
*
|
|
1892
|
+
* const formatTypeError: TypeErrorFormatter<AppErrors> = (error) => {
|
|
1893
|
+
* // In the real code, we would use the createTypeErrorFormatter helper
|
|
1894
|
+
* // that safely stringifies error value.
|
|
1895
|
+
* switch (error.type) {
|
|
1896
|
+
* case "Id":
|
|
1897
|
+
* return `Invalid Id on table: ${error.table}.`;
|
|
1898
|
+
* case "MaxLength":
|
|
1899
|
+
* return `Max length is ${error.max}.`;
|
|
1900
|
+
* case "MinLength":
|
|
1901
|
+
* return `Min length is ${error.min}.`;
|
|
1902
|
+
* case "Mnemonic":
|
|
1903
|
+
* return `Invalid mnemonic: ${String(error.value)}`;
|
|
1904
|
+
* case "Null":
|
|
1905
|
+
* return `Not null`;
|
|
1906
|
+
* case "String":
|
|
1907
|
+
* // We can reuse existing formatter.
|
|
1908
|
+
* return formatStringError(error);
|
|
1909
|
+
* case "Trimmed":
|
|
1910
|
+
* return "Value is not trimmed.";
|
|
1911
|
+
* case "ValidMutationSize":
|
|
1912
|
+
* return "A developer made an error, this should not happen.";
|
|
1913
|
+
* case "Literal":
|
|
1914
|
+
* return formatLiteralError(error);
|
|
1915
|
+
* // Composite Types
|
|
1916
|
+
* case "Union":
|
|
1917
|
+
* return `Union errors: ${error.errors.map(formatTypeError).join(", ")}`;
|
|
1918
|
+
* case "Object": {
|
|
1919
|
+
* if (
|
|
1920
|
+
* error.reason.kind === "ExtraKeys" ||
|
|
1921
|
+
* error.reason.kind === "NotObject"
|
|
1922
|
+
* )
|
|
1923
|
+
* return "A developer made an error, this should not happen.";
|
|
1924
|
+
* const firstError = Object.values(error.reason.errors).find(
|
|
1925
|
+
* (e) => e !== undefined,
|
|
1926
|
+
* )!;
|
|
1927
|
+
* return formatTypeError(firstError);
|
|
1928
|
+
* }
|
|
1929
|
+
* }
|
|
1930
|
+
* };
|
|
1931
|
+
* ```
|
|
1932
|
+
*
|
|
1933
|
+
* @category Utilities
|
|
1934
|
+
*/
|
|
1935
|
+
export declare const createFormatTypeError: <ExtraErrors extends TypeError = never>(extraFormatter?: TypeErrorFormatter<ExtraErrors>) => TypeErrorFormatter<TypeErrors<ExtraErrors>>;
|
|
1936
|
+
export {};
|
|
1937
|
+
//# sourceMappingURL=Type.d.ts.map
|