@evolu/common 6.0.1-preview.3 → 6.0.1-preview.31
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/dist/src/Array.d.ts +69 -5
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +64 -5
- package/dist/src/Assert.d.ts +6 -16
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +6 -18
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +8 -7
- package/dist/src/Cache.d.ts +44 -0
- package/dist/src/Cache.d.ts.map +1 -0
- package/dist/src/Cache.js +52 -0
- package/dist/src/Callbacks.d.ts +45 -12
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +14 -7
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +61 -34
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +32 -45
- package/dist/src/Evolu/Db.d.ts +161 -65
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +286 -694
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +208 -133
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +188 -183
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +150 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +174 -0
- package/dist/src/Evolu/Owner.d.ts +273 -120
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +130 -104
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +277 -232
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +603 -378
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.js +3 -3
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.d.ts +92 -7
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +238 -76
- package/dist/src/Evolu/Schema.d.ts +129 -73
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +169 -89
- package/dist/src/Evolu/Storage.d.ts +240 -26
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +189 -91
- package/dist/src/Evolu/Sync.d.ts +67 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +441 -20
- package/dist/src/Evolu/Timestamp.d.ts +85 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +77 -18
- package/dist/src/Identicon.d.ts +35 -0
- package/dist/src/Identicon.d.ts.map +1 -0
- package/dist/src/Identicon.js +143 -0
- package/dist/src/Instances.d.ts +34 -0
- package/dist/src/Instances.d.ts.map +1 -0
- package/dist/src/Instances.js +44 -0
- package/dist/src/ManyToManyMap.d.ts +71 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Number.d.ts +4 -3
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +5 -4
- package/dist/src/Platform.d.ts +20 -0
- package/dist/src/Platform.d.ts.map +1 -0
- package/dist/src/Platform.js +22 -0
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Resources.d.ts +118 -0
- package/dist/src/Resources.d.ts.map +1 -0
- package/dist/src/Resources.js +197 -0
- package/dist/src/Result.d.ts +184 -52
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Skiplist.js +2 -1
- package/dist/src/Sqlite.d.ts +63 -5
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +110 -9
- package/dist/src/Task.d.ts +586 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +469 -0
- package/dist/src/Time.d.ts +66 -1
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +99 -5
- package/dist/src/Type.d.ts +622 -340
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +666 -464
- package/dist/src/Types.d.ts +1 -75
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -18
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +7 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -2
- package/package.json +14 -13
- package/src/Array.ts +90 -11
- package/src/Assert.ts +6 -24
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +7 -7
- package/src/Cache.ts +85 -0
- package/src/Callbacks.ts +62 -22
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +97 -82
- package/src/Evolu/Db.ts +517 -1020
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +464 -355
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +463 -0
- package/src/Evolu/Owner.ts +355 -228
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +859 -676
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Query.ts +2 -1
- package/src/Evolu/Relay.ts +437 -93
- package/src/Evolu/Schema.ts +391 -191
- package/src/Evolu/Storage.ts +532 -135
- package/src/Evolu/Sync.ts +766 -37
- package/src/Evolu/Timestamp.ts +88 -35
- package/src/Identicon.ts +197 -0
- package/src/Instances.ts +90 -0
- package/src/ManyToManyMap.ts +124 -24
- package/src/Number.ts +6 -10
- package/src/Platform.ts +26 -0
- package/src/Random.ts +3 -2
- package/src/Resources.ts +367 -0
- package/src/Result.ts +191 -54
- package/src/Skiplist.ts +1 -1
- package/src/Sqlite.ts +122 -17
- package/src/Task.ts +901 -0
- package/src/Time.ts +180 -5
- package/src/Type.ts +1084 -727
- package/src/Types.ts +1 -77
- package/src/WebSocket.ts +27 -25
- package/src/Worker.ts +72 -23
- package/src/index.ts +7 -2
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
package/dist/src/Type.d.ts
CHANGED
|
@@ -1,77 +1,182 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 🧩
|
|
2
|
+
* 🧩 Type-safe runtime types
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Evolu {@link Type} is like a type guard that returns typed errors (via
|
|
5
|
+
* {@link Result}) instead of throwing. We either get a safely typed value or a
|
|
6
|
+
* precise, composable error value telling us exactly why validation failed.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
8
|
+
* Evolu Type supports [Standard Schema](https://standardschema.dev/) for
|
|
9
|
+
* interoperability with 40+ validation-compatible tools and frameworks.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
* library fully met our needs:
|
|
11
|
+
* Why another validation library?
|
|
10
12
|
*
|
|
11
|
-
* - **Result-based error handling
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* - **
|
|
15
|
-
*
|
|
16
|
-
* - **No user-land chaining
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
13
|
+
* - **Result-based error handling** – no exceptions for normal control flow.
|
|
14
|
+
* - **Typed errors with decoupled formatters** – validation logic ≠ user
|
|
15
|
+
* messages.
|
|
16
|
+
* - **Consistent constraints via {@link Brand}** – every constraint becomes part
|
|
17
|
+
* of the type.
|
|
18
|
+
* - **No user-land chaining DSL** – designed with the upcoming ES pipe operator
|
|
19
|
+
* in mind.
|
|
20
|
+
* - **Selective validation** – parent validations are skipped when already proved
|
|
21
|
+
* by typing.
|
|
22
|
+
* - **Simple, top-down implementation** – readable source code from top to bottom
|
|
23
|
+
* with no hidden magic; just plain functions and composition.
|
|
22
24
|
*
|
|
23
|
-
*
|
|
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.
|
|
25
|
+
* ### Base Types Quick Start
|
|
27
26
|
*
|
|
28
|
-
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* // Validate unknown values
|
|
29
|
+
* const value: unknown = "hello";
|
|
30
|
+
* const stringResult = String.fromUnknown(value);
|
|
31
|
+
* if (!stringResult.ok) {
|
|
32
|
+
* // console.error(formatStringError(stringResult.error));
|
|
33
|
+
* return stringResult; // inside a function returning Result<string, _>
|
|
34
|
+
* }
|
|
35
|
+
* // Safe branch: value is now string
|
|
36
|
+
* const upper = stringResult.value.toUpperCase();
|
|
37
|
+
*
|
|
38
|
+
* // Type guard style
|
|
39
|
+
* if (String.is(value)) {
|
|
40
|
+
* // narrowed to string
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* // Composing: arrays & objects
|
|
44
|
+
* const Numbers = array(Number); // ReadonlyArray<number>
|
|
45
|
+
* const Point = object({ x: Number, y: Number });
|
|
46
|
+
*
|
|
47
|
+
* Numbers.from([1, 2, 3]); // ok
|
|
48
|
+
* Point.from({ x: 1, y: 2 }); // ok
|
|
49
|
+
* Point.from({ x: 1, y: "2" }); // err -> nested Number error
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* ### Branding Basics
|
|
53
|
+
*
|
|
54
|
+
* Branding adds semantic meaning & constraints while preserving the runtime
|
|
55
|
+
* shape:
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* const CurrencyCode = brand("CurrencyCode", String, (value) =>
|
|
59
|
+
* /^[A-Z]{3}$/.test(value)
|
|
60
|
+
* ? ok(value)
|
|
61
|
+
* : err<CurrencyCodeError>({ type: "CurrencyCode", value }),
|
|
62
|
+
* );
|
|
63
|
+
* type CurrencyCode = typeof CurrencyCode.Type; // string & Brand<"CurrencyCode">
|
|
64
|
+
*
|
|
65
|
+
* interface CurrencyCodeError extends TypeError<"CurrencyCode"> {}
|
|
66
|
+
*
|
|
67
|
+
* const formatCurrencyCodeError =
|
|
68
|
+
* createTypeErrorFormatter<CurrencyCodeError>(
|
|
69
|
+
* (error) => `Invalid currency code: ${error.value}`,
|
|
70
|
+
* );
|
|
29
71
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* cannot fail.
|
|
72
|
+
* const r = CurrencyCode.from("USD"); // ok("USD")
|
|
73
|
+
* const e = CurrencyCode.from("usd"); // err(...)
|
|
74
|
+
* ```
|
|
34
75
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* transformations. For example, `NonEmptyTrimmedString100` chain looks like
|
|
38
|
-
* this:
|
|
76
|
+
* See also reusable brand factories like `minLength`, `maxLength`, `trimmed`,
|
|
77
|
+
* `positive`, `between`, etc.
|
|
39
78
|
*
|
|
40
|
-
*
|
|
79
|
+
* ### Objects & Optional Fields
|
|
41
80
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* const User = object({
|
|
83
|
+
* name: NonEmptyTrimmedString100,
|
|
84
|
+
* age: optional(PositiveInt),
|
|
85
|
+
* });
|
|
86
|
+
* type User = typeof User.Type;
|
|
44
87
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
88
|
+
* User.from({ name: "Alice" }); // ok
|
|
89
|
+
* User.from({ name: "Alice", age: -1 }); // err(PositiveInt)
|
|
90
|
+
* ```
|
|
48
91
|
*
|
|
49
|
-
*
|
|
50
|
-
* parent Types when we can rely on TypeScript types.
|
|
92
|
+
* ### Deriving JSON String Types
|
|
51
93
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const Person = object({
|
|
96
|
+
* name: NonEmptyString50,
|
|
97
|
+
* // Did you know that JSON.stringify converts NaN (a number) into null?
|
|
98
|
+
* // To prevent this, use FiniteNumber.
|
|
99
|
+
* age: FiniteNumber,
|
|
100
|
+
* });
|
|
101
|
+
* type Person = typeof Person.Type;
|
|
102
|
+
*
|
|
103
|
+
* const [PersonJson, personToPersonJson, personJsonToPerson] = json(
|
|
104
|
+
* Person,
|
|
105
|
+
* "PersonJson",
|
|
106
|
+
* );
|
|
107
|
+
* // string & Brand<"PersonJson">
|
|
108
|
+
* type PersonJson = typeof PersonJson.Type;
|
|
109
|
+
*
|
|
110
|
+
* const person = Person.orThrow({
|
|
111
|
+
* name: "Alice",
|
|
112
|
+
* age: 30,
|
|
113
|
+
* });
|
|
114
|
+
*
|
|
115
|
+
* const personJson = personToPersonJson(person);
|
|
116
|
+
* expect(personJsonToPerson(personJson)).toEqual(person);
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* ### Error Formatting
|
|
120
|
+
*
|
|
121
|
+
* Evolu separates validation logic from human-readable messages. There are two
|
|
122
|
+
* layers:
|
|
123
|
+
*
|
|
124
|
+
* 1. Per-type formatters (e.g. `formatStringError`) – simple, focused, already
|
|
125
|
+
* used earlier in the quick start example.
|
|
126
|
+
* 2. A unified formatter via `createFormatTypeError` – composes all built-in and
|
|
127
|
+
* custom errors (including nested composite types) and lets us override
|
|
128
|
+
* selected messages.
|
|
129
|
+
*
|
|
130
|
+
* #### 1. Per-Type Formatter (recap)
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* const r = String.fromUnknown(42);
|
|
134
|
+
* if (!r.ok) console.error(formatStringError(r.error));
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* #### 2. Unified Formatter with Overrides
|
|
138
|
+
*
|
|
139
|
+
* ```ts
|
|
140
|
+
* // Override only what we care about; fall back to built-ins for the rest.
|
|
141
|
+
* const formatTypeError = createFormatTypeError((error) => {
|
|
142
|
+
* if (error.type === "MinLength") return `Min length is ${error.min}`;
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
* const User = object({ name: NonEmptyTrimmedString100 });
|
|
146
|
+
* const resultUser = User.from({ name: "" });
|
|
147
|
+
* if (!resultUser.ok) console.error(formatTypeError(resultUser.error));
|
|
148
|
+
*
|
|
149
|
+
* const badPoint = object({ x: Number, y: Number }).from({
|
|
150
|
+
* x: 1,
|
|
151
|
+
* y: "foo",
|
|
152
|
+
* });
|
|
153
|
+
* if (!badPoint.ok) console.error(formatTypeError(badPoint.error));
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* The unified formatter walks nested structures (object / array / record /
|
|
157
|
+
* tuple / union) and applies overrides only where specified, greatly reducing
|
|
158
|
+
* boilerplate when formatting complex validation errors.
|
|
57
159
|
*
|
|
58
160
|
* ### Tip
|
|
59
161
|
*
|
|
60
162
|
* If necessary, write `globalThis.String` instead of `String` to avoid naming
|
|
61
|
-
* clashes with
|
|
163
|
+
* clashes with native types.
|
|
62
164
|
*
|
|
63
|
-
* ### Design Decision:
|
|
165
|
+
* ### Design Decision: No Bidirectional Transformations
|
|
64
166
|
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
167
|
+
* Evolu Type intentionally does not support bidirectional transformations. It
|
|
168
|
+
* previously did, but supporting that while keeping typed error fidelity added
|
|
169
|
+
* complexity that hurt readability & reliability. Most persistence pipelines
|
|
170
|
+
* (e.g. SQLite) already require explicit mapping of query results, so implicit
|
|
171
|
+
* reverse transforms would not buy much. We may revisit this if we can design a
|
|
172
|
+
* minimal, 100% safe API that preserves simplicity.
|
|
69
173
|
*
|
|
70
174
|
* @module
|
|
71
175
|
*/
|
|
72
|
-
import {
|
|
73
|
-
import {
|
|
74
|
-
import
|
|
176
|
+
import type { Brand } from "./Brand.js";
|
|
177
|
+
import { type RandomBytesDep } from "./Crypto.js";
|
|
178
|
+
import { Result } from "./Result.js";
|
|
179
|
+
import type { Literal, Simplify, WidenLiteral } from "./Types.js";
|
|
75
180
|
export interface Type<Name extends TypeName,
|
|
76
181
|
/** The type this Type resolves to. */
|
|
77
182
|
T,
|
|
@@ -82,7 +187,7 @@ Error extends TypeError = never,
|
|
|
82
187
|
/** The parent type. */
|
|
83
188
|
Parent = T,
|
|
84
189
|
/** The parent's error. */
|
|
85
|
-
ParentError extends TypeError = Error> {
|
|
190
|
+
ParentError extends TypeError = Error> extends StandardSchemaV1<Input, T> {
|
|
86
191
|
readonly name: Name;
|
|
87
192
|
/**
|
|
88
193
|
* Creates `T` from an `Input` value.
|
|
@@ -93,38 +198,95 @@ ParentError extends TypeError = Error> {
|
|
|
93
198
|
*/
|
|
94
199
|
readonly from: (value: Input) => Result<T, ParentError | Error>;
|
|
95
200
|
/**
|
|
96
|
-
* Creates `T` from an
|
|
201
|
+
* Creates `T` from an `Input` value, throwing an error if validation fails.
|
|
97
202
|
*
|
|
98
|
-
*
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
203
|
+
* Throws an Error with the Type validation error in its `cause` property,
|
|
204
|
+
* making it debuggable while avoiding the need for custom error messages.
|
|
205
|
+
*
|
|
206
|
+
* This is a convenience method that combines `from` with `getOrThrow`.
|
|
207
|
+
*
|
|
208
|
+
* **When to use:**
|
|
209
|
+
*
|
|
210
|
+
* - Configuration values that are guaranteed to be valid (e.g., hardcoded
|
|
211
|
+
* constants)
|
|
212
|
+
* - Application startup where failure should crash the program
|
|
213
|
+
* - As an alternative to assertions when the Type error in the thrown Error's
|
|
214
|
+
* `cause` provides sufficient debugging information
|
|
215
|
+
* - Test code with known valid inputs (when error message clarity is not
|
|
216
|
+
* critical; for better test error messages, use Vitest `schemaMatching` +
|
|
217
|
+
* `assert` with `.is()`)
|
|
218
|
+
*
|
|
219
|
+
* ### Example
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
* // ✅ Good: Known valid constant
|
|
223
|
+
* const maxRetries = PositiveInt.orThrow(3);
|
|
224
|
+
*
|
|
225
|
+
* // ✅ Good: App configuration that should crash on invalid values
|
|
226
|
+
* const appName = SimpleName.orThrow("MyApp");
|
|
227
|
+
*
|
|
228
|
+
* // ✅ Good: Instead of assert when Type error is clear enough
|
|
229
|
+
* // Context makes it obvious: count increments from non-negative value
|
|
230
|
+
* const currentCount = counts.get(id) ?? 0;
|
|
231
|
+
* const newCount = PositiveInt.orThrow(currentCount + 1);
|
|
103
232
|
*
|
|
104
|
-
*
|
|
233
|
+
* // ✅ Good: Test setup with known valid values
|
|
234
|
+
* const testUser = User.orThrow({ name: "Alice", age: 30 });
|
|
105
235
|
*
|
|
106
|
-
*
|
|
236
|
+
* // ❌ Avoid: User input (use `from` instead)
|
|
237
|
+
* const userAge = PositiveInt.orThrow(userInput); // Could crash!
|
|
238
|
+
*
|
|
239
|
+
* // ✅ Better: Handle user input gracefully
|
|
240
|
+
* const ageResult = PositiveInt.from(userInput);
|
|
241
|
+
* if (!ageResult.ok) {
|
|
242
|
+
* // Handle validation error
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
107
245
|
*/
|
|
108
|
-
readonly
|
|
246
|
+
readonly orThrow: (value: Input) => T;
|
|
109
247
|
/**
|
|
110
|
-
* Creates `T` from `
|
|
248
|
+
* Creates `T` from an `Input` value, returning `null` if validation fails.
|
|
249
|
+
*
|
|
250
|
+
* This is a convenience method that combines `from` with `getOrNull`.
|
|
111
251
|
*
|
|
112
|
-
*
|
|
113
|
-
* already partially validated/transformed value.
|
|
252
|
+
* **When to use:**
|
|
114
253
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
254
|
+
* - When you need to convert a validation result to a nullable value
|
|
255
|
+
* - When the error is not important and you just want the value or nothing
|
|
117
256
|
*
|
|
118
257
|
* ### Example
|
|
119
258
|
*
|
|
120
259
|
* ```ts
|
|
121
|
-
* //
|
|
122
|
-
* const
|
|
260
|
+
* // ✅ Good: Optional user input
|
|
261
|
+
* const age = PositiveInt.orNull(userInput);
|
|
262
|
+
* if (age != null) {
|
|
263
|
+
* console.log("Valid age:", age);
|
|
264
|
+
* }
|
|
265
|
+
*
|
|
266
|
+
* // ✅ Good: Default fallback
|
|
267
|
+
* const maxRetries = PositiveInt.orNull(config.retries) ?? 3;
|
|
268
|
+
*
|
|
269
|
+
* // ❌ Avoid: When you need to know why validation failed (use `from` instead)
|
|
270
|
+
* const result = PositiveInt.from(userInput);
|
|
271
|
+
* if (!result.ok) {
|
|
272
|
+
* console.error(formatPositiveError(result.error));
|
|
273
|
+
* }
|
|
123
274
|
* ```
|
|
124
275
|
*/
|
|
276
|
+
readonly orNull: (value: Input) => T | null;
|
|
277
|
+
/**
|
|
278
|
+
* Creates `T` from an unknown value.
|
|
279
|
+
*
|
|
280
|
+
* This is useful when a value is unknown.
|
|
281
|
+
*/
|
|
282
|
+
readonly fromUnknown: (value: unknown) => Result<T, ParentError | Error>;
|
|
283
|
+
/**
|
|
284
|
+
* Creates `T` from `Parent` type.
|
|
285
|
+
*
|
|
286
|
+
* This function skips parent Types validations when we have already partially
|
|
287
|
+
* validated value.
|
|
288
|
+
*/
|
|
125
289
|
readonly fromParent: (value: Parent) => Result<T, Error>;
|
|
126
|
-
/** The opposite of `fromParent`. */
|
|
127
|
-
readonly toParent: (value: T) => Parent;
|
|
128
290
|
/**
|
|
129
291
|
* A **type guard** that checks whether an unknown value satisfies the
|
|
130
292
|
* {@link Type}.
|
|
@@ -197,8 +359,6 @@ ParentError extends TypeError = Error> {
|
|
|
197
359
|
*/
|
|
198
360
|
readonly ParentError: ParentError;
|
|
199
361
|
/**
|
|
200
|
-
* Error | ParentError
|
|
201
|
-
*
|
|
202
362
|
* ### Example
|
|
203
363
|
*
|
|
204
364
|
* ```ts
|
|
@@ -230,12 +390,47 @@ export interface TypeErrorWithReason<Name extends TypeName = TypeName, Reason ex
|
|
|
230
390
|
readonly reason: Reason;
|
|
231
391
|
}
|
|
232
392
|
export type AnyType = Type<any, any, any, any, any, any>;
|
|
393
|
+
/**
|
|
394
|
+
* Extracts the name from a {@link Type}.
|
|
395
|
+
*
|
|
396
|
+
* @category Utilities
|
|
397
|
+
*/
|
|
233
398
|
export type InferName<A extends AnyType> = A extends Type<infer Name, any, any, any, any, any> ? Name : never;
|
|
399
|
+
/**
|
|
400
|
+
* Extracts the type from a {@link Type}.
|
|
401
|
+
*
|
|
402
|
+
* @category Utilities
|
|
403
|
+
*/
|
|
234
404
|
export type InferType<A extends AnyType> = A extends Type<any, infer T, any, any, any, any> ? T : never;
|
|
405
|
+
/**
|
|
406
|
+
* Extracts the input type from a {@link Type}.
|
|
407
|
+
*
|
|
408
|
+
* @category Utilities
|
|
409
|
+
*/
|
|
235
410
|
export type InferInput<A extends AnyType> = A extends Type<any, any, infer Input, any, any, any> ? Input : never;
|
|
411
|
+
/**
|
|
412
|
+
* Extracts the specific error type from a {@link Type}.
|
|
413
|
+
*
|
|
414
|
+
* @category Utilities
|
|
415
|
+
*/
|
|
236
416
|
export type InferError<A extends AnyType> = A extends Type<any, any, any, infer Error, any, any> ? Error : never;
|
|
417
|
+
/**
|
|
418
|
+
* Extracts the parent type from a {@link Type}.
|
|
419
|
+
*
|
|
420
|
+
* @category Utilities
|
|
421
|
+
*/
|
|
237
422
|
export type InferParent<A extends AnyType> = A extends Type<any, any, any, any, infer Parent, any> ? Parent : never;
|
|
423
|
+
/**
|
|
424
|
+
* Extracts the parent error type from a {@link Type}.
|
|
425
|
+
*
|
|
426
|
+
* @category Utilities
|
|
427
|
+
*/
|
|
238
428
|
export type InferParentError<A extends AnyType> = A extends Type<any, any, any, any, any, infer ParentError> ? ParentError : never;
|
|
429
|
+
/**
|
|
430
|
+
* Extracts all error types from a {@link Type}.
|
|
431
|
+
*
|
|
432
|
+
* @category Utilities
|
|
433
|
+
*/
|
|
239
434
|
export type InferErrors<T extends AnyType> = T extends Type<any, any, any, infer Error, any, infer ParentError> ? Error | ParentError : never;
|
|
240
435
|
declare const EvoluTypeSymbol: unique symbol;
|
|
241
436
|
/**
|
|
@@ -268,12 +463,6 @@ export type TypeErrorFormatter<Error extends TypeError> = (error: Error) => stri
|
|
|
268
463
|
* Base {@link Type}.
|
|
269
464
|
*
|
|
270
465
|
* 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
466
|
*
|
|
278
467
|
* ### Example
|
|
279
468
|
*
|
|
@@ -410,7 +599,7 @@ export declare const formatIsTypeError: TypeErrorFormatter<EvoluTypeError>;
|
|
|
410
599
|
* The `brand` Type Factory takes the name of a new {@link Brand}, a parent Type
|
|
411
600
|
* to be branded, and the optional `refine` function for additional constraint.
|
|
412
601
|
*
|
|
413
|
-
*
|
|
602
|
+
* The `refine` function can be omitted if we only want to add a brand.
|
|
414
603
|
*
|
|
415
604
|
* ### Examples
|
|
416
605
|
*
|
|
@@ -489,7 +678,7 @@ export declare const formatIsTypeError: TypeErrorFormatter<EvoluTypeError>;
|
|
|
489
678
|
* confirmPassword: SimplePassword,
|
|
490
679
|
* });
|
|
491
680
|
*
|
|
492
|
-
* const ValidForm = brand("
|
|
681
|
+
* const ValidForm = brand("ValidForm", Form, (value) => {
|
|
493
682
|
* if (value.password !== value.confirmPassword)
|
|
494
683
|
* return err<ValidFormError>({
|
|
495
684
|
* type: "ValidForm",
|
|
@@ -567,17 +756,19 @@ export declare const formatCurrencyCodeError: TypeErrorFormatter<CurrencyCodeErr
|
|
|
567
756
|
* ### Example
|
|
568
757
|
*
|
|
569
758
|
* ```ts
|
|
570
|
-
* const result =
|
|
571
|
-
* const error =
|
|
759
|
+
* const result = DateIso.from("2023-01-01T12:00:00.000Z"); // ok
|
|
760
|
+
* const error = DateIso.from("10000-01-01T00:00:00.000Z"); // err
|
|
572
761
|
* ```
|
|
573
762
|
*
|
|
574
763
|
* @category String
|
|
575
764
|
*/
|
|
576
|
-
export declare const
|
|
577
|
-
export type
|
|
578
|
-
export interface
|
|
765
|
+
export declare const DateIso: BrandType<Type<"String", string, string, StringError, string, StringError>, "DateIso", DateIsoError, StringError>;
|
|
766
|
+
export type DateIso = typeof DateIso.Type;
|
|
767
|
+
export interface DateIsoError extends TypeError<"DateIso"> {
|
|
579
768
|
}
|
|
580
|
-
export declare const
|
|
769
|
+
export declare const formatDateIsoError: TypeErrorFormatter<DateIsoError>;
|
|
770
|
+
export declare const dateToDateIso: (value: Date) => Result<DateIso, DateIsoError>;
|
|
771
|
+
export declare const dateIsoToDate: (value: DateIso) => Date;
|
|
581
772
|
/**
|
|
582
773
|
* Helper type for Type Factory that creates a branded Type.
|
|
583
774
|
*
|
|
@@ -600,18 +791,12 @@ export type BrandFactory<Name extends TypeName, Input, RefineError extends TypeE
|
|
|
600
791
|
/**
|
|
601
792
|
* Trimmed string.
|
|
602
793
|
*
|
|
603
|
-
* This Type Factory
|
|
604
|
-
*
|
|
605
|
-
* Factory.
|
|
794
|
+
* This Type Factory validates whether a string has no leading or trailing
|
|
795
|
+
* whitespaces.
|
|
606
796
|
*
|
|
607
|
-
* ###
|
|
797
|
+
* ### Example
|
|
608
798
|
*
|
|
609
799
|
* ```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
800
|
* const TrimmedNonEmptyString = trimmed(minLength(1)(String));
|
|
616
801
|
* // string & Brand<"MinLength1"> & Brand<"Trimmed">
|
|
617
802
|
* type TrimmedNonEmptyString = typeof TrimmedNonEmptyString.Type;
|
|
@@ -623,33 +808,6 @@ export declare const trimmed: BrandFactory<"Trimmed", string, TrimmedError>;
|
|
|
623
808
|
export interface TrimmedError extends TypeError<"Trimmed"> {
|
|
624
809
|
}
|
|
625
810
|
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
811
|
/**
|
|
654
812
|
* Trimmed string
|
|
655
813
|
*
|
|
@@ -660,6 +818,7 @@ export declare const trim: TransformBrandFactory<"Trimmed", string>;
|
|
|
660
818
|
*/
|
|
661
819
|
export declare const TrimmedString: BrandType<Type<"String", string, string, StringError, string, StringError>, "Trimmed", TrimmedError, StringError>;
|
|
662
820
|
export type TrimmedString = typeof TrimmedString.Type;
|
|
821
|
+
export declare const trim: (value: string) => TrimmedString;
|
|
663
822
|
/**
|
|
664
823
|
* Minimum length.
|
|
665
824
|
*
|
|
@@ -782,9 +941,9 @@ export interface RegexError<Name extends TypeName = TypeName> extends TypeError<
|
|
|
782
941
|
}
|
|
783
942
|
export declare const formatRegexError: TypeErrorFormatter<RegexError<Capitalize<string>>>;
|
|
784
943
|
/**
|
|
785
|
-
* URL-safe
|
|
944
|
+
* URL-safe string.
|
|
786
945
|
*
|
|
787
|
-
* A `
|
|
946
|
+
* A `UrlSafeString` uses a limited alphabet that is safe for URLs:
|
|
788
947
|
*
|
|
789
948
|
* - Uppercase letters (`A-Z`)
|
|
790
949
|
* - Lowercase letters (`a-z`)
|
|
@@ -792,31 +951,47 @@ export declare const formatRegexError: TypeErrorFormatter<RegexError<Capitalize<
|
|
|
792
951
|
* - Dash (`-`)
|
|
793
952
|
* - Underscore (`_`)
|
|
794
953
|
*
|
|
954
|
+
* This is the same character set used by Base64Url encoding, but this type does
|
|
955
|
+
* not validate that the string is actually Base64Url-encoded data.
|
|
956
|
+
*
|
|
795
957
|
* ### Example
|
|
796
958
|
*
|
|
797
959
|
* ```ts
|
|
798
|
-
* const result =
|
|
960
|
+
* const result = UrlSafeString.from("abc123_-");
|
|
799
961
|
* if (result.ok) {
|
|
800
|
-
* console.log("Valid
|
|
962
|
+
* console.log("Valid URL-safe string:", result.value);
|
|
801
963
|
* } else {
|
|
802
|
-
* console.error("Invalid
|
|
964
|
+
* console.error("Invalid URL-safe string:", result.error);
|
|
803
965
|
* }
|
|
804
966
|
* ```
|
|
805
967
|
*
|
|
806
968
|
* @category String
|
|
807
969
|
*/
|
|
808
|
-
export declare const
|
|
809
|
-
export type
|
|
810
|
-
export type
|
|
970
|
+
export declare const UrlSafeString: BrandType<Type<"String", string, string, StringError, string, StringError>, "UrlSafeString", RegexError<"UrlSafeString">, StringError>;
|
|
971
|
+
export type UrlSafeString = typeof UrlSafeString.Type;
|
|
972
|
+
export type UrlSafeStringError = typeof UrlSafeString.Error;
|
|
811
973
|
/**
|
|
812
|
-
*
|
|
974
|
+
* Base64Url without padding.
|
|
813
975
|
*
|
|
814
|
-
*
|
|
976
|
+
* Encode with {@link uint8ArrayToBase64Url}, decode with
|
|
977
|
+
* {@link base64UrlToUint8Array}.
|
|
815
978
|
*
|
|
816
|
-
*
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
979
|
+
* @category String
|
|
980
|
+
*/
|
|
981
|
+
export declare const Base64Url: BrandType<Type<"String", string, string, StringError, string, StringError>, "Base64Url", Base64UrlError, StringError>;
|
|
982
|
+
export type Base64Url = typeof Base64Url.Type;
|
|
983
|
+
export interface Base64UrlError extends TypeError<"Base64Url"> {
|
|
984
|
+
}
|
|
985
|
+
export declare const formatBase64UrlError: TypeErrorFormatter<Base64UrlError>;
|
|
986
|
+
/** Encodes a Uint8Array to a {@link Base64Url} string. */
|
|
987
|
+
export declare const uint8ArrayToBase64Url: (bytes: Uint8Array) => Base64Url;
|
|
988
|
+
/** Decodes a {@link Base64Url} string to a Uint8Array. */
|
|
989
|
+
export declare const base64UrlToUint8Array: (str: Base64Url) => Uint8Array;
|
|
990
|
+
/**
|
|
991
|
+
* Simple alphanumeric string for naming in file systems, URLs, and identifiers.
|
|
992
|
+
*
|
|
993
|
+
* Uses the same safe alphabet as {@link UrlSafeString} (letters, digits, `-`,
|
|
994
|
+
* `_`). See `UrlSafeString` for details.
|
|
820
995
|
*
|
|
821
996
|
* The string must be between 1 and 42 characters.
|
|
822
997
|
*
|
|
@@ -833,17 +1008,10 @@ export type Base64UrlError = typeof Base64Url.Error;
|
|
|
833
1008
|
*
|
|
834
1009
|
* @category String
|
|
835
1010
|
*/
|
|
836
|
-
export declare const SimpleName: BrandType<Type<"String", string, string, StringError, string, StringError>, "
|
|
1011
|
+
export declare const SimpleName: BrandType<BrandType<Type<"String", string, string, StringError, string, StringError>, "UrlSafeString", RegexError<"UrlSafeString">, StringError>, "SimpleName", SimpleNameError, StringError | RegexError<"UrlSafeString">>;
|
|
837
1012
|
export type SimpleName = typeof SimpleName.Type;
|
|
838
|
-
export
|
|
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;
|
|
1013
|
+
export interface SimpleNameError extends TypeError<"SimpleName"> {
|
|
1014
|
+
}
|
|
847
1015
|
/**
|
|
848
1016
|
* Trimmed string between 8 and 64 characters, branded as `SimplePassword`.
|
|
849
1017
|
*
|
|
@@ -854,16 +1022,49 @@ export type SimplePassword = typeof SimplePassword.Type;
|
|
|
854
1022
|
export type SimplePasswordError = typeof SimplePassword.Error;
|
|
855
1023
|
export declare const formatSimplePasswordError: (formatTypeError: TypeErrorFormatter<StringError | MinLengthError<8> | MaxLengthError<64> | TrimmedError>) => TypeErrorFormatter<SimplePasswordError>;
|
|
856
1024
|
/**
|
|
857
|
-
*
|
|
1025
|
+
* Globally unique identifier.
|
|
1026
|
+
*
|
|
1027
|
+
* **Evolu Id** is 16 random bytes from a cryptographically secure random
|
|
1028
|
+
* generator, encoded as 22-character Base64Url string. This provides strong
|
|
1029
|
+
* collision resistance for distributed ID generation.
|
|
1030
|
+
*
|
|
1031
|
+
* ### Design Rationale
|
|
1032
|
+
*
|
|
1033
|
+
* Why Evolu Id over alternatives:
|
|
858
1034
|
*
|
|
859
|
-
*
|
|
860
|
-
*
|
|
1035
|
+
* - **NanoID**: No standard binary serialization format, and uses only ~126 bits
|
|
1036
|
+
* of entropy (21 characters from 64-symbol alphabet) compared to Evolu Id's
|
|
1037
|
+
* 128 bits.
|
|
1038
|
+
* - **UUID (v4)**: String format is 36 characters (with hyphens) compared to
|
|
1039
|
+
* Evolu Id's 22 characters. While UUIDs can be stored as 16 bytes, their
|
|
1040
|
+
* standard string representation is verbose.
|
|
1041
|
+
* - **UUID v7**: Includes timestamp in the ID, which leaks information about when
|
|
1042
|
+
* data was created. This is a privacy concern for local-first applications
|
|
1043
|
+
* where creation time must remain private.
|
|
1044
|
+
*
|
|
1045
|
+
* Evolu Id provides 128 bits of entropy, compact string representation (22
|
|
1046
|
+
* characters), standard and native string serialization (Base64Url), and no
|
|
1047
|
+
* privacy leaks.
|
|
1048
|
+
*
|
|
1049
|
+
* ### Future Consideration
|
|
1050
|
+
*
|
|
1051
|
+
* For database-heavy workloads where insert performance is critical, a hybrid
|
|
1052
|
+
* approach could be considered: `timestamp ^ H(cluster_id, timestamp >> N)`
|
|
1053
|
+
* where H is a keyed hash function and N is a configurable parameter. This
|
|
1054
|
+
* would maintain spatial locality for database caches (improving insert
|
|
1055
|
+
* performance by an order of magnitude) while adding entropy to prevent
|
|
1056
|
+
* timestamp leakage and correlation across systems. The parameter N would allow
|
|
1057
|
+
* trading off cache locality (larger N = better locality) versus entropy
|
|
1058
|
+
* distribution. See https://brooker.co.za/blog/2025/10/22/uuidv7.html for
|
|
1059
|
+
* details on this approach.
|
|
861
1060
|
*
|
|
862
1061
|
* @category String
|
|
863
1062
|
*/
|
|
864
|
-
export declare const Id: BrandType<Type<"String", string, string, StringError, string, StringError>, "Id",
|
|
1063
|
+
export declare const Id: BrandType<Type<"String", string, string, StringError, string, StringError>, "Id", IdError, StringError>;
|
|
865
1064
|
export type Id = typeof Id.Type;
|
|
866
|
-
export
|
|
1065
|
+
export interface IdError extends TypeError<"Id"> {
|
|
1066
|
+
}
|
|
1067
|
+
export declare const formatIdError: TypeErrorFormatter<IdError>;
|
|
867
1068
|
/**
|
|
868
1069
|
* Creates an {@link Id}.
|
|
869
1070
|
*
|
|
@@ -872,11 +1073,49 @@ export declare const idTypeValueLength = 21;
|
|
|
872
1073
|
* ```ts
|
|
873
1074
|
* // string & Brand<"Id">
|
|
874
1075
|
* const id = createId(deps);
|
|
1076
|
+
*
|
|
1077
|
+
* // string & Brand<"Id"> & Brand<"Todo">
|
|
1078
|
+
* const todoId = createId<"Todo">(deps);
|
|
875
1079
|
* ```
|
|
876
1080
|
*/
|
|
877
|
-
export declare const createId: (deps:
|
|
1081
|
+
export declare const createId: <B extends string = never>(deps: RandomBytesDep) => [B] extends [never] ? Id : Id & Brand<B>;
|
|
878
1082
|
/**
|
|
879
|
-
*
|
|
1083
|
+
* Creates an {@link Id} from a string using SHA-256.
|
|
1084
|
+
*
|
|
1085
|
+
* When integrating with external systems that use different ID formats, use
|
|
1086
|
+
* this function to convert external IDs into valid Evolu IDs.
|
|
1087
|
+
*
|
|
1088
|
+
* In Evolu's CRDT, the ID serves as the unique identifier for conflict
|
|
1089
|
+
* resolution across distributed clients. When multiple clients create records
|
|
1090
|
+
* with the same external identifier, they must resolve to the same Evolu ID to
|
|
1091
|
+
* ensure data consistency.
|
|
1092
|
+
*
|
|
1093
|
+
* ### Example
|
|
1094
|
+
*
|
|
1095
|
+
* ```ts
|
|
1096
|
+
* // Both clients will generate the same ID
|
|
1097
|
+
* const id1 = createIdFromString("user-api-123");
|
|
1098
|
+
* const id2 = createIdFromString("user-api-123");
|
|
1099
|
+
* console.log(id1 === id2); // true
|
|
1100
|
+
*
|
|
1101
|
+
* upsert("todo", {
|
|
1102
|
+
* id: createIdFromString("external-todo-456"),
|
|
1103
|
+
* title: "Synced from external system",
|
|
1104
|
+
* });
|
|
1105
|
+
* ```
|
|
1106
|
+
*
|
|
1107
|
+
* **Important**: This transformation uses the first 16 bytes of SHA-256 hash of
|
|
1108
|
+
* the string bytes, therefore it's not possible to recover the original
|
|
1109
|
+
* external string from the generated {@link Id}. If you need to preserve the
|
|
1110
|
+
* original external ID, store it in a separate column.
|
|
1111
|
+
*
|
|
1112
|
+
* @category String
|
|
1113
|
+
*/
|
|
1114
|
+
export declare const createIdFromString: <B extends string = never>(value: string) => [B] extends [never] ? Id : Id & Brand<B>;
|
|
1115
|
+
/**
|
|
1116
|
+
* Creates a branded {@link Id} Type for a table's primary key.
|
|
1117
|
+
*
|
|
1118
|
+
* The table name becomes an additional brand for type safety.
|
|
880
1119
|
*
|
|
881
1120
|
* ### Example
|
|
882
1121
|
*
|
|
@@ -888,16 +1127,22 @@ export declare const createId: (deps: NanoIdLibDep) => Id;
|
|
|
888
1127
|
*
|
|
889
1128
|
* @category String
|
|
890
1129
|
*/
|
|
891
|
-
export declare const id: <Table extends TypeName>(table: Table) =>
|
|
892
|
-
export interface
|
|
1130
|
+
export declare const id: <Table extends TypeName>(table: Table) => TableId<Table>;
|
|
1131
|
+
export interface TableId<Table extends TypeName> extends Type<"Id", string & Brand<"Id"> & Brand<Table>, string, TableIdError<Table>, string, StringError> {
|
|
893
1132
|
table: Table;
|
|
894
1133
|
}
|
|
895
|
-
export interface
|
|
1134
|
+
export interface TableIdError<Table extends TypeName = TypeName> extends TypeError<"TableId"> {
|
|
896
1135
|
readonly table: Table;
|
|
897
1136
|
}
|
|
898
|
-
export declare const
|
|
1137
|
+
export declare const formatTableIdError: TypeErrorFormatter<TableIdError<Capitalize<string>>>;
|
|
1138
|
+
/** Binary representation of an {@link Id}. */
|
|
1139
|
+
export declare const IdBytes: BrandType<BrandType<Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, Uint8ArrayError, Uint8Array<ArrayBufferLike>, Uint8ArrayError>, "Length16", LengthError<16>, Uint8ArrayError>, "IdBytes", BrandWithoutRefineError<"IdBytes", LengthError<16> | Uint8ArrayError>, never>;
|
|
1140
|
+
export type IdBytes = typeof IdBytes.Type;
|
|
1141
|
+
export declare const idBytesTypeValueLength: NonNegativeInt;
|
|
1142
|
+
export declare const idToIdBytes: (id: Id) => IdBytes;
|
|
1143
|
+
export declare const idBytesToId: (idBytes: IdBytes) => Id;
|
|
899
1144
|
/**
|
|
900
|
-
* Positive number.
|
|
1145
|
+
* Positive number (> 0).
|
|
901
1146
|
*
|
|
902
1147
|
* ### Example
|
|
903
1148
|
*
|
|
@@ -915,7 +1160,7 @@ export interface PositiveError extends TypeError<"Positive"> {
|
|
|
915
1160
|
}
|
|
916
1161
|
export declare const formatPositiveError: TypeErrorFormatter<PositiveError>;
|
|
917
1162
|
/**
|
|
918
|
-
* Negative number.
|
|
1163
|
+
* Negative number (< 0).
|
|
919
1164
|
*
|
|
920
1165
|
* ### Example
|
|
921
1166
|
*
|
|
@@ -930,7 +1175,7 @@ export interface NegativeError extends TypeError<"Negative"> {
|
|
|
930
1175
|
}
|
|
931
1176
|
export declare const formatNegativeError: TypeErrorFormatter<NegativeError>;
|
|
932
1177
|
/**
|
|
933
|
-
* Non-positive number.
|
|
1178
|
+
* Non-positive number (≤ 0).
|
|
934
1179
|
*
|
|
935
1180
|
* ### Example
|
|
936
1181
|
*
|
|
@@ -945,7 +1190,7 @@ export interface NonPositiveError extends TypeError<"NonPositive"> {
|
|
|
945
1190
|
}
|
|
946
1191
|
export declare const formatNonPositiveError: TypeErrorFormatter<NonPositiveError>;
|
|
947
1192
|
/**
|
|
948
|
-
* Non-negative number.
|
|
1193
|
+
* Non-negative number (≥ 0).
|
|
949
1194
|
*
|
|
950
1195
|
* ### Example
|
|
951
1196
|
*
|
|
@@ -959,16 +1204,32 @@ export declare const nonNegative: BrandFactory<"NonNegative", number, NonNegativ
|
|
|
959
1204
|
export interface NonNegativeError extends TypeError<"NonNegative"> {
|
|
960
1205
|
}
|
|
961
1206
|
export declare const formatNonNegativeError: TypeErrorFormatter<NonNegativeError>;
|
|
962
|
-
/**
|
|
1207
|
+
/**
|
|
1208
|
+
* Non-negative number (≥ 0).
|
|
1209
|
+
*
|
|
1210
|
+
* @category Number
|
|
1211
|
+
*/
|
|
963
1212
|
export declare const NonNegativeNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonNegative", NonNegativeError, NumberError>;
|
|
964
1213
|
export type NonNegativeNumber = typeof NonNegativeNumber.Type;
|
|
965
|
-
/**
|
|
966
|
-
|
|
1214
|
+
/**
|
|
1215
|
+
* Positive number (> 0).
|
|
1216
|
+
*
|
|
1217
|
+
* @category Number
|
|
1218
|
+
*/
|
|
1219
|
+
export declare const PositiveNumber: BrandType<Type<"Brand", number & Brand<"NonNegative">, number, NonNegativeError, number, NumberError>, "Positive", PositiveError, NonNegativeError | NumberError>;
|
|
967
1220
|
export type PositiveNumber = typeof PositiveNumber.Type;
|
|
968
|
-
/**
|
|
1221
|
+
/**
|
|
1222
|
+
* Non-positive number (≤ 0).
|
|
1223
|
+
*
|
|
1224
|
+
* @category Number
|
|
1225
|
+
*/
|
|
969
1226
|
export declare const NonPositiveNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonPositive", NonPositiveError, NumberError>;
|
|
970
1227
|
export type NonPositiveNumber = typeof NonPositiveNumber.Type;
|
|
971
|
-
/**
|
|
1228
|
+
/**
|
|
1229
|
+
* Negative number (< 0).
|
|
1230
|
+
*
|
|
1231
|
+
* @category Number
|
|
1232
|
+
*/
|
|
972
1233
|
export declare const NegativeNumber: BrandType<Type<"Brand", number & Brand<"NonPositive">, number, NonPositiveError, number, NumberError>, "Negative", NegativeError, NumberError | NonPositiveError>;
|
|
973
1234
|
export type NegativeNumber = typeof NegativeNumber.Type;
|
|
974
1235
|
/**
|
|
@@ -993,17 +1254,35 @@ export declare const formatIntError: TypeErrorFormatter<IntError>;
|
|
|
993
1254
|
*/
|
|
994
1255
|
export declare const Int: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Int", IntError, NumberError>;
|
|
995
1256
|
export type Int = typeof Int.Type;
|
|
996
|
-
/**
|
|
997
|
-
|
|
1257
|
+
/**
|
|
1258
|
+
* Non-negative integer (≥ 0).
|
|
1259
|
+
*
|
|
1260
|
+
* @category Number
|
|
1261
|
+
*/
|
|
1262
|
+
export declare const NonNegativeInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonNegative", NonNegativeError, IntError | NumberError>;
|
|
998
1263
|
export type NonNegativeInt = typeof NonNegativeInt.Type;
|
|
999
|
-
/**
|
|
1000
|
-
|
|
1264
|
+
/**
|
|
1265
|
+
* Positive integer (> 0).
|
|
1266
|
+
*
|
|
1267
|
+
* @category Number
|
|
1268
|
+
*/
|
|
1269
|
+
export declare const PositiveInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, NonNegativeError, number & Brand<"Int">, IntError | NumberError>, "Positive", PositiveError, NonNegativeError | IntError | NumberError>;
|
|
1001
1270
|
export type PositiveInt = typeof PositiveInt.Type;
|
|
1002
|
-
/**
|
|
1003
|
-
export declare const
|
|
1271
|
+
/** Maximum safe positive integer value for practically infinite operations. */
|
|
1272
|
+
export declare const maxPositiveInt: number & Brand<"Int"> & Brand<"NonNegative"> & Brand<"Positive">;
|
|
1273
|
+
/**
|
|
1274
|
+
* Non-positive integer (≤ 0).
|
|
1275
|
+
*
|
|
1276
|
+
* @category Number
|
|
1277
|
+
*/
|
|
1278
|
+
export declare const NonPositiveInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonPositive", NonPositiveError, IntError | NumberError>;
|
|
1004
1279
|
export type NonPositiveInt = typeof NonPositiveInt.Type;
|
|
1005
|
-
/**
|
|
1006
|
-
|
|
1280
|
+
/**
|
|
1281
|
+
* Negative integer (< 0).
|
|
1282
|
+
*
|
|
1283
|
+
* @category Number
|
|
1284
|
+
*/
|
|
1285
|
+
export declare const NegativeInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonPositive">, number, NonPositiveError, number & Brand<"Int">, IntError | NumberError>, "Negative", NegativeError, IntError | NumberError | NonPositiveError>;
|
|
1007
1286
|
export type NegativeInt = typeof NegativeInt.Type;
|
|
1008
1287
|
/**
|
|
1009
1288
|
* Number greater than a specified value.
|
|
@@ -1110,9 +1389,6 @@ export interface BetweenError<Min extends number = number, Max extends number =
|
|
|
1110
1389
|
readonly max: Max;
|
|
1111
1390
|
}
|
|
1112
1391
|
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
1392
|
/**
|
|
1117
1393
|
* Literal {@link Type}.
|
|
1118
1394
|
*
|
|
@@ -1138,67 +1414,6 @@ export interface LiteralError<T extends Literal = Literal> extends TypeError<"Li
|
|
|
1138
1414
|
readonly expected: T;
|
|
1139
1415
|
}
|
|
1140
1416
|
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
1417
|
/**
|
|
1203
1418
|
* Array of a specific {@link Type}.
|
|
1204
1419
|
*
|
|
@@ -1656,16 +1871,12 @@ export type Int64 = typeof Int64.Type;
|
|
|
1656
1871
|
export interface Int64Error extends TypeError<"Int64"> {
|
|
1657
1872
|
}
|
|
1658
1873
|
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
1874
|
/**
|
|
1664
1875
|
* Stringified {@link Int64}.
|
|
1665
1876
|
*
|
|
1666
|
-
* @category
|
|
1877
|
+
* @category String
|
|
1667
1878
|
*/
|
|
1668
|
-
export declare const Int64String: BrandType<Type<"
|
|
1879
|
+
export declare const Int64String: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>, "Int64", Int64StringError, StringError | TrimmedError | MinLengthError<1>>;
|
|
1669
1880
|
export type Int64String = typeof Int64String.Type;
|
|
1670
1881
|
export interface Int64StringError extends TypeError<"Int64String"> {
|
|
1671
1882
|
}
|
|
@@ -1700,42 +1911,63 @@ export declare const JsonArray: ArrayType<RecursiveType<UnionType<[Type<"String"
|
|
|
1700
1911
|
* @category Object
|
|
1701
1912
|
*/
|
|
1702
1913
|
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>>]>>>;
|
|
1914
|
+
export declare const parseJson: (value: string) => Result<JsonValue, JsonError>;
|
|
1703
1915
|
/**
|
|
1704
|
-
*
|
|
1705
|
-
* JsonValue back into a JSON string.
|
|
1916
|
+
* JSON-string {@link Type}.
|
|
1706
1917
|
*
|
|
1707
1918
|
* ### Example
|
|
1708
1919
|
*
|
|
1709
1920
|
* ```ts
|
|
1710
|
-
*
|
|
1711
|
-
*
|
|
1921
|
+
* const result = Json.from('{"key":"value"}'); // ok
|
|
1922
|
+
* const error = Json.from("invalid json"); // err
|
|
1712
1923
|
* ```
|
|
1713
1924
|
*
|
|
1714
1925
|
* @category String
|
|
1715
1926
|
*/
|
|
1716
|
-
export declare const
|
|
1717
|
-
export
|
|
1927
|
+
export declare const Json: BrandType<Type<"String", string, string, StringError, string, StringError>, "Json", JsonError, StringError>;
|
|
1928
|
+
export type Json = typeof Json.Type;
|
|
1929
|
+
export interface JsonError extends TypeError<"Json"> {
|
|
1718
1930
|
readonly message: string;
|
|
1719
1931
|
}
|
|
1720
|
-
export declare const
|
|
1932
|
+
export declare const formatJsonError: TypeErrorFormatter<JsonError>;
|
|
1933
|
+
export declare const jsonValueToJson: (value: JsonValue) => Json;
|
|
1934
|
+
export declare const jsonToJsonValue: (value: Json) => JsonValue;
|
|
1721
1935
|
/**
|
|
1722
|
-
* JSON
|
|
1936
|
+
* Creates a branded JSON string {@link Type} and type-safe conversion functions
|
|
1937
|
+
* for a given Type.
|
|
1938
|
+
*
|
|
1939
|
+
* This factory creates:
|
|
1940
|
+
*
|
|
1941
|
+
* 1. A branded string Type that validates JSON parsing and structural conformity
|
|
1942
|
+
* 2. A serialization function (Type → branded JSON string)
|
|
1943
|
+
* 3. A parsing function (branded JSON string → Type, skipping validation)
|
|
1944
|
+
*
|
|
1945
|
+
* Optimized for Evolu's SQLite workflow where we store typed JSON strings and
|
|
1946
|
+
* need type-safe conversions without double parsing.
|
|
1723
1947
|
*
|
|
1724
1948
|
* ### Example
|
|
1725
1949
|
*
|
|
1726
1950
|
* ```ts
|
|
1727
|
-
* const
|
|
1728
|
-
*
|
|
1729
|
-
*
|
|
1951
|
+
* const Person = object({
|
|
1952
|
+
* name: NonEmptyString100,
|
|
1953
|
+
* age: FiniteNumber,
|
|
1954
|
+
* });
|
|
1955
|
+
* type Person = typeof Person.Type;
|
|
1730
1956
|
*
|
|
1731
|
-
*
|
|
1957
|
+
* const [PersonJson, personToPersonJson, personJsonToPerson] = json(
|
|
1958
|
+
* Person,
|
|
1959
|
+
* "PersonJson",
|
|
1960
|
+
* );
|
|
1961
|
+
* // string & Brand<"PersonJson">
|
|
1962
|
+
* type PersonJson = typeof PersonJson.Type;
|
|
1963
|
+
*
|
|
1964
|
+
* // Usage:
|
|
1965
|
+
* const person: Person = { name: "Alice", age: 30 };
|
|
1966
|
+
* const jsonString = personToPersonJson(person); // PersonJson
|
|
1967
|
+
* const backToPerson = personJsonToPerson(jsonString); // Person
|
|
1968
|
+
* ```
|
|
1732
1969
|
*/
|
|
1733
|
-
export declare const
|
|
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>;
|
|
1970
|
+
export declare const json: <T extends AnyType, Name extends TypeName>(type: T, name: Name) => [BrandType<typeof String, Name, JsonError | InferErrors<T>, StringError>, (value: InferType<T>) => InferType<BrandType<typeof String, Name, JsonError | InferErrors<T>, StringError>>, (value: InferType<BrandType<typeof String, Name, JsonError | InferErrors<T>, StringError>>) => InferType<T>];
|
|
1739
1971
|
/**
|
|
1740
1972
|
* Optional {@link Type}.
|
|
1741
1973
|
*
|
|
@@ -1766,7 +1998,7 @@ export declare const isOptionalType: (x: unknown) => x is OptionalType<any>;
|
|
|
1766
1998
|
/**
|
|
1767
1999
|
* Creates a partial object type where all properties are optional.
|
|
1768
2000
|
*
|
|
1769
|
-
* This is useful when
|
|
2001
|
+
* This is useful when we want to validate an object in which none of the keys
|
|
1770
2002
|
* are required, but if they are present they must conform to their
|
|
1771
2003
|
* corresponding Types.
|
|
1772
2004
|
*
|
|
@@ -1812,36 +2044,18 @@ export type NullTypeInMembers<Members extends [AnyType, ...Array<AnyType>]> = Me
|
|
|
1812
2044
|
* @category Object
|
|
1813
2045
|
*/
|
|
1814
2046
|
export declare function omit<T extends ObjectType<any>, Keys extends keyof T["props"]>(objectType: T, ...keys: ReadonlyArray<Keys>): ObjectType<Omit<T["props"], Keys>>;
|
|
2047
|
+
export declare const maxMutationSize = 655360;
|
|
1815
2048
|
/**
|
|
1816
|
-
*
|
|
1817
|
-
*
|
|
1818
|
-
*
|
|
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
|
-
* ```
|
|
2049
|
+
* Evolu has to limit the maximum mutation size. Otherwise, sync couldn't use
|
|
2050
|
+
* the `maxProtocolMessageRangesSize`. The max size is 640KB in bytes, measured
|
|
2051
|
+
* via MessagePack. Evolu Protocol DbChange will be smaller thanks to various
|
|
2052
|
+
* optimizations.
|
|
1843
2053
|
*/
|
|
1844
|
-
export declare const
|
|
2054
|
+
export declare const validMutationSize: <T extends AnyType>(type: T) => BrandType<T, "ValidMutationSize", ValidMutationSizeError, InferErrors<T>>;
|
|
2055
|
+
export interface ValidMutationSizeError extends TypeError<"ValidMutationSize"> {
|
|
2056
|
+
}
|
|
2057
|
+
export declare const formatValidMutationSizeError: TypeErrorFormatter<ValidMutationSizeError>;
|
|
2058
|
+
export type ValidMutationSize<Props extends Record<string, AnyType>> = BrandType<ObjectType<Props>, "ValidMutationSize", ValidMutationSizeError, InferErrors<ObjectType<Props>>>;
|
|
1845
2059
|
/**
|
|
1846
2060
|
* Union of all `TypeError`s defined in the `Type.ts` file, including base type
|
|
1847
2061
|
* errors (e.g., `StringError`, `NumberError`), composite type errors
|
|
@@ -1855,72 +2069,76 @@ export declare const json: <T extends AnyType, Name extends TypeName>(type: T, n
|
|
|
1855
2069
|
*
|
|
1856
2070
|
* @category Utilities
|
|
1857
2071
|
*/
|
|
1858
|
-
export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | NumberError | BigIntError | BooleanError | UndefinedError | NullError | FunctionError | Uint8ArrayError | InstanceOfError | EvoluTypeError | CurrencyCodeError |
|
|
2072
|
+
export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | NumberError | BigIntError | BooleanError | UndefinedError | NullError | FunctionError | Uint8ArrayError | InstanceOfError | EvoluTypeError | CurrencyCodeError | DateIsoError | TrimmedError | MinLengthError | MaxLengthError | LengthError | MnemonicError | RegexError | SimplePasswordError | IdError | TableIdError | PositiveError | NegativeError | NonPositiveError | NonNegativeError | IntError | GreaterThanError | LessThanError | GreaterThanOrEqualToError | LessThanOrEqualToError | NonNaNError | FiniteError | MultipleOfError | BetweenError | LiteralError | Int64Error | Int64StringError | JsonError | ValidMutationSizeError | 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
2073
|
/**
|
|
1860
|
-
*
|
|
1861
|
-
* {@link TypeErrors} and custom errors. It also lets us override the default
|
|
1862
|
-
* formatting for specific errors.
|
|
2074
|
+
* Formats Evolu Type errors into user-friendly messages.
|
|
1863
2075
|
*
|
|
1864
|
-
*
|
|
1865
|
-
*
|
|
2076
|
+
* Evolu Type typed errors ensure every error type must have a formatter.
|
|
2077
|
+
* TypeScript enforces this at compile-time, preventing unhandled validation
|
|
2078
|
+
* errors from reaching users.
|
|
1866
2079
|
*
|
|
1867
|
-
*
|
|
2080
|
+
* The `createFormatTypeError` function handles both built-in {@link TypeErrors}
|
|
2081
|
+
* and custom errors, and lets us override default formatting for specific
|
|
2082
|
+
* errors.
|
|
2083
|
+
*
|
|
2084
|
+
* ### Example
|
|
1868
2085
|
*
|
|
1869
2086
|
* ```ts
|
|
1870
|
-
* const
|
|
1871
|
-
*
|
|
1872
|
-
*
|
|
2087
|
+
* const formatTypeError = createFormatTypeError<
|
|
2088
|
+
* MinLengthError | MaxLengthError
|
|
2089
|
+
* >((error): string => {
|
|
2090
|
+
* switch (error.type) {
|
|
2091
|
+
* case "MinLength":
|
|
2092
|
+
* return `Text must be at least ${error.min} character${error.min === 1 ? "" : "s"} long`;
|
|
2093
|
+
* case "MaxLength":
|
|
2094
|
+
* return `Text is too long (maximum ${error.max} characters)`;
|
|
2095
|
+
* }
|
|
2096
|
+
* });
|
|
1873
2097
|
* ```
|
|
1874
2098
|
*
|
|
1875
|
-
*
|
|
2099
|
+
* Alternatively, write a custom formatter from scratch without using
|
|
2100
|
+
* `createFormatTypeError`. This gives us full control over error formatting:
|
|
1876
2101
|
*
|
|
1877
2102
|
* ```ts
|
|
1878
|
-
*
|
|
1879
|
-
*
|
|
2103
|
+
* const Person = object({
|
|
2104
|
+
* name: NonEmptyTrimmedString100,
|
|
2105
|
+
* age: optional(PositiveInt),
|
|
2106
|
+
* });
|
|
2107
|
+
*
|
|
2108
|
+
* // Define only the errors actually used by Person Type
|
|
2109
|
+
* type PersonErrors =
|
|
1880
2110
|
* | StringError
|
|
1881
|
-
* | MinLengthError
|
|
1882
2111
|
* | MaxLengthError
|
|
1883
|
-
* |
|
|
1884
|
-
* | IdError
|
|
2112
|
+
* | MinLengthError
|
|
1885
2113
|
* | TrimmedError
|
|
1886
|
-
* |
|
|
1887
|
-
* |
|
|
1888
|
-
*
|
|
1889
|
-
* |
|
|
1890
|
-
* |
|
|
1891
|
-
*
|
|
1892
|
-
* const formatTypeError: TypeErrorFormatter<
|
|
1893
|
-
* // In the real code, we would use the createTypeErrorFormatter helper
|
|
1894
|
-
* // that safely stringifies error value.
|
|
2114
|
+
* | PositiveError
|
|
2115
|
+
* | NonNegativeError
|
|
2116
|
+
* | IntError
|
|
2117
|
+
* | NumberError
|
|
2118
|
+
* | ObjectError<Record<string, PersonErrors>>;
|
|
2119
|
+
*
|
|
2120
|
+
* const formatTypeError: TypeErrorFormatter<PersonErrors> = (error) => {
|
|
1895
2121
|
* 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
2122
|
* case "String":
|
|
1907
|
-
* // We can reuse existing formatter.
|
|
1908
2123
|
* return formatStringError(error);
|
|
2124
|
+
* case "Number":
|
|
2125
|
+
* return "Must be a number";
|
|
2126
|
+
* case "MinLength":
|
|
2127
|
+
* return `Must be at least ${error.min} characters`;
|
|
2128
|
+
* case "MaxLength":
|
|
2129
|
+
* return `Cannot exceed ${error.max} characters`;
|
|
1909
2130
|
* case "Trimmed":
|
|
1910
|
-
* return "
|
|
1911
|
-
* case "
|
|
1912
|
-
* return "
|
|
1913
|
-
* case "
|
|
1914
|
-
* return
|
|
1915
|
-
*
|
|
1916
|
-
*
|
|
1917
|
-
* return `Union errors: ${error.errors.map(formatTypeError).join(", ")}`;
|
|
2131
|
+
* return "Cannot have leading or trailing spaces";
|
|
2132
|
+
* case "Positive":
|
|
2133
|
+
* return "Must be a positive number";
|
|
2134
|
+
* case "NonNegative":
|
|
2135
|
+
* return "Must be zero or positive";
|
|
2136
|
+
* case "Int":
|
|
2137
|
+
* return "Must be an integer";
|
|
1918
2138
|
* case "Object": {
|
|
1919
|
-
* if (
|
|
1920
|
-
*
|
|
1921
|
-
*
|
|
1922
|
-
* )
|
|
1923
|
-
* return "A developer made an error, this should not happen.";
|
|
2139
|
+
* if (error.reason.kind === "NotObject") return "Must be an object";
|
|
2140
|
+
* if (error.reason.kind === "ExtraKeys")
|
|
2141
|
+
* return "Contains unexpected fields";
|
|
1924
2142
|
* const firstError = Object.values(error.reason.errors).find(
|
|
1925
2143
|
* (e) => e !== undefined,
|
|
1926
2144
|
* )!;
|
|
@@ -1933,5 +2151,69 @@ export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | Nu
|
|
|
1933
2151
|
* @category Utilities
|
|
1934
2152
|
*/
|
|
1935
2153
|
export declare const createFormatTypeError: <ExtraErrors extends TypeError = never>(extraFormatter?: TypeErrorFormatter<ExtraErrors>) => TypeErrorFormatter<TypeErrors<ExtraErrors>>;
|
|
2154
|
+
/**
|
|
2155
|
+
* Converts an Evolu {@link TypeError} to Standard Schema V1 issues format.
|
|
2156
|
+
*
|
|
2157
|
+
* This function recursively converts Evolu's typed errors into the Standard
|
|
2158
|
+
* Schema issue format with proper path tracking for nested structures.
|
|
2159
|
+
*
|
|
2160
|
+
* @category Utilities
|
|
2161
|
+
*/
|
|
2162
|
+
export declare const typeErrorToStandardSchemaIssues: <ExtraErrors extends TypeError = never>(error: TypeErrors<ExtraErrors>, formatTypeError: TypeErrorFormatter<TypeErrors<ExtraErrors>>, path?: ReadonlyArray<PropertyKey>) => ReadonlyArray<StandardSchemaV1.Issue>;
|
|
2163
|
+
/** The Standard Schema interface. */
|
|
2164
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
2165
|
+
/** The Standard Schema properties. */
|
|
2166
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
2167
|
+
}
|
|
2168
|
+
export declare namespace StandardSchemaV1 {
|
|
2169
|
+
/** The Standard Schema properties interface. */
|
|
2170
|
+
interface Props<Input = unknown, Output = Input> {
|
|
2171
|
+
/** The version number of the standard. */
|
|
2172
|
+
readonly version: 1;
|
|
2173
|
+
/** The vendor name of the schema library. */
|
|
2174
|
+
readonly vendor: string;
|
|
2175
|
+
/** Validates unknown input values. */
|
|
2176
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
2177
|
+
/** Inferred types associated with the schema. */
|
|
2178
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
2179
|
+
}
|
|
2180
|
+
/** The result interface of the validate function. */
|
|
2181
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
2182
|
+
/** The result interface if validation succeeds. */
|
|
2183
|
+
interface SuccessResult<Output> {
|
|
2184
|
+
/** The typed output value. */
|
|
2185
|
+
readonly value: Output;
|
|
2186
|
+
/** The non-existent issues. */
|
|
2187
|
+
readonly issues?: undefined;
|
|
2188
|
+
}
|
|
2189
|
+
/** The result interface if validation fails. */
|
|
2190
|
+
interface FailureResult {
|
|
2191
|
+
/** The issues of failed validation. */
|
|
2192
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
2193
|
+
}
|
|
2194
|
+
/** The issue interface of the failure output. */
|
|
2195
|
+
interface Issue {
|
|
2196
|
+
/** The error message of the issue. */
|
|
2197
|
+
readonly message: string;
|
|
2198
|
+
/** The path of the issue, if any. */
|
|
2199
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
2200
|
+
}
|
|
2201
|
+
/** The path segment interface of the issue. */
|
|
2202
|
+
interface PathSegment {
|
|
2203
|
+
/** The key representing a path segment. */
|
|
2204
|
+
readonly key: PropertyKey;
|
|
2205
|
+
}
|
|
2206
|
+
/** The Standard Schema types interface. */
|
|
2207
|
+
interface Types<Input = unknown, Output = Input> {
|
|
2208
|
+
/** The input type of the schema. */
|
|
2209
|
+
readonly input: Input;
|
|
2210
|
+
/** The output type of the schema. */
|
|
2211
|
+
readonly output: Output;
|
|
2212
|
+
}
|
|
2213
|
+
/** Infers the input type of a Standard Schema. */
|
|
2214
|
+
type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
2215
|
+
/** Infers the output type of a Standard Schema. */
|
|
2216
|
+
type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
2217
|
+
}
|
|
1936
2218
|
export {};
|
|
1937
2219
|
//# sourceMappingURL=Type.d.ts.map
|