effect 4.0.0-beta.30 → 4.0.0-beta.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/Channel.d.ts +2 -2
- package/dist/Channel.d.ts.map +1 -1
- package/dist/Channel.js.map +1 -1
- package/dist/Cron.js +3 -3
- package/dist/Cron.js.map +1 -1
- package/dist/DateTime.d.ts +33 -77
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +1 -1
- package/dist/DateTime.js.map +1 -1
- package/dist/Duration.d.ts +29 -1
- package/dist/Duration.d.ts.map +1 -1
- package/dist/Duration.js +77 -48
- package/dist/Duration.js.map +1 -1
- package/dist/Layer.d.ts +97 -2
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +8 -2
- package/dist/Layer.js.map +1 -1
- package/dist/Schema.d.ts +2316 -114
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +1331 -67
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaAST.d.ts +2 -2
- package/dist/SchemaAST.js +2 -2
- package/dist/SchemaRepresentation.d.ts +2 -2
- package/dist/SchemaRepresentation.d.ts.map +1 -1
- package/dist/SchemaRepresentation.js.map +1 -1
- package/dist/internal/dateTime.js +57 -50
- package/dist/internal/dateTime.js.map +1 -1
- package/dist/internal/effect.js +1 -1
- package/dist/internal/effect.js.map +1 -1
- package/dist/unstable/ai/Chat.d.ts +3 -4
- package/dist/unstable/ai/Chat.d.ts.map +1 -1
- package/dist/unstable/ai/Chat.js.map +1 -1
- package/dist/unstable/ai/LanguageModel.d.ts +30 -16
- package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
- package/dist/unstable/ai/LanguageModel.js +10 -87
- package/dist/unstable/ai/LanguageModel.js.map +1 -1
- package/dist/unstable/cli/Completions.d.ts +16 -0
- package/dist/unstable/cli/Completions.d.ts.map +1 -0
- package/dist/unstable/cli/Completions.js +23 -0
- package/dist/unstable/cli/Completions.js.map +1 -0
- package/dist/unstable/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/unstable/cli/GlobalFlag.js +4 -4
- package/dist/unstable/cli/GlobalFlag.js.map +1 -1
- package/dist/unstable/cli/index.d.ts +4 -0
- package/dist/unstable/cli/index.d.ts.map +1 -1
- package/dist/unstable/cli/index.js +4 -0
- package/dist/unstable/cli/index.js.map +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/unstable/cluster/DeliverAt.js +1 -1
- package/dist/unstable/cluster/DeliverAt.js.map +1 -1
- package/dist/unstable/encoding/Ndjson.d.ts +8 -8
- package/dist/unstable/encoding/Ndjson.d.ts.map +1 -1
- package/dist/unstable/encoding/Ndjson.js.map +1 -1
- package/dist/unstable/http/HttpClientResponse.js.map +1 -1
- package/dist/unstable/http/HttpIncomingMessage.d.ts +1 -1
- package/dist/unstable/http/HttpIncomingMessage.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerRequest.js.map +1 -1
- package/dist/unstable/http/HttpServerResponse.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts +2 -2
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
- package/dist/unstable/persistence/Persistable.js +1 -1
- package/dist/unstable/persistence/Persistable.js.map +1 -1
- package/dist/unstable/rpc/RpcGroup.d.ts +1 -3
- package/dist/unstable/rpc/RpcGroup.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Channel.ts +3 -3
- package/src/Cron.ts +3 -3
- package/src/DateTime.ts +35 -77
- package/src/Duration.ts +78 -23
- package/src/Layer.ts +123 -19
- package/src/Schema.ts +2317 -115
- package/src/SchemaAST.ts +2 -2
- package/src/SchemaRepresentation.ts +2 -2
- package/src/internal/dateTime.ts +60 -51
- package/src/internal/effect.ts +1 -1
- package/src/unstable/ai/Chat.ts +11 -13
- package/src/unstable/ai/LanguageModel.ts +148 -97
- package/src/unstable/cli/Completions.ts +36 -0
- package/src/unstable/cli/GlobalFlag.ts +4 -5
- package/src/unstable/cli/index.ts +5 -0
- package/src/unstable/cluster/ClusterWorkflowEngine.ts +1 -1
- package/src/unstable/cluster/DeliverAt.ts +1 -1
- package/src/unstable/encoding/Ndjson.ts +17 -17
- package/src/unstable/http/HttpClientResponse.ts +2 -2
- package/src/unstable/http/HttpIncomingMessage.ts +1 -1
- package/src/unstable/http/HttpServerRequest.ts +4 -4
- package/src/unstable/http/HttpServerResponse.ts +2 -2
- package/src/unstable/httpapi/HttpApiBuilder.ts +4 -4
- package/src/unstable/persistence/Persistable.ts +1 -1
- package/src/unstable/rpc/RpcGroup.ts +3 -3
- package/dist/unstable/cli/internal/completions/Completions.d.ts +0 -2
- package/dist/unstable/cli/internal/completions/Completions.d.ts.map +0 -1
- package/dist/unstable/cli/internal/completions/Completions.js +0 -23
- package/dist/unstable/cli/internal/completions/Completions.js.map +0 -1
- package/src/unstable/cli/internal/completions/Completions.ts +0 -31
package/src/Schema.ts
CHANGED
|
@@ -1,4 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Define data shapes, validate unknown input, and transform values between formats.
|
|
3
|
+
*
|
|
4
|
+
* ## Mental model
|
|
5
|
+
*
|
|
6
|
+
* - **Schema** — a description of a data shape. Every schema carries a decoded
|
|
7
|
+
* *Type* (the value you work with) and an *Encoded* representation (the
|
|
8
|
+
* serialized form, e.g. JSON).
|
|
9
|
+
* - **Decoding** — turning unknown external data (API responses, form
|
|
10
|
+
* submissions, config files) into typed, validated values.
|
|
11
|
+
* - **Encoding** — turning typed values back into a serializable format.
|
|
12
|
+
* - **Codec** — a schema that tracks both Type and Encoded, so it can decode
|
|
13
|
+
* *and* encode. Most concrete schemas are Codecs.
|
|
14
|
+
* - **Check / Filter** — a constraint attached to a schema (e.g. `isMinLength`,
|
|
15
|
+
* `isGreaterThan`). Attach them with `.check(...)`.
|
|
16
|
+
* - **Transformation** — a pair of functions (decode + encode) that convert
|
|
17
|
+
* values between two schemas. Created with {@link decodeTo} / {@link encodeTo}.
|
|
18
|
+
* - **Annotation** — metadata attached to a schema (title, description, custom
|
|
19
|
+
* keys). Attach with `.annotate(...)`.
|
|
20
|
+
*
|
|
21
|
+
* ## Common tasks
|
|
22
|
+
*
|
|
23
|
+
* - Define a struct: {@link Struct}
|
|
24
|
+
* - Define a union: {@link Union}, {@link TaggedUnion}, {@link Literals}
|
|
25
|
+
* - Define an array: {@link Array}, {@link NonEmptyArray}
|
|
26
|
+
* - Define a record: {@link Record}
|
|
27
|
+
* - Define a tuple: {@link Tuple}, {@link TupleWithRest}
|
|
28
|
+
* - Validate unknown data synchronously: {@link decodeUnknownSync}
|
|
29
|
+
* - Validate unknown data (Effect): {@link decodeUnknownEffect}
|
|
30
|
+
* - Encode a value: {@link encodeUnknownSync}, {@link encodeUnknownEffect}
|
|
31
|
+
* - Type guard: {@link is}
|
|
32
|
+
* - Assertion: {@link asserts}
|
|
33
|
+
* - Add constraints: `.check(...)` with filters like {@link isMinLength},
|
|
34
|
+
* {@link isGreaterThan}, {@link isPattern}, {@link isUUID}
|
|
35
|
+
* - Transform between schemas: {@link decodeTo}, {@link encodeTo}
|
|
36
|
+
* - Add a default for missing keys: {@link withDecodingDefault}, {@link withDecodingDefaultKey}
|
|
37
|
+
* - Create branded types: {@link brand}
|
|
38
|
+
* - Define classes with validation: {@link Class}, {@link TaggedClass}
|
|
39
|
+
* - Define error classes: {@link ErrorClass}, {@link TaggedErrorClass}
|
|
40
|
+
* - Generate JSON Schema: {@link toJsonSchemaDocument}
|
|
41
|
+
* - Generate test data: {@link toArbitrary}
|
|
42
|
+
* - Derive equivalence: {@link toEquivalence}
|
|
43
|
+
*
|
|
44
|
+
* ## Gotchas
|
|
45
|
+
*
|
|
46
|
+
* - `Schema.optional` creates `T | undefined` (key can be missing *or*
|
|
47
|
+
* `undefined`). Use `Schema.optionalKey` for exact optional properties.
|
|
48
|
+
* - `decodeTo` is curried: use `from.pipe(Schema.decodeTo(to, ...))`.
|
|
49
|
+
* - `decodeUnknownSync` throws on failure. Use `decodeUnknownExit` or
|
|
50
|
+
* `decodeUnknownOption` for non-throwing alternatives.
|
|
51
|
+
* - Filters do not change the TypeScript type. Use {@link refine} or
|
|
52
|
+
* {@link brand} to narrow the type.
|
|
53
|
+
* - Recursive schemas require {@link suspend} to avoid infinite loops.
|
|
54
|
+
*
|
|
55
|
+
* ## Quickstart
|
|
56
|
+
*
|
|
57
|
+
* **Example** (Validate a user object)
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { Schema } from "effect"
|
|
61
|
+
*
|
|
62
|
+
* const User = Schema.Struct({
|
|
63
|
+
* name: Schema.String.check(Schema.isMinLength(1)),
|
|
64
|
+
* age: Schema.Number.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
65
|
+
* email: Schema.optionalKey(Schema.String)
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // Decode unknown input — throws on failure
|
|
69
|
+
* const user = Schema.decodeUnknownSync(User)({
|
|
70
|
+
* name: "Alice",
|
|
71
|
+
* age: 30
|
|
72
|
+
* })
|
|
73
|
+
*
|
|
74
|
+
* console.log(user)
|
|
75
|
+
* // { name: "Alice", age: 30 }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @see {@link Schema} — type-level view tracking only the decoded Type
|
|
79
|
+
* @see {@link Codec} — type-level view tracking both Type and Encoded
|
|
80
|
+
* @see {@link Struct} — define object shapes
|
|
81
|
+
* @see {@link decodeUnknownSync} — synchronous validation
|
|
82
|
+
* @see {@link decodeTo} — schema transformations
|
|
83
|
+
*
|
|
2
84
|
* @since 4.0.0
|
|
3
85
|
*/
|
|
4
86
|
|
|
@@ -58,29 +140,42 @@ import type { Unify } from "./Unify.ts"
|
|
|
58
140
|
const TypeId = InternalSchema.TypeId
|
|
59
141
|
|
|
60
142
|
/**
|
|
61
|
-
*
|
|
143
|
+
* Whether a schema field is required or optional within a struct.
|
|
144
|
+
*
|
|
145
|
+
* @see {@link optionalKey} — mark a struct field as optional
|
|
146
|
+
* @see {@link optional} — mark a struct field as optional with `| undefined`
|
|
62
147
|
*
|
|
63
148
|
* @since 4.0.0
|
|
64
149
|
*/
|
|
65
150
|
export type Optionality = "required" | "optional"
|
|
66
151
|
|
|
67
152
|
/**
|
|
68
|
-
*
|
|
153
|
+
* Whether a schema field is readonly or mutable within a struct.
|
|
154
|
+
*
|
|
155
|
+
* @see {@link mutableKey} — mark a struct field as mutable
|
|
69
156
|
*
|
|
70
157
|
* @since 4.0.0
|
|
71
158
|
*/
|
|
72
159
|
export type Mutability = "readonly" | "mutable"
|
|
73
160
|
|
|
74
161
|
/**
|
|
75
|
-
*
|
|
162
|
+
* Whether a schema field has a constructor default value.
|
|
163
|
+
*
|
|
164
|
+
* @see {@link withConstructorDefault} — add a default to a schema field
|
|
165
|
+
* @see {@link tag} — creates a literal field with a constructor default
|
|
76
166
|
*
|
|
77
167
|
* @since 4.0.0
|
|
78
168
|
*/
|
|
79
169
|
export type ConstructorDefault = "no-default" | "with-default"
|
|
80
170
|
|
|
81
171
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
172
|
+
* Options for `makeUnsafe` and Class constructors.
|
|
173
|
+
*
|
|
174
|
+
* When to use:
|
|
175
|
+
* - Pass `disableValidation: true` to skip validation when you trust the data.
|
|
176
|
+
* - Pass `parseOptions` to control error reporting behavior.
|
|
177
|
+
*
|
|
178
|
+
* @see {@link Bottom.makeUnsafe}
|
|
84
179
|
*
|
|
85
180
|
* @since 4.0.0
|
|
86
181
|
*/
|
|
@@ -96,15 +191,19 @@ export interface MakeOptions {
|
|
|
96
191
|
}
|
|
97
192
|
|
|
98
193
|
/**
|
|
99
|
-
* The base interface for all schemas
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
194
|
+
* The fully-parameterized base interface for all schemas. Exposes all 14 type
|
|
195
|
+
* parameters controlling type inference, mutability, optionality, services,
|
|
196
|
+
* and transformation behavior.
|
|
197
|
+
*
|
|
198
|
+
* When to use:
|
|
199
|
+
* - You are writing advanced generic schema utilities or performing schema
|
|
200
|
+
* introspection.
|
|
201
|
+
* - In user code, prefer {@link Schema}, {@link Codec}, {@link Decoder}, or
|
|
202
|
+
* {@link Encoder} instead.
|
|
103
203
|
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* transformation characteristics.
|
|
204
|
+
* @see {@link Top} — the existential "any schema" type (erased type params)
|
|
205
|
+
* @see {@link Schema} — tracks only the decoded Type
|
|
206
|
+
* @see {@link Codec} — tracks Type + Encoded
|
|
108
207
|
*
|
|
109
208
|
* @since 4.0.0
|
|
110
209
|
*/
|
|
@@ -159,6 +258,11 @@ export interface Bottom<
|
|
|
159
258
|
}
|
|
160
259
|
|
|
161
260
|
/**
|
|
261
|
+
* The schema type returned by {@link declareConstructor}, tracking the decoded
|
|
262
|
+
* type `T`, the encoded type `E`, and the list of type-parameter schemas
|
|
263
|
+
* `TypeParameters`.
|
|
264
|
+
*
|
|
265
|
+
* @category Constructors
|
|
162
266
|
* @since 4.0.0
|
|
163
267
|
*/
|
|
164
268
|
export interface declareConstructor<T, E, TypeParameters extends ReadonlyArray<Top>, Iso = T> extends
|
|
@@ -178,9 +282,51 @@ export interface declareConstructor<T, E, TypeParameters extends ReadonlyArray<T
|
|
|
178
282
|
}
|
|
179
283
|
|
|
180
284
|
/**
|
|
181
|
-
*
|
|
285
|
+
* Creates a schema for a **parametric** type (a generic container such as
|
|
286
|
+
* `Array<A>`, `Option<A>`, etc.) by accepting a list of type-parameter schemas
|
|
287
|
+
* and a decoder factory.
|
|
288
|
+
*
|
|
289
|
+
* The outer call `declareConstructor<T, E, Iso>()` fixes the decoded type `T`,
|
|
290
|
+
* the encoded type `E`, and the optional iso type. The inner call receives:
|
|
291
|
+
* - `typeParameters` — the concrete schemas for each type variable
|
|
292
|
+
* - `run` — a factory that, given resolved codecs for each type parameter,
|
|
293
|
+
* returns a parsing function `(u, ast, options) => Effect<T, Issue>`
|
|
294
|
+
* - `annotations` — optional metadata
|
|
295
|
+
*
|
|
296
|
+
* @see {@link declare} for creating schemas for non-parametric types.
|
|
297
|
+
*
|
|
298
|
+
* **Example** (Schema for a parametric `Box<A>` type)
|
|
299
|
+
*
|
|
300
|
+
* ```ts
|
|
301
|
+
* import { Effect, Schema } from "effect"
|
|
302
|
+
* import * as SchemaParser from "effect/SchemaParser"
|
|
303
|
+
* import * as Issue from "effect/SchemaIssue"
|
|
304
|
+
* import * as Option from "effect/Option"
|
|
182
305
|
*
|
|
183
|
-
*
|
|
306
|
+
* interface Box<A> {
|
|
307
|
+
* readonly value: A
|
|
308
|
+
* }
|
|
309
|
+
*
|
|
310
|
+
* const isBox = (u: unknown): u is Box<unknown> =>
|
|
311
|
+
* typeof u === "object" && u !== null && "value" in u
|
|
312
|
+
*
|
|
313
|
+
* const Box = <A extends Schema.Top>(item: A) =>
|
|
314
|
+
* Schema.declareConstructor<Box<A["Type"]>, Box<A["Encoded"]>>()(
|
|
315
|
+
* [item],
|
|
316
|
+
* ([itemCodec]) =>
|
|
317
|
+
* (u, ast, options) => {
|
|
318
|
+
* if (!isBox(u)) {
|
|
319
|
+
* return Effect.fail(new Issue.InvalidType(ast, Option.some(u)))
|
|
320
|
+
* }
|
|
321
|
+
* return Effect.map(
|
|
322
|
+
* SchemaParser.decodeUnknownEffect(itemCodec)(u.value, options),
|
|
323
|
+
* (value) => ({ value })
|
|
324
|
+
* )
|
|
325
|
+
* }
|
|
326
|
+
* )
|
|
327
|
+
*
|
|
328
|
+
* const schema = Box(Schema.Number)
|
|
329
|
+
* ```
|
|
184
330
|
*
|
|
185
331
|
* @category Constructors
|
|
186
332
|
* @since 4.0.0
|
|
@@ -206,16 +352,41 @@ export function declareConstructor<T, E = T, Iso = T>() {
|
|
|
206
352
|
}
|
|
207
353
|
|
|
208
354
|
/**
|
|
355
|
+
* The schema type returned by {@link declare}, representing a non-parametric
|
|
356
|
+
* opaque type `T` with no type parameters.
|
|
357
|
+
*
|
|
209
358
|
* @category Constructors
|
|
210
359
|
* @since 4.0.0
|
|
211
360
|
*/
|
|
212
361
|
export interface declare<T, Iso = T> extends declareConstructor<T, T, readonly [], Iso> {}
|
|
213
362
|
|
|
214
363
|
/**
|
|
215
|
-
*
|
|
364
|
+
* Creates a schema for a **non-parametric** opaque type using a type-guard
|
|
365
|
+
* function. The schema accepts any unknown value and succeeds when `is` returns
|
|
366
|
+
* `true`, failing with an `InvalidType` issue otherwise.
|
|
367
|
+
*
|
|
368
|
+
* Use this when the type has no type parameters. For parametric types such as
|
|
369
|
+
* `Option<A>` or `Array<A>`, use {@link declareConstructor} instead.
|
|
370
|
+
*
|
|
371
|
+
* **Example** (Schema for a custom `UserId` branded type)
|
|
372
|
+
*
|
|
373
|
+
* ```ts
|
|
374
|
+
* import { Schema } from "effect"
|
|
375
|
+
*
|
|
376
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
377
|
+
*
|
|
378
|
+
* const isUserId = (u: unknown): u is UserId =>
|
|
379
|
+
* typeof u === "string" && u.startsWith("user_")
|
|
380
|
+
*
|
|
381
|
+
* const UserId = Schema.declare<UserId>(isUserId, {
|
|
382
|
+
* title: "UserId",
|
|
383
|
+
* description: "A user identifier starting with 'user_'"
|
|
384
|
+
* })
|
|
385
|
+
* ```
|
|
216
386
|
*
|
|
217
387
|
* @see {@link declareConstructor} for creating schemas for parametric types.
|
|
218
388
|
*
|
|
389
|
+
* @category Constructors
|
|
219
390
|
* @since 4.0.0
|
|
220
391
|
*/
|
|
221
392
|
export function declare<T, Iso = T>(
|
|
@@ -233,8 +404,28 @@ export function declare<T, Iso = T>(
|
|
|
233
404
|
}
|
|
234
405
|
|
|
235
406
|
/**
|
|
236
|
-
*
|
|
237
|
-
* parameters.
|
|
407
|
+
* Widens a schema's type to the fully-parameterized {@link Bottom} interface,
|
|
408
|
+
* making all 14 type parameters visible to TypeScript.
|
|
409
|
+
*
|
|
410
|
+
* Normally, concrete schema interfaces (e.g. `Schema<string>`) hide most type
|
|
411
|
+
* parameters. `revealBottom` is useful when writing generic utilities that need
|
|
412
|
+
* to inspect or propagate the complete set of type parameters.
|
|
413
|
+
*
|
|
414
|
+
* **Example** (Inspecting all type parameters of a schema)
|
|
415
|
+
*
|
|
416
|
+
* ```ts
|
|
417
|
+
* import { Schema } from "effect"
|
|
418
|
+
*
|
|
419
|
+
* const schema = Schema.String
|
|
420
|
+
*
|
|
421
|
+
* // Widen to Bottom to access all 14 type parameters
|
|
422
|
+
* const bottom = Schema.revealBottom(schema)
|
|
423
|
+
*
|
|
424
|
+
* // `bottom` now exposes Type, Encoded, DecodingServices, EncodingServices,
|
|
425
|
+
* // ast, ~rebuild.out, ~type.make.in, Iso, ~type.parameters, etc.
|
|
426
|
+
* type T = typeof bottom["Type"] // string
|
|
427
|
+
* type E = typeof bottom["Encoded"] // string
|
|
428
|
+
* ```
|
|
238
429
|
*
|
|
239
430
|
* @since 4.0.0
|
|
240
431
|
*/
|
|
@@ -262,8 +453,24 @@ export function revealBottom<S extends Top>(
|
|
|
262
453
|
|
|
263
454
|
/**
|
|
264
455
|
* Adds metadata annotations to a schema without changing its runtime behavior.
|
|
265
|
-
*
|
|
266
|
-
*
|
|
456
|
+
* This is the pipeable (curried) counterpart of the `.annotate` method.
|
|
457
|
+
*
|
|
458
|
+
* Annotations provide extra context used by documentation generators, JSON
|
|
459
|
+
* Schema converters, error formatters, and other tooling. Common keys include
|
|
460
|
+
* `title`, `description`, `examples`, `message`, and `identifier`.
|
|
461
|
+
*
|
|
462
|
+
* **Example** (Adding a title and description)
|
|
463
|
+
*
|
|
464
|
+
* ```ts
|
|
465
|
+
* import { Schema } from "effect"
|
|
466
|
+
*
|
|
467
|
+
* const Age = Schema.Number.pipe(
|
|
468
|
+
* Schema.annotate({
|
|
469
|
+
* title: "Age",
|
|
470
|
+
* description: "A non-negative integer representing age in years"
|
|
471
|
+
* })
|
|
472
|
+
* )
|
|
473
|
+
* ```
|
|
267
474
|
*
|
|
268
475
|
* @category Annotations
|
|
269
476
|
* @since 4.0.0
|
|
@@ -275,9 +482,29 @@ export function annotate<S extends Top>(annotations: S["~annotate.in"]) {
|
|
|
275
482
|
}
|
|
276
483
|
|
|
277
484
|
/**
|
|
278
|
-
* Adds key-
|
|
279
|
-
*
|
|
280
|
-
*
|
|
485
|
+
* Adds key-level annotations to a schema field. This is the pipeable
|
|
486
|
+
* (curried) counterpart of the `.annotateKey` method.
|
|
487
|
+
*
|
|
488
|
+
* Key annotations apply to a field's position inside a `Struct` or `Tuple`
|
|
489
|
+
* rather than to the field's value type. They can carry a
|
|
490
|
+
* `messageMissingKey` to customise the error shown when the field is absent,
|
|
491
|
+
* as well as standard documentation fields such as `title`, `description`,
|
|
492
|
+
* and `examples`.
|
|
493
|
+
*
|
|
494
|
+
* **Example** (Custom missing-key message for a required field)
|
|
495
|
+
*
|
|
496
|
+
* ```ts
|
|
497
|
+
* import { Schema } from "effect"
|
|
498
|
+
*
|
|
499
|
+
* const schema = Schema.Struct({
|
|
500
|
+
* username: Schema.String.pipe(
|
|
501
|
+
* Schema.annotateKey({
|
|
502
|
+
* description: "The username used to log in",
|
|
503
|
+
* messageMissingKey: "Username is required"
|
|
504
|
+
* })
|
|
505
|
+
* )
|
|
506
|
+
* })
|
|
507
|
+
* ```
|
|
281
508
|
*
|
|
282
509
|
* @category Annotations
|
|
283
510
|
* @since 4.0.0
|
|
@@ -289,15 +516,17 @@ export function annotateKey<S extends Top>(annotations: Annotations.Key<S["Type"
|
|
|
289
516
|
}
|
|
290
517
|
|
|
291
518
|
/**
|
|
292
|
-
* The
|
|
519
|
+
* The existential "any schema" type — all type parameters are erased to `unknown`.
|
|
293
520
|
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
521
|
+
* Use `Top` as a constraint when writing generic utilities that must accept *any*
|
|
522
|
+
* schema regardless of its `Type`, `Encoded`, or service requirements. It is the
|
|
523
|
+
* widest possible schema type and therefore gives you the least static information.
|
|
297
524
|
*
|
|
298
|
-
*
|
|
299
|
-
* -
|
|
300
|
-
* -
|
|
525
|
+
* In user code prefer the narrower interfaces:
|
|
526
|
+
* - {@link Schema}`<T>` — when you only care about the decoded type
|
|
527
|
+
* - {@link Codec}`<T, E, RD, RE>` — when you need the encoded type and service requirements
|
|
528
|
+
* - {@link Decoder}`<T, RD>` — for decode-only APIs
|
|
529
|
+
* - {@link Encoder}`<E, RE>` — for encode-only APIs
|
|
301
530
|
*
|
|
302
531
|
* @since 4.0.0
|
|
303
532
|
*/
|
|
@@ -322,25 +551,53 @@ export interface Top extends
|
|
|
322
551
|
{}
|
|
323
552
|
|
|
324
553
|
/**
|
|
554
|
+
* Namespace of type-level helpers for {@link Schema}.
|
|
555
|
+
*
|
|
325
556
|
* @since 4.0.0
|
|
326
557
|
*/
|
|
327
558
|
export declare namespace Schema {
|
|
328
559
|
/**
|
|
560
|
+
* Extracts the decoded `Type` from a schema.
|
|
561
|
+
*
|
|
562
|
+
* **Example** (Extracting the decoded type)
|
|
563
|
+
*
|
|
564
|
+
* ```ts
|
|
565
|
+
* import { Schema } from "effect"
|
|
566
|
+
*
|
|
567
|
+
* const Person = Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
568
|
+
* type Person = Schema.Schema.Type<typeof Person>
|
|
569
|
+
* // { readonly name: string; readonly age: number }
|
|
570
|
+
* ```
|
|
571
|
+
*
|
|
329
572
|
* @since 4.0.0
|
|
330
573
|
*/
|
|
331
574
|
export type Type<S> = S extends Top ? S["Type"] : never
|
|
332
575
|
}
|
|
333
576
|
|
|
334
577
|
/**
|
|
335
|
-
* A typed view of a schema that tracks the decoded (output) type `T`.
|
|
578
|
+
* A typed view of a schema that tracks only the decoded (output) type `T`.
|
|
336
579
|
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
580
|
+
* Use `Schema<T>` as a constraint when you want to accept "any schema that
|
|
581
|
+
* decodes to `T`" and do not need to know or constrain the encoded
|
|
582
|
+
* representation, required services, or any other type parameters.
|
|
340
583
|
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
584
|
+
* This is a structural interface — concrete schema values are produced by the
|
|
585
|
+
* constructors in this module (e.g. {@link Struct}, {@link String}, {@link Number}).
|
|
586
|
+
* When you also need the encoded type or service requirements, use {@link Codec}.
|
|
587
|
+
*
|
|
588
|
+
* **Example** (Function that accepts any schema decoding to `string`)
|
|
589
|
+
*
|
|
590
|
+
* ```ts
|
|
591
|
+
* import { Schema } from "effect"
|
|
592
|
+
*
|
|
593
|
+
* declare function print(schema: Schema.Schema<string>): void
|
|
594
|
+
*
|
|
595
|
+
* print(Schema.String) // ok
|
|
596
|
+
* print(Schema.NonEmptyString) // ok
|
|
597
|
+
* ```
|
|
598
|
+
*
|
|
599
|
+
* @see {@link Codec} — also tracks Encoded, DecodingServices, EncodingServices
|
|
600
|
+
* @see {@link Schema.Type} — extract the decoded type at the type level
|
|
344
601
|
*
|
|
345
602
|
* @since 4.0.0
|
|
346
603
|
*/
|
|
@@ -350,22 +607,66 @@ export interface Schema<out T> extends Top {
|
|
|
350
607
|
}
|
|
351
608
|
|
|
352
609
|
/**
|
|
610
|
+
* Namespace of type-level helpers for {@link Codec}.
|
|
611
|
+
*
|
|
353
612
|
* @since 4.0.0
|
|
354
613
|
*/
|
|
355
614
|
export declare namespace Codec {
|
|
356
615
|
/**
|
|
616
|
+
* Extracts the encoded (`Encoded`) type from a schema.
|
|
617
|
+
*
|
|
618
|
+
* **Example** (Extracting the encoded type)
|
|
619
|
+
*
|
|
620
|
+
* ```ts
|
|
621
|
+
* import { Schema } from "effect"
|
|
622
|
+
*
|
|
623
|
+
* const schema = Schema.NumberFromString
|
|
624
|
+
* type Enc = Schema.Codec.Encoded<typeof schema>
|
|
625
|
+
* // string
|
|
626
|
+
* ```
|
|
627
|
+
*
|
|
357
628
|
* @since 4.0.0
|
|
358
629
|
*/
|
|
359
630
|
export type Encoded<S> = S extends Top ? S["Encoded"] : never
|
|
360
631
|
/**
|
|
632
|
+
* Extracts the Effect services required during *decoding* from a schema.
|
|
633
|
+
*
|
|
634
|
+
* **Example** (Checking decoding service requirements)
|
|
635
|
+
*
|
|
636
|
+
* ```ts
|
|
637
|
+
* import { Schema } from "effect"
|
|
638
|
+
*
|
|
639
|
+
* const schema = Schema.String
|
|
640
|
+
* type RD = Schema.Codec.DecodingServices<typeof schema>
|
|
641
|
+
* // never
|
|
642
|
+
* ```
|
|
643
|
+
*
|
|
361
644
|
* @since 4.0.0
|
|
362
645
|
*/
|
|
363
646
|
export type DecodingServices<S> = S extends Top ? S["DecodingServices"] : never
|
|
364
647
|
/**
|
|
648
|
+
* Extracts the Effect services required during *encoding* from a schema.
|
|
649
|
+
*
|
|
650
|
+
* **Example** (Checking encoding service requirements)
|
|
651
|
+
*
|
|
652
|
+
* ```ts
|
|
653
|
+
* import { Schema } from "effect"
|
|
654
|
+
*
|
|
655
|
+
* const schema = Schema.String
|
|
656
|
+
* type RE = Schema.Codec.EncodingServices<typeof schema>
|
|
657
|
+
* // never
|
|
658
|
+
* ```
|
|
659
|
+
*
|
|
365
660
|
* @since 4.0.0
|
|
366
661
|
*/
|
|
367
662
|
export type EncodingServices<S> = S extends Top ? S["EncodingServices"] : never
|
|
368
663
|
/**
|
|
664
|
+
* Converts a schema type into an assertion function signature. The resulting
|
|
665
|
+
* function narrows its argument to `I & S["Type"]`. Only schemas with
|
|
666
|
+
* `DecodingServices: never` (i.e. no required services) can be used here.
|
|
667
|
+
*
|
|
668
|
+
* Produced by {@link asserts}.
|
|
669
|
+
*
|
|
369
670
|
* @since 4.0.0
|
|
370
671
|
*/
|
|
371
672
|
export type ToAsserts<S extends Top & { readonly DecodingServices: never }> = <I>(
|
|
@@ -374,6 +675,18 @@ export declare namespace Codec {
|
|
|
374
675
|
}
|
|
375
676
|
|
|
376
677
|
/**
|
|
678
|
+
* A schema that additionally supports optic (lens/prism) operations.
|
|
679
|
+
*
|
|
680
|
+
* `Optic<T, Iso>` extends {@link Schema}`<T>` with an `Iso` type that
|
|
681
|
+
* describes the isomorphic counterpart used by the optic layer. Crucially,
|
|
682
|
+
* decoding and encoding require *no* Effect services (`DecodingServices` and
|
|
683
|
+
* `EncodingServices` are both `never`), which means the optic can operate
|
|
684
|
+
* purely without an Effect runtime.
|
|
685
|
+
*
|
|
686
|
+
* Most primitive schemas (e.g. `Schema.String`, `Schema.Number`) implement
|
|
687
|
+
* `Optic` automatically. You normally interact with this interface through
|
|
688
|
+
* {@link Optic_} utilities rather than constructing it directly.
|
|
689
|
+
*
|
|
377
690
|
* @since 4.0.0
|
|
378
691
|
*/
|
|
379
692
|
export interface Optic<out T, out Iso> extends Schema<T> {
|
|
@@ -384,16 +697,32 @@ export interface Optic<out T, out Iso> extends Schema<T> {
|
|
|
384
697
|
}
|
|
385
698
|
|
|
386
699
|
/**
|
|
387
|
-
* A schema that tracks
|
|
700
|
+
* A schema that tracks the decoded type `T`, the encoded type `E`, and the
|
|
701
|
+
* Effect services required during decoding (`RD`) and encoding (`RE`).
|
|
388
702
|
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
703
|
+
* Use `Codec<T, E, RD, RE>` when you need to preserve full type information
|
|
704
|
+
* about a schema — both what it decodes to and what it serializes from/to.
|
|
705
|
+
* Most concrete schemas produced by this module implement `Codec`.
|
|
706
|
+
*
|
|
707
|
+
* For APIs that only need one direction, prefer the narrower views:
|
|
708
|
+
* - {@link Decoder}`<T, RD>` — decode-only
|
|
709
|
+
* - {@link Encoder}`<E, RE>` — encode-only
|
|
710
|
+
* - {@link Schema}`<T>` — type-only (no encoded representation)
|
|
711
|
+
*
|
|
712
|
+
* **Example** (Accepting a codec that decodes to `number` from `string`)
|
|
713
|
+
*
|
|
714
|
+
* ```ts
|
|
715
|
+
* import { Schema } from "effect"
|
|
393
716
|
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
717
|
+
* declare function serialize<T>(codec: Schema.Codec<T, string>): string
|
|
718
|
+
*
|
|
719
|
+
* serialize(Schema.NumberFromString) // ok — decodes number, encoded as string
|
|
720
|
+
* ```
|
|
721
|
+
*
|
|
722
|
+
* @see {@link Codec.Encoded} — extract the encoded type
|
|
723
|
+
* @see {@link Codec.DecodingServices} — extract required decoding services
|
|
724
|
+
* @see {@link Codec.EncodingServices} — extract required encoding services
|
|
725
|
+
* @see {@link revealCodec} — helper to make TypeScript infer the full Codec type
|
|
397
726
|
*
|
|
398
727
|
* @since 4.0.0
|
|
399
728
|
*/
|
|
@@ -405,11 +734,22 @@ export interface Codec<out T, out E = T, out RD = never, out RE = never> extends
|
|
|
405
734
|
}
|
|
406
735
|
|
|
407
736
|
/**
|
|
408
|
-
* A
|
|
737
|
+
* A {@link Codec} view for APIs that only *decode* (parse/validate) values.
|
|
409
738
|
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
739
|
+
* Use `Decoder<T, RD>` to accept "any schema that can decode to `T`" without
|
|
740
|
+
* constraining or depending on the encoded representation (`Encoded` is
|
|
741
|
+
* `unknown`) or encoding services.
|
|
742
|
+
*
|
|
743
|
+
* **Example** (Function that only needs to decode)
|
|
744
|
+
*
|
|
745
|
+
* ```ts
|
|
746
|
+
* import { Schema } from "effect"
|
|
747
|
+
*
|
|
748
|
+
* declare function validate<T>(decoder: Schema.Decoder<T>): (input: unknown) => T
|
|
749
|
+
*
|
|
750
|
+
* validate(Schema.String) // ok
|
|
751
|
+
* validate(Schema.NumberFromString) // ok
|
|
752
|
+
* ```
|
|
413
753
|
*
|
|
414
754
|
* @since 4.0.0
|
|
415
755
|
*/
|
|
@@ -418,11 +758,22 @@ export interface Decoder<out T, out RD = never> extends Codec<T, unknown, RD, un
|
|
|
418
758
|
}
|
|
419
759
|
|
|
420
760
|
/**
|
|
421
|
-
* A
|
|
761
|
+
* A {@link Codec} view for APIs that only *encode* values.
|
|
422
762
|
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
763
|
+
* Use `Encoder<E, RE>` to accept "any schema that can encode to `E`" without
|
|
764
|
+
* constraining or depending on the decoded `Type` (`Type` is `unknown`) or
|
|
765
|
+
* decoding services.
|
|
766
|
+
*
|
|
767
|
+
* **Example** (Function that only needs to encode)
|
|
768
|
+
*
|
|
769
|
+
* ```ts
|
|
770
|
+
* import { Schema } from "effect"
|
|
771
|
+
*
|
|
772
|
+
* declare function serialize<E>(encoder: Schema.Encoder<E>): (value: unknown) => E
|
|
773
|
+
*
|
|
774
|
+
* serialize(Schema.String) // ok — encodes to string
|
|
775
|
+
* serialize(Schema.NumberFromString) // ok — encodes number to string
|
|
776
|
+
* ```
|
|
426
777
|
*
|
|
427
778
|
* @since 4.0.0
|
|
428
779
|
*/
|
|
@@ -431,6 +782,25 @@ export interface Encoder<out E, out RE = never> extends Codec<unknown, E, unknow
|
|
|
431
782
|
}
|
|
432
783
|
|
|
433
784
|
/**
|
|
785
|
+
* Identity function that widens a value to the full {@link Codec} interface,
|
|
786
|
+
* prompting TypeScript to infer all four type parameters (`T`, `E`, `RD`, `RE`).
|
|
787
|
+
*
|
|
788
|
+
* When a schema is stored in a variable typed as `Schema<T>` or `Top`, the
|
|
789
|
+
* encoded type and service requirements are erased. Passing the value through
|
|
790
|
+
* `revealCodec` recovers those parameters without any runtime cost.
|
|
791
|
+
*
|
|
792
|
+
* **Example** (Recovering encoded type from a schema variable)
|
|
793
|
+
*
|
|
794
|
+
* ```ts
|
|
795
|
+
* import { Schema } from "effect"
|
|
796
|
+
*
|
|
797
|
+
* const schema: Schema.Schema<number> = Schema.NumberFromString
|
|
798
|
+
*
|
|
799
|
+
* // Without revealCodec, Encoded is unknown
|
|
800
|
+
* const codec = Schema.revealCodec(schema)
|
|
801
|
+
* type Enc = typeof codec["Encoded"] // string
|
|
802
|
+
* ```
|
|
803
|
+
*
|
|
434
804
|
* @since 4.0.0
|
|
435
805
|
*/
|
|
436
806
|
export function revealCodec<T, E, RD, RE>(codec: Codec<T, E, RD, RE>) {
|
|
@@ -440,12 +810,30 @@ export function revealCodec<T, E, RD, RE>(codec: Codec<T, E, RD, RE>) {
|
|
|
440
810
|
const SchemaErrorTypeId = "~effect/Schema/SchemaError"
|
|
441
811
|
|
|
442
812
|
/**
|
|
443
|
-
*
|
|
813
|
+
* Error thrown (or returned as the error channel value) when schema decoding
|
|
814
|
+
* or encoding fails.
|
|
444
815
|
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
816
|
+
* The `issue` field contains a structured {@link Issue.Issue} tree describing
|
|
817
|
+
* every validation failure, including the path to the problematic value,
|
|
818
|
+
* expected types, and actual values received. `message` renders the issue tree
|
|
819
|
+
* as a human-readable string.
|
|
820
|
+
*
|
|
821
|
+
* Use {@link isSchemaError} to narrow an unknown value to `SchemaError`.
|
|
822
|
+
*
|
|
823
|
+
* **Example** (Catching a SchemaError)
|
|
824
|
+
*
|
|
825
|
+
* ```ts
|
|
826
|
+
* import { Schema } from "effect"
|
|
827
|
+
*
|
|
828
|
+
* try {
|
|
829
|
+
* Schema.decodeUnknownSync(Schema.Number)("not a number")
|
|
830
|
+
* } catch (err) {
|
|
831
|
+
* if (Schema.isSchemaError(err)) {
|
|
832
|
+
* console.log(err.message)
|
|
833
|
+
* // Expected number, actual "not a number"
|
|
834
|
+
* }
|
|
835
|
+
* }
|
|
836
|
+
* ```
|
|
449
837
|
*
|
|
450
838
|
* @since 4.0.0
|
|
451
839
|
*/
|
|
@@ -466,6 +854,22 @@ export class SchemaError {
|
|
|
466
854
|
}
|
|
467
855
|
|
|
468
856
|
/**
|
|
857
|
+
* Returns `true` if `u` is a {@link SchemaError}.
|
|
858
|
+
*
|
|
859
|
+
* **Example** (Type guard in a catch block)
|
|
860
|
+
*
|
|
861
|
+
* ```ts
|
|
862
|
+
* import { Schema } from "effect"
|
|
863
|
+
*
|
|
864
|
+
* try {
|
|
865
|
+
* Schema.decodeUnknownSync(Schema.Number)("oops")
|
|
866
|
+
* } catch (err) {
|
|
867
|
+
* if (Schema.isSchemaError(err)) {
|
|
868
|
+
* console.log(err._tag) // "SchemaError"
|
|
869
|
+
* }
|
|
870
|
+
* }
|
|
871
|
+
* ```
|
|
872
|
+
*
|
|
469
873
|
* @since 4.0.0
|
|
470
874
|
*/
|
|
471
875
|
export function isSchemaError(u: unknown): u is SchemaError {
|
|
@@ -714,6 +1118,11 @@ export const is = Parser.is
|
|
|
714
1118
|
export const asserts = Parser.asserts
|
|
715
1119
|
|
|
716
1120
|
/**
|
|
1121
|
+
* Decodes an `unknown` input against a schema, returning an `Effect` that
|
|
1122
|
+
* succeeds with the decoded value or fails with a {@link SchemaError}. Use this
|
|
1123
|
+
* when the input type is not statically known. Prefer {@link decodeEffect} when
|
|
1124
|
+
* the input is already typed as the schema's `Encoded` type.
|
|
1125
|
+
*
|
|
717
1126
|
* @category Decoding
|
|
718
1127
|
* @since 4.0.0
|
|
719
1128
|
*/
|
|
@@ -725,6 +1134,11 @@ export function decodeUnknownEffect<S extends Top>(schema: S) {
|
|
|
725
1134
|
}
|
|
726
1135
|
|
|
727
1136
|
/**
|
|
1137
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
1138
|
+
* returning an `Effect` that succeeds with the decoded value or fails with a
|
|
1139
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
1140
|
+
* input use {@link decodeUnknownEffect}.
|
|
1141
|
+
*
|
|
728
1142
|
* @category Decoding
|
|
729
1143
|
* @since 4.0.0
|
|
730
1144
|
*/
|
|
@@ -734,6 +1148,12 @@ export const decodeEffect: <S extends Top>(
|
|
|
734
1148
|
decodeUnknownEffect
|
|
735
1149
|
|
|
736
1150
|
/**
|
|
1151
|
+
* Decodes an `unknown` input against a schema synchronously, returning an
|
|
1152
|
+
* `Exit` that is either a `Success` with the decoded value or a `Failure` with
|
|
1153
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
1154
|
+
* `DecodingServices` requirement. Prefer {@link decodeExit} when the input is
|
|
1155
|
+
* already typed as the schema's `Encoded` type.
|
|
1156
|
+
*
|
|
737
1157
|
* @category Decoding
|
|
738
1158
|
* @since 4.0.0
|
|
739
1159
|
*/
|
|
@@ -745,6 +1165,12 @@ export function decodeUnknownExit<S extends Top & { readonly DecodingServices: n
|
|
|
745
1165
|
}
|
|
746
1166
|
|
|
747
1167
|
/**
|
|
1168
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
1169
|
+
* synchronously, returning an `Exit` that is either a `Success` with the
|
|
1170
|
+
* decoded value or a `Failure` with a {@link SchemaError}. Only usable with
|
|
1171
|
+
* schemas that have no `DecodingServices` requirement. For `unknown` input use
|
|
1172
|
+
* {@link decodeUnknownExit}.
|
|
1173
|
+
*
|
|
748
1174
|
* @category Decoding
|
|
749
1175
|
* @since 4.0.0
|
|
750
1176
|
*/
|
|
@@ -753,42 +1179,105 @@ export const decodeExit: <S extends Top & { readonly DecodingServices: never }>(
|
|
|
753
1179
|
) => (input: S["Encoded"], options?: AST.ParseOptions) => Exit_.Exit<S["Type"], SchemaError> = decodeUnknownExit
|
|
754
1180
|
|
|
755
1181
|
/**
|
|
1182
|
+
* Decodes an `unknown` input against a schema, returning an `Option` that is
|
|
1183
|
+
* `Some` with the decoded value on success or `None` on failure. Prefer this
|
|
1184
|
+
* over {@link decodeUnknownExit} or {@link decodeUnknownEffect} when you only
|
|
1185
|
+
* need to know whether decoding succeeded and don't need error details. For
|
|
1186
|
+
* typed input use {@link decodeOption}.
|
|
1187
|
+
*
|
|
756
1188
|
* @category Decoding
|
|
757
1189
|
* @since 4.0.0
|
|
758
1190
|
*/
|
|
759
1191
|
export const decodeUnknownOption = Parser.decodeUnknownOption
|
|
760
1192
|
|
|
761
1193
|
/**
|
|
1194
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
1195
|
+
* returning an `Option` that is `Some` with the decoded value on success or
|
|
1196
|
+
* `None` on failure. For `unknown` input use {@link decodeUnknownOption}.
|
|
1197
|
+
*
|
|
762
1198
|
* @category Decoding
|
|
763
1199
|
* @since 4.0.0
|
|
764
1200
|
*/
|
|
765
1201
|
export const decodeOption = Parser.decodeOption
|
|
766
1202
|
|
|
767
1203
|
/**
|
|
1204
|
+
* Decodes an `unknown` input against a schema, returning a `Promise` that
|
|
1205
|
+
* resolves with the decoded value or rejects with a {@link SchemaError}. Useful
|
|
1206
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
1207
|
+
* {@link decodePromise}.
|
|
1208
|
+
*
|
|
768
1209
|
* @category Decoding
|
|
769
1210
|
* @since 4.0.0
|
|
770
1211
|
*/
|
|
771
1212
|
export const decodeUnknownPromise = Parser.decodeUnknownPromise
|
|
772
1213
|
|
|
773
1214
|
/**
|
|
1215
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
1216
|
+
* returning a `Promise` that resolves with the decoded value or rejects with a
|
|
1217
|
+
* {@link SchemaError}. For `unknown` input use {@link decodeUnknownPromise}.
|
|
1218
|
+
*
|
|
774
1219
|
* @category Decoding
|
|
775
1220
|
* @since 4.0.0
|
|
776
1221
|
*/
|
|
777
1222
|
export const decodePromise = Parser.decodePromise
|
|
778
1223
|
|
|
779
1224
|
/**
|
|
1225
|
+
* Decodes an `unknown` input against a schema synchronously, throwing a
|
|
1226
|
+
* {@link SchemaError} on failure. Use this when you want to validate data at a
|
|
1227
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
1228
|
+
* non-throwing alternatives see {@link decodeUnknownOption},
|
|
1229
|
+
* {@link decodeUnknownExit}, or {@link decodeUnknownEffect}. For typed input
|
|
1230
|
+
* use {@link decodeSync}.
|
|
1231
|
+
*
|
|
1232
|
+
* **Example** (Decoding with a transformation schema)
|
|
1233
|
+
*
|
|
1234
|
+
* ```ts
|
|
1235
|
+
* import { Schema } from "effect"
|
|
1236
|
+
*
|
|
1237
|
+
* const NumberFromString = Schema.NumberFromString
|
|
1238
|
+
*
|
|
1239
|
+
* console.log(Schema.decodeUnknownSync(NumberFromString)("42"))
|
|
1240
|
+
* // Output: 42
|
|
1241
|
+
*
|
|
1242
|
+
* Schema.decodeUnknownSync(NumberFromString)("not a number")
|
|
1243
|
+
* // throws SchemaError: NumberFromString
|
|
1244
|
+
* // └─ Encoded side transformation failure
|
|
1245
|
+
* // └─ NumberFromString
|
|
1246
|
+
* // └─ Expected a numeric string, actual "not a number"
|
|
1247
|
+
* ```
|
|
1248
|
+
*
|
|
780
1249
|
* @category Decoding
|
|
781
1250
|
* @since 4.0.0
|
|
782
1251
|
*/
|
|
783
1252
|
export const decodeUnknownSync = Parser.decodeUnknownSync
|
|
784
1253
|
|
|
785
1254
|
/**
|
|
1255
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
1256
|
+
* synchronously, throwing a {@link SchemaError} on failure. For `unknown` input
|
|
1257
|
+
* use {@link decodeUnknownSync}.
|
|
1258
|
+
*
|
|
786
1259
|
* @category Decoding
|
|
787
1260
|
* @since 4.0.0
|
|
788
1261
|
*/
|
|
789
1262
|
export const decodeSync = Parser.decodeSync
|
|
790
1263
|
|
|
791
1264
|
/**
|
|
1265
|
+
* Encodes an `unknown` input against a schema, returning an `Effect` that
|
|
1266
|
+
* succeeds with the encoded value or fails with a {@link SchemaError}. Use this
|
|
1267
|
+
* when the input type is not statically known. Prefer {@link encodeEffect} when
|
|
1268
|
+
* the input is already typed as the schema's `Type`.
|
|
1269
|
+
*
|
|
1270
|
+
* **Example** (Encoding a value to a string)
|
|
1271
|
+
*
|
|
1272
|
+
* ```ts
|
|
1273
|
+
* import { Effect, Schema } from "effect"
|
|
1274
|
+
*
|
|
1275
|
+
* const NumberFromString = Schema.NumberFromString
|
|
1276
|
+
*
|
|
1277
|
+
* Effect.runPromise(Schema.encodeUnknownEffect(NumberFromString)(42)).then(console.log)
|
|
1278
|
+
* // Output: "42"
|
|
1279
|
+
* ```
|
|
1280
|
+
*
|
|
792
1281
|
* @category Encoding
|
|
793
1282
|
* @since 4.0.0
|
|
794
1283
|
*/
|
|
@@ -803,6 +1292,11 @@ export function encodeUnknownEffect<S extends Top>(schema: S) {
|
|
|
803
1292
|
}
|
|
804
1293
|
|
|
805
1294
|
/**
|
|
1295
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
1296
|
+
* `Effect` that succeeds with the encoded value or fails with a
|
|
1297
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
1298
|
+
* input use {@link encodeUnknownEffect}.
|
|
1299
|
+
*
|
|
806
1300
|
* @category Encoding
|
|
807
1301
|
* @since 4.0.0
|
|
808
1302
|
*/
|
|
@@ -812,6 +1306,12 @@ export const encodeEffect: <S extends Top>(
|
|
|
812
1306
|
encodeUnknownEffect
|
|
813
1307
|
|
|
814
1308
|
/**
|
|
1309
|
+
* Encodes an `unknown` input against a schema synchronously, returning an
|
|
1310
|
+
* `Exit` that is either a `Success` with the encoded value or a `Failure` with
|
|
1311
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
1312
|
+
* `EncodingServices` requirement. Prefer {@link encodeExit} when the input is
|
|
1313
|
+
* already typed as the schema's `Type`.
|
|
1314
|
+
*
|
|
815
1315
|
* @category Encoding
|
|
816
1316
|
* @since 4.0.0
|
|
817
1317
|
*/
|
|
@@ -823,6 +1323,12 @@ export function encodeUnknownExit<S extends Top & { readonly EncodingServices: n
|
|
|
823
1323
|
}
|
|
824
1324
|
|
|
825
1325
|
/**
|
|
1326
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
1327
|
+
* returning an `Exit` that is either a `Success` with the encoded value or a
|
|
1328
|
+
* `Failure` with a {@link SchemaError}. Only usable with schemas that have no
|
|
1329
|
+
* `EncodingServices` requirement. For `unknown` input use
|
|
1330
|
+
* {@link encodeUnknownExit}.
|
|
1331
|
+
*
|
|
826
1332
|
* @category Encoding
|
|
827
1333
|
* @since 4.0.0
|
|
828
1334
|
*/
|
|
@@ -831,36 +1337,66 @@ export const encodeExit: <S extends Top & { readonly EncodingServices: never }>(
|
|
|
831
1337
|
) => (input: S["Type"], options?: AST.ParseOptions) => Exit_.Exit<S["Encoded"], SchemaError> = encodeUnknownExit
|
|
832
1338
|
|
|
833
1339
|
/**
|
|
1340
|
+
* Encodes an `unknown` input against a schema, returning an `Option` that is
|
|
1341
|
+
* `Some` with the encoded value on success or `None` on failure. Prefer this
|
|
1342
|
+
* over {@link encodeUnknownExit} or {@link encodeUnknownEffect} when you only
|
|
1343
|
+
* need to know whether encoding succeeded and don't need error details. For
|
|
1344
|
+
* typed input use {@link encodeOption}.
|
|
1345
|
+
*
|
|
834
1346
|
* @category Encoding
|
|
835
1347
|
* @since 4.0.0
|
|
836
1348
|
*/
|
|
837
1349
|
export const encodeUnknownOption = Parser.encodeUnknownOption
|
|
838
1350
|
|
|
839
1351
|
/**
|
|
1352
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
1353
|
+
* `Option` that is `Some` with the encoded value on success or `None` on
|
|
1354
|
+
* failure. For `unknown` input use {@link encodeUnknownOption}.
|
|
1355
|
+
*
|
|
840
1356
|
* @category Encoding
|
|
841
1357
|
* @since 4.0.0
|
|
842
1358
|
*/
|
|
843
1359
|
export const encodeOption = Parser.encodeOption
|
|
844
1360
|
|
|
845
1361
|
/**
|
|
1362
|
+
* Encodes an `unknown` input against a schema, returning a `Promise` that
|
|
1363
|
+
* resolves with the encoded value or rejects with a {@link SchemaError}. Useful
|
|
1364
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
1365
|
+
* {@link encodePromise}.
|
|
1366
|
+
*
|
|
846
1367
|
* @category Encoding
|
|
847
1368
|
* @since 4.0.0
|
|
848
1369
|
*/
|
|
849
1370
|
export const encodeUnknownPromise = Parser.encodeUnknownPromise
|
|
850
1371
|
|
|
851
1372
|
/**
|
|
1373
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning a
|
|
1374
|
+
* `Promise` that resolves with the encoded value or rejects with a
|
|
1375
|
+
* {@link SchemaError}. For `unknown` input use {@link encodeUnknownPromise}.
|
|
1376
|
+
*
|
|
852
1377
|
* @category Encoding
|
|
853
1378
|
* @since 4.0.0
|
|
854
1379
|
*/
|
|
855
1380
|
export const encodePromise = Parser.encodePromise
|
|
856
1381
|
|
|
857
1382
|
/**
|
|
1383
|
+
* Encodes an `unknown` input against a schema synchronously, throwing a
|
|
1384
|
+
* {@link SchemaError} on failure. Use this when you want to serialize data at a
|
|
1385
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
1386
|
+
* non-throwing alternatives see {@link encodeUnknownOption},
|
|
1387
|
+
* {@link encodeUnknownExit}, or {@link encodeUnknownEffect}. For typed input
|
|
1388
|
+
* use {@link encodeSync}.
|
|
1389
|
+
*
|
|
858
1390
|
* @category Encoding
|
|
859
1391
|
* @since 4.0.0
|
|
860
1392
|
*/
|
|
861
1393
|
export const encodeUnknownSync = Parser.encodeUnknownSync
|
|
862
1394
|
|
|
863
1395
|
/**
|
|
1396
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
1397
|
+
* throwing a {@link SchemaError} on failure. For `unknown` input use
|
|
1398
|
+
* {@link encodeUnknownSync}.
|
|
1399
|
+
*
|
|
864
1400
|
* @category Encoding
|
|
865
1401
|
* @since 4.0.0
|
|
866
1402
|
*/
|
|
@@ -894,6 +1430,10 @@ export function isSchema(u: unknown): u is Top {
|
|
|
894
1430
|
}
|
|
895
1431
|
|
|
896
1432
|
/**
|
|
1433
|
+
* Schema type for an exact optional struct key. The key may be absent, but
|
|
1434
|
+
* when present must match the wrapped schema (no implicit `undefined`).
|
|
1435
|
+
* Produced by {@link optionalKey}.
|
|
1436
|
+
*
|
|
897
1437
|
* @since 4.0.0
|
|
898
1438
|
*/
|
|
899
1439
|
export interface optionalKey<S extends Top> extends
|
|
@@ -954,11 +1494,17 @@ interface requiredKeyLambda extends Lambda {
|
|
|
954
1494
|
}
|
|
955
1495
|
|
|
956
1496
|
/**
|
|
1497
|
+
* Reverses {@link optionalKey}, returning the inner required schema. Only
|
|
1498
|
+
* applicable to schemas already wrapped with `optionalKey`.
|
|
1499
|
+
*
|
|
957
1500
|
* @since 4.0.0
|
|
958
1501
|
*/
|
|
959
1502
|
export const requiredKey = Struct_.lambda<requiredKeyLambda>((self) => self.schema)
|
|
960
1503
|
|
|
961
1504
|
/**
|
|
1505
|
+
* Schema type for an optional struct key that also accepts `undefined`.
|
|
1506
|
+
* Equivalent to `optionalKey<UndefinedOr<S>>`. Produced by {@link optional}.
|
|
1507
|
+
*
|
|
962
1508
|
* @since 4.0.0
|
|
963
1509
|
*/
|
|
964
1510
|
export interface optional<S extends Top> extends optionalKey<UndefinedOr<S>> {}
|
|
@@ -969,27 +1515,26 @@ interface optionalLambda extends Lambda {
|
|
|
969
1515
|
}
|
|
970
1516
|
|
|
971
1517
|
/**
|
|
972
|
-
*
|
|
1518
|
+
* Marks a struct field as optional, allowing the key to be absent or
|
|
973
1519
|
* `undefined`.
|
|
974
1520
|
*
|
|
975
|
-
*
|
|
976
|
-
*
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
* - Can contain the specified schema type
|
|
1521
|
+
* explicitly set to `undefined`. Equivalent to `optionalKey(UndefinedOr(S))`.
|
|
1522
|
+
*
|
|
1523
|
+
* Use {@link optionalKey} instead if you want exact optional semantics (absent
|
|
1524
|
+
* only, not `undefined`).
|
|
980
1525
|
*
|
|
981
|
-
* **Example** (
|
|
1526
|
+
* **Example** (Optional field accepting undefined)
|
|
982
1527
|
*
|
|
983
1528
|
* ```ts
|
|
984
1529
|
* import { Schema } from "effect"
|
|
985
1530
|
*
|
|
986
1531
|
* const schema = Schema.Struct({
|
|
987
1532
|
* name: Schema.String,
|
|
988
|
-
* age: Schema.
|
|
1533
|
+
* age: Schema.optional(Schema.Number)
|
|
989
1534
|
* })
|
|
990
1535
|
*
|
|
991
|
-
* //
|
|
992
|
-
* type Person = typeof schema
|
|
1536
|
+
* // { readonly name: string; readonly age?: number | undefined }
|
|
1537
|
+
* type Person = typeof schema.Type
|
|
993
1538
|
* ```
|
|
994
1539
|
*
|
|
995
1540
|
* @since 4.0.0
|
|
@@ -1003,11 +1548,17 @@ interface requiredLambda extends Lambda {
|
|
|
1003
1548
|
}
|
|
1004
1549
|
|
|
1005
1550
|
/**
|
|
1551
|
+
* Reverses {@link optional}, returning the inner schema (unwrapping `UndefinedOr`).
|
|
1552
|
+
* Only applicable to schemas already wrapped with `optional`.
|
|
1553
|
+
*
|
|
1006
1554
|
* @since 4.0.0
|
|
1007
1555
|
*/
|
|
1008
1556
|
export const required = Struct_.lambda<requiredLambda>((self) => self.schema.members[0])
|
|
1009
1557
|
|
|
1010
1558
|
/**
|
|
1559
|
+
* Schema type for a mutable struct key. The key's property is writable.
|
|
1560
|
+
* Produced by {@link mutableKey}.
|
|
1561
|
+
*
|
|
1011
1562
|
* @since 4.0.0
|
|
1012
1563
|
*/
|
|
1013
1564
|
export interface mutableKey<S extends Top> extends
|
|
@@ -1039,6 +1590,9 @@ interface mutableKeyLambda extends Lambda {
|
|
|
1039
1590
|
}
|
|
1040
1591
|
|
|
1041
1592
|
/**
|
|
1593
|
+
* Makes a struct field mutable (removes the `readonly` modifier on the property).
|
|
1594
|
+
* Use {@link readonlyKey} to reverse.
|
|
1595
|
+
*
|
|
1042
1596
|
* @since 4.0.0
|
|
1043
1597
|
*/
|
|
1044
1598
|
export const mutableKey = Struct_.lambda<mutableKeyLambda>((schema) => make(AST.mutableKey(schema.ast), { schema }))
|
|
@@ -1050,11 +1604,17 @@ interface readonlyKeyLambda extends Lambda {
|
|
|
1050
1604
|
}
|
|
1051
1605
|
|
|
1052
1606
|
/**
|
|
1607
|
+
* Reverses {@link mutableKey}, returning the inner schema as readonly again.
|
|
1608
|
+
* Only applicable to schemas already wrapped with `mutableKey`.
|
|
1609
|
+
*
|
|
1053
1610
|
* @since 4.0.0
|
|
1054
1611
|
*/
|
|
1055
1612
|
export const readonlyKey = Struct_.lambda<readonlyKeyLambda>((self) => self.schema)
|
|
1056
1613
|
|
|
1057
1614
|
/**
|
|
1615
|
+
* Schema type that collapses a transformation schema to its decoded `Type` on
|
|
1616
|
+
* both sides (Type = Encoded = S["Type"]). Produced by {@link toType}.
|
|
1617
|
+
*
|
|
1058
1618
|
* @since 4.0.0
|
|
1059
1619
|
*/
|
|
1060
1620
|
export interface toType<S extends Top> extends
|
|
@@ -1085,11 +1645,17 @@ interface toTypeLambda extends Lambda {
|
|
|
1085
1645
|
}
|
|
1086
1646
|
|
|
1087
1647
|
/**
|
|
1648
|
+
* Extracts the type-side schema: sets `Encoded` to equal the decoded `Type`,
|
|
1649
|
+
* discarding the encoding transformation path.
|
|
1650
|
+
*
|
|
1088
1651
|
* @since 4.0.0
|
|
1089
1652
|
*/
|
|
1090
1653
|
export const toType = Struct_.lambda<toTypeLambda>((schema) => make(AST.toType(schema.ast), { schema }))
|
|
1091
1654
|
|
|
1092
1655
|
/**
|
|
1656
|
+
* Schema type that collapses a transformation schema to its `Encoded` side on
|
|
1657
|
+
* both sides (Type = Encoded = S["Encoded"]). Produced by {@link toEncoded}.
|
|
1658
|
+
*
|
|
1093
1659
|
* @since 4.0.0
|
|
1094
1660
|
*/
|
|
1095
1661
|
export interface toEncoded<S extends Top> extends
|
|
@@ -1120,6 +1686,9 @@ interface toEncodedLambda extends Lambda {
|
|
|
1120
1686
|
}
|
|
1121
1687
|
|
|
1122
1688
|
/**
|
|
1689
|
+
* Extracts the encoded-side schema: sets `Type` to equal the `Encoded`,
|
|
1690
|
+
* discarding the decoding transformation path.
|
|
1691
|
+
*
|
|
1123
1692
|
* @since 4.0.0
|
|
1124
1693
|
*/
|
|
1125
1694
|
export const toEncoded = Struct_.lambda<toEncodedLambda>((schema) => make(AST.toEncoded(schema.ast), { schema }))
|
|
@@ -1127,6 +1696,9 @@ export const toEncoded = Struct_.lambda<toEncodedLambda>((schema) => make(AST.to
|
|
|
1127
1696
|
const FlipTypeId = "~effect/Schema/flip"
|
|
1128
1697
|
|
|
1129
1698
|
/**
|
|
1699
|
+
* Schema type representing a flipped schema where `Type` and `Encoded` are
|
|
1700
|
+
* swapped. Produced by {@link flip}.
|
|
1701
|
+
*
|
|
1130
1702
|
* @since 4.0.0
|
|
1131
1703
|
*/
|
|
1132
1704
|
export interface flip<S extends Top> extends
|
|
@@ -1158,6 +1730,19 @@ function isFlip$(schema: Top): schema is flip<any> {
|
|
|
1158
1730
|
}
|
|
1159
1731
|
|
|
1160
1732
|
/**
|
|
1733
|
+
* Swaps the `Type` and `Encoded` of a schema, inverting the transformation
|
|
1734
|
+
* direction. Calling `flip` twice returns the original schema.
|
|
1735
|
+
*
|
|
1736
|
+
* **Example** (Flip a number-from-string schema)
|
|
1737
|
+
*
|
|
1738
|
+
* ```ts
|
|
1739
|
+
* import { Schema } from "effect"
|
|
1740
|
+
*
|
|
1741
|
+
* // NumberFromString: decodes string → number
|
|
1742
|
+
* const flipped = Schema.flip(Schema.NumberFromString)
|
|
1743
|
+
* // flipped: decodes number → string
|
|
1744
|
+
* ```
|
|
1745
|
+
*
|
|
1161
1746
|
* @since 4.0.0
|
|
1162
1747
|
*/
|
|
1163
1748
|
export function flip<S extends Top>(schema: S): S extends flip<infer F> ? F["~rebuild.out"] : flip<S>
|
|
@@ -1169,6 +1754,9 @@ export function flip<S extends Top>(schema: S): flip<S> {
|
|
|
1169
1754
|
}
|
|
1170
1755
|
|
|
1171
1756
|
/**
|
|
1757
|
+
* Represents a schema for a single literal value.
|
|
1758
|
+
*
|
|
1759
|
+
* @see {@link Literal} for the constructor function.
|
|
1172
1760
|
* @since 4.0.0
|
|
1173
1761
|
*/
|
|
1174
1762
|
export interface Literal<L extends AST.LiteralValue> extends Bottom<L, L, never, never, AST.Literal, Literal<L>> {
|
|
@@ -1178,6 +1766,16 @@ export interface Literal<L extends AST.LiteralValue> extends Bottom<L, L, never,
|
|
|
1178
1766
|
}
|
|
1179
1767
|
|
|
1180
1768
|
/**
|
|
1769
|
+
* Creates a schema for a single literal value (string, number, bigint, boolean, or null).
|
|
1770
|
+
*
|
|
1771
|
+
* **Example** (String literal)
|
|
1772
|
+
* ```ts
|
|
1773
|
+
* import { Schema } from "effect"
|
|
1774
|
+
*
|
|
1775
|
+
* const schema = Schema.Literal("hello")
|
|
1776
|
+
* // Type: Schema.Literal<"hello">
|
|
1777
|
+
* ```
|
|
1778
|
+
*
|
|
1181
1779
|
* @see {@link Literals} for a schema that represents a union of literals.
|
|
1182
1780
|
* @see {@link tag} for a schema that represents a literal value that can be
|
|
1183
1781
|
* used as a discriminator field in tagged unions and has a constructor default.
|
|
@@ -1197,6 +1795,8 @@ export function Literal<L extends AST.LiteralValue>(literal: L): Literal<L> {
|
|
|
1197
1795
|
}
|
|
1198
1796
|
|
|
1199
1797
|
/**
|
|
1798
|
+
* Namespace for {@link TemplateLiteral} helper types.
|
|
1799
|
+
*
|
|
1200
1800
|
* @since 4.0.0
|
|
1201
1801
|
*/
|
|
1202
1802
|
export declare namespace TemplateLiteral {
|
|
@@ -1237,6 +1837,10 @@ export declare namespace TemplateLiteral {
|
|
|
1237
1837
|
}
|
|
1238
1838
|
|
|
1239
1839
|
/**
|
|
1840
|
+
* Represents a schema that validates strings matching a template literal pattern.
|
|
1841
|
+
* The encoded type is a string formed by concatenating the parts.
|
|
1842
|
+
*
|
|
1843
|
+
* @see {@link TemplateLiteral} for the constructor function.
|
|
1240
1844
|
* @since 4.0.0
|
|
1241
1845
|
*/
|
|
1242
1846
|
export interface TemplateLiteral<Parts extends TemplateLiteral.Parts> extends
|
|
@@ -1258,6 +1862,18 @@ function templateLiteralFromParts<Parts extends TemplateLiteral.Parts>(parts: Pa
|
|
|
1258
1862
|
}
|
|
1259
1863
|
|
|
1260
1864
|
/**
|
|
1865
|
+
* Creates a schema that validates strings matching a template literal pattern. Each part can be
|
|
1866
|
+
* a literal string/number/bigint or a schema whose encoded type is a string, number, or bigint.
|
|
1867
|
+
*
|
|
1868
|
+
* **Example** (URL path pattern)
|
|
1869
|
+
* ```ts
|
|
1870
|
+
* import { Schema } from "effect"
|
|
1871
|
+
*
|
|
1872
|
+
* const schema = Schema.TemplateLiteral(["/user/", Schema.Number])
|
|
1873
|
+
* // matches strings like "/user/123", "/user/42", etc.
|
|
1874
|
+
* ```
|
|
1875
|
+
*
|
|
1876
|
+
* @see {@link TemplateLiteralParser} for a schema that also parses matched parts into a tuple.
|
|
1261
1877
|
* @since 4.0.0
|
|
1262
1878
|
*/
|
|
1263
1879
|
export function TemplateLiteral<const Parts extends TemplateLiteral.Parts>(parts: Parts): TemplateLiteral<Parts> {
|
|
@@ -1265,6 +1881,8 @@ export function TemplateLiteral<const Parts extends TemplateLiteral.Parts>(parts
|
|
|
1265
1881
|
}
|
|
1266
1882
|
|
|
1267
1883
|
/**
|
|
1884
|
+
* Namespace for {@link TemplateLiteralParser} helper types.
|
|
1885
|
+
*
|
|
1268
1886
|
* @since 4.0.0
|
|
1269
1887
|
*/
|
|
1270
1888
|
export declare namespace TemplateLiteralParser {
|
|
@@ -1281,6 +1899,10 @@ export declare namespace TemplateLiteralParser {
|
|
|
1281
1899
|
}
|
|
1282
1900
|
|
|
1283
1901
|
/**
|
|
1902
|
+
* Represents a schema that validates strings matching a template literal pattern and decodes
|
|
1903
|
+
* them into a tuple of typed values, one per schema part.
|
|
1904
|
+
*
|
|
1905
|
+
* @see {@link TemplateLiteralParser} for the constructor function.
|
|
1284
1906
|
* @since 4.0.0
|
|
1285
1907
|
*/
|
|
1286
1908
|
export interface TemplateLiteralParser<Parts extends TemplateLiteral.Parts> extends
|
|
@@ -1298,6 +1920,18 @@ export interface TemplateLiteralParser<Parts extends TemplateLiteral.Parts> exte
|
|
|
1298
1920
|
}
|
|
1299
1921
|
|
|
1300
1922
|
/**
|
|
1923
|
+
* Like {@link TemplateLiteral} but decodes the matched string into a readonly tuple of typed values,
|
|
1924
|
+
* one element per schema part.
|
|
1925
|
+
*
|
|
1926
|
+
* **Example** (Parse path parameters)
|
|
1927
|
+
* ```ts
|
|
1928
|
+
* import { Schema } from "effect"
|
|
1929
|
+
*
|
|
1930
|
+
* const schema = Schema.TemplateLiteralParser(["/user/", Schema.NumberFromString])
|
|
1931
|
+
* // decodes "/user/42" => readonly ["/user/", 42]
|
|
1932
|
+
* ```
|
|
1933
|
+
*
|
|
1934
|
+
* @see {@link TemplateLiteral} for a validation-only version that keeps the string encoded.
|
|
1301
1935
|
* @since 4.0.0
|
|
1302
1936
|
*/
|
|
1303
1937
|
export function TemplateLiteralParser<const Parts extends TemplateLiteral.Parts>(
|
|
@@ -1307,6 +1941,10 @@ export function TemplateLiteralParser<const Parts extends TemplateLiteral.Parts>
|
|
|
1307
1941
|
}
|
|
1308
1942
|
|
|
1309
1943
|
/**
|
|
1944
|
+
* Represents a schema derived from a TypeScript `const enum` or a plain enum object,
|
|
1945
|
+
* accepting any of its values.
|
|
1946
|
+
*
|
|
1947
|
+
* @see {@link Enum} for the constructor function.
|
|
1310
1948
|
* @since 4.0.0
|
|
1311
1949
|
*/
|
|
1312
1950
|
export interface Enum<A extends { [x: string]: string | number }>
|
|
@@ -1317,6 +1955,21 @@ export interface Enum<A extends { [x: string]: string | number }>
|
|
|
1317
1955
|
}
|
|
1318
1956
|
|
|
1319
1957
|
/**
|
|
1958
|
+
* Creates a schema from a TypeScript enum object. Validates that the input is one of the enum's values.
|
|
1959
|
+
*
|
|
1960
|
+
* **Example** (Direction enum)
|
|
1961
|
+
* ```ts
|
|
1962
|
+
* import { Schema } from "effect"
|
|
1963
|
+
*
|
|
1964
|
+
* enum Direction {
|
|
1965
|
+
* Up = "Up",
|
|
1966
|
+
* Down = "Down"
|
|
1967
|
+
* }
|
|
1968
|
+
*
|
|
1969
|
+
* const schema = Schema.Enum(Direction)
|
|
1970
|
+
* // accepts "Up" or "Down"
|
|
1971
|
+
* ```
|
|
1972
|
+
*
|
|
1320
1973
|
* @since 4.0.0
|
|
1321
1974
|
*/
|
|
1322
1975
|
export function Enum<A extends { [x: string]: string | number }>(enums: A): Enum<A> {
|
|
@@ -1331,6 +1984,9 @@ export function Enum<A extends { [x: string]: string | number }>(enums: A): Enum
|
|
|
1331
1984
|
}
|
|
1332
1985
|
|
|
1333
1986
|
/**
|
|
1987
|
+
* Schema for the `never` type. Always fails validation.
|
|
1988
|
+
*
|
|
1989
|
+
* @see {@link Never} for the schema value.
|
|
1334
1990
|
* @since 4.0.0
|
|
1335
1991
|
*/
|
|
1336
1992
|
export interface Never extends Bottom<never, never, never, never, AST.Never, Never> {
|
|
@@ -1338,11 +1994,16 @@ export interface Never extends Bottom<never, never, never, never, AST.Never, Nev
|
|
|
1338
1994
|
}
|
|
1339
1995
|
|
|
1340
1996
|
/**
|
|
1997
|
+
* Schema for the `never` type. Always fails validation — no value satisfies it.
|
|
1998
|
+
*
|
|
1341
1999
|
* @since 4.0.0
|
|
1342
2000
|
*/
|
|
1343
2001
|
export const Never: Never = make(AST.never)
|
|
1344
2002
|
|
|
1345
2003
|
/**
|
|
2004
|
+
* Schema for the `any` type. Accepts any value without validation.
|
|
2005
|
+
*
|
|
2006
|
+
* @see {@link Any} for the schema value.
|
|
1346
2007
|
* @since 4.0.0
|
|
1347
2008
|
*/
|
|
1348
2009
|
export interface Any extends Bottom<any, any, never, never, AST.Any, Any> {
|
|
@@ -1350,11 +2011,17 @@ export interface Any extends Bottom<any, any, never, never, AST.Any, Any> {
|
|
|
1350
2011
|
}
|
|
1351
2012
|
|
|
1352
2013
|
/**
|
|
2014
|
+
* Schema for the `any` type. Accepts any value without validation.
|
|
2015
|
+
*
|
|
2016
|
+
* @see {@link Unknown} for a safer alternative that uses `unknown`.
|
|
1353
2017
|
* @since 4.0.0
|
|
1354
2018
|
*/
|
|
1355
2019
|
export const Any: Any = make(AST.any)
|
|
1356
2020
|
|
|
1357
2021
|
/**
|
|
2022
|
+
* Schema for the `unknown` type. Accepts any value without validation.
|
|
2023
|
+
*
|
|
2024
|
+
* @see {@link Unknown} for the schema value.
|
|
1358
2025
|
* @since 4.0.0
|
|
1359
2026
|
*/
|
|
1360
2027
|
export interface Unknown extends Bottom<unknown, unknown, never, never, AST.Unknown, Unknown> {
|
|
@@ -1362,11 +2029,17 @@ export interface Unknown extends Bottom<unknown, unknown, never, never, AST.Unkn
|
|
|
1362
2029
|
}
|
|
1363
2030
|
|
|
1364
2031
|
/**
|
|
2032
|
+
* Schema for the `unknown` type. Accepts any value without validation.
|
|
2033
|
+
*
|
|
2034
|
+
* @see {@link Any} for the `any` variant.
|
|
1365
2035
|
* @since 4.0.0
|
|
1366
2036
|
*/
|
|
1367
2037
|
export const Unknown: Unknown = make(AST.unknown)
|
|
1368
2038
|
|
|
1369
2039
|
/**
|
|
2040
|
+
* Schema for the `null` literal. Validates that the input is strictly `null`.
|
|
2041
|
+
*
|
|
2042
|
+
* @see {@link Null} for the schema value.
|
|
1370
2043
|
* @since 4.0.0
|
|
1371
2044
|
*/
|
|
1372
2045
|
export interface Null extends Bottom<null, null, never, never, AST.Null, Null> {
|
|
@@ -1374,11 +2047,17 @@ export interface Null extends Bottom<null, null, never, never, AST.Null, Null> {
|
|
|
1374
2047
|
}
|
|
1375
2048
|
|
|
1376
2049
|
/**
|
|
2050
|
+
* Schema for the `null` literal. Validates that the input is strictly `null`.
|
|
2051
|
+
*
|
|
2052
|
+
* @see {@link NullOr} for a union with another schema.
|
|
1377
2053
|
* @since 4.0.0
|
|
1378
2054
|
*/
|
|
1379
2055
|
export const Null: Null = make(AST.null)
|
|
1380
2056
|
|
|
1381
2057
|
/**
|
|
2058
|
+
* Schema for the `undefined` literal. Validates that the input is strictly `undefined`.
|
|
2059
|
+
*
|
|
2060
|
+
* @see {@link Undefined} for the schema value.
|
|
1382
2061
|
* @since 4.0.0
|
|
1383
2062
|
*/
|
|
1384
2063
|
export interface Undefined extends Bottom<undefined, undefined, never, never, AST.Undefined, Undefined> {
|
|
@@ -1386,11 +2065,17 @@ export interface Undefined extends Bottom<undefined, undefined, never, never, AS
|
|
|
1386
2065
|
}
|
|
1387
2066
|
|
|
1388
2067
|
/**
|
|
2068
|
+
* Schema for the `undefined` literal. Validates that the input is strictly `undefined`.
|
|
2069
|
+
*
|
|
2070
|
+
* @see {@link UndefinedOr} for a union with another schema.
|
|
1389
2071
|
* @since 4.0.0
|
|
1390
2072
|
*/
|
|
1391
2073
|
export const Undefined: Undefined = make(AST.undefined)
|
|
1392
2074
|
|
|
1393
2075
|
/**
|
|
2076
|
+
* Schema for `string` values.
|
|
2077
|
+
*
|
|
2078
|
+
* @see {@link String} for the schema value.
|
|
1394
2079
|
* @since 4.0.0
|
|
1395
2080
|
*/
|
|
1396
2081
|
export interface String extends Bottom<string, string, never, never, AST.String, String> {
|
|
@@ -1398,13 +2083,16 @@ export interface String extends Bottom<string, string, never, never, AST.String,
|
|
|
1398
2083
|
}
|
|
1399
2084
|
|
|
1400
2085
|
/**
|
|
1401
|
-
*
|
|
2086
|
+
* Schema for `string` values. Validates that the input is `typeof` `"string"`.
|
|
1402
2087
|
*
|
|
1403
2088
|
* @since 4.0.0
|
|
1404
2089
|
*/
|
|
1405
2090
|
export const String: String = make(AST.string)
|
|
1406
2091
|
|
|
1407
2092
|
/**
|
|
2093
|
+
* Schema for `number` values, including `NaN`, `Infinity`, and `-Infinity`.
|
|
2094
|
+
*
|
|
2095
|
+
* @see {@link Number} for the schema value.
|
|
1408
2096
|
* @since 4.0.0
|
|
1409
2097
|
*/
|
|
1410
2098
|
export interface Number extends Bottom<number, number, never, never, AST.Number, Number> {
|
|
@@ -1412,18 +2100,22 @@ export interface Number extends Bottom<number, number, never, never, AST.Number,
|
|
|
1412
2100
|
}
|
|
1413
2101
|
|
|
1414
2102
|
/**
|
|
1415
|
-
*
|
|
2103
|
+
* Schema for `number` values, including `NaN`, `Infinity`, and `-Infinity`.
|
|
1416
2104
|
*
|
|
1417
2105
|
* **Default Json Serializer**
|
|
1418
2106
|
*
|
|
1419
|
-
* -
|
|
1420
|
-
* -
|
|
2107
|
+
* - Finite numbers are serialized as numbers.
|
|
2108
|
+
* - Non-finite values are serialized as strings (`"NaN"`, `"Infinity"`, `"-Infinity"`).
|
|
1421
2109
|
*
|
|
2110
|
+
* @see {@link Finite} for a schema that excludes non-finite values.
|
|
1422
2111
|
* @since 4.0.0
|
|
1423
2112
|
*/
|
|
1424
2113
|
export const Number: Number = make(AST.number)
|
|
1425
2114
|
|
|
1426
2115
|
/**
|
|
2116
|
+
* Schema for `boolean` values.
|
|
2117
|
+
*
|
|
2118
|
+
* @see {@link Boolean} for the schema value.
|
|
1427
2119
|
* @since 4.0.0
|
|
1428
2120
|
*/
|
|
1429
2121
|
export interface Boolean extends Bottom<boolean, boolean, never, never, AST.Boolean, Boolean> {
|
|
@@ -1431,7 +2123,7 @@ export interface Boolean extends Bottom<boolean, boolean, never, never, AST.Bool
|
|
|
1431
2123
|
}
|
|
1432
2124
|
|
|
1433
2125
|
/**
|
|
1434
|
-
*
|
|
2126
|
+
* Schema for `boolean` values. Validates that the input is `typeof` `"boolean"`.
|
|
1435
2127
|
*
|
|
1436
2128
|
* @category Boolean
|
|
1437
2129
|
* @since 4.0.0
|
|
@@ -1439,6 +2131,9 @@ export interface Boolean extends Bottom<boolean, boolean, never, never, AST.Bool
|
|
|
1439
2131
|
export const Boolean: Boolean = make(AST.boolean)
|
|
1440
2132
|
|
|
1441
2133
|
/**
|
|
2134
|
+
* Schema for `symbol` values.
|
|
2135
|
+
*
|
|
2136
|
+
* @see {@link Symbol} for the schema value.
|
|
1442
2137
|
* @since 4.0.0
|
|
1443
2138
|
*/
|
|
1444
2139
|
export interface Symbol extends Bottom<symbol, symbol, never, never, AST.Symbol, Symbol> {
|
|
@@ -1446,13 +2141,17 @@ export interface Symbol extends Bottom<symbol, symbol, never, never, AST.Symbol,
|
|
|
1446
2141
|
}
|
|
1447
2142
|
|
|
1448
2143
|
/**
|
|
1449
|
-
*
|
|
2144
|
+
* Schema for `symbol` values. Validates that the input is `typeof` `"symbol"`.
|
|
1450
2145
|
*
|
|
2146
|
+
* @see {@link UniqueSymbol} for a schema that matches a specific symbol.
|
|
1451
2147
|
* @since 4.0.0
|
|
1452
2148
|
*/
|
|
1453
2149
|
export const Symbol: Symbol = make(AST.symbol)
|
|
1454
2150
|
|
|
1455
2151
|
/**
|
|
2152
|
+
* Schema for `bigint` values.
|
|
2153
|
+
*
|
|
2154
|
+
* @see {@link BigInt} for the schema value.
|
|
1456
2155
|
* @since 4.0.0
|
|
1457
2156
|
*/
|
|
1458
2157
|
export interface BigInt extends Bottom<bigint, bigint, never, never, AST.BigInt, BigInt> {
|
|
@@ -1460,13 +2159,16 @@ export interface BigInt extends Bottom<bigint, bigint, never, never, AST.BigInt,
|
|
|
1460
2159
|
}
|
|
1461
2160
|
|
|
1462
2161
|
/**
|
|
1463
|
-
*
|
|
2162
|
+
* Schema for `bigint` values. Validates that the input is `typeof` `"bigint"`.
|
|
1464
2163
|
*
|
|
1465
2164
|
* @since 4.0.0
|
|
1466
2165
|
*/
|
|
1467
2166
|
export const BigInt: BigInt = make(AST.bigInt)
|
|
1468
2167
|
|
|
1469
2168
|
/**
|
|
2169
|
+
* Schema for the `void` type.
|
|
2170
|
+
*
|
|
2171
|
+
* @see {@link Void} for the schema value.
|
|
1470
2172
|
* @since 4.0.0
|
|
1471
2173
|
*/
|
|
1472
2174
|
export interface Void extends Bottom<void, void, never, never, AST.Void, Void> {
|
|
@@ -1474,13 +2176,16 @@ export interface Void extends Bottom<void, void, never, never, AST.Void, Void> {
|
|
|
1474
2176
|
}
|
|
1475
2177
|
|
|
1476
2178
|
/**
|
|
1477
|
-
*
|
|
2179
|
+
* Schema for the `void` type. Accepts `undefined` as the encoded value.
|
|
1478
2180
|
*
|
|
1479
2181
|
* @since 4.0.0
|
|
1480
2182
|
*/
|
|
1481
2183
|
export const Void: Void = make(AST.void)
|
|
1482
2184
|
|
|
1483
2185
|
/**
|
|
2186
|
+
* Schema for the `object` type keyword.
|
|
2187
|
+
*
|
|
2188
|
+
* @see {@link ObjectKeyword} for the schema value.
|
|
1484
2189
|
* @since 4.0.0
|
|
1485
2190
|
*/
|
|
1486
2191
|
export interface ObjectKeyword extends Bottom<object, object, never, never, AST.ObjectKeyword, ObjectKeyword> {
|
|
@@ -1488,13 +2193,17 @@ export interface ObjectKeyword extends Bottom<object, object, never, never, AST.
|
|
|
1488
2193
|
}
|
|
1489
2194
|
|
|
1490
2195
|
/**
|
|
1491
|
-
*
|
|
2196
|
+
* Schema for the `object` type. Validates that the input is a non-null object or function
|
|
2197
|
+
* (i.e. `typeof value === "object" && value !== null || typeof value === "function"`).
|
|
1492
2198
|
*
|
|
1493
2199
|
* @since 4.0.0
|
|
1494
2200
|
*/
|
|
1495
2201
|
export const ObjectKeyword: ObjectKeyword = make(AST.objectKeyword)
|
|
1496
2202
|
|
|
1497
2203
|
/**
|
|
2204
|
+
* Represents a schema for a specific unique symbol.
|
|
2205
|
+
*
|
|
2206
|
+
* @see {@link UniqueSymbol} for the constructor function.
|
|
1498
2207
|
* @since 4.0.0
|
|
1499
2208
|
*/
|
|
1500
2209
|
export interface UniqueSymbol<sym extends symbol>
|
|
@@ -1504,16 +2213,17 @@ export interface UniqueSymbol<sym extends symbol>
|
|
|
1504
2213
|
}
|
|
1505
2214
|
|
|
1506
2215
|
/**
|
|
1507
|
-
*
|
|
1508
|
-
*
|
|
1509
|
-
* **Example**
|
|
2216
|
+
* Creates a schema for a specific symbol. Only that exact symbol satisfies the schema.
|
|
1510
2217
|
*
|
|
2218
|
+
* **Example** (Specific symbol)
|
|
1511
2219
|
* ```ts
|
|
1512
2220
|
* import { Schema } from "effect"
|
|
1513
2221
|
*
|
|
1514
|
-
* const
|
|
1515
|
-
* const schema = Schema.UniqueSymbol(
|
|
2222
|
+
* const mySymbol = Symbol.for("mySymbol")
|
|
2223
|
+
* const schema = Schema.UniqueSymbol(mySymbol)
|
|
1516
2224
|
* ```
|
|
2225
|
+
*
|
|
2226
|
+
* @see {@link Symbol} for a schema that accepts any symbol.
|
|
1517
2227
|
* @since 4.0.0
|
|
1518
2228
|
*/
|
|
1519
2229
|
export function UniqueSymbol<const sym extends symbol>(symbol: sym): UniqueSymbol<sym> {
|
|
@@ -1521,10 +2231,24 @@ export function UniqueSymbol<const sym extends symbol>(symbol: sym): UniqueSymbo
|
|
|
1521
2231
|
}
|
|
1522
2232
|
|
|
1523
2233
|
/**
|
|
2234
|
+
* Namespace for struct field type utilities.
|
|
2235
|
+
*
|
|
2236
|
+
* These types compute the decoded `Type`, encoded `Encoded`, and constructor
|
|
2237
|
+
* input `MakeIn` of a {@link Struct} from its field map, handling optional,
|
|
2238
|
+
* mutable, and other field modifiers automatically.
|
|
2239
|
+
*
|
|
2240
|
+
* - `Struct.Fields` — constraint for the field map object
|
|
2241
|
+
* - `Struct.Type<F>` — decoded type of the struct
|
|
2242
|
+
* - `Struct.Encoded<F>` — encoded type of the struct
|
|
2243
|
+
* - `Struct.MakeIn<F>` — constructor input (optional/defaulted fields may be omitted)
|
|
2244
|
+
* - `Struct.DecodingServices<F>` / `Struct.EncodingServices<F>` — required services
|
|
2245
|
+
*
|
|
1524
2246
|
* @since 4.0.0
|
|
1525
2247
|
*/
|
|
1526
2248
|
export declare namespace Struct {
|
|
1527
2249
|
/**
|
|
2250
|
+
* Constraint for a struct field map: an object whose values are schemas.
|
|
2251
|
+
*
|
|
1528
2252
|
* @since 4.0.0
|
|
1529
2253
|
*/
|
|
1530
2254
|
export type Fields = { readonly [x: PropertyKey]: Top }
|
|
@@ -1699,6 +2423,34 @@ function makeStruct<const Fields extends Struct.Fields>(ast: AST.Objects, fields
|
|
|
1699
2423
|
}
|
|
1700
2424
|
|
|
1701
2425
|
/**
|
|
2426
|
+
* Defines a struct schema from a map of field schemas.
|
|
2427
|
+
*
|
|
2428
|
+
* Each field value is a schema. Use {@link optionalKey} or {@link optional} to
|
|
2429
|
+
* mark fields as optional, and {@link mutableKey} to mark them as mutable.
|
|
2430
|
+
*
|
|
2431
|
+
* The resulting schema's `Type` is a readonly object type with the fields'
|
|
2432
|
+
* decoded types. The `Encoded` form mirrors the field schemas' encoded types.
|
|
2433
|
+
*
|
|
2434
|
+
* **Example** (Basic struct)
|
|
2435
|
+
*
|
|
2436
|
+
* ```ts
|
|
2437
|
+
* import { Schema } from "effect"
|
|
2438
|
+
*
|
|
2439
|
+
* const Person = Schema.Struct({
|
|
2440
|
+
* name: Schema.String,
|
|
2441
|
+
* age: Schema.Number,
|
|
2442
|
+
* email: Schema.optionalKey(Schema.String)
|
|
2443
|
+
* })
|
|
2444
|
+
*
|
|
2445
|
+
* // { readonly name: string; readonly age: number; readonly email?: string }
|
|
2446
|
+
* type Person = typeof Person.Type
|
|
2447
|
+
*
|
|
2448
|
+
* const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
|
|
2449
|
+
* console.log(alice)
|
|
2450
|
+
* // { name: 'Alice', age: 30 }
|
|
2451
|
+
* ```
|
|
2452
|
+
*
|
|
2453
|
+
* @category Constructors
|
|
1702
2454
|
* @since 4.0.0
|
|
1703
2455
|
*/
|
|
1704
2456
|
export function Struct<const Fields extends Struct.Fields>(fields: Fields): Struct<Fields> {
|
|
@@ -1737,6 +2489,26 @@ export function fieldsAssign<const NewFields extends Struct.Fields>(fields: NewF
|
|
|
1737
2489
|
}
|
|
1738
2490
|
|
|
1739
2491
|
/**
|
|
2492
|
+
* Renames struct keys in the encoded form without changing the decoded type.
|
|
2493
|
+
*
|
|
2494
|
+
* Takes a partial mapping `{ decodedKey: encodedKey }` and produces a
|
|
2495
|
+
* transformation schema that decodes from the renamed keys and encodes back to
|
|
2496
|
+
* the renamed keys. Keys not present in the mapping are left unchanged.
|
|
2497
|
+
*
|
|
2498
|
+
* **Example** (Rename `name` to `full_name` in the encoded form)
|
|
2499
|
+
*
|
|
2500
|
+
* ```ts
|
|
2501
|
+
* import { Schema } from "effect"
|
|
2502
|
+
*
|
|
2503
|
+
* const Person = Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
2504
|
+
* const Encoded = Person.pipe(Schema.encodeKeys({ name: "full_name" }))
|
|
2505
|
+
*
|
|
2506
|
+
* // Decodes { full_name: "Alice", age: 30 } → { name: "Alice", age: 30 }
|
|
2507
|
+
* const alice = Schema.decodeUnknownSync(Encoded)({ full_name: "Alice", age: 30 })
|
|
2508
|
+
* console.log(alice)
|
|
2509
|
+
* // { name: 'Alice', age: 30 }
|
|
2510
|
+
* ```
|
|
2511
|
+
*
|
|
1740
2512
|
* @category Struct transformations
|
|
1741
2513
|
* @since 4.0.0
|
|
1742
2514
|
*/
|
|
@@ -1778,6 +2550,31 @@ export function encodeKeys<
|
|
|
1778
2550
|
}
|
|
1779
2551
|
|
|
1780
2552
|
/**
|
|
2553
|
+
* Adds derived fields to a struct schema during decoding.
|
|
2554
|
+
*
|
|
2555
|
+
* Each new field is derived from the decoded struct value via a function that
|
|
2556
|
+
* returns `Option`. On encoding the derived fields are stripped. This allows
|
|
2557
|
+
* computed or enriched fields to live in the decoded type without appearing in
|
|
2558
|
+
* the encoded form.
|
|
2559
|
+
*
|
|
2560
|
+
* **Example** (Add a computed `fullName` field)
|
|
2561
|
+
*
|
|
2562
|
+
* ```ts
|
|
2563
|
+
* import { Option, Schema } from "effect"
|
|
2564
|
+
*
|
|
2565
|
+
* const Person = Schema.Struct({ first: Schema.String, last: Schema.String })
|
|
2566
|
+
* const Extended = Person.pipe(
|
|
2567
|
+
* Schema.extendTo(
|
|
2568
|
+
* { fullName: Schema.String },
|
|
2569
|
+
* { fullName: (p) => Option.some(`${p.first} ${p.last}`) }
|
|
2570
|
+
* )
|
|
2571
|
+
* )
|
|
2572
|
+
*
|
|
2573
|
+
* const alice = Schema.decodeUnknownSync(Extended)({ first: "Alice", last: "Smith" })
|
|
2574
|
+
* console.log(alice.fullName)
|
|
2575
|
+
* // Alice Smith
|
|
2576
|
+
* ```
|
|
2577
|
+
*
|
|
1781
2578
|
* @since 4.0.0
|
|
1782
2579
|
* @experimental
|
|
1783
2580
|
*/
|
|
@@ -1819,6 +2616,12 @@ export function extendTo<S extends Struct<Struct.Fields>, const Fields extends S
|
|
|
1819
2616
|
}
|
|
1820
2617
|
|
|
1821
2618
|
/**
|
|
2619
|
+
* Namespace for `Record` type utilities.
|
|
2620
|
+
*
|
|
2621
|
+
* - `Record.Key` — constraint for the key schema (must encode to `PropertyKey`)
|
|
2622
|
+
* - `Record.Type<K, V>` — decoded type of the record
|
|
2623
|
+
* - `Record.Encoded<K, V>` — encoded type of the record
|
|
2624
|
+
*
|
|
1822
2625
|
* @since 4.0.0
|
|
1823
2626
|
*/
|
|
1824
2627
|
export declare namespace Record {
|
|
@@ -1886,6 +2689,9 @@ export declare namespace Record {
|
|
|
1886
2689
|
}
|
|
1887
2690
|
|
|
1888
2691
|
/**
|
|
2692
|
+
* Schema type for a key-value record (map) with a typed key and value schema.
|
|
2693
|
+
* Produced by {@link Record}.
|
|
2694
|
+
*
|
|
1889
2695
|
* @since 4.0.0
|
|
1890
2696
|
*/
|
|
1891
2697
|
export interface $Record<Key extends Record.Key, Value extends Top> extends
|
|
@@ -1906,6 +2712,24 @@ export interface $Record<Key extends Record.Key, Value extends Top> extends
|
|
|
1906
2712
|
}
|
|
1907
2713
|
|
|
1908
2714
|
/**
|
|
2715
|
+
* Defines a record (dictionary) schema with typed keys and values.
|
|
2716
|
+
*
|
|
2717
|
+
* **Example** (String-keyed record of numbers)
|
|
2718
|
+
*
|
|
2719
|
+
* ```ts
|
|
2720
|
+
* import { Schema } from "effect"
|
|
2721
|
+
*
|
|
2722
|
+
* const schema = Schema.Record(Schema.String, Schema.Number)
|
|
2723
|
+
*
|
|
2724
|
+
* // { readonly [x: string]: number }
|
|
2725
|
+
* type R = typeof schema.Type
|
|
2726
|
+
*
|
|
2727
|
+
* const result = Schema.decodeUnknownSync(schema)({ a: 1, b: 2 })
|
|
2728
|
+
* console.log(result)
|
|
2729
|
+
* // { a: 1, b: 2 }
|
|
2730
|
+
* ```
|
|
2731
|
+
*
|
|
2732
|
+
* @category Constructors
|
|
1909
2733
|
* @since 4.0.0
|
|
1910
2734
|
*/
|
|
1911
2735
|
export function Record<Key extends Record.Key, Value extends Top>(
|
|
@@ -1925,6 +2749,11 @@ export function Record<Key extends Record.Key, Value extends Top>(
|
|
|
1925
2749
|
}
|
|
1926
2750
|
|
|
1927
2751
|
/**
|
|
2752
|
+
* Namespace for `StructWithRest` type utilities.
|
|
2753
|
+
*
|
|
2754
|
+
* - `StructWithRest.Type<S, R>` — decoded type (struct type intersected with record types)
|
|
2755
|
+
* - `StructWithRest.Encoded<S, R>` — encoded type
|
|
2756
|
+
*
|
|
1928
2757
|
* @since 4.0.0
|
|
1929
2758
|
*/
|
|
1930
2759
|
export declare namespace StructWithRest {
|
|
@@ -1986,6 +2815,9 @@ export declare namespace StructWithRest {
|
|
|
1986
2815
|
}
|
|
1987
2816
|
|
|
1988
2817
|
/**
|
|
2818
|
+
* Schema type for a struct combined with one or more record schemas. Produced
|
|
2819
|
+
* by {@link StructWithRest}.
|
|
2820
|
+
*
|
|
1989
2821
|
* @since 4.0.0
|
|
1990
2822
|
*/
|
|
1991
2823
|
export interface StructWithRest<
|
|
@@ -2009,6 +2841,24 @@ export interface StructWithRest<
|
|
|
2009
2841
|
}
|
|
2010
2842
|
|
|
2011
2843
|
/**
|
|
2844
|
+
* Extends a struct schema with one or more record (index-signature) schemas,
|
|
2845
|
+
* producing a schema whose decoded type intersects the struct and all records.
|
|
2846
|
+
*
|
|
2847
|
+
* **Example** (Struct with string-indexed extra keys)
|
|
2848
|
+
*
|
|
2849
|
+
* ```ts
|
|
2850
|
+
* import { Schema } from "effect"
|
|
2851
|
+
*
|
|
2852
|
+
* const schema = Schema.StructWithRest(
|
|
2853
|
+
* Schema.Struct({ id: Schema.Number }),
|
|
2854
|
+
* [Schema.Record(Schema.String, Schema.String)]
|
|
2855
|
+
* )
|
|
2856
|
+
*
|
|
2857
|
+
* // { readonly id: number } & { readonly [x: string]: string }
|
|
2858
|
+
* type T = typeof schema.Type
|
|
2859
|
+
* ```
|
|
2860
|
+
*
|
|
2861
|
+
* @category Constructors
|
|
2012
2862
|
* @since 4.0.0
|
|
2013
2863
|
*/
|
|
2014
2864
|
export function StructWithRest<
|
|
@@ -2022,6 +2872,13 @@ export function StructWithRest<
|
|
|
2022
2872
|
}
|
|
2023
2873
|
|
|
2024
2874
|
/**
|
|
2875
|
+
* Namespace for `Tuple` type utilities.
|
|
2876
|
+
*
|
|
2877
|
+
* - `Tuple.Elements` — constraint for the element schema array
|
|
2878
|
+
* - `Tuple.Type<E>` — decoded tuple type
|
|
2879
|
+
* - `Tuple.Encoded<E>` — encoded tuple type
|
|
2880
|
+
* - `Tuple.MakeIn<E>` — constructor input tuple
|
|
2881
|
+
*
|
|
2025
2882
|
* @since 4.0.0
|
|
2026
2883
|
*/
|
|
2027
2884
|
export declare namespace Tuple {
|
|
@@ -2104,6 +2961,8 @@ export declare namespace Tuple {
|
|
|
2104
2961
|
}
|
|
2105
2962
|
|
|
2106
2963
|
/**
|
|
2964
|
+
* Schema type for a fixed-length tuple. Produced by {@link Tuple}.
|
|
2965
|
+
*
|
|
2107
2966
|
* @since 4.0.0
|
|
2108
2967
|
*/
|
|
2109
2968
|
export interface Tuple<Elements extends Tuple.Elements> extends
|
|
@@ -2159,6 +3018,20 @@ function makeTuple<Elements extends Tuple.Elements>(ast: AST.Arrays, elements: E
|
|
|
2159
3018
|
}
|
|
2160
3019
|
|
|
2161
3020
|
/**
|
|
3021
|
+
* Defines a fixed-length tuple schema from an array of element schemas.
|
|
3022
|
+
*
|
|
3023
|
+
* **Example** (Pair of string and number)
|
|
3024
|
+
*
|
|
3025
|
+
* ```ts
|
|
3026
|
+
* import { Schema } from "effect"
|
|
3027
|
+
*
|
|
3028
|
+
* const schema = Schema.Tuple([Schema.String, Schema.Number])
|
|
3029
|
+
*
|
|
3030
|
+
* const pair = Schema.decodeUnknownSync(schema)(["hello", 42])
|
|
3031
|
+
* console.log(pair)
|
|
3032
|
+
* // [ 'hello', 42 ]
|
|
3033
|
+
* ```
|
|
3034
|
+
*
|
|
2162
3035
|
* @category Constructors
|
|
2163
3036
|
* @since 4.0.0
|
|
2164
3037
|
*/
|
|
@@ -2167,6 +3040,13 @@ export function Tuple<const Elements extends ReadonlyArray<Top>>(elements: Eleme
|
|
|
2167
3040
|
}
|
|
2168
3041
|
|
|
2169
3042
|
/**
|
|
3043
|
+
* Namespace for `TupleWithRest` type utilities.
|
|
3044
|
+
*
|
|
3045
|
+
* - `TupleWithRest.TupleType` — constraint for the leading tuple schema
|
|
3046
|
+
* - `TupleWithRest.Rest` — the rest element schema(s)
|
|
3047
|
+
* - `TupleWithRest.Type<T, R>` — decoded type (fixed elements + rest)
|
|
3048
|
+
* - `TupleWithRest.Encoded<T, R>` — encoded type
|
|
3049
|
+
*
|
|
2170
3050
|
* @since 4.0.0
|
|
2171
3051
|
*/
|
|
2172
3052
|
export declare namespace TupleWithRest {
|
|
@@ -2232,6 +3112,9 @@ export declare namespace TupleWithRest {
|
|
|
2232
3112
|
}
|
|
2233
3113
|
|
|
2234
3114
|
/**
|
|
3115
|
+
* Schema type for a tuple with additional rest elements. Produced by
|
|
3116
|
+
* {@link TupleWithRest}.
|
|
3117
|
+
*
|
|
2235
3118
|
* @since 4.0.0
|
|
2236
3119
|
*/
|
|
2237
3120
|
export interface TupleWithRest<
|
|
@@ -2255,6 +3138,26 @@ export interface TupleWithRest<
|
|
|
2255
3138
|
}
|
|
2256
3139
|
|
|
2257
3140
|
/**
|
|
3141
|
+
* Extends a fixed-length tuple schema with rest elements, creating a variadic
|
|
3142
|
+
* tuple that starts with the fixed elements and ends with zero or more rest
|
|
3143
|
+
* elements.
|
|
3144
|
+
*
|
|
3145
|
+
* **Example** (Tuple with rest)
|
|
3146
|
+
*
|
|
3147
|
+
* ```ts
|
|
3148
|
+
* import { Schema } from "effect"
|
|
3149
|
+
*
|
|
3150
|
+
* // [string, number, ...boolean[]]
|
|
3151
|
+
* const schema = Schema.TupleWithRest(
|
|
3152
|
+
* Schema.Tuple([Schema.String, Schema.Number]),
|
|
3153
|
+
* [Schema.Boolean]
|
|
3154
|
+
* )
|
|
3155
|
+
*
|
|
3156
|
+
* const result = Schema.decodeUnknownSync(schema)(["hello", 1, true, false])
|
|
3157
|
+
* console.log(result)
|
|
3158
|
+
* // [ 'hello', 1, true, false ]
|
|
3159
|
+
* ```
|
|
3160
|
+
*
|
|
2258
3161
|
* @category Constructors
|
|
2259
3162
|
* @since 4.0.0
|
|
2260
3163
|
*/
|
|
@@ -2266,6 +3169,8 @@ export function TupleWithRest<S extends Tuple<Tuple.Elements>, const Rest extend
|
|
|
2266
3169
|
}
|
|
2267
3170
|
|
|
2268
3171
|
/**
|
|
3172
|
+
* Schema type for a `ReadonlyArray`. Produced by {@link Array}.
|
|
3173
|
+
*
|
|
2269
3174
|
* @since 4.0.0
|
|
2270
3175
|
*/
|
|
2271
3176
|
export interface $Array<S extends Top> extends
|
|
@@ -2290,12 +3195,28 @@ interface ArrayLambda extends Lambda {
|
|
|
2290
3195
|
}
|
|
2291
3196
|
|
|
2292
3197
|
/**
|
|
3198
|
+
* Defines a `ReadonlyArray` schema for a given element schema.
|
|
3199
|
+
*
|
|
3200
|
+
* **Example** (Array of strings)
|
|
3201
|
+
*
|
|
3202
|
+
* ```ts
|
|
3203
|
+
* import { Schema } from "effect"
|
|
3204
|
+
*
|
|
3205
|
+
* const schema = Schema.Array(Schema.String)
|
|
3206
|
+
*
|
|
3207
|
+
* const result = Schema.decodeUnknownSync(schema)(["a", "b", "c"])
|
|
3208
|
+
* console.log(result)
|
|
3209
|
+
* // [ 'a', 'b', 'c' ]
|
|
3210
|
+
* ```
|
|
3211
|
+
*
|
|
2293
3212
|
* @category Constructors
|
|
2294
3213
|
* @since 4.0.0
|
|
2295
3214
|
*/
|
|
2296
3215
|
export const Array = Struct_.lambda<ArrayLambda>((schema) => make(new AST.Arrays(false, [], [schema.ast]), { schema }))
|
|
2297
3216
|
|
|
2298
3217
|
/**
|
|
3218
|
+
* Schema type for a non-empty `ReadonlyArray`. Produced by {@link NonEmptyArray}.
|
|
3219
|
+
*
|
|
2299
3220
|
* @since 4.0.0
|
|
2300
3221
|
*/
|
|
2301
3222
|
export interface NonEmptyArray<S extends Top> extends
|
|
@@ -2320,6 +3241,20 @@ interface NonEmptyArrayLambda extends Lambda {
|
|
|
2320
3241
|
}
|
|
2321
3242
|
|
|
2322
3243
|
/**
|
|
3244
|
+
* Defines a non-empty `ReadonlyArray` schema — at least one element required.
|
|
3245
|
+
* Type is `readonly [T, ...T[]]`.
|
|
3246
|
+
*
|
|
3247
|
+
* **Example** (Non-empty array of numbers)
|
|
3248
|
+
*
|
|
3249
|
+
* ```ts
|
|
3250
|
+
* import { Schema } from "effect"
|
|
3251
|
+
*
|
|
3252
|
+
* const schema = Schema.NonEmptyArray(Schema.Number)
|
|
3253
|
+
*
|
|
3254
|
+
* Schema.decodeUnknownSync(schema)([1, 2, 3]) // ok
|
|
3255
|
+
* Schema.decodeUnknownSync(schema)([]) // throws
|
|
3256
|
+
* ```
|
|
3257
|
+
*
|
|
2323
3258
|
* @category Constructors
|
|
2324
3259
|
* @since 4.0.0
|
|
2325
3260
|
*/
|
|
@@ -2328,6 +3263,8 @@ export const NonEmptyArray = Struct_.lambda<NonEmptyArrayLambda>((schema) =>
|
|
|
2328
3263
|
)
|
|
2329
3264
|
|
|
2330
3265
|
/**
|
|
3266
|
+
* Schema type for an array with unique elements. Produced by {@link UniqueArray}.
|
|
3267
|
+
*
|
|
2331
3268
|
* @since 4.0.0
|
|
2332
3269
|
*/
|
|
2333
3270
|
export interface UniqueArray<S extends Top> extends $Array<S> {}
|
|
@@ -2346,6 +3283,9 @@ export function UniqueArray<S extends Top>(item: S): UniqueArray<S> {
|
|
|
2346
3283
|
}
|
|
2347
3284
|
|
|
2348
3285
|
/**
|
|
3286
|
+
* Schema type that makes array or tuple elements mutable (removes `readonly`).
|
|
3287
|
+
* Produced by {@link mutable}.
|
|
3288
|
+
*
|
|
2349
3289
|
* @since 4.0.0
|
|
2350
3290
|
*/
|
|
2351
3291
|
export interface mutable<S extends Top & { readonly "ast": AST.Arrays }> extends
|
|
@@ -2379,7 +3319,18 @@ interface mutableLambda extends Lambda {
|
|
|
2379
3319
|
}
|
|
2380
3320
|
|
|
2381
3321
|
/**
|
|
2382
|
-
* Makes
|
|
3322
|
+
* Makes an array or tuple schema mutable, removing the `readonly` modifier.
|
|
3323
|
+
*
|
|
3324
|
+
* **Example** (Mutable array)
|
|
3325
|
+
*
|
|
3326
|
+
* ```ts
|
|
3327
|
+
* import { Schema } from "effect"
|
|
3328
|
+
*
|
|
3329
|
+
* const schema = Schema.mutable(Schema.Array(Schema.Number))
|
|
3330
|
+
*
|
|
3331
|
+
* // number[] (mutable)
|
|
3332
|
+
* type T = typeof schema.Type
|
|
3333
|
+
* ```
|
|
2383
3334
|
*
|
|
2384
3335
|
* @since 4.0.0
|
|
2385
3336
|
*/
|
|
@@ -2388,6 +3339,8 @@ export const mutable = Struct_.lambda<mutableLambda>((schema) => {
|
|
|
2388
3339
|
})
|
|
2389
3340
|
|
|
2390
3341
|
/**
|
|
3342
|
+
* Schema type for a union of multiple schemas. Produced by {@link Union}.
|
|
3343
|
+
*
|
|
2391
3344
|
* @since 4.0.0
|
|
2392
3345
|
*/
|
|
2393
3346
|
export interface Union<Members extends ReadonlyArray<Top>> extends
|
|
@@ -2449,12 +3402,23 @@ function makeUnion<Members extends ReadonlyArray<Top>>(
|
|
|
2449
3402
|
}
|
|
2450
3403
|
|
|
2451
3404
|
/**
|
|
2452
|
-
* Creates a schema
|
|
3405
|
+
* Creates a union schema from an array of member schemas. Members are tested in
|
|
3406
|
+
* order; the first match is returned.
|
|
3407
|
+
*
|
|
3408
|
+
* Optionally, specify `mode`:
|
|
3409
|
+
* - `"anyOf"` (default) — matches if any member matches.
|
|
3410
|
+
* - `"oneOf"` — matches if exactly one member matches.
|
|
2453
3411
|
*
|
|
2454
|
-
*
|
|
3412
|
+
* **Example** (String or number union)
|
|
2455
3413
|
*
|
|
2456
|
-
*
|
|
2457
|
-
*
|
|
3414
|
+
* ```ts
|
|
3415
|
+
* import { Schema } from "effect"
|
|
3416
|
+
*
|
|
3417
|
+
* const schema = Schema.Union([Schema.String, Schema.Number])
|
|
3418
|
+
*
|
|
3419
|
+
* Schema.decodeUnknownSync(schema)("hello") // "hello"
|
|
3420
|
+
* Schema.decodeUnknownSync(schema)(42) // 42
|
|
3421
|
+
* ```
|
|
2458
3422
|
*
|
|
2459
3423
|
* @category Constructors
|
|
2460
3424
|
* @since 4.0.0
|
|
@@ -2467,6 +3431,9 @@ export function Union<const Members extends ReadonlyArray<Top>>(
|
|
|
2467
3431
|
}
|
|
2468
3432
|
|
|
2469
3433
|
/**
|
|
3434
|
+
* Represents a union schema of multiple literal values.
|
|
3435
|
+
*
|
|
3436
|
+
* @see {@link Literals} for the constructor function.
|
|
2470
3437
|
* @since 4.0.0
|
|
2471
3438
|
*/
|
|
2472
3439
|
export interface Literals<L extends ReadonlyArray<AST.LiteralValue>>
|
|
@@ -2488,6 +3455,16 @@ export interface Literals<L extends ReadonlyArray<AST.LiteralValue>>
|
|
|
2488
3455
|
}
|
|
2489
3456
|
|
|
2490
3457
|
/**
|
|
3458
|
+
* Creates a union schema from an array of literal values.
|
|
3459
|
+
*
|
|
3460
|
+
* **Example** (Status codes)
|
|
3461
|
+
* ```ts
|
|
3462
|
+
* import { Schema } from "effect"
|
|
3463
|
+
*
|
|
3464
|
+
* const schema = Schema.Literals(["active", "inactive", "pending"])
|
|
3465
|
+
* // accepts "active", "inactive", or "pending"
|
|
3466
|
+
* ```
|
|
3467
|
+
*
|
|
2491
3468
|
* @see {@link Literal} for a schema that represents a single literal.
|
|
2492
3469
|
* @category Constructors
|
|
2493
3470
|
* @since 4.0.0
|
|
@@ -2515,6 +3492,8 @@ export function Literals<const L extends ReadonlyArray<AST.LiteralValue>>(litera
|
|
|
2515
3492
|
}
|
|
2516
3493
|
|
|
2517
3494
|
/**
|
|
3495
|
+
* Schema type for `S | null`. Produced by {@link NullOr}.
|
|
3496
|
+
*
|
|
2518
3497
|
* @since 4.0.0
|
|
2519
3498
|
*/
|
|
2520
3499
|
export interface NullOr<S extends Top> extends Union<readonly [S, Null]> {}
|
|
@@ -2525,12 +3504,16 @@ interface NullOrLambda extends Lambda {
|
|
|
2525
3504
|
}
|
|
2526
3505
|
|
|
2527
3506
|
/**
|
|
3507
|
+
* Creates a union schema of `S | null`.
|
|
3508
|
+
*
|
|
2528
3509
|
* @category Constructors
|
|
2529
3510
|
* @since 4.0.0
|
|
2530
3511
|
*/
|
|
2531
3512
|
export const NullOr = Struct_.lambda<NullOrLambda>((self) => Union([self, Null]))
|
|
2532
3513
|
|
|
2533
3514
|
/**
|
|
3515
|
+
* Schema type for `S | undefined`. Produced by {@link UndefinedOr}.
|
|
3516
|
+
*
|
|
2534
3517
|
* @since 4.0.0
|
|
2535
3518
|
*/
|
|
2536
3519
|
export interface UndefinedOr<S extends Top> extends Union<readonly [S, Undefined]> {}
|
|
@@ -2541,12 +3524,15 @@ interface UndefinedOrLambda extends Lambda {
|
|
|
2541
3524
|
}
|
|
2542
3525
|
|
|
2543
3526
|
/**
|
|
3527
|
+
* Creates a union schema of `S | undefined`.
|
|
3528
|
+
*
|
|
2544
3529
|
* @category Constructors
|
|
2545
3530
|
* @since 4.0.0
|
|
2546
3531
|
*/
|
|
2547
3532
|
export const UndefinedOr = Struct_.lambda<UndefinedOrLambda>((self) => Union([self, Undefined]))
|
|
2548
3533
|
|
|
2549
3534
|
/**
|
|
3535
|
+
* Schema type for `S | null | undefined`. Produced by {@link NullishOr}.
|
|
2550
3536
|
* @since 4.0.0
|
|
2551
3537
|
*/
|
|
2552
3538
|
export interface NullishOr<S extends Top> extends Union<readonly [S, Null, Undefined]> {}
|
|
@@ -2557,12 +3543,14 @@ interface NullishOrLambda extends Lambda {
|
|
|
2557
3543
|
}
|
|
2558
3544
|
|
|
2559
3545
|
/**
|
|
3546
|
+
* Creates a union schema of `S | null | undefined`.
|
|
2560
3547
|
* @category Constructors
|
|
2561
3548
|
* @since 4.0.0
|
|
2562
3549
|
*/
|
|
2563
3550
|
export const NullishOr = Struct_.lambda<NullishOrLambda>((self) => Union([self, Null, Undefined]))
|
|
2564
3551
|
|
|
2565
3552
|
/**
|
|
3553
|
+
* Schema type wrapping a lazily-evaluated schema. Produced by {@link suspend}.
|
|
2566
3554
|
* @since 4.0.0
|
|
2567
3555
|
*/
|
|
2568
3556
|
export interface suspend<S extends Top> extends
|
|
@@ -2592,6 +3580,21 @@ export interface suspend<S extends Top> extends
|
|
|
2592
3580
|
* essential for creating recursive schemas where a schema references itself,
|
|
2593
3581
|
* preventing infinite recursion during schema definition.
|
|
2594
3582
|
*
|
|
3583
|
+
* **Example** (Recursive tree schema)
|
|
3584
|
+
* ```ts
|
|
3585
|
+
* import { Schema } from "effect"
|
|
3586
|
+
*
|
|
3587
|
+
* interface Tree {
|
|
3588
|
+
* readonly value: number
|
|
3589
|
+
* readonly children: ReadonlyArray<Tree>
|
|
3590
|
+
* }
|
|
3591
|
+
*
|
|
3592
|
+
* const Tree = Schema.Struct({
|
|
3593
|
+
* value: Schema.Number,
|
|
3594
|
+
* children: Schema.Array(Schema.suspend((): Schema.Codec<Tree> => Tree))
|
|
3595
|
+
* })
|
|
3596
|
+
* ```
|
|
3597
|
+
*
|
|
2595
3598
|
* @category Constructors
|
|
2596
3599
|
* @since 4.0.0
|
|
2597
3600
|
*/
|
|
@@ -2600,6 +3603,18 @@ export function suspend<S extends Top>(f: () => S): suspend<S> {
|
|
|
2600
3603
|
}
|
|
2601
3604
|
|
|
2602
3605
|
/**
|
|
3606
|
+
* Pipeable function that attaches one or more filter checks to a schema without
|
|
3607
|
+
* changing the TypeScript type.
|
|
3608
|
+
*
|
|
3609
|
+
* **Example** (Adding checks to a schema)
|
|
3610
|
+
* ```ts
|
|
3611
|
+
* import { Schema } from "effect"
|
|
3612
|
+
*
|
|
3613
|
+
* const AgeSchema = Schema.Number.pipe(
|
|
3614
|
+
* Schema.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(120))
|
|
3615
|
+
* )
|
|
3616
|
+
* ```
|
|
3617
|
+
*
|
|
2603
3618
|
* @category Filtering
|
|
2604
3619
|
* @since 4.0.0
|
|
2605
3620
|
*/
|
|
@@ -2608,6 +3623,9 @@ export function check<S extends Top>(...checks: readonly [AST.Check<S["Type"]>,
|
|
|
2608
3623
|
}
|
|
2609
3624
|
|
|
2610
3625
|
/**
|
|
3626
|
+
* The output type of {@link refine}, narrowing the schema's `Type` to `T` via a
|
|
3627
|
+
* type guard.
|
|
3628
|
+
*
|
|
2611
3629
|
* @since 4.0.0
|
|
2612
3630
|
*/
|
|
2613
3631
|
export interface refine<T extends S["Type"], S extends Top> extends
|
|
@@ -2634,6 +3652,9 @@ export interface refine<T extends S["Type"], S extends Top> extends
|
|
|
2634
3652
|
}
|
|
2635
3653
|
|
|
2636
3654
|
/**
|
|
3655
|
+
* Narrows the TypeScript type of a schema's output via a type guard predicate,
|
|
3656
|
+
* attaching the guard as a runtime filter check.
|
|
3657
|
+
*
|
|
2637
3658
|
* @category Filtering
|
|
2638
3659
|
* @since 4.0.0
|
|
2639
3660
|
*/
|
|
@@ -2648,6 +3669,9 @@ export function refine<S extends Top, T extends S["Type"]>(
|
|
|
2648
3669
|
type DistributeBrands<B> = UnionToIntersection<B extends infer U extends string ? Brand.Brand<U> : never>
|
|
2649
3670
|
|
|
2650
3671
|
/**
|
|
3672
|
+
* The output type of {@link brand}, intersecting the schema's `Type` with one or
|
|
3673
|
+
* more {@link Brand.Brand} tags.
|
|
3674
|
+
*
|
|
2651
3675
|
* @since 4.0.0
|
|
2652
3676
|
*/
|
|
2653
3677
|
export interface brand<S extends Top, B> extends
|
|
@@ -2675,7 +3699,8 @@ export interface brand<S extends Top, B> extends
|
|
|
2675
3699
|
}
|
|
2676
3700
|
|
|
2677
3701
|
/**
|
|
2678
|
-
* Adds a brand to a schema
|
|
3702
|
+
* Adds a nominal brand to a schema, intersecting the output type with
|
|
3703
|
+
* `Brand.Brand<B>` to prevent accidental mixing of structurally identical types.
|
|
2679
3704
|
*
|
|
2680
3705
|
* @category Branding
|
|
2681
3706
|
* @since 4.0.0
|
|
@@ -2686,7 +3711,10 @@ export function brand<B extends string>(identifier: B) {
|
|
|
2686
3711
|
}
|
|
2687
3712
|
|
|
2688
3713
|
/**
|
|
2689
|
-
* @
|
|
3714
|
+
* Creates a branded schema from a {@link Brand.Constructor}, applying the
|
|
3715
|
+
* constructor's checks and brand tag to the underlying schema.
|
|
3716
|
+
*
|
|
3717
|
+
* @category Branding
|
|
2690
3718
|
* @since 4.0.0
|
|
2691
3719
|
*/
|
|
2692
3720
|
export function fromBrand<A extends Brand.Brand<any>>(identifier: string, ctor: Brand.Constructor<A>) {
|
|
@@ -2698,6 +3726,12 @@ export function fromBrand<A extends Brand.Brand<any>>(identifier: string, ctor:
|
|
|
2698
3726
|
}
|
|
2699
3727
|
|
|
2700
3728
|
/**
|
|
3729
|
+
* A schema that wraps another schema and intercepts its decoding pipeline.
|
|
3730
|
+
*
|
|
3731
|
+
* The interceptor receives the full decoding `Effect` and may replace, modify,
|
|
3732
|
+
* or augment it — including adding service requirements via `RD`.
|
|
3733
|
+
*
|
|
3734
|
+
* @see {@link middlewareDecoding} for the constructor
|
|
2701
3735
|
* @since 4.0.0
|
|
2702
3736
|
*/
|
|
2703
3737
|
export interface middlewareDecoding<S extends Top, RD> extends
|
|
@@ -2724,6 +3758,25 @@ export interface middlewareDecoding<S extends Top, RD> extends
|
|
|
2724
3758
|
}
|
|
2725
3759
|
|
|
2726
3760
|
/**
|
|
3761
|
+
* Intercepts the decoding pipeline of a schema.
|
|
3762
|
+
*
|
|
3763
|
+
* The provided function receives the current decoding `Effect` and `ParseOptions`,
|
|
3764
|
+
* and returns a new `Effect` — potentially adding service requirements (`RD`),
|
|
3765
|
+
* recovering from errors, or augmenting the result.
|
|
3766
|
+
*
|
|
3767
|
+
* **Example** (Logging decode failures)
|
|
3768
|
+
*
|
|
3769
|
+
* ```ts
|
|
3770
|
+
* import { Effect, Schema } from "effect"
|
|
3771
|
+
*
|
|
3772
|
+
* const Logged = Schema.String.pipe(
|
|
3773
|
+
* Schema.middlewareDecoding((effect) =>
|
|
3774
|
+
* Effect.tapError(effect, (issue) => Effect.log("decode failed", issue))
|
|
3775
|
+
* )
|
|
3776
|
+
* )
|
|
3777
|
+
* ```
|
|
3778
|
+
*
|
|
3779
|
+
* @see {@link catchDecoding} for a simpler error-recovery variant
|
|
2727
3780
|
* @since 4.0.0
|
|
2728
3781
|
*/
|
|
2729
3782
|
export function middlewareDecoding<S extends Top, RD>(
|
|
@@ -2740,6 +3793,12 @@ export function middlewareDecoding<S extends Top, RD>(
|
|
|
2740
3793
|
}
|
|
2741
3794
|
|
|
2742
3795
|
/**
|
|
3796
|
+
* A schema that wraps another schema and intercepts its encoding pipeline.
|
|
3797
|
+
*
|
|
3798
|
+
* The interceptor receives the full encoding `Effect` and may replace, modify,
|
|
3799
|
+
* or augment it — including adding service requirements via `RE`.
|
|
3800
|
+
*
|
|
3801
|
+
* @see {@link middlewareEncoding} for the constructor
|
|
2743
3802
|
* @since 4.0.0
|
|
2744
3803
|
*/
|
|
2745
3804
|
export interface middlewareEncoding<S extends Top, RE> extends
|
|
@@ -2766,6 +3825,25 @@ export interface middlewareEncoding<S extends Top, RE> extends
|
|
|
2766
3825
|
}
|
|
2767
3826
|
|
|
2768
3827
|
/**
|
|
3828
|
+
* Intercepts the encoding pipeline of a schema.
|
|
3829
|
+
*
|
|
3830
|
+
* The provided function receives the current encoding `Effect` and `ParseOptions`,
|
|
3831
|
+
* and returns a new `Effect` — potentially adding service requirements (`RE`),
|
|
3832
|
+
* recovering from errors, or augmenting the result.
|
|
3833
|
+
*
|
|
3834
|
+
* **Example** (Logging encode failures)
|
|
3835
|
+
*
|
|
3836
|
+
* ```ts
|
|
3837
|
+
* import { Effect, Schema } from "effect"
|
|
3838
|
+
*
|
|
3839
|
+
* const Logged = Schema.String.pipe(
|
|
3840
|
+
* Schema.middlewareEncoding((effect) =>
|
|
3841
|
+
* Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
|
|
3842
|
+
* )
|
|
3843
|
+
* )
|
|
3844
|
+
* ```
|
|
3845
|
+
*
|
|
3846
|
+
* @see {@link catchEncoding} for a simpler error-recovery variant
|
|
2769
3847
|
* @since 4.0.0
|
|
2770
3848
|
*/
|
|
2771
3849
|
export function middlewareEncoding<S extends Top, RE>(
|
|
@@ -2782,6 +3860,22 @@ export function middlewareEncoding<S extends Top, RE>(
|
|
|
2782
3860
|
}
|
|
2783
3861
|
|
|
2784
3862
|
/**
|
|
3863
|
+
* Recovers from a decoding error by providing a fallback value.
|
|
3864
|
+
*
|
|
3865
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
3866
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
3867
|
+
*
|
|
3868
|
+
* **Example** (Returning a default on decode failure)
|
|
3869
|
+
*
|
|
3870
|
+
* ```ts
|
|
3871
|
+
* import { Effect, Option, Schema } from "effect"
|
|
3872
|
+
*
|
|
3873
|
+
* const schema = Schema.Number.pipe(
|
|
3874
|
+
* Schema.catchDecoding((_issue) => Effect.succeed(Option.some(0)))
|
|
3875
|
+
* )
|
|
3876
|
+
* ```
|
|
3877
|
+
*
|
|
3878
|
+
* @see {@link catchDecodingWithContext} to add service requirements to the handler
|
|
2785
3879
|
* @since 4.0.0
|
|
2786
3880
|
*/
|
|
2787
3881
|
export function catchDecoding<S extends Top>(
|
|
@@ -2791,6 +3885,8 @@ export function catchDecoding<S extends Top>(
|
|
|
2791
3885
|
}
|
|
2792
3886
|
|
|
2793
3887
|
/**
|
|
3888
|
+
* Like {@link catchDecoding}, but the handler may require Effect services (`R`).
|
|
3889
|
+
*
|
|
2794
3890
|
* @since 4.0.0
|
|
2795
3891
|
*/
|
|
2796
3892
|
export function catchDecodingWithContext<S extends Top, R = never>(
|
|
@@ -2801,6 +3897,12 @@ export function catchDecodingWithContext<S extends Top, R = never>(
|
|
|
2801
3897
|
}
|
|
2802
3898
|
|
|
2803
3899
|
/**
|
|
3900
|
+
* Recovers from an encoding error by providing a fallback value.
|
|
3901
|
+
*
|
|
3902
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
3903
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
3904
|
+
*
|
|
3905
|
+
* @see {@link catchEncodingWithContext} to add service requirements to the handler
|
|
2804
3906
|
* @since 4.0.0
|
|
2805
3907
|
*/
|
|
2806
3908
|
export function catchEncoding<S extends Top>(
|
|
@@ -2810,6 +3912,8 @@ export function catchEncoding<S extends Top>(
|
|
|
2810
3912
|
}
|
|
2811
3913
|
|
|
2812
3914
|
/**
|
|
3915
|
+
* Like {@link catchEncoding}, but the handler may require Effect services (`R`).
|
|
3916
|
+
*
|
|
2813
3917
|
* @since 4.0.0
|
|
2814
3918
|
*/
|
|
2815
3919
|
export function catchEncodingWithContext<S extends Top, R = never>(
|
|
@@ -2820,6 +3924,12 @@ export function catchEncodingWithContext<S extends Top, R = never>(
|
|
|
2820
3924
|
}
|
|
2821
3925
|
|
|
2822
3926
|
/**
|
|
3927
|
+
* The type produced by {@link decodeTo} when a custom transformation is provided.
|
|
3928
|
+
*
|
|
3929
|
+
* - `Type` is `To["Type"]`, `Encoded` is `From["Encoded"]`
|
|
3930
|
+
* - Decoding services from both `from` and `to` are combined
|
|
3931
|
+
*
|
|
3932
|
+
* @see {@link compose} for the passthrough (no transformation) variant
|
|
2823
3933
|
* @since 4.0.0
|
|
2824
3934
|
*/
|
|
2825
3935
|
export interface decodeTo<To extends Top, From extends Top, RD = never, RE = never> extends
|
|
@@ -2847,6 +3957,12 @@ export interface decodeTo<To extends Top, From extends Top, RD = never, RE = nev
|
|
|
2847
3957
|
}
|
|
2848
3958
|
|
|
2849
3959
|
/**
|
|
3960
|
+
* The type produced by {@link decodeTo} when called without a custom transformation (passthrough composition).
|
|
3961
|
+
*
|
|
3962
|
+
* Equivalent to {@link decodeTo} with `RD = never` and `RE = never`, meaning the schemas
|
|
3963
|
+
* are composed using their natural encoding/decoding chain.
|
|
3964
|
+
*
|
|
3965
|
+
* @see {@link decodeTo} for the transformation variant
|
|
2850
3966
|
* @since 4.0.0
|
|
2851
3967
|
*/
|
|
2852
3968
|
export interface compose<To extends Top, From extends Top> extends decodeTo<To, From> {}
|
|
@@ -2970,6 +4086,25 @@ export function decode<S extends Top, RD = never, RE = never>(transformation: {
|
|
|
2970
4086
|
}
|
|
2971
4087
|
|
|
2972
4088
|
/**
|
|
4089
|
+
* Like {@link decodeTo} but reverses the direction: the `from` schema acts as the target (decoded type)
|
|
4090
|
+
* and `to` acts as the encoded source.
|
|
4091
|
+
*
|
|
4092
|
+
* `encodeTo(to)(from)` is equivalent to `to.pipe(decodeTo(from))` — useful when it reads more
|
|
4093
|
+
* naturally to specify the encoded schema first.
|
|
4094
|
+
*
|
|
4095
|
+
* **Example** (Encode a number back to string)
|
|
4096
|
+
*
|
|
4097
|
+
* ```ts
|
|
4098
|
+
* import { Schema, SchemaGetter } from "effect"
|
|
4099
|
+
*
|
|
4100
|
+
* const NumberFromString = Schema.Number.pipe(
|
|
4101
|
+
* Schema.encodeTo(Schema.String, {
|
|
4102
|
+
* decode: SchemaGetter.transform((s: string) => Number(s)),
|
|
4103
|
+
* encode: SchemaGetter.transform((n: number) => String(n))
|
|
4104
|
+
* })
|
|
4105
|
+
* )
|
|
4106
|
+
* ```
|
|
4107
|
+
*
|
|
2973
4108
|
* @since 4.0.0
|
|
2974
4109
|
*/
|
|
2975
4110
|
export function encodeTo<To extends Top>(
|
|
@@ -2997,6 +4132,25 @@ export function encodeTo<To extends Top, From extends Top, RD = never, RE = neve
|
|
|
2997
4132
|
}
|
|
2998
4133
|
|
|
2999
4134
|
/**
|
|
4135
|
+
* Applies a transformation to a schema's encoded type, creating a new schema where encoding/decoding
|
|
4136
|
+
* operate on `S["Encoded"]` rather than `S["Type"]`.
|
|
4137
|
+
*
|
|
4138
|
+
* The `decode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during decoding),
|
|
4139
|
+
* and the `encode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during encoding).
|
|
4140
|
+
*
|
|
4141
|
+
* **Example** (Upper-casing encoded strings)
|
|
4142
|
+
*
|
|
4143
|
+
* ```ts
|
|
4144
|
+
* import { Schema, SchemaGetter } from "effect"
|
|
4145
|
+
*
|
|
4146
|
+
* const UpperFromLower = Schema.String.pipe(
|
|
4147
|
+
* Schema.encode({
|
|
4148
|
+
* decode: SchemaGetter.transform((s: string) => s.toLowerCase()),
|
|
4149
|
+
* encode: SchemaGetter.transform((s: string) => s.toUpperCase())
|
|
4150
|
+
* })
|
|
4151
|
+
* )
|
|
4152
|
+
* ```
|
|
4153
|
+
*
|
|
3000
4154
|
* @since 4.0.0
|
|
3001
4155
|
*/
|
|
3002
4156
|
export function encode<S extends Top, RD = never, RE = never>(transformation: {
|
|
@@ -3009,6 +4163,10 @@ export function encode<S extends Top, RD = never, RE = never>(transformation: {
|
|
|
3009
4163
|
}
|
|
3010
4164
|
|
|
3011
4165
|
/**
|
|
4166
|
+
* Constraint used to ensure a schema field does not already have a constructor default.
|
|
4167
|
+
*
|
|
4168
|
+
* Only schemas that satisfy this constraint can be passed to {@link withConstructorDefault}.
|
|
4169
|
+
*
|
|
3012
4170
|
* @since 4.0.0
|
|
3013
4171
|
*/
|
|
3014
4172
|
export interface WithoutConstructorDefault {
|
|
@@ -3016,6 +4174,9 @@ export interface WithoutConstructorDefault {
|
|
|
3016
4174
|
}
|
|
3017
4175
|
|
|
3018
4176
|
/**
|
|
4177
|
+
* The type produced by {@link withConstructorDefault} — a schema with `"~type.constructor.default": "with-default"`.
|
|
4178
|
+
*
|
|
4179
|
+
* @see {@link withConstructorDefault} for the constructor
|
|
3019
4180
|
* @since 4.0.0
|
|
3020
4181
|
*/
|
|
3021
4182
|
export interface withConstructorDefault<S extends Top & WithoutConstructorDefault> extends
|
|
@@ -3042,6 +4203,32 @@ export interface withConstructorDefault<S extends Top & WithoutConstructorDefaul
|
|
|
3042
4203
|
}
|
|
3043
4204
|
|
|
3044
4205
|
/**
|
|
4206
|
+
* Attaches a constructor default value to a schema field.
|
|
4207
|
+
*
|
|
4208
|
+
* The `defaultValue` function receives `Option.some(undefined)` when the field is
|
|
4209
|
+
* explicitly set to `undefined`, or `Option.none()` when the field is absent.
|
|
4210
|
+
* Return `Option.some(value)` to supply a default, or `Option.none()` to leave the
|
|
4211
|
+
* field required. An `Effect` may be returned for async or service-dependent defaults.
|
|
4212
|
+
*
|
|
4213
|
+
* Constructor defaults are applied only during `makeUnsafe` / `make`, not during
|
|
4214
|
+
* decoding or encoding.
|
|
4215
|
+
*
|
|
4216
|
+
* **Example** (Optional field with a static default)
|
|
4217
|
+
*
|
|
4218
|
+
* ```ts
|
|
4219
|
+
* import { Option, Schema } from "effect"
|
|
4220
|
+
*
|
|
4221
|
+
* const MySchema = Schema.Struct({
|
|
4222
|
+
* name: Schema.String.pipe(
|
|
4223
|
+
* Schema.optionalKey,
|
|
4224
|
+
* Schema.withConstructorDefault(() => Option.some("anonymous"))
|
|
4225
|
+
* )
|
|
4226
|
+
* })
|
|
4227
|
+
*
|
|
4228
|
+
* const value = MySchema.makeUnsafe({})
|
|
4229
|
+
* // value: { name: "anonymous" }
|
|
4230
|
+
* ```
|
|
4231
|
+
*
|
|
3045
4232
|
* @since 4.0.0
|
|
3046
4233
|
*/
|
|
3047
4234
|
export function withConstructorDefault<S extends Top & WithoutConstructorDefault>(
|
|
@@ -3060,11 +4247,20 @@ export function withConstructorDefault<S extends Top & WithoutConstructorDefault
|
|
|
3060
4247
|
}
|
|
3061
4248
|
|
|
3062
4249
|
/**
|
|
4250
|
+
* The type produced by {@link withDecodingDefaultKey} — a schema that decodes from an `optionalKey` encoded source.
|
|
4251
|
+
*
|
|
4252
|
+
* @see {@link withDecodingDefaultKey} for the constructor
|
|
3063
4253
|
* @since 4.0.0
|
|
3064
4254
|
*/
|
|
3065
4255
|
export interface withDecodingDefaultKey<S extends Top> extends decodeTo<S, optionalKey<toEncoded<S>>> {}
|
|
3066
4256
|
|
|
3067
4257
|
/**
|
|
4258
|
+
* Options for {@link withDecodingDefaultKey} and {@link withDecodingDefault}.
|
|
4259
|
+
*
|
|
4260
|
+
* - `encodingStrategy`:
|
|
4261
|
+
* - `"passthrough"` (default): pass the value through during encoding
|
|
4262
|
+
* - `"omit"`: omit the key from the encoded output
|
|
4263
|
+
*
|
|
3068
4264
|
* @since 4.0.0
|
|
3069
4265
|
*/
|
|
3070
4266
|
export type DecodingDefaultOptions = {
|
|
@@ -3078,6 +4274,27 @@ export type DecodingDefaultOptions = {
|
|
|
3078
4274
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
3079
4275
|
* - `omit`: Omit the value from the output.
|
|
3080
4276
|
*
|
|
4277
|
+
* Provides a default value for a missing key during decoding.
|
|
4278
|
+
*
|
|
4279
|
+
* When the key is absent from the input, `defaultValue()` is called to supply a value.
|
|
4280
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
4281
|
+
* - `"passthrough"` (default): include the value in the output
|
|
4282
|
+
* - `"omit"`: omit the key from the output
|
|
4283
|
+
*
|
|
4284
|
+
* **Example** (Default for a missing struct key)
|
|
4285
|
+
*
|
|
4286
|
+
* ```ts
|
|
4287
|
+
* import { Schema } from "effect"
|
|
4288
|
+
*
|
|
4289
|
+
* const MySchema = Schema.Struct({
|
|
4290
|
+
* name: Schema.String.pipe(Schema.withDecodingDefaultKey(() => "anonymous"))
|
|
4291
|
+
* })
|
|
4292
|
+
*
|
|
4293
|
+
* const result = Schema.decodeUnknownSync(MySchema)({})
|
|
4294
|
+
* // result: { name: "anonymous" }
|
|
4295
|
+
* ```
|
|
4296
|
+
*
|
|
4297
|
+
* @see {@link withDecodingDefault} for the `optional` (value-level) variant
|
|
3081
4298
|
* @since 4.0.0
|
|
3082
4299
|
*/
|
|
3083
4300
|
export function withDecodingDefaultKey<S extends Top>(
|
|
@@ -3094,6 +4311,9 @@ export function withDecodingDefaultKey<S extends Top>(
|
|
|
3094
4311
|
}
|
|
3095
4312
|
|
|
3096
4313
|
/**
|
|
4314
|
+
* The type produced by {@link withDecodingDefault} — a schema that decodes from an `optional` encoded source.
|
|
4315
|
+
*
|
|
4316
|
+
* @see {@link withDecodingDefault} for the constructor
|
|
3097
4317
|
* @since 4.0.0
|
|
3098
4318
|
*/
|
|
3099
4319
|
export interface withDecodingDefault<S extends Top> extends decodeTo<S, optional<toEncoded<S>>> {}
|
|
@@ -3105,6 +4325,30 @@ export interface withDecodingDefault<S extends Top> extends decodeTo<S, optional
|
|
|
3105
4325
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
3106
4326
|
* - `omit`: Omit the value from the output.
|
|
3107
4327
|
*
|
|
4328
|
+
* Provides a default value for an `optional` field during decoding.
|
|
4329
|
+
*
|
|
4330
|
+
* Similar to {@link withDecodingDefaultKey} but works on `optional` (value-level optional)
|
|
4331
|
+
* rather than `optionalKey` (key-level optional).
|
|
4332
|
+
*
|
|
4333
|
+
* When the value is `undefined` or absent, `defaultValue()` is called to supply a value.
|
|
4334
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
4335
|
+
* - `"passthrough"` (default): include the value in the output
|
|
4336
|
+
* - `"omit"`: omit the value from the output
|
|
4337
|
+
*
|
|
4338
|
+
* **Example** (Default for an optional field value)
|
|
4339
|
+
*
|
|
4340
|
+
* ```ts
|
|
4341
|
+
* import { Schema } from "effect"
|
|
4342
|
+
*
|
|
4343
|
+
* const MySchema = Schema.Struct({
|
|
4344
|
+
* name: Schema.String.pipe(Schema.optional, Schema.withDecodingDefault(() => "anonymous"))
|
|
4345
|
+
* })
|
|
4346
|
+
*
|
|
4347
|
+
* const result = Schema.decodeUnknownSync(MySchema)({ name: undefined })
|
|
4348
|
+
* // result: { name: "anonymous" }
|
|
4349
|
+
* ```
|
|
4350
|
+
*
|
|
4351
|
+
* @see {@link withDecodingDefaultKey} for the key-level variant
|
|
3108
4352
|
* @since 4.0.0
|
|
3109
4353
|
*/
|
|
3110
4354
|
export function withDecodingDefault<S extends Top>(
|
|
@@ -3121,19 +4365,34 @@ export function withDecodingDefault<S extends Top>(
|
|
|
3121
4365
|
}
|
|
3122
4366
|
|
|
3123
4367
|
/**
|
|
4368
|
+
* The type produced by {@link tag} — a literal schema with a constructor default.
|
|
4369
|
+
*
|
|
4370
|
+
* Used as the type of the `_tag` field in {@link TaggedStruct} and related helpers.
|
|
4371
|
+
*
|
|
4372
|
+
* @see {@link tag} for the constructor
|
|
3124
4373
|
* @since 4.0.0
|
|
3125
4374
|
*/
|
|
3126
4375
|
export interface tag<Tag extends AST.LiteralValue> extends withConstructorDefault<Literal<Tag>> {}
|
|
3127
4376
|
|
|
3128
4377
|
/**
|
|
3129
|
-
*
|
|
3130
|
-
*
|
|
4378
|
+
* Combines a {@link Literal} schema with {@link withConstructorDefault}, making it ideal
|
|
4379
|
+
* for discriminator fields in tagged unions. When constructing via `makeUnsafe`, the
|
|
4380
|
+
* `_tag` field can be omitted and will be filled automatically.
|
|
4381
|
+
*
|
|
4382
|
+
* **Example** (Discriminated union tag)
|
|
4383
|
+
*
|
|
4384
|
+
* ```ts
|
|
4385
|
+
* import { Schema } from "effect"
|
|
3131
4386
|
*
|
|
3132
|
-
*
|
|
3133
|
-
*
|
|
3134
|
-
*
|
|
3135
|
-
*
|
|
4387
|
+
* const A = Schema.Struct({ _tag: Schema.tag("A"), value: Schema.Number })
|
|
4388
|
+
*
|
|
4389
|
+
* // _tag is optional in makeUnsafe, auto-filled to "A"
|
|
4390
|
+
* const a = A.makeUnsafe({ value: 42 })
|
|
4391
|
+
* // a: { _tag: "A", value: 42 }
|
|
4392
|
+
* ```
|
|
3136
4393
|
*
|
|
4394
|
+
* @see {@link tagDefaultOmit} to also omit the tag during encoding
|
|
4395
|
+
* @see {@link TaggedStruct} for a shorthand that adds `_tag` automatically
|
|
3137
4396
|
* @since 4.0.0
|
|
3138
4397
|
*/
|
|
3139
4398
|
export function tag<Tag extends AST.LiteralValue>(literal: Tag): tag<Tag> {
|
|
@@ -3141,9 +4400,26 @@ export function tag<Tag extends AST.LiteralValue>(literal: Tag): tag<Tag> {
|
|
|
3141
4400
|
}
|
|
3142
4401
|
|
|
3143
4402
|
/**
|
|
3144
|
-
*
|
|
3145
|
-
* the
|
|
4403
|
+
* Like {@link tag}, but additionally omits the tag field from the encoded output.
|
|
4404
|
+
* Useful when the encoded form (e.g. JSON) does not include the discriminator key,
|
|
4405
|
+
* but the decoded type and constructor still need it.
|
|
3146
4406
|
*
|
|
4407
|
+
* **Example** (Tag omitted during encoding)
|
|
4408
|
+
*
|
|
4409
|
+
* ```ts
|
|
4410
|
+
* import { Schema } from "effect"
|
|
4411
|
+
*
|
|
4412
|
+
* const A = Schema.Struct({
|
|
4413
|
+
* _tag: Schema.tagDefaultOmit("A"),
|
|
4414
|
+
* value: Schema.Number
|
|
4415
|
+
* })
|
|
4416
|
+
*
|
|
4417
|
+
* // Encode strips the _tag field
|
|
4418
|
+
* const encoded = Schema.encodeUnknownSync(A)({ _tag: "A", value: 1 })
|
|
4419
|
+
* // encoded: { value: 1 }
|
|
4420
|
+
* ```
|
|
4421
|
+
*
|
|
4422
|
+
* @see {@link tag} for the variant that keeps the tag during encoding
|
|
3147
4423
|
* @since 4.0.0
|
|
3148
4424
|
*/
|
|
3149
4425
|
export function tagDefaultOmit<Tag extends AST.LiteralValue>(literal: Tag) {
|
|
@@ -3151,6 +4427,9 @@ export function tagDefaultOmit<Tag extends AST.LiteralValue>(literal: Tag) {
|
|
|
3151
4427
|
}
|
|
3152
4428
|
|
|
3153
4429
|
/**
|
|
4430
|
+
* The type produced by {@link TaggedStruct} — a {@link Struct} with an extra `_tag` field of type {@link tag}.
|
|
4431
|
+
*
|
|
4432
|
+
* @see {@link TaggedStruct} for the constructor
|
|
3154
4433
|
* @since 4.0.0
|
|
3155
4434
|
*/
|
|
3156
4435
|
export type TaggedStruct<Tag extends AST.LiteralValue, Fields extends Struct.Fields> = Struct<
|
|
@@ -3244,6 +4523,9 @@ type TaggedUnionUtils<
|
|
|
3244
4523
|
}
|
|
3245
4524
|
|
|
3246
4525
|
/**
|
|
4526
|
+
* The type produced by {@link toTaggedUnion} — a {@link Union} augmented with `cases`, `guards`, `isAnyOf`, and `match` utilities.
|
|
4527
|
+
*
|
|
4528
|
+
* @see {@link toTaggedUnion} for the constructor
|
|
3247
4529
|
* @since 4.0.0
|
|
3248
4530
|
* @experimental
|
|
3249
4531
|
*/
|
|
@@ -3253,6 +4535,26 @@ export type toTaggedUnion<
|
|
|
3253
4535
|
> = Union<Members> & TaggedUnionUtils<Tag, Members>
|
|
3254
4536
|
|
|
3255
4537
|
/**
|
|
4538
|
+
* Augments an existing {@link Union} of tagged structs with utility methods keyed by the discriminant field.
|
|
4539
|
+
*
|
|
4540
|
+
* **Example** (Adding tagged-union utilities to an existing union)
|
|
4541
|
+
*
|
|
4542
|
+
* ```ts
|
|
4543
|
+
* import { Schema } from "effect"
|
|
4544
|
+
*
|
|
4545
|
+
* const A = Schema.TaggedStruct("A", { value: Schema.Number })
|
|
4546
|
+
* const B = Schema.TaggedStruct("B", { name: Schema.String })
|
|
4547
|
+
*
|
|
4548
|
+
* const MyUnion = Schema.Union([A, B]).pipe(Schema.toTaggedUnion("_tag"))
|
|
4549
|
+
*
|
|
4550
|
+
* // Pattern-match on the union
|
|
4551
|
+
* const result = MyUnion.match({ _tag: "A", value: 1 }, {
|
|
4552
|
+
* A: (a) => `number: ${a.value}`,
|
|
4553
|
+
* B: (b) => `name: ${b.name}`
|
|
4554
|
+
* })
|
|
4555
|
+
* ```
|
|
4556
|
+
*
|
|
4557
|
+
* @see {@link TaggedUnion} for a shorthand that builds the union from scratch
|
|
3256
4558
|
* @since 4.0.0
|
|
3257
4559
|
* @experimental
|
|
3258
4560
|
*/
|
|
@@ -3306,6 +4608,10 @@ export function toTaggedUnion<const Tag extends PropertyKey>(tag: Tag) {
|
|
|
3306
4608
|
}
|
|
3307
4609
|
|
|
3308
4610
|
/**
|
|
4611
|
+
* A union schema that exposes `cases`, `guards`, `isAnyOf`, and `match` utilities keyed by the `_tag` discriminant.
|
|
4612
|
+
* Produced by {@link TaggedUnion}.
|
|
4613
|
+
*
|
|
4614
|
+
* @see {@link TaggedUnion} for the constructor
|
|
3309
4615
|
* @since 4.0.0
|
|
3310
4616
|
* @experimental
|
|
3311
4617
|
*/
|
|
@@ -3338,6 +4644,28 @@ export interface TaggedUnion<Cases extends Record<string, Top>> extends
|
|
|
3338
4644
|
}
|
|
3339
4645
|
|
|
3340
4646
|
/**
|
|
4647
|
+
* Builds a discriminated union from a record of field sets, one per variant.
|
|
4648
|
+
* Each key becomes the `_tag` literal and the value is passed to {@link TaggedStruct}.
|
|
4649
|
+
* The result includes `cases`, `guards`, `isAnyOf`, and `match` utilities.
|
|
4650
|
+
*
|
|
4651
|
+
* **Example** (Discriminated union with pattern matching)
|
|
4652
|
+
*
|
|
4653
|
+
* ```ts
|
|
4654
|
+
* import { Schema } from "effect"
|
|
4655
|
+
*
|
|
4656
|
+
* const Shape = Schema.TaggedUnion({
|
|
4657
|
+
* Circle: { radius: Schema.Number },
|
|
4658
|
+
* Rectangle: { width: Schema.Number, height: Schema.Number }
|
|
4659
|
+
* })
|
|
4660
|
+
*
|
|
4661
|
+
* // Pattern-match on a decoded value
|
|
4662
|
+
* const area = Shape.match({ _tag: "Circle", radius: 5 }, {
|
|
4663
|
+
* Circle: (c) => Math.PI * c.radius ** 2,
|
|
4664
|
+
* Rectangle: (r) => r.width * r.height
|
|
4665
|
+
* })
|
|
4666
|
+
* ```
|
|
4667
|
+
*
|
|
4668
|
+
* @see {@link toTaggedUnion} to augment an existing union instead
|
|
3341
4669
|
* @category Constructors
|
|
3342
4670
|
* @since 4.0.0
|
|
3343
4671
|
*/
|
|
@@ -3355,6 +4683,11 @@ export function TaggedUnion<const CasesByTag extends Record<string, Struct.Field
|
|
|
3355
4683
|
}
|
|
3356
4684
|
|
|
3357
4685
|
/**
|
|
4686
|
+
* The interface type for schemas created by {@link Opaque}.
|
|
4687
|
+
* Carries the same encoded/decoded shape as `S` but replaces `Type` with `Self & Brand`,
|
|
4688
|
+
* making the decoded value nominally distinct.
|
|
4689
|
+
*
|
|
4690
|
+
* @see {@link Opaque} for the constructor
|
|
3358
4691
|
* @since 4.0.0
|
|
3359
4692
|
*/
|
|
3360
4693
|
export interface Opaque<Self, S extends Top, Brand> extends
|
|
@@ -3381,6 +4714,23 @@ export interface Opaque<Self, S extends Top, Brand> extends
|
|
|
3381
4714
|
}
|
|
3382
4715
|
|
|
3383
4716
|
/**
|
|
4717
|
+
* Wraps a schema so that its decoded `Type` becomes a nominally distinct type `Self`.
|
|
4718
|
+
* Useful for creating opaque types that are structurally identical to a base schema
|
|
4719
|
+
* but type-incompatible with it.
|
|
4720
|
+
*
|
|
4721
|
+
* **Example** (Opaque user ID)
|
|
4722
|
+
*
|
|
4723
|
+
* ```ts
|
|
4724
|
+
* import { Schema } from "effect"
|
|
4725
|
+
*
|
|
4726
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
4727
|
+
* const UserId = Schema.Opaque<UserId>()(Schema.String)
|
|
4728
|
+
*
|
|
4729
|
+
* // Decoded value is UserId, not plain string
|
|
4730
|
+
* const id = Schema.decodeUnknownSync(UserId)("abc")
|
|
4731
|
+
* // id: UserId
|
|
4732
|
+
* ```
|
|
4733
|
+
*
|
|
3384
4734
|
* @since 4.0.0
|
|
3385
4735
|
*/
|
|
3386
4736
|
export function Opaque<Self, Brand = {}>() {
|
|
@@ -3393,6 +4743,9 @@ export function Opaque<Self, Brand = {}>() {
|
|
|
3393
4743
|
}
|
|
3394
4744
|
|
|
3395
4745
|
/**
|
|
4746
|
+
* The type produced by {@link instanceOf} — a declaration schema that validates class instances.
|
|
4747
|
+
*
|
|
4748
|
+
* @see {@link instanceOf} for the constructor
|
|
3396
4749
|
* @since 4.0.0
|
|
3397
4750
|
*/
|
|
3398
4751
|
export interface instanceOf<T, Iso = T> extends declare<T, Iso> {
|
|
@@ -3400,7 +4753,19 @@ export interface instanceOf<T, Iso = T> extends declare<T, Iso> {
|
|
|
3400
4753
|
}
|
|
3401
4754
|
|
|
3402
4755
|
/**
|
|
3403
|
-
* Creates a schema that validates
|
|
4756
|
+
* Creates a schema that validates values using `instanceof`.
|
|
4757
|
+
* Decoding and encoding pass the value through unchanged.
|
|
4758
|
+
*
|
|
4759
|
+
* **Example** (Schema for a built-in class)
|
|
4760
|
+
*
|
|
4761
|
+
* ```ts
|
|
4762
|
+
* import { Schema } from "effect"
|
|
4763
|
+
*
|
|
4764
|
+
* const DateSchema = Schema.instanceOf(Date)
|
|
4765
|
+
*
|
|
4766
|
+
* const decoded = Schema.decodeUnknownSync(DateSchema)(new Date("2024-01-01"))
|
|
4767
|
+
* // decoded: Date
|
|
4768
|
+
* ```
|
|
3404
4769
|
*
|
|
3405
4770
|
* @category Constructors
|
|
3406
4771
|
* @since 4.0.0
|
|
@@ -3413,6 +4778,9 @@ export function instanceOf<C extends abstract new(...args: any) => any, Iso = In
|
|
|
3413
4778
|
}
|
|
3414
4779
|
|
|
3415
4780
|
/**
|
|
4781
|
+
* Constructs an `AST.Link` that describes how a value of type `T` encodes to and decodes from a `To` schema.
|
|
4782
|
+
* Used when building low-level AST transformations that bridge two schema types.
|
|
4783
|
+
*
|
|
3416
4784
|
* @since 4.0.0
|
|
3417
4785
|
* @experimental
|
|
3418
4786
|
*/
|
|
@@ -3433,6 +4801,22 @@ export function link<T>() { // TODO: better name
|
|
|
3433
4801
|
// -----------------------------------------------------------------------------
|
|
3434
4802
|
|
|
3435
4803
|
/**
|
|
4804
|
+
* Creates a custom filter check from a predicate function. The predicate
|
|
4805
|
+
* receives the input value, the schema's AST, and parse options, and returns
|
|
4806
|
+
* `true`/`undefined` on success or a failure description on error.
|
|
4807
|
+
*
|
|
4808
|
+
* **Example** (Custom filter check)
|
|
4809
|
+
* ```ts
|
|
4810
|
+
* import { Schema } from "effect"
|
|
4811
|
+
*
|
|
4812
|
+
* // Check that a number is even
|
|
4813
|
+
* const isEven = Schema.makeFilter(
|
|
4814
|
+
* (n: number) => n % 2 === 0 || "expected an even number"
|
|
4815
|
+
* )
|
|
4816
|
+
*
|
|
4817
|
+
* const EvenNumber = Schema.Number.check(isEven)
|
|
4818
|
+
* ```
|
|
4819
|
+
*
|
|
3436
4820
|
* @category Checks Constructors
|
|
3437
4821
|
* @since 4.0.0
|
|
3438
4822
|
*/
|
|
@@ -3446,6 +4830,9 @@ export const makeFilter: <T>(
|
|
|
3446
4830
|
) => AST.Filter<T> = AST.makeFilter
|
|
3447
4831
|
|
|
3448
4832
|
/**
|
|
4833
|
+
* Groups multiple checks into a single {@link AST.FilterGroup}, applying
|
|
4834
|
+
* optional shared annotations to the group as a whole.
|
|
4835
|
+
*
|
|
3449
4836
|
* @category Checks Constructors
|
|
3450
4837
|
* @since 4.0.0
|
|
3451
4838
|
*/
|
|
@@ -4002,6 +5389,9 @@ export function isFinite(annotations?: Annotations.Filter) {
|
|
|
4002
5389
|
}
|
|
4003
5390
|
|
|
4004
5391
|
/**
|
|
5392
|
+
* Generic factory for creating a "greater than" (`>`) check for any ordered
|
|
5393
|
+
* type by supplying an {@link Order.Order} instance.
|
|
5394
|
+
*
|
|
4005
5395
|
* @category Order checks
|
|
4006
5396
|
* @since 4.0.0
|
|
4007
5397
|
*/
|
|
@@ -4025,6 +5415,9 @@ export function makeIsGreaterThan<T>(options: {
|
|
|
4025
5415
|
}
|
|
4026
5416
|
|
|
4027
5417
|
/**
|
|
5418
|
+
* Generic factory for creating a ">=" check for any ordered type by supplying
|
|
5419
|
+
* an {@link Order.Order} instance.
|
|
5420
|
+
*
|
|
4028
5421
|
* @category Order checks
|
|
4029
5422
|
* @since 4.0.0
|
|
4030
5423
|
*/
|
|
@@ -4048,6 +5441,9 @@ export function makeIsGreaterThanOrEqualTo<T>(options: {
|
|
|
4048
5441
|
}
|
|
4049
5442
|
|
|
4050
5443
|
/**
|
|
5444
|
+
* Generic factory for creating a "<" check for any ordered type by supplying
|
|
5445
|
+
* an {@link Order.Order} instance.
|
|
5446
|
+
*
|
|
4051
5447
|
* @category Order checks
|
|
4052
5448
|
* @since 4.0.0
|
|
4053
5449
|
*/
|
|
@@ -4071,6 +5467,9 @@ export function makeIsLessThan<T>(options: {
|
|
|
4071
5467
|
}
|
|
4072
5468
|
|
|
4073
5469
|
/**
|
|
5470
|
+
* Generic factory for creating a "<=" check for any ordered type by supplying
|
|
5471
|
+
* an {@link Order.Order} instance.
|
|
5472
|
+
*
|
|
4074
5473
|
* @category Order checks
|
|
4075
5474
|
* @since 4.0.0
|
|
4076
5475
|
*/
|
|
@@ -4094,6 +5493,9 @@ export function makeIsLessThanOrEqualTo<T>(options: {
|
|
|
4094
5493
|
}
|
|
4095
5494
|
|
|
4096
5495
|
/**
|
|
5496
|
+
* Generic factory for creating an inclusive/exclusive range check for any
|
|
5497
|
+
* ordered type by supplying an {@link Order.Order} instance.
|
|
5498
|
+
*
|
|
4097
5499
|
* @category Order checks
|
|
4098
5500
|
* @since 4.0.0
|
|
4099
5501
|
*/
|
|
@@ -4136,6 +5538,9 @@ export function makeIsBetween<T>(deriveOptions: {
|
|
|
4136
5538
|
}
|
|
4137
5539
|
|
|
4138
5540
|
/**
|
|
5541
|
+
* Generic factory for creating a divisibility check for any numeric type by
|
|
5542
|
+
* supplying a remainder function and a zero value.
|
|
5543
|
+
*
|
|
4139
5544
|
* @category Numeric checks
|
|
4140
5545
|
* @since 4.0.0
|
|
4141
5546
|
*/
|
|
@@ -4846,6 +6251,14 @@ export const isBetweenBigDecimal = makeIsBetween({
|
|
|
4846
6251
|
* constraint to ensure generated strings or arrays have at least the required
|
|
4847
6252
|
* length.
|
|
4848
6253
|
*
|
|
6254
|
+
* **Example** (Minimum length check)
|
|
6255
|
+
* ```ts
|
|
6256
|
+
* import { Schema } from "effect"
|
|
6257
|
+
*
|
|
6258
|
+
* const NonEmptyStringSchema = Schema.String.check(Schema.isMinLength(1))
|
|
6259
|
+
* const NonEmptyArraySchema = Schema.Array(Schema.Number).check(Schema.isMinLength(1))
|
|
6260
|
+
* ```
|
|
6261
|
+
*
|
|
4849
6262
|
* @category Length checks
|
|
4850
6263
|
* @since 4.0.0
|
|
4851
6264
|
*/
|
|
@@ -5285,6 +6698,7 @@ export function isPropertyNames(keySchema: Top, annotations?: Annotations.Filter
|
|
|
5285
6698
|
* constraint using the provided equivalence function to ensure generated arrays
|
|
5286
6699
|
* contain only unique items.
|
|
5287
6700
|
*
|
|
6701
|
+
* @category Array checks
|
|
5288
6702
|
* @since 4.0.0
|
|
5289
6703
|
*/
|
|
5290
6704
|
export function isUnique<T>(annotations?: Annotations.Filter) {
|
|
@@ -5326,6 +6740,23 @@ export const NonEmptyString = String.check(isNonEmpty())
|
|
|
5326
6740
|
export const Char = String.check(isLengthBetween(1, 1))
|
|
5327
6741
|
|
|
5328
6742
|
/**
|
|
6743
|
+
* Schema for the `Option<A>` type, representing an optional value that is
|
|
6744
|
+
* either `None` or `Some<A>`.
|
|
6745
|
+
*
|
|
6746
|
+
* **Example** (Option schema)
|
|
6747
|
+
*
|
|
6748
|
+
* ```ts
|
|
6749
|
+
* import { Schema } from "effect"
|
|
6750
|
+
* import { Option } from "effect"
|
|
6751
|
+
*
|
|
6752
|
+
* const schema = Schema.Option(Schema.Number)
|
|
6753
|
+
*
|
|
6754
|
+
* Schema.decodeUnknownSync(schema)(Option.some(1))
|
|
6755
|
+
* // => Some(1)
|
|
6756
|
+
* Schema.decodeUnknownSync(schema)(Option.none())
|
|
6757
|
+
* // => None
|
|
6758
|
+
* ```
|
|
6759
|
+
*
|
|
5329
6760
|
* @category Option
|
|
5330
6761
|
* @since 4.0.0
|
|
5331
6762
|
*/
|
|
@@ -5349,6 +6780,8 @@ export type OptionIso<A extends Top> =
|
|
|
5349
6780
|
| { readonly _tag: "Some"; readonly value: A["Iso"] }
|
|
5350
6781
|
|
|
5351
6782
|
/**
|
|
6783
|
+
* Creates a schema for `Option<A>`. See {@link Option} for details.
|
|
6784
|
+
*
|
|
5352
6785
|
* @category Option
|
|
5353
6786
|
* @since 4.0.0
|
|
5354
6787
|
*/
|
|
@@ -5414,6 +6847,9 @@ export function Option<A extends Top>(value: A): Option<A> {
|
|
|
5414
6847
|
}
|
|
5415
6848
|
|
|
5416
6849
|
/**
|
|
6850
|
+
* Schema type for {@link OptionFromNullOr}.
|
|
6851
|
+
*
|
|
6852
|
+
* @category Option
|
|
5417
6853
|
* @since 4.0.0
|
|
5418
6854
|
*/
|
|
5419
6855
|
export interface OptionFromNullOr<S extends Top> extends decodeTo<Option<toType<S>>, NullOr<S>> {}
|
|
@@ -5440,6 +6876,9 @@ export function OptionFromNullOr<S extends Top>(schema: S): OptionFromNullOr<S>
|
|
|
5440
6876
|
}
|
|
5441
6877
|
|
|
5442
6878
|
/**
|
|
6879
|
+
* Schema type for {@link OptionFromUndefinedOr}.
|
|
6880
|
+
*
|
|
6881
|
+
* @category Option
|
|
5443
6882
|
* @since 4.0.0
|
|
5444
6883
|
*/
|
|
5445
6884
|
export interface OptionFromUndefinedOr<S extends Top> extends decodeTo<Option<toType<S>>, UndefinedOr<S>> {}
|
|
@@ -5466,6 +6905,9 @@ export function OptionFromUndefinedOr<S extends Top>(schema: S): OptionFromUndef
|
|
|
5466
6905
|
}
|
|
5467
6906
|
|
|
5468
6907
|
/**
|
|
6908
|
+
* Schema type for {@link OptionFromNullishOr}.
|
|
6909
|
+
*
|
|
6910
|
+
* @category Option
|
|
5469
6911
|
* @since 4.0.0
|
|
5470
6912
|
*/
|
|
5471
6913
|
export interface OptionFromNullishOr<S extends Top> extends decodeTo<Option<toType<S>>, NullishOr<S>> {}
|
|
@@ -5497,6 +6939,9 @@ export function OptionFromNullishOr<S extends Top>(
|
|
|
5497
6939
|
}
|
|
5498
6940
|
|
|
5499
6941
|
/**
|
|
6942
|
+
* Schema type for {@link OptionFromOptionalKey}.
|
|
6943
|
+
*
|
|
6944
|
+
* @category Option
|
|
5500
6945
|
* @since 4.0.0
|
|
5501
6946
|
*/
|
|
5502
6947
|
export interface OptionFromOptionalKey<S extends Top> extends decodeTo<Option<toType<S>>, optionalKey<S>> {}
|
|
@@ -5523,6 +6968,9 @@ export function OptionFromOptionalKey<S extends Top>(schema: S): OptionFromOptio
|
|
|
5523
6968
|
}
|
|
5524
6969
|
|
|
5525
6970
|
/**
|
|
6971
|
+
* Schema type for {@link OptionFromOptional}.
|
|
6972
|
+
*
|
|
6973
|
+
* @category Option
|
|
5526
6974
|
* @since 4.0.0
|
|
5527
6975
|
*/
|
|
5528
6976
|
export interface OptionFromOptional<S extends Top> extends decodeTo<Option<toType<S>>, optional<S>> {}
|
|
@@ -5551,6 +6999,9 @@ export function OptionFromOptional<S extends Top>(schema: S): OptionFromOptional
|
|
|
5551
6999
|
}
|
|
5552
7000
|
|
|
5553
7001
|
/**
|
|
7002
|
+
* Schema for the `Result<A, E>` type, representing a computation that either
|
|
7003
|
+
* succeeds with `A` or fails with `E`.
|
|
7004
|
+
*
|
|
5554
7005
|
* @category Result
|
|
5555
7006
|
* @since 4.0.0
|
|
5556
7007
|
*/
|
|
@@ -5575,6 +7026,8 @@ export type ResultIso<A extends Top, E extends Top> =
|
|
|
5575
7026
|
| { readonly _tag: "Failure"; readonly failure: E["Iso"] }
|
|
5576
7027
|
|
|
5577
7028
|
/**
|
|
7029
|
+
* Creates a schema for `Result<A, E>`. See {@link Result} for details.
|
|
7030
|
+
*
|
|
5578
7031
|
* @category Result
|
|
5579
7032
|
* @since 4.0.0
|
|
5580
7033
|
*/
|
|
@@ -5649,6 +7102,9 @@ export function Result<A extends Top, E extends Top>(
|
|
|
5649
7102
|
}
|
|
5650
7103
|
|
|
5651
7104
|
/**
|
|
7105
|
+
* Schema for the `Redacted<A>` type, providing secure handling of sensitive
|
|
7106
|
+
* values. The inner value is hidden from error messages.
|
|
7107
|
+
*
|
|
5652
7108
|
* @category Redacted
|
|
5653
7109
|
* @since 4.0.0
|
|
5654
7110
|
*/
|
|
@@ -5750,6 +7206,8 @@ export function Redacted<S extends Top>(value: S, options?: {
|
|
|
5750
7206
|
}
|
|
5751
7207
|
|
|
5752
7208
|
/**
|
|
7209
|
+
* Schema type for {@link RedactedFromValue}.
|
|
7210
|
+
*
|
|
5753
7211
|
* @category Redacted
|
|
5754
7212
|
* @since 4.0.0
|
|
5755
7213
|
*/
|
|
@@ -5758,6 +7216,9 @@ export interface RedactedFromValue<S extends Top>
|
|
|
5758
7216
|
{}
|
|
5759
7217
|
|
|
5760
7218
|
/**
|
|
7219
|
+
* Middleware that wraps decoded errors in `Redacted`, preventing sensitive
|
|
7220
|
+
* schema details from leaking in error messages.
|
|
7221
|
+
*
|
|
5761
7222
|
* @category Redacted
|
|
5762
7223
|
* @since 4.0.0
|
|
5763
7224
|
*/
|
|
@@ -5766,6 +7227,10 @@ export function redact<S extends Top>(schema: S): middlewareDecoding<S, S["Decod
|
|
|
5766
7227
|
}
|
|
5767
7228
|
|
|
5768
7229
|
/**
|
|
7230
|
+
* Decodes a value and wraps it in `Redacted<A>`. Unlike {@link Redacted} which
|
|
7231
|
+
* expects the input to already be a `Redacted` instance, this schema decodes
|
|
7232
|
+
* the raw value and wraps it.
|
|
7233
|
+
*
|
|
5769
7234
|
* @category Redacted
|
|
5770
7235
|
* @since 4.0.0
|
|
5771
7236
|
*/
|
|
@@ -5784,6 +7249,10 @@ export function RedactedFromValue<S extends Top>(value: S, options?: {
|
|
|
5784
7249
|
}
|
|
5785
7250
|
|
|
5786
7251
|
/**
|
|
7252
|
+
* Schema for a single `Cause.Reason<E>`, representing one reason a fiber may
|
|
7253
|
+
* fail: a typed error (`Fail`), an unexpected defect (`Die`), or an interrupt
|
|
7254
|
+
* (`Interrupt`).
|
|
7255
|
+
*
|
|
5787
7256
|
* @category CauseReason
|
|
5788
7257
|
* @since 4.0.0
|
|
5789
7258
|
*/
|
|
@@ -5815,6 +7284,8 @@ export type CauseReasonIso<E extends Top, D extends Top> = {
|
|
|
5815
7284
|
}
|
|
5816
7285
|
|
|
5817
7286
|
/**
|
|
7287
|
+
* Creates a schema for `Cause.Reason<E>`. See {@link CauseReason} for details.
|
|
7288
|
+
*
|
|
5818
7289
|
* @category CauseReason
|
|
5819
7290
|
* @since 4.0.0
|
|
5820
7291
|
*/
|
|
@@ -5926,6 +7397,9 @@ function causeReasonToFormatter<E>(error: Formatter<E>, defect: Formatter<unknow
|
|
|
5926
7397
|
}
|
|
5927
7398
|
|
|
5928
7399
|
/**
|
|
7400
|
+
* Schema for `Cause<E>`, an ordered collection of reasons a fiber failed,
|
|
7401
|
+
* combining typed errors, defects, and interrupts.
|
|
7402
|
+
*
|
|
5929
7403
|
* @category Cause
|
|
5930
7404
|
* @since 4.0.0
|
|
5931
7405
|
*/
|
|
@@ -5948,6 +7422,8 @@ export interface Cause<E extends Top, D extends Top> extends
|
|
|
5948
7422
|
export type CauseIso<E extends Top, D extends Top> = ReadonlyArray<CauseReasonIso<E, D>>
|
|
5949
7423
|
|
|
5950
7424
|
/**
|
|
7425
|
+
* Creates a schema for `Cause<E>`. See {@link Cause} for details.
|
|
7426
|
+
*
|
|
5951
7427
|
* @category Cause
|
|
5952
7428
|
* @since 4.0.0
|
|
5953
7429
|
*/
|
|
@@ -6009,6 +7485,9 @@ function causeToFormatter<E>(error: Formatter<E>, defect: Formatter<unknown>) {
|
|
|
6009
7485
|
}
|
|
6010
7486
|
|
|
6011
7487
|
/**
|
|
7488
|
+
* Schema type for {@link Error}.
|
|
7489
|
+
*
|
|
7490
|
+
* @category Schemas
|
|
6012
7491
|
* @since 4.0.0
|
|
6013
7492
|
*/
|
|
6014
7493
|
export interface Error extends instanceOf<globalThis.Error> {}
|
|
@@ -6070,6 +7549,9 @@ export const ErrorWithStack: Error = instanceOf(globalThis.Error, {
|
|
|
6070
7549
|
})
|
|
6071
7550
|
|
|
6072
7551
|
/**
|
|
7552
|
+
* Schema type for {@link Defect}.
|
|
7553
|
+
*
|
|
7554
|
+
* @category Schemas
|
|
6073
7555
|
* @since 4.0.0
|
|
6074
7556
|
*/
|
|
6075
7557
|
export interface Defect extends
|
|
@@ -6140,6 +7622,9 @@ export const DefectWithStack: Defect = Union([
|
|
|
6140
7622
|
])
|
|
6141
7623
|
|
|
6142
7624
|
/**
|
|
7625
|
+
* Schema for `Exit<A, E>`, representing the result of a fiber execution —
|
|
7626
|
+
* either a success with value `A` or a failure with `Cause<E>`.
|
|
7627
|
+
*
|
|
6143
7628
|
* @category Exit
|
|
6144
7629
|
* @since 4.0.0
|
|
6145
7630
|
*/
|
|
@@ -6169,6 +7654,8 @@ export type ExitIso<A extends Top, E extends Top, D extends Top> = {
|
|
|
6169
7654
|
}
|
|
6170
7655
|
|
|
6171
7656
|
/**
|
|
7657
|
+
* Creates a schema for `Exit<A, E>`. See {@link Exit} for details.
|
|
7658
|
+
*
|
|
6172
7659
|
* @category Exit
|
|
6173
7660
|
* @since 4.0.0
|
|
6174
7661
|
*/
|
|
@@ -6267,6 +7754,8 @@ export function Exit<A extends Top, E extends Top, D extends Top>(value: A, erro
|
|
|
6267
7754
|
}
|
|
6268
7755
|
|
|
6269
7756
|
/**
|
|
7757
|
+
* Schema type for {@link ReadonlyMap}.
|
|
7758
|
+
*
|
|
6270
7759
|
* @category ReadonlyMap
|
|
6271
7760
|
* @since 4.0.0
|
|
6272
7761
|
*/
|
|
@@ -6357,6 +7846,9 @@ export function ReadonlyMap<Key extends Top, Value extends Top>(key: Key, value:
|
|
|
6357
7846
|
}
|
|
6358
7847
|
|
|
6359
7848
|
/**
|
|
7849
|
+
* Schema for an Effect `HashMap` where keys and values must conform to the
|
|
7850
|
+
* provided schemas.
|
|
7851
|
+
*
|
|
6360
7852
|
* @category HashMap
|
|
6361
7853
|
* @since 4.0.0
|
|
6362
7854
|
*/
|
|
@@ -6448,6 +7940,8 @@ export function HashMap<Key extends Top, Value extends Top>(key: Key, value: Val
|
|
|
6448
7940
|
}
|
|
6449
7941
|
|
|
6450
7942
|
/**
|
|
7943
|
+
* Schema type for {@link ReadonlySet}.
|
|
7944
|
+
*
|
|
6451
7945
|
* @category ReadonlySet
|
|
6452
7946
|
* @since 4.0.0
|
|
6453
7947
|
*/
|
|
@@ -6534,6 +8028,9 @@ export function ReadonlySet<Value extends Top>(value: Value): $ReadonlySet<Value
|
|
|
6534
8028
|
}
|
|
6535
8029
|
|
|
6536
8030
|
/**
|
|
8031
|
+
* Schema for an Effect `HashSet` where values must conform to the provided
|
|
8032
|
+
* schema.
|
|
8033
|
+
*
|
|
6537
8034
|
* @category HashSet
|
|
6538
8035
|
* @since 4.0.0
|
|
6539
8036
|
*/
|
|
@@ -6623,6 +8120,9 @@ export function HashSet<Value extends Top>(value: Value): HashSet<Value> {
|
|
|
6623
8120
|
}
|
|
6624
8121
|
|
|
6625
8122
|
/**
|
|
8123
|
+
* Schema for an Effect `Chunk` (immutable array-like collection) where values
|
|
8124
|
+
* must conform to the provided schema.
|
|
8125
|
+
*
|
|
6626
8126
|
* @category Chunk
|
|
6627
8127
|
* @since 4.0.0
|
|
6628
8128
|
*/
|
|
@@ -6712,11 +8212,19 @@ export function Chunk<Value extends Top>(value: Value): Chunk<Value> {
|
|
|
6712
8212
|
}
|
|
6713
8213
|
|
|
6714
8214
|
/**
|
|
8215
|
+
* Schema type for {@link RegExp}.
|
|
8216
|
+
*
|
|
8217
|
+
* @category Schemas
|
|
6715
8218
|
* @since 4.0.0
|
|
6716
8219
|
*/
|
|
6717
8220
|
export interface RegExp extends instanceOf<globalThis.RegExp> {}
|
|
6718
8221
|
|
|
6719
8222
|
/**
|
|
8223
|
+
* Schema for JavaScript `RegExp` objects.
|
|
8224
|
+
*
|
|
8225
|
+
* The default JSON serializer encodes a `RegExp` as `{ source, flags }`.
|
|
8226
|
+
*
|
|
8227
|
+
* @category Schemas
|
|
6720
8228
|
* @since 4.0.0
|
|
6721
8229
|
*/
|
|
6722
8230
|
export const RegExp: RegExp = instanceOf(
|
|
@@ -6776,6 +8284,9 @@ export const RegExp: RegExp = instanceOf(
|
|
|
6776
8284
|
)
|
|
6777
8285
|
|
|
6778
8286
|
/**
|
|
8287
|
+
* Schema type for {@link URL}.
|
|
8288
|
+
*
|
|
8289
|
+
* @category URL
|
|
6779
8290
|
* @since 4.0.0
|
|
6780
8291
|
*/
|
|
6781
8292
|
export interface URL extends instanceOf<globalThis.URL> {}
|
|
@@ -6812,6 +8323,9 @@ export const URL: URL = instanceOf(
|
|
|
6812
8323
|
)
|
|
6813
8324
|
|
|
6814
8325
|
/**
|
|
8326
|
+
* Schema type for {@link URLFromString}.
|
|
8327
|
+
*
|
|
8328
|
+
* @category URL
|
|
6815
8329
|
* @since 4.0.0
|
|
6816
8330
|
*/
|
|
6817
8331
|
export interface URLFromString extends decodeTo<URL, String> {}
|
|
@@ -6832,6 +8346,9 @@ export const URLFromString: URLFromString = String.annotate({ expected: "a strin
|
|
|
6832
8346
|
.pipe(decodeTo(URL, Transformation.urlFromString)) // TODO: remove duplication with URL schema
|
|
6833
8347
|
|
|
6834
8348
|
/**
|
|
8349
|
+
* Schema type for {@link Date}.
|
|
8350
|
+
*
|
|
8351
|
+
* @category Schemas
|
|
6835
8352
|
* @since 4.0.0
|
|
6836
8353
|
*/
|
|
6837
8354
|
export interface Date extends instanceOf<globalThis.Date> {}
|
|
@@ -6840,8 +8357,19 @@ export interface Date extends instanceOf<globalThis.Date> {}
|
|
|
6840
8357
|
* A schema for JavaScript `Date` objects.
|
|
6841
8358
|
*
|
|
6842
8359
|
* This schema accepts any `Date` instance, including invalid dates (e.g., `new
|
|
6843
|
-
* Date("invalid")`). For validating only valid dates, use
|
|
8360
|
+
* Date("invalid")`). For validating only valid dates, use {@link DateValid}
|
|
8361
|
+
* instead. The default JSON serializer encodes `Date` as an ISO 8601 string.
|
|
8362
|
+
*
|
|
8363
|
+
* **Example** (Date schema)
|
|
8364
|
+
*
|
|
8365
|
+
* ```ts
|
|
8366
|
+
* import { Schema } from "effect"
|
|
8367
|
+
*
|
|
8368
|
+
* Schema.decodeUnknownSync(Schema.Date)(new Date("2024-01-01"))
|
|
8369
|
+
* // => Date { 2024-01-01T00:00:00.000Z }
|
|
8370
|
+
* ```
|
|
6844
8371
|
*
|
|
8372
|
+
* @category Schemas
|
|
6845
8373
|
* @since 4.0.0
|
|
6846
8374
|
*/
|
|
6847
8375
|
export const Date: Date = instanceOf(
|
|
@@ -6868,6 +8396,9 @@ export const Date: Date = instanceOf(
|
|
|
6868
8396
|
)
|
|
6869
8397
|
|
|
6870
8398
|
/**
|
|
8399
|
+
* Schema type for {@link DateValid}.
|
|
8400
|
+
*
|
|
8401
|
+
* @category Schemas
|
|
6871
8402
|
* @since 4.0.0
|
|
6872
8403
|
*/
|
|
6873
8404
|
export interface DateValid extends Date {}
|
|
@@ -6883,6 +8414,9 @@ export interface DateValid extends Date {}
|
|
|
6883
8414
|
export const DateValid = Date.check(isDateValid())
|
|
6884
8415
|
|
|
6885
8416
|
/**
|
|
8417
|
+
* Schema type for {@link Duration}.
|
|
8418
|
+
*
|
|
8419
|
+
* @category Duration
|
|
6886
8420
|
* @since 4.0.0
|
|
6887
8421
|
*/
|
|
6888
8422
|
export interface Duration extends declare<Duration_.Duration> {}
|
|
@@ -6890,9 +8424,20 @@ export interface Duration extends declare<Duration_.Duration> {}
|
|
|
6890
8424
|
/**
|
|
6891
8425
|
* A schema for `Duration` values.
|
|
6892
8426
|
*
|
|
6893
|
-
*
|
|
8427
|
+
* The default JSON serializer encodes `Duration` as a tagged object with the
|
|
8428
|
+
* duration type and value.
|
|
8429
|
+
*
|
|
8430
|
+
* **Example** (Duration schema)
|
|
8431
|
+
*
|
|
8432
|
+
* ```ts
|
|
8433
|
+
* import { Schema } from "effect"
|
|
8434
|
+
* import { Duration } from "effect"
|
|
8435
|
+
*
|
|
8436
|
+
* Schema.decodeUnknownSync(Schema.Duration)(Duration.seconds(5))
|
|
8437
|
+
* // => Duration(5s)
|
|
8438
|
+
* ```
|
|
6894
8439
|
*
|
|
6895
|
-
*
|
|
8440
|
+
* @category Duration
|
|
6896
8441
|
*
|
|
6897
8442
|
* @since 4.0.0
|
|
6898
8443
|
*/
|
|
@@ -6956,6 +8501,9 @@ export const Duration: Duration = declare(
|
|
|
6956
8501
|
)
|
|
6957
8502
|
|
|
6958
8503
|
/**
|
|
8504
|
+
* Schema type for {@link DurationFromNanos}.
|
|
8505
|
+
*
|
|
8506
|
+
* @category Duration
|
|
6959
8507
|
* @since 4.0.0
|
|
6960
8508
|
*/
|
|
6961
8509
|
export interface DurationFromNanos extends decodeTo<Duration, BigInt> {}
|
|
@@ -6978,6 +8526,9 @@ export const DurationFromNanos: DurationFromNanos = BigInt.check(isGreaterThanOr
|
|
|
6978
8526
|
)
|
|
6979
8527
|
|
|
6980
8528
|
/**
|
|
8529
|
+
* Schema type for {@link DurationFromMillis}.
|
|
8530
|
+
*
|
|
8531
|
+
* @category Duration
|
|
6981
8532
|
* @since 4.0.0
|
|
6982
8533
|
*/
|
|
6983
8534
|
export interface DurationFromMillis extends decodeTo<Duration, Number> {}
|
|
@@ -7003,6 +8554,9 @@ export const DurationFromMillis: DurationFromMillis = Number.check(isGreaterThan
|
|
|
7003
8554
|
)
|
|
7004
8555
|
|
|
7005
8556
|
/**
|
|
8557
|
+
* Schema type for {@link BigDecimal}.
|
|
8558
|
+
*
|
|
8559
|
+
* @category Schemas
|
|
7006
8560
|
* @since 4.0.0
|
|
7007
8561
|
*/
|
|
7008
8562
|
export interface BigDecimal extends declare<BigDecimal_.BigDecimal> {}
|
|
@@ -7042,6 +8596,9 @@ export const BigDecimal: BigDecimal = declare(
|
|
|
7042
8596
|
)
|
|
7043
8597
|
|
|
7044
8598
|
/**
|
|
8599
|
+
* Schema type for {@link UnknownFromJsonString}.
|
|
8600
|
+
*
|
|
8601
|
+
* @category JSON
|
|
7045
8602
|
* @since 4.0.0
|
|
7046
8603
|
*/
|
|
7047
8604
|
export interface UnknownFromJsonString extends fromJsonString<Unknown> {}
|
|
@@ -7071,6 +8628,9 @@ export interface UnknownFromJsonString extends fromJsonString<Unknown> {}
|
|
|
7071
8628
|
export const UnknownFromJsonString = fromJsonString(Unknown)
|
|
7072
8629
|
|
|
7073
8630
|
/**
|
|
8631
|
+
* Schema type for {@link fromJsonString}.
|
|
8632
|
+
*
|
|
8633
|
+
* @category JSON
|
|
7074
8634
|
* @since 4.0.0
|
|
7075
8635
|
*/
|
|
7076
8636
|
export interface fromJsonString<S extends Top> extends decodeTo<S, String> {}
|
|
@@ -7147,11 +8707,20 @@ export function fromJsonString<S extends Top>(schema: S): fromJsonString<S> {
|
|
|
7147
8707
|
}
|
|
7148
8708
|
|
|
7149
8709
|
/**
|
|
8710
|
+
* Schema type for {@link File}.
|
|
8711
|
+
*
|
|
8712
|
+
* @category Schemas
|
|
7150
8713
|
* @since 4.0.0
|
|
7151
8714
|
*/
|
|
7152
8715
|
export interface File extends instanceOf<globalThis.File> {}
|
|
7153
8716
|
|
|
7154
8717
|
/**
|
|
8718
|
+
* Schema for JavaScript `File` objects.
|
|
8719
|
+
*
|
|
8720
|
+
* The default JSON serializer encodes a `File` as `{ data, type, name, lastModified }`
|
|
8721
|
+
* where `data` is base64-encoded.
|
|
8722
|
+
*
|
|
8723
|
+
* @category Schemas
|
|
7155
8724
|
* @since 4.0.0
|
|
7156
8725
|
*/
|
|
7157
8726
|
export const File: File = instanceOf(globalThis.File, {
|
|
@@ -7208,11 +8777,20 @@ export const File: File = instanceOf(globalThis.File, {
|
|
|
7208
8777
|
})
|
|
7209
8778
|
|
|
7210
8779
|
/**
|
|
8780
|
+
* Schema type for {@link FormData}.
|
|
8781
|
+
*
|
|
8782
|
+
* @category Schemas
|
|
7211
8783
|
* @since 4.0.0
|
|
7212
8784
|
*/
|
|
7213
8785
|
export interface FormData extends instanceOf<globalThis.FormData> {}
|
|
7214
8786
|
|
|
7215
8787
|
/**
|
|
8788
|
+
* Schema for JavaScript `FormData` objects.
|
|
8789
|
+
*
|
|
8790
|
+
* The default JSON serializer encodes a `FormData` as an array of `[key, entry]`
|
|
8791
|
+
* pairs where each entry is tagged as `"String"` or `"File"`.
|
|
8792
|
+
*
|
|
8793
|
+
* @category Schemas
|
|
7216
8794
|
* @since 4.0.0
|
|
7217
8795
|
*/
|
|
7218
8796
|
export const FormData: FormData = instanceOf(globalThis.FormData, {
|
|
@@ -7259,6 +8837,9 @@ export const FormData: FormData = instanceOf(globalThis.FormData, {
|
|
|
7259
8837
|
})
|
|
7260
8838
|
|
|
7261
8839
|
/**
|
|
8840
|
+
* Schema type for {@link fromFormData}.
|
|
8841
|
+
*
|
|
8842
|
+
* @category Schemas
|
|
7262
8843
|
* @since 4.0.0
|
|
7263
8844
|
*/
|
|
7264
8845
|
export interface fromFormData<S extends Top> extends decodeTo<S, FormData> {}
|
|
@@ -7351,11 +8932,19 @@ export function fromFormData<S extends Top>(schema: S): fromFormData<S> {
|
|
|
7351
8932
|
}
|
|
7352
8933
|
|
|
7353
8934
|
/**
|
|
8935
|
+
* Schema type for {@link URLSearchParams}.
|
|
8936
|
+
*
|
|
8937
|
+
* @category Schemas
|
|
7354
8938
|
* @since 4.0.0
|
|
7355
8939
|
*/
|
|
7356
8940
|
export interface URLSearchParams extends instanceOf<globalThis.URLSearchParams> {}
|
|
7357
8941
|
|
|
7358
8942
|
/**
|
|
8943
|
+
* Schema for JavaScript `URLSearchParams` objects.
|
|
8944
|
+
*
|
|
8945
|
+
* The default JSON serializer encodes a `URLSearchParams` as a query string.
|
|
8946
|
+
*
|
|
8947
|
+
* @category Schemas
|
|
7359
8948
|
* @since 4.0.0
|
|
7360
8949
|
*/
|
|
7361
8950
|
export const URLSearchParams: URLSearchParams = instanceOf(globalThis.URLSearchParams, {
|
|
@@ -7378,6 +8967,9 @@ export const URLSearchParams: URLSearchParams = instanceOf(globalThis.URLSearchP
|
|
|
7378
8967
|
})
|
|
7379
8968
|
|
|
7380
8969
|
/**
|
|
8970
|
+
* Schema type for {@link fromURLSearchParams}.
|
|
8971
|
+
*
|
|
8972
|
+
* @category Schemas
|
|
7381
8973
|
* @since 4.0.0
|
|
7382
8974
|
*/
|
|
7383
8975
|
export interface fromURLSearchParams<S extends Top> extends decodeTo<S, URLSearchParams> {}
|
|
@@ -7461,6 +9053,9 @@ export function fromURLSearchParams<S extends Top>(schema: S): fromURLSearchPara
|
|
|
7461
9053
|
}
|
|
7462
9054
|
|
|
7463
9055
|
/**
|
|
9056
|
+
* Schema type for {@link Finite}.
|
|
9057
|
+
*
|
|
9058
|
+
* @category Schemas
|
|
7464
9059
|
* @since 4.0.0
|
|
7465
9060
|
*/
|
|
7466
9061
|
export interface Finite extends Number {}
|
|
@@ -7473,6 +9068,9 @@ export interface Finite extends Number {}
|
|
|
7473
9068
|
export const Finite: Finite = Number.check(isFinite())
|
|
7474
9069
|
|
|
7475
9070
|
/**
|
|
9071
|
+
* Schema type for {@link Int}.
|
|
9072
|
+
*
|
|
9073
|
+
* @category Schemas
|
|
7476
9074
|
* @since 4.0.0
|
|
7477
9075
|
*/
|
|
7478
9076
|
export interface Int extends Number {}
|
|
@@ -7485,6 +9083,9 @@ export interface Int extends Number {}
|
|
|
7485
9083
|
export const Int: Int = Number.check(isInt())
|
|
7486
9084
|
|
|
7487
9085
|
/**
|
|
9086
|
+
* Schema type for {@link NumberFromString}.
|
|
9087
|
+
*
|
|
9088
|
+
* @category Schemas
|
|
7488
9089
|
* @since 4.0.0
|
|
7489
9090
|
*/
|
|
7490
9091
|
export interface NumberFromString extends decodeTo<Finite, String> {}
|
|
@@ -7505,6 +9106,9 @@ export const NumberFromString: NumberFromString = String.annotate({
|
|
|
7505
9106
|
}).pipe(decodeTo(Number, Transformation.numberFromString))
|
|
7506
9107
|
|
|
7507
9108
|
/**
|
|
9109
|
+
* Schema type for {@link FiniteFromString}.
|
|
9110
|
+
*
|
|
9111
|
+
* @category Schemas
|
|
7508
9112
|
* @since 4.0.0
|
|
7509
9113
|
*/
|
|
7510
9114
|
export interface FiniteFromString extends decodeTo<Finite, String> {}
|
|
@@ -7526,6 +9130,9 @@ export const FiniteFromString: FiniteFromString = String.annotate({
|
|
|
7526
9130
|
}).pipe(decodeTo(Finite, Transformation.numberFromString))
|
|
7527
9131
|
|
|
7528
9132
|
/**
|
|
9133
|
+
* Schema type for {@link Trimmed}.
|
|
9134
|
+
*
|
|
9135
|
+
* @category Schemas
|
|
7529
9136
|
* @since 4.0.0
|
|
7530
9137
|
*/
|
|
7531
9138
|
export interface Trimmed extends String {}
|
|
@@ -7538,6 +9145,9 @@ export interface Trimmed extends String {}
|
|
|
7538
9145
|
export const Trimmed: Trimmed = String.check(isTrimmed())
|
|
7539
9146
|
|
|
7540
9147
|
/**
|
|
9148
|
+
* Schema type for {@link Trim}.
|
|
9149
|
+
*
|
|
9150
|
+
* @category Schemas
|
|
7541
9151
|
* @since 4.0.0
|
|
7542
9152
|
*/
|
|
7543
9153
|
export interface Trim extends decodeTo<Trimmed, String> {}
|
|
@@ -7558,6 +9168,9 @@ export const Trim: Trim = String.annotate({
|
|
|
7558
9168
|
}).pipe(decodeTo(Trimmed, Transformation.trim()))
|
|
7559
9169
|
|
|
7560
9170
|
/**
|
|
9171
|
+
* A union schema for JavaScript property keys: `number | symbol | string`.
|
|
9172
|
+
*
|
|
9173
|
+
* @category Schemas
|
|
7561
9174
|
* @since 4.0.0
|
|
7562
9175
|
*/
|
|
7563
9176
|
export const PropertyKey = Union([Finite, Symbol, String])
|
|
@@ -7573,6 +9186,9 @@ export const StandardSchemaV1FailureResult = Struct({
|
|
|
7573
9186
|
})
|
|
7574
9187
|
|
|
7575
9188
|
/**
|
|
9189
|
+
* Schema type for {@link BooleanFromBit}.
|
|
9190
|
+
*
|
|
9191
|
+
* @category Schemas
|
|
7576
9192
|
* @since 4.0.0
|
|
7577
9193
|
*/
|
|
7578
9194
|
export interface BooleanFromBit extends decodeTo<Boolean, Literals<readonly [0, 1]>> {}
|
|
@@ -7594,6 +9210,9 @@ export const BooleanFromBit: BooleanFromBit = Literals([0, 1]).pipe(
|
|
|
7594
9210
|
)
|
|
7595
9211
|
|
|
7596
9212
|
/**
|
|
9213
|
+
* Schema type for {@link Uint8Array}.
|
|
9214
|
+
*
|
|
9215
|
+
* @category Schemas
|
|
7597
9216
|
* @since 4.0.0
|
|
7598
9217
|
*/
|
|
7599
9218
|
export interface Uint8Array extends instanceOf<globalThis.Uint8Array<ArrayBufferLike>> {}
|
|
@@ -7632,6 +9251,9 @@ export const Uint8Array: Uint8Array = instanceOf(globalThis.Uint8Array<ArrayBuff
|
|
|
7632
9251
|
})
|
|
7633
9252
|
|
|
7634
9253
|
/**
|
|
9254
|
+
* Schema type for {@link Uint8ArrayFromBase64}.
|
|
9255
|
+
*
|
|
9256
|
+
* @category Schemas
|
|
7635
9257
|
* @since 4.0.0
|
|
7636
9258
|
*/
|
|
7637
9259
|
export interface Uint8ArrayFromBase64 extends decodeTo<Uint8Array, String> {}
|
|
@@ -7654,6 +9276,9 @@ export const Uint8ArrayFromBase64: Uint8ArrayFromBase64 = Base64String.pipe(
|
|
|
7654
9276
|
)
|
|
7655
9277
|
|
|
7656
9278
|
/**
|
|
9279
|
+
* Schema type for {@link Uint8ArrayFromBase64Url}.
|
|
9280
|
+
*
|
|
9281
|
+
* @category Schemas
|
|
7657
9282
|
* @since 4.0.0
|
|
7658
9283
|
*/
|
|
7659
9284
|
export interface Uint8ArrayFromBase64Url extends decodeTo<Uint8Array, String> {}
|
|
@@ -7681,6 +9306,9 @@ export const Uint8ArrayFromBase64Url: Uint8ArrayFromBase64Url = String.annotate(
|
|
|
7681
9306
|
)
|
|
7682
9307
|
|
|
7683
9308
|
/**
|
|
9309
|
+
* Schema type for {@link Uint8ArrayFromHex}.
|
|
9310
|
+
*
|
|
9311
|
+
* @category Schemas
|
|
7684
9312
|
* @since 4.0.0
|
|
7685
9313
|
*/
|
|
7686
9314
|
export interface Uint8ArrayFromHex extends decodeTo<Uint8Array, String> {}
|
|
@@ -7708,6 +9336,9 @@ export const Uint8ArrayFromHex: Uint8ArrayFromHex = String.annotate({
|
|
|
7708
9336
|
)
|
|
7709
9337
|
|
|
7710
9338
|
/**
|
|
9339
|
+
* Schema type for {@link DateTimeUtc}.
|
|
9340
|
+
*
|
|
9341
|
+
* @category DateTime
|
|
7711
9342
|
* @since 4.0.0
|
|
7712
9343
|
*/
|
|
7713
9344
|
export interface DateTimeUtc extends declare<DateTime.Utc> {}
|
|
@@ -7747,6 +9378,9 @@ export const DateTimeUtc: DateTimeUtc = declare(
|
|
|
7747
9378
|
)
|
|
7748
9379
|
|
|
7749
9380
|
/**
|
|
9381
|
+
* Schema type for {@link DateTimeUtcFromDate}.
|
|
9382
|
+
*
|
|
9383
|
+
* @category DateTime
|
|
7750
9384
|
* @since 4.0.0
|
|
7751
9385
|
*/
|
|
7752
9386
|
export interface DateTimeUtcFromDate extends decodeTo<DateTimeUtc, Date> {}
|
|
@@ -7771,6 +9405,9 @@ export const DateTimeUtcFromDate: DateTimeUtcFromDate = DateValid.pipe(
|
|
|
7771
9405
|
)
|
|
7772
9406
|
|
|
7773
9407
|
/**
|
|
9408
|
+
* Schema type for {@link DateTimeUtcFromString}.
|
|
9409
|
+
*
|
|
9410
|
+
* @category DateTime
|
|
7774
9411
|
* @since 4.0.0
|
|
7775
9412
|
*/
|
|
7776
9413
|
export interface DateTimeUtcFromString extends decodeTo<DateTimeUtc, String> {}
|
|
@@ -7799,6 +9436,9 @@ export const DateTimeUtcFromString: DateTimeUtcFromString = String.annotate({
|
|
|
7799
9436
|
)
|
|
7800
9437
|
|
|
7801
9438
|
/**
|
|
9439
|
+
* Schema type for {@link DateTimeUtcFromMillis}.
|
|
9440
|
+
*
|
|
9441
|
+
* @category DateTime
|
|
7802
9442
|
* @since 4.0.0
|
|
7803
9443
|
*/
|
|
7804
9444
|
export interface DateTimeUtcFromMillis extends decodeTo<instanceOf<DateTime.Utc>, Number> {}
|
|
@@ -7823,6 +9463,9 @@ export const DateTimeUtcFromMillis: DateTimeUtcFromMillis = Number.pipe(
|
|
|
7823
9463
|
)
|
|
7824
9464
|
|
|
7825
9465
|
/**
|
|
9466
|
+
* Schema type for {@link TimeZoneOffset}.
|
|
9467
|
+
*
|
|
9468
|
+
* @category DateTime
|
|
7826
9469
|
* @since 4.0.0
|
|
7827
9470
|
*/
|
|
7828
9471
|
export interface TimeZoneOffset extends declare<DateTime.TimeZone.Offset> {}
|
|
@@ -7862,6 +9505,9 @@ export const TimeZoneOffset: TimeZoneOffset = declare(
|
|
|
7862
9505
|
)
|
|
7863
9506
|
|
|
7864
9507
|
/**
|
|
9508
|
+
* Schema type for {@link TimeZoneNamed}.
|
|
9509
|
+
*
|
|
9510
|
+
* @category DateTime
|
|
7865
9511
|
* @since 4.0.0
|
|
7866
9512
|
*/
|
|
7867
9513
|
export interface TimeZoneNamed extends declare<DateTime.TimeZone.Named> {}
|
|
@@ -7905,6 +9551,9 @@ export const TimeZoneNamed: TimeZoneNamed = declare(
|
|
|
7905
9551
|
)
|
|
7906
9552
|
|
|
7907
9553
|
/**
|
|
9554
|
+
* Schema type for {@link TimeZone}.
|
|
9555
|
+
*
|
|
9556
|
+
* @category DateTime
|
|
7908
9557
|
* @since 4.0.0
|
|
7909
9558
|
*/
|
|
7910
9559
|
export interface TimeZone extends declare<DateTime.TimeZone> {}
|
|
@@ -7952,6 +9601,9 @@ export const TimeZone: TimeZone = declare(
|
|
|
7952
9601
|
)
|
|
7953
9602
|
|
|
7954
9603
|
/**
|
|
9604
|
+
* Schema type for {@link DateTimeZoned}.
|
|
9605
|
+
*
|
|
9606
|
+
* @category DateTime
|
|
7955
9607
|
* @since 4.0.0
|
|
7956
9608
|
*/
|
|
7957
9609
|
export interface DateTimeZoned extends declare<DateTime.Zoned> {}
|
|
@@ -8004,6 +9656,14 @@ export const DateTimeZoned: DateTimeZoned = declare(
|
|
|
8004
9656
|
// -----------------------------------------------------------------------------
|
|
8005
9657
|
|
|
8006
9658
|
/**
|
|
9659
|
+
* Interface for schema-backed classes created with {@link Class}.
|
|
9660
|
+
*
|
|
9661
|
+
* A `Class` is a TypeScript class whose constructor validates its input
|
|
9662
|
+
* against a {@link Struct} schema. Instances are always structurally valid.
|
|
9663
|
+
*
|
|
9664
|
+
* The interface exposes the schema's `fields`, an `identifier` string, and
|
|
9665
|
+
* helpers such as `mapFields`, `annotate`, `check`, and `extend`.
|
|
9666
|
+
*
|
|
8007
9667
|
* @since 4.0.0
|
|
8008
9668
|
*/
|
|
8009
9669
|
export interface Class<Self, S extends Top & { readonly fields: Struct.Fields }, Inherited> extends
|
|
@@ -8057,6 +9717,10 @@ export interface Class<Self, S extends Top & { readonly fields: Struct.Fields },
|
|
|
8057
9717
|
type AddStaticMembers<C, Static> = C & Pick<Static, Exclude<keyof Static, keyof C>>
|
|
8058
9718
|
|
|
8059
9719
|
/**
|
|
9720
|
+
* A {@link Class} that additionally exposes an `extend` method, allowing
|
|
9721
|
+
* subclasses to be derived with extra fields while inheriting all parent
|
|
9722
|
+
* fields and validation.
|
|
9723
|
+
*
|
|
8060
9724
|
* @since 4.0.0
|
|
8061
9725
|
*/
|
|
8062
9726
|
export interface ExtendableClass<Self, S extends Top & { readonly fields: Struct.Fields }, Inherited>
|
|
@@ -8237,16 +9901,136 @@ function isStruct(schema: Struct.Fields | Struct<Struct.Fields>): schema is Stru
|
|
|
8237
9901
|
}
|
|
8238
9902
|
|
|
8239
9903
|
/**
|
|
9904
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
9905
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
9906
|
+
* input (unless `disableValidation` is set in the options).
|
|
9907
|
+
*
|
|
9908
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
9909
|
+
* type parameter can be used to add nominal brands.
|
|
9910
|
+
*
|
|
9911
|
+
* **Example** (Basic class)
|
|
9912
|
+
*
|
|
9913
|
+
* ```ts
|
|
9914
|
+
* import { Schema } from "effect"
|
|
9915
|
+
*
|
|
9916
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
9917
|
+
* name: Schema.String,
|
|
9918
|
+
* age: Schema.Number
|
|
9919
|
+
* }) {}
|
|
9920
|
+
*
|
|
9921
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
9922
|
+
* console.log(alice.name) // "Alice"
|
|
9923
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
9924
|
+
* ```
|
|
9925
|
+
*
|
|
9926
|
+
* **Example** (Extending a class)
|
|
9927
|
+
*
|
|
9928
|
+
* ```ts
|
|
9929
|
+
* import { Schema } from "effect"
|
|
9930
|
+
*
|
|
9931
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
9932
|
+
* name: Schema.String
|
|
9933
|
+
* }) {}
|
|
9934
|
+
*
|
|
9935
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
9936
|
+
* breed: Schema.String
|
|
9937
|
+
* }) {}
|
|
9938
|
+
*
|
|
9939
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
9940
|
+
* console.log(dog.name) // "Rex"
|
|
9941
|
+
* console.log(dog.breed) // "Labrador"
|
|
9942
|
+
* ```
|
|
9943
|
+
*
|
|
8240
9944
|
* @category Constructors
|
|
8241
9945
|
* @since 4.0.0
|
|
8242
9946
|
*/
|
|
8243
9947
|
export const Class: {
|
|
8244
9948
|
/**
|
|
9949
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
9950
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
9951
|
+
* input (unless `disableValidation` is set in the options).
|
|
9952
|
+
*
|
|
9953
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
9954
|
+
* type parameter can be used to add nominal brands.
|
|
9955
|
+
*
|
|
9956
|
+
* **Example** (Basic class)
|
|
9957
|
+
*
|
|
9958
|
+
* ```ts
|
|
9959
|
+
* import { Schema } from "effect"
|
|
9960
|
+
*
|
|
9961
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
9962
|
+
* name: Schema.String,
|
|
9963
|
+
* age: Schema.Number
|
|
9964
|
+
* }) {}
|
|
9965
|
+
*
|
|
9966
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
9967
|
+
* console.log(alice.name) // "Alice"
|
|
9968
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
9969
|
+
* ```
|
|
9970
|
+
*
|
|
9971
|
+
* **Example** (Extending a class)
|
|
9972
|
+
*
|
|
9973
|
+
* ```ts
|
|
9974
|
+
* import { Schema } from "effect"
|
|
9975
|
+
*
|
|
9976
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
9977
|
+
* name: Schema.String
|
|
9978
|
+
* }) {}
|
|
9979
|
+
*
|
|
9980
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
9981
|
+
* breed: Schema.String
|
|
9982
|
+
* }) {}
|
|
9983
|
+
*
|
|
9984
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
9985
|
+
* console.log(dog.name) // "Rex"
|
|
9986
|
+
* console.log(dog.breed) // "Labrador"
|
|
9987
|
+
* ```
|
|
9988
|
+
*
|
|
8245
9989
|
* @category Constructors
|
|
8246
9990
|
* @since 4.0.0
|
|
8247
9991
|
*/
|
|
8248
9992
|
<Self, Brand = {}>(identifier: string): {
|
|
8249
9993
|
/**
|
|
9994
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
9995
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
9996
|
+
* input (unless `disableValidation` is set in the options).
|
|
9997
|
+
*
|
|
9998
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
9999
|
+
* type parameter can be used to add nominal brands.
|
|
10000
|
+
*
|
|
10001
|
+
* **Example** (Basic class)
|
|
10002
|
+
*
|
|
10003
|
+
* ```ts
|
|
10004
|
+
* import { Schema } from "effect"
|
|
10005
|
+
*
|
|
10006
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
10007
|
+
* name: Schema.String,
|
|
10008
|
+
* age: Schema.Number
|
|
10009
|
+
* }) {}
|
|
10010
|
+
*
|
|
10011
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
10012
|
+
* console.log(alice.name) // "Alice"
|
|
10013
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
10014
|
+
* ```
|
|
10015
|
+
*
|
|
10016
|
+
* **Example** (Extending a class)
|
|
10017
|
+
*
|
|
10018
|
+
* ```ts
|
|
10019
|
+
* import { Schema } from "effect"
|
|
10020
|
+
*
|
|
10021
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
10022
|
+
* name: Schema.String
|
|
10023
|
+
* }) {}
|
|
10024
|
+
*
|
|
10025
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
10026
|
+
* breed: Schema.String
|
|
10027
|
+
* }) {}
|
|
10028
|
+
*
|
|
10029
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
10030
|
+
* console.log(dog.name) // "Rex"
|
|
10031
|
+
* console.log(dog.breed) // "Labrador"
|
|
10032
|
+
* ```
|
|
10033
|
+
*
|
|
8250
10034
|
* @category Constructors
|
|
8251
10035
|
* @since 4.0.0
|
|
8252
10036
|
*/
|
|
@@ -8255,6 +10039,46 @@ export const Class: {
|
|
|
8255
10039
|
annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>
|
|
8256
10040
|
): ExtendableClass<Self, Struct<Fields>, Brand>
|
|
8257
10041
|
/**
|
|
10042
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
10043
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
10044
|
+
* input (unless `disableValidation` is set in the options).
|
|
10045
|
+
*
|
|
10046
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
10047
|
+
* type parameter can be used to add nominal brands.
|
|
10048
|
+
*
|
|
10049
|
+
* **Example** (Basic class)
|
|
10050
|
+
*
|
|
10051
|
+
* ```ts
|
|
10052
|
+
* import { Schema } from "effect"
|
|
10053
|
+
*
|
|
10054
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
10055
|
+
* name: Schema.String,
|
|
10056
|
+
* age: Schema.Number
|
|
10057
|
+
* }) {}
|
|
10058
|
+
*
|
|
10059
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
10060
|
+
* console.log(alice.name) // "Alice"
|
|
10061
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
10062
|
+
* ```
|
|
10063
|
+
*
|
|
10064
|
+
* **Example** (Extending a class)
|
|
10065
|
+
*
|
|
10066
|
+
* ```ts
|
|
10067
|
+
* import { Schema } from "effect"
|
|
10068
|
+
*
|
|
10069
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
10070
|
+
* name: Schema.String
|
|
10071
|
+
* }) {}
|
|
10072
|
+
*
|
|
10073
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
10074
|
+
* breed: Schema.String
|
|
10075
|
+
* }) {}
|
|
10076
|
+
*
|
|
10077
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
10078
|
+
* console.log(dog.name) // "Rex"
|
|
10079
|
+
* console.log(dog.breed) // "Labrador"
|
|
10080
|
+
* ```
|
|
10081
|
+
*
|
|
8258
10082
|
* @category Constructors
|
|
8259
10083
|
* @since 4.0.0
|
|
8260
10084
|
*/
|
|
@@ -8270,16 +10094,79 @@ export const Class: {
|
|
|
8270
10094
|
}
|
|
8271
10095
|
|
|
8272
10096
|
/**
|
|
10097
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
10098
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
10099
|
+
* discrimination patterns.
|
|
10100
|
+
*
|
|
10101
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
10102
|
+
* it defaults to the `tag` value.
|
|
10103
|
+
*
|
|
10104
|
+
* **Example** (Tagged class)
|
|
10105
|
+
*
|
|
10106
|
+
* ```ts
|
|
10107
|
+
* import { Schema } from "effect"
|
|
10108
|
+
*
|
|
10109
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
10110
|
+
* radius: Schema.Number
|
|
10111
|
+
* }) {}
|
|
10112
|
+
*
|
|
10113
|
+
* const c = new Circle({ radius: 5 })
|
|
10114
|
+
* console.log(c._tag) // "Circle"
|
|
10115
|
+
* console.log(c.radius) // 5
|
|
10116
|
+
* ```
|
|
10117
|
+
*
|
|
8273
10118
|
* @category Constructors
|
|
8274
10119
|
* @since 4.0.0
|
|
8275
10120
|
*/
|
|
8276
10121
|
export const TaggedClass: {
|
|
8277
10122
|
/**
|
|
10123
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
10124
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
10125
|
+
* discrimination patterns.
|
|
10126
|
+
*
|
|
10127
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
10128
|
+
* it defaults to the `tag` value.
|
|
10129
|
+
*
|
|
10130
|
+
* **Example** (Tagged class)
|
|
10131
|
+
*
|
|
10132
|
+
* ```ts
|
|
10133
|
+
* import { Schema } from "effect"
|
|
10134
|
+
*
|
|
10135
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
10136
|
+
* radius: Schema.Number
|
|
10137
|
+
* }) {}
|
|
10138
|
+
*
|
|
10139
|
+
* const c = new Circle({ radius: 5 })
|
|
10140
|
+
* console.log(c._tag) // "Circle"
|
|
10141
|
+
* console.log(c.radius) // 5
|
|
10142
|
+
* ```
|
|
10143
|
+
*
|
|
8278
10144
|
* @category Constructors
|
|
8279
10145
|
* @since 4.0.0
|
|
8280
10146
|
*/
|
|
8281
10147
|
<Self, Brand = {}>(identifier?: string): {
|
|
8282
10148
|
/**
|
|
10149
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
10150
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
10151
|
+
* discrimination patterns.
|
|
10152
|
+
*
|
|
10153
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
10154
|
+
* it defaults to the `tag` value.
|
|
10155
|
+
*
|
|
10156
|
+
* **Example** (Tagged class)
|
|
10157
|
+
*
|
|
10158
|
+
* ```ts
|
|
10159
|
+
* import { Schema } from "effect"
|
|
10160
|
+
*
|
|
10161
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
10162
|
+
* radius: Schema.Number
|
|
10163
|
+
* }) {}
|
|
10164
|
+
*
|
|
10165
|
+
* const c = new Circle({ radius: 5 })
|
|
10166
|
+
* console.log(c._tag) // "Circle"
|
|
10167
|
+
* console.log(c.radius) // 5
|
|
10168
|
+
* ```
|
|
10169
|
+
*
|
|
8283
10170
|
* @category Constructors
|
|
8284
10171
|
* @since 4.0.0
|
|
8285
10172
|
*/
|
|
@@ -8289,6 +10176,27 @@ export const TaggedClass: {
|
|
|
8289
10176
|
annotations?: Annotations.Declaration<Self, readonly [TaggedStruct<Tag, Fields>]>
|
|
8290
10177
|
): ExtendableClass<Self, TaggedStruct<Tag, Fields>, Brand>
|
|
8291
10178
|
/**
|
|
10179
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
10180
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
10181
|
+
* discrimination patterns.
|
|
10182
|
+
*
|
|
10183
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
10184
|
+
* it defaults to the `tag` value.
|
|
10185
|
+
*
|
|
10186
|
+
* **Example** (Tagged class)
|
|
10187
|
+
*
|
|
10188
|
+
* ```ts
|
|
10189
|
+
* import { Schema } from "effect"
|
|
10190
|
+
*
|
|
10191
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
10192
|
+
* radius: Schema.Number
|
|
10193
|
+
* }) {}
|
|
10194
|
+
*
|
|
10195
|
+
* const c = new Circle({ radius: 5 })
|
|
10196
|
+
* console.log(c._tag) // "Circle"
|
|
10197
|
+
* console.log(c.radius) // 5
|
|
10198
|
+
* ```
|
|
10199
|
+
*
|
|
8292
10200
|
* @category Constructors
|
|
8293
10201
|
* @since 4.0.0
|
|
8294
10202
|
*/
|
|
@@ -8319,6 +10227,10 @@ export const TaggedClass: {
|
|
|
8319
10227
|
}
|
|
8320
10228
|
|
|
8321
10229
|
/**
|
|
10230
|
+
* Interface for schema-backed error classes created with {@link ErrorClass}.
|
|
10231
|
+
* Extends {@link ExtendableClass} and is also a `YieldableError`, so instances
|
|
10232
|
+
* can be yielded inside `Effect.gen` as failures.
|
|
10233
|
+
*
|
|
8322
10234
|
* @since 4.0.0
|
|
8323
10235
|
*/
|
|
8324
10236
|
export interface ErrorClass<Self, S extends Top & { readonly fields: Struct.Fields }, Inherited>
|
|
@@ -8326,16 +10238,73 @@ export interface ErrorClass<Self, S extends Top & { readonly fields: Struct.Fiel
|
|
|
8326
10238
|
{}
|
|
8327
10239
|
|
|
8328
10240
|
/**
|
|
10241
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
10242
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
10243
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
10244
|
+
* `Effect.gen`.
|
|
10245
|
+
*
|
|
10246
|
+
* **Example** (Schema-backed error)
|
|
10247
|
+
*
|
|
10248
|
+
* ```ts
|
|
10249
|
+
* import { Effect, Schema } from "effect"
|
|
10250
|
+
*
|
|
10251
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
10252
|
+
* id: Schema.Number
|
|
10253
|
+
* }) {}
|
|
10254
|
+
*
|
|
10255
|
+
* const program = Effect.gen(function*() {
|
|
10256
|
+
* yield* new NotFound({ id: 1 })
|
|
10257
|
+
* })
|
|
10258
|
+
* ```
|
|
10259
|
+
*
|
|
8329
10260
|
* @category Constructors
|
|
8330
10261
|
* @since 4.0.0
|
|
8331
10262
|
*/
|
|
8332
10263
|
export const ErrorClass: {
|
|
8333
10264
|
/**
|
|
10265
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
10266
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
10267
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
10268
|
+
* `Effect.gen`.
|
|
10269
|
+
*
|
|
10270
|
+
* **Example** (Schema-backed error)
|
|
10271
|
+
*
|
|
10272
|
+
* ```ts
|
|
10273
|
+
* import { Effect, Schema } from "effect"
|
|
10274
|
+
*
|
|
10275
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
10276
|
+
* id: Schema.Number
|
|
10277
|
+
* }) {}
|
|
10278
|
+
*
|
|
10279
|
+
* const program = Effect.gen(function*() {
|
|
10280
|
+
* yield* new NotFound({ id: 1 })
|
|
10281
|
+
* })
|
|
10282
|
+
* ```
|
|
10283
|
+
*
|
|
8334
10284
|
* @category Constructors
|
|
8335
10285
|
* @since 4.0.0
|
|
8336
10286
|
*/
|
|
8337
10287
|
<Self, Brand = {}>(identifier: string): {
|
|
8338
10288
|
/**
|
|
10289
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
10290
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
10291
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
10292
|
+
* `Effect.gen`.
|
|
10293
|
+
*
|
|
10294
|
+
* **Example** (Schema-backed error)
|
|
10295
|
+
*
|
|
10296
|
+
* ```ts
|
|
10297
|
+
* import { Effect, Schema } from "effect"
|
|
10298
|
+
*
|
|
10299
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
10300
|
+
* id: Schema.Number
|
|
10301
|
+
* }) {}
|
|
10302
|
+
*
|
|
10303
|
+
* const program = Effect.gen(function*() {
|
|
10304
|
+
* yield* new NotFound({ id: 1 })
|
|
10305
|
+
* })
|
|
10306
|
+
* ```
|
|
10307
|
+
*
|
|
8339
10308
|
* @category Constructors
|
|
8340
10309
|
* @since 4.0.0
|
|
8341
10310
|
*/
|
|
@@ -8344,6 +10313,25 @@ export const ErrorClass: {
|
|
|
8344
10313
|
annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>
|
|
8345
10314
|
): ErrorClass<Self, Struct<Fields>, Cause_.YieldableError & Brand>
|
|
8346
10315
|
/**
|
|
10316
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
10317
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
10318
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
10319
|
+
* `Effect.gen`.
|
|
10320
|
+
*
|
|
10321
|
+
* **Example** (Schema-backed error)
|
|
10322
|
+
*
|
|
10323
|
+
* ```ts
|
|
10324
|
+
* import { Effect, Schema } from "effect"
|
|
10325
|
+
*
|
|
10326
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
10327
|
+
* id: Schema.Number
|
|
10328
|
+
* }) {}
|
|
10329
|
+
*
|
|
10330
|
+
* const program = Effect.gen(function*() {
|
|
10331
|
+
* yield* new NotFound({ id: 1 })
|
|
10332
|
+
* })
|
|
10333
|
+
* ```
|
|
10334
|
+
*
|
|
8347
10335
|
* @category Constructors
|
|
8348
10336
|
* @since 4.0.0
|
|
8349
10337
|
*/
|
|
@@ -8361,16 +10349,70 @@ export const ErrorClass: {
|
|
|
8361
10349
|
}
|
|
8362
10350
|
|
|
8363
10351
|
/**
|
|
10352
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
10353
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
10354
|
+
* union member.
|
|
10355
|
+
*
|
|
10356
|
+
* **Example** (Tagged error class)
|
|
10357
|
+
*
|
|
10358
|
+
* ```ts
|
|
10359
|
+
* import { Effect, Schema } from "effect"
|
|
10360
|
+
*
|
|
10361
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
10362
|
+
* id: Schema.Number
|
|
10363
|
+
* }) {}
|
|
10364
|
+
*
|
|
10365
|
+
* const program = Effect.gen(function*() {
|
|
10366
|
+
* yield* new NotFound({ id: 42 })
|
|
10367
|
+
* })
|
|
10368
|
+
* ```
|
|
10369
|
+
*
|
|
8364
10370
|
* @category Constructors
|
|
8365
10371
|
* @since 4.0.0
|
|
8366
10372
|
*/
|
|
8367
10373
|
export const TaggedErrorClass: {
|
|
8368
10374
|
/**
|
|
10375
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
10376
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
10377
|
+
* union member.
|
|
10378
|
+
*
|
|
10379
|
+
* **Example** (Tagged error class)
|
|
10380
|
+
*
|
|
10381
|
+
* ```ts
|
|
10382
|
+
* import { Effect, Schema } from "effect"
|
|
10383
|
+
*
|
|
10384
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
10385
|
+
* id: Schema.Number
|
|
10386
|
+
* }) {}
|
|
10387
|
+
*
|
|
10388
|
+
* const program = Effect.gen(function*() {
|
|
10389
|
+
* yield* new NotFound({ id: 42 })
|
|
10390
|
+
* })
|
|
10391
|
+
* ```
|
|
10392
|
+
*
|
|
8369
10393
|
* @category Constructors
|
|
8370
10394
|
* @since 4.0.0
|
|
8371
10395
|
*/
|
|
8372
10396
|
<Self, Brand = {}>(identifier?: string): {
|
|
8373
10397
|
/**
|
|
10398
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
10399
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
10400
|
+
* union member.
|
|
10401
|
+
*
|
|
10402
|
+
* **Example** (Tagged error class)
|
|
10403
|
+
*
|
|
10404
|
+
* ```ts
|
|
10405
|
+
* import { Effect, Schema } from "effect"
|
|
10406
|
+
*
|
|
10407
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
10408
|
+
* id: Schema.Number
|
|
10409
|
+
* }) {}
|
|
10410
|
+
*
|
|
10411
|
+
* const program = Effect.gen(function*() {
|
|
10412
|
+
* yield* new NotFound({ id: 42 })
|
|
10413
|
+
* })
|
|
10414
|
+
* ```
|
|
10415
|
+
*
|
|
8374
10416
|
* @category Constructors
|
|
8375
10417
|
* @since 4.0.0
|
|
8376
10418
|
*/
|
|
@@ -8380,6 +10422,24 @@ export const TaggedErrorClass: {
|
|
|
8380
10422
|
annotations?: Annotations.Declaration<Self, readonly [TaggedStruct<Tag, Fields>]>
|
|
8381
10423
|
): ErrorClass<Self, TaggedStruct<Tag, Fields>, Cause_.YieldableError & Brand>
|
|
8382
10424
|
/**
|
|
10425
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
10426
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
10427
|
+
* union member.
|
|
10428
|
+
*
|
|
10429
|
+
* **Example** (Tagged error class)
|
|
10430
|
+
*
|
|
10431
|
+
* ```ts
|
|
10432
|
+
* import { Effect, Schema } from "effect"
|
|
10433
|
+
*
|
|
10434
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
10435
|
+
* id: Schema.Number
|
|
10436
|
+
* }) {}
|
|
10437
|
+
*
|
|
10438
|
+
* const program = Effect.gen(function*() {
|
|
10439
|
+
* yield* new NotFound({ id: 42 })
|
|
10440
|
+
* })
|
|
10441
|
+
* ```
|
|
10442
|
+
*
|
|
8383
10443
|
* @category Constructors
|
|
8384
10444
|
* @since 4.0.0
|
|
8385
10445
|
*/
|
|
@@ -8414,12 +10474,21 @@ export const TaggedErrorClass: {
|
|
|
8414
10474
|
// -----------------------------------------------------------------------------
|
|
8415
10475
|
|
|
8416
10476
|
/**
|
|
10477
|
+
* A thunk that, given the `fast-check` module, returns an `Arbitrary<T>`.
|
|
10478
|
+
* Use this type when you need to defer instantiation of the arbitrary, for
|
|
10479
|
+
* example to support recursive schemas.
|
|
10480
|
+
*
|
|
8417
10481
|
* @category Arbitrary
|
|
8418
10482
|
* @since 4.0.0
|
|
8419
10483
|
*/
|
|
8420
10484
|
export type LazyArbitrary<T> = (fc: typeof FastCheck) => FastCheck.Arbitrary<T>
|
|
8421
10485
|
|
|
8422
10486
|
/**
|
|
10487
|
+
* Derives a {@link LazyArbitrary} from a schema. The result is memoized so
|
|
10488
|
+
* repeated calls with the same schema are cheap.
|
|
10489
|
+
*
|
|
10490
|
+
* Prefer {@link toArbitrary} when you just need the arbitrary directly.
|
|
10491
|
+
*
|
|
8423
10492
|
* @category Arbitrary
|
|
8424
10493
|
* @since 4.0.0
|
|
8425
10494
|
*/
|
|
@@ -8429,6 +10498,24 @@ export function toArbitraryLazy<S extends Top>(schema: S): LazyArbitrary<S["Type
|
|
|
8429
10498
|
}
|
|
8430
10499
|
|
|
8431
10500
|
/**
|
|
10501
|
+
* Derives a `fast-check` `Arbitrary` from a schema for property-based
|
|
10502
|
+
* testing. The derived arbitrary generates values that satisfy the schema.
|
|
10503
|
+
*
|
|
10504
|
+
* **Example** (Generating arbitrary values)
|
|
10505
|
+
*
|
|
10506
|
+
* ```ts
|
|
10507
|
+
* import { Schema } from "effect"
|
|
10508
|
+
* import * as FastCheck from "fast-check"
|
|
10509
|
+
*
|
|
10510
|
+
* const PersonArb = Schema.toArbitrary(
|
|
10511
|
+
* Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
10512
|
+
* )
|
|
10513
|
+
*
|
|
10514
|
+
* // Sample a random value
|
|
10515
|
+
* const sample = FastCheck.sample(PersonArb, 1)[0]
|
|
10516
|
+
* console.log(typeof sample.name) // "string"
|
|
10517
|
+
* ```
|
|
10518
|
+
*
|
|
8432
10519
|
* @category Arbitrary
|
|
8433
10520
|
* @since 4.0.0
|
|
8434
10521
|
*/
|
|
@@ -8456,6 +10543,13 @@ export function overrideToFormatter<S extends Top>(toFormatter: () => Formatter<
|
|
|
8456
10543
|
}
|
|
8457
10544
|
|
|
8458
10545
|
/**
|
|
10546
|
+
* Derives a string formatter function from a schema. The formatter converts
|
|
10547
|
+
* a value to its human-readable string representation, recursing into structs,
|
|
10548
|
+
* arrays, and unions.
|
|
10549
|
+
*
|
|
10550
|
+
* The optional `onBefore` hook lets you intercept specific AST nodes before
|
|
10551
|
+
* the default formatting logic runs.
|
|
10552
|
+
*
|
|
8459
10553
|
* @category Formatter
|
|
8460
10554
|
* @since 4.0.0
|
|
8461
10555
|
*/
|
|
@@ -8600,10 +10694,9 @@ export function toFormatter<T>(schema: Schema<T>, options?: {
|
|
|
8600
10694
|
// -----------------------------------------------------------------------------
|
|
8601
10695
|
|
|
8602
10696
|
/**
|
|
8603
|
-
*
|
|
8604
|
-
*
|
|
8605
|
-
*
|
|
8606
|
-
* the schema invariant.
|
|
10697
|
+
* Overrides the equivalence derivation for a schema by supplying a custom
|
|
10698
|
+
* `Equivalence`. Use this when the default structural equivalence derived by
|
|
10699
|
+
* {@link toEquivalence} is not appropriate for a type.
|
|
8607
10700
|
*
|
|
8608
10701
|
* @category Equivalence
|
|
8609
10702
|
* @since 4.0.0
|
|
@@ -8613,6 +10706,21 @@ export function overrideToEquivalence<S extends Top>(toEquivalence: () => Equiva
|
|
|
8613
10706
|
}
|
|
8614
10707
|
|
|
8615
10708
|
/**
|
|
10709
|
+
* Derives an `Equivalence` from a schema. Two values are considered equal when
|
|
10710
|
+
* every field (and nested field) compares equal according to the schema
|
|
10711
|
+
* structure.
|
|
10712
|
+
*
|
|
10713
|
+
* **Example** (Struct equivalence)
|
|
10714
|
+
*
|
|
10715
|
+
* ```ts
|
|
10716
|
+
* import { Schema } from "effect"
|
|
10717
|
+
*
|
|
10718
|
+
* const eq = Schema.toEquivalence(Schema.Struct({ id: Schema.Number, name: Schema.String }))
|
|
10719
|
+
*
|
|
10720
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 1, name: "Alice" })) // true
|
|
10721
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 2, name: "Alice" })) // false
|
|
10722
|
+
* ```
|
|
10723
|
+
*
|
|
8616
10724
|
* @category Equivalence
|
|
8617
10725
|
* @since 4.0.0
|
|
8618
10726
|
*/
|
|
@@ -8625,6 +10733,10 @@ export function toEquivalence<T>(schema: Schema<T>): Equivalence.Equivalence<T>
|
|
|
8625
10733
|
// -----------------------------------------------------------------------------
|
|
8626
10734
|
|
|
8627
10735
|
/**
|
|
10736
|
+
* Derives an intermediate `SchemaRepresentation.Document` from a schema. This
|
|
10737
|
+
* document is used internally by {@link toJsonSchemaDocument} and related
|
|
10738
|
+
* functions to produce JSON Schema output.
|
|
10739
|
+
*
|
|
8628
10740
|
* @category Representation
|
|
8629
10741
|
* @since 4.0.0
|
|
8630
10742
|
*/
|
|
@@ -8637,6 +10749,8 @@ export function toRepresentation(schema: Top): SchemaRepresentation.Document {
|
|
|
8637
10749
|
// -----------------------------------------------------------------------------
|
|
8638
10750
|
|
|
8639
10751
|
/**
|
|
10752
|
+
* Options for {@link toJsonSchemaDocument}.
|
|
10753
|
+
*
|
|
8640
10754
|
* @since 4.0.0
|
|
8641
10755
|
*/
|
|
8642
10756
|
export interface ToJsonSchemaOptions {
|
|
@@ -8680,14 +10794,20 @@ export function toJsonSchemaDocument(schema: Top, options?: ToJsonSchemaOptions)
|
|
|
8680
10794
|
// -----------------------------------------------------------------------------
|
|
8681
10795
|
|
|
8682
10796
|
/**
|
|
10797
|
+
* Derives a canonical JSON codec from a schema. The encoded form is `unknown`
|
|
10798
|
+
* (any JSON-compatible value), decoded to the schema's `Type`.
|
|
10799
|
+
*
|
|
8683
10800
|
* @category Canonical Codecs
|
|
8684
10801
|
* @since 4.0.0
|
|
8685
10802
|
*/
|
|
8686
|
-
export function toCodecJson<T, E, RD, RE>(schema: Codec<T, E, RD, RE>): Codec<T,
|
|
10803
|
+
export function toCodecJson<T, E, RD, RE>(schema: Codec<T, E, RD, RE>): Codec<T, Json, RD, RE> {
|
|
8687
10804
|
return make(InternalToCodec.toCodecJson(schema.ast))
|
|
8688
10805
|
}
|
|
8689
10806
|
|
|
8690
10807
|
/**
|
|
10808
|
+
* Derives an isomorphism codec from a schema. The encoded form is the
|
|
10809
|
+
* schema's `Iso` type — the intermediate representation used for round-tripping.
|
|
10810
|
+
*
|
|
8691
10811
|
* @category Canonical Codecs
|
|
8692
10812
|
* @since 4.0.0
|
|
8693
10813
|
*/
|
|
@@ -8696,6 +10816,9 @@ export function toCodecIso<S extends Top>(schema: S): Codec<S["Type"], S["Iso"]>
|
|
|
8696
10816
|
}
|
|
8697
10817
|
|
|
8698
10818
|
/**
|
|
10819
|
+
* A {@link Tree} of `string | undefined` nodes. Leaf values are either a
|
|
10820
|
+
* string representation or `undefined` for opaque/declaration types.
|
|
10821
|
+
*
|
|
8699
10822
|
* @category Canonical Codecs
|
|
8700
10823
|
* @since 4.0.0
|
|
8701
10824
|
*/
|
|
@@ -8749,6 +10872,10 @@ type XmlEncoderOptions = {
|
|
|
8749
10872
|
}
|
|
8750
10873
|
|
|
8751
10874
|
/**
|
|
10875
|
+
* Derives an XML encoder from a codec. Encodes a value to an XML string by
|
|
10876
|
+
* first converting it through {@link toCodecStringTree}, then serializing the
|
|
10877
|
+
* resulting tree to XML.
|
|
10878
|
+
*
|
|
8752
10879
|
* @category Canonical Codecs
|
|
8753
10880
|
* @since 4.0.0
|
|
8754
10881
|
*/
|
|
@@ -9009,6 +11136,9 @@ function onSerializerEnsureArray(ast: AST.AST): AST.AST {
|
|
|
9009
11136
|
// -----------------------------------------------------------------------------
|
|
9010
11137
|
|
|
9011
11138
|
/**
|
|
11139
|
+
* Derives an `Iso` optic from a schema that isomorphically converts between
|
|
11140
|
+
* the schema's `Type` and its `Iso` (intermediate / serialized form).
|
|
11141
|
+
*
|
|
9012
11142
|
* @category Optic
|
|
9013
11143
|
* @since 4.0.0
|
|
9014
11144
|
*/
|
|
@@ -9018,6 +11148,8 @@ export function toIso<S extends Top>(schema: S): Optic_.Iso<S["Type"], S["Iso"]>
|
|
|
9018
11148
|
}
|
|
9019
11149
|
|
|
9020
11150
|
/**
|
|
11151
|
+
* Returns an identity `Iso` over the schema's source (`Type`) side.
|
|
11152
|
+
*
|
|
9021
11153
|
* @category Optic
|
|
9022
11154
|
* @since 4.0.0
|
|
9023
11155
|
*/
|
|
@@ -9026,6 +11158,8 @@ export function toIsoSource<S extends Top>(_: S): Optic_.Iso<S["Type"], S["Type"
|
|
|
9026
11158
|
}
|
|
9027
11159
|
|
|
9028
11160
|
/**
|
|
11161
|
+
* Returns an identity `Iso` over the schema's focus (`Iso`) side.
|
|
11162
|
+
*
|
|
9029
11163
|
* @category Optic
|
|
9030
11164
|
* @since 4.0.0
|
|
9031
11165
|
*/
|
|
@@ -9034,6 +11168,9 @@ export function toIsoFocus<S extends Top>(_: S): Optic_.Iso<S["Iso"], S["Iso"]>
|
|
|
9034
11168
|
}
|
|
9035
11169
|
|
|
9036
11170
|
/**
|
|
11171
|
+
* The schema type returned by {@link overrideToCodecIso}. Carries a custom
|
|
11172
|
+
* `Iso` type parameter and exposes the original `schema`.
|
|
11173
|
+
*
|
|
9037
11174
|
* @category Optic
|
|
9038
11175
|
* @since 4.0.0
|
|
9039
11176
|
*/
|
|
@@ -9061,10 +11198,11 @@ export interface overrideToCodecIso<S extends Top, Iso> extends
|
|
|
9061
11198
|
}
|
|
9062
11199
|
|
|
9063
11200
|
/**
|
|
9064
|
-
*
|
|
9065
|
-
*
|
|
9066
|
-
*
|
|
9067
|
-
* the
|
|
11201
|
+
* Overrides the ISO codec derivation for a schema by providing a target codec
|
|
11202
|
+
* and explicit `decode`/`encode` getters. The resulting schema carries a
|
|
11203
|
+
* custom `Iso` type, which changes the schema's type parameter — use
|
|
11204
|
+
* {@link overrideToCodecIso} when the default ISO transformation is not
|
|
11205
|
+
* appropriate.
|
|
9068
11206
|
*
|
|
9069
11207
|
* @category Optic
|
|
9070
11208
|
* @since 4.0.0
|
|
@@ -9091,11 +11229,15 @@ export function overrideToCodecIso<S extends Top, Iso>(
|
|
|
9091
11229
|
// -----------------------------------------------------------------------------
|
|
9092
11230
|
|
|
9093
11231
|
/**
|
|
11232
|
+
* Derives a JSON Patch differ from a codec. Serializes values to JSON (via
|
|
11233
|
+
* {@link toCodecJson}), computes RFC 6902 JSON Patch operations between old
|
|
11234
|
+
* and new values, and can apply patches back to the typed value.
|
|
11235
|
+
*
|
|
9094
11236
|
* @category JsonPatch
|
|
9095
11237
|
* @since 4.0.0
|
|
9096
11238
|
*/
|
|
9097
11239
|
export function toDifferJsonPatch<T, E>(schema: Codec<T, E>): Differ<T, JsonPatch.JsonPatch> {
|
|
9098
|
-
const serializer = toCodecJson(schema)
|
|
11240
|
+
const serializer = toCodecJson(schema)
|
|
9099
11241
|
const get = Parser.encodeSync(serializer)
|
|
9100
11242
|
const set = Parser.decodeSync(serializer)
|
|
9101
11243
|
return {
|
|
@@ -9117,6 +11259,9 @@ export function toDifferJsonPatch<T, E>(schema: Codec<T, E>): Differ<T, JsonPatc
|
|
|
9117
11259
|
export type Tree<Node> = Node | TreeRecord<Node> | ReadonlyArray<Tree<Node>>
|
|
9118
11260
|
|
|
9119
11261
|
/**
|
|
11262
|
+
* A record node in a {@link Tree}: an object mapping string keys to child
|
|
11263
|
+
* `Tree` nodes.
|
|
11264
|
+
*
|
|
9120
11265
|
* @category Tree
|
|
9121
11266
|
* @since 4.0.0
|
|
9122
11267
|
*/
|
|
@@ -9125,6 +11270,10 @@ export interface TreeRecord<A> {
|
|
|
9125
11270
|
}
|
|
9126
11271
|
|
|
9127
11272
|
/**
|
|
11273
|
+
* Creates a recursive schema for a {@link Tree} of values described by `node`.
|
|
11274
|
+
* The resulting schema accepts a single node value, an array of trees, or an
|
|
11275
|
+
* object whose values are trees.
|
|
11276
|
+
*
|
|
9128
11277
|
* @category Tree
|
|
9129
11278
|
* @since 4.0.0
|
|
9130
11279
|
*/
|
|
@@ -9144,18 +11293,27 @@ export function Tree<S extends Top>(node: S) {
|
|
|
9144
11293
|
}
|
|
9145
11294
|
|
|
9146
11295
|
/**
|
|
11296
|
+
* Recursive TypeScript type for any valid immutable JSON value: `null`,
|
|
11297
|
+
* `number`, `boolean`, `string`, a readonly array of `Json` values, or a
|
|
11298
|
+
* readonly record of `string → Json`. For the corresponding schema, see the
|
|
11299
|
+
* {@link Json} const.
|
|
11300
|
+
*
|
|
9147
11301
|
* @category JSON
|
|
9148
11302
|
* @since 4.0.0
|
|
9149
11303
|
*/
|
|
9150
11304
|
export type Json = null | number | boolean | string | JsonArray | JsonObject
|
|
9151
11305
|
|
|
9152
11306
|
/**
|
|
11307
|
+
* A readonly array of {@link Json} values.
|
|
11308
|
+
*
|
|
9153
11309
|
* @category JSON
|
|
9154
11310
|
* @since 4.0.0
|
|
9155
11311
|
*/
|
|
9156
11312
|
export interface JsonArray extends ReadonlyArray<Json> {}
|
|
9157
11313
|
|
|
9158
11314
|
/**
|
|
11315
|
+
* A readonly record whose values are {@link Json} values.
|
|
11316
|
+
*
|
|
9159
11317
|
* @category JSON
|
|
9160
11318
|
* @since 4.0.0
|
|
9161
11319
|
*/
|
|
@@ -9164,6 +11322,17 @@ export interface JsonObject {
|
|
|
9164
11322
|
}
|
|
9165
11323
|
|
|
9166
11324
|
/**
|
|
11325
|
+
* Schema that accepts and validates any immutable JSON-compatible value.
|
|
11326
|
+
*
|
|
11327
|
+
* **Example** (Validating a JSON value)
|
|
11328
|
+
*
|
|
11329
|
+
* ```ts
|
|
11330
|
+
* import { Schema } from "effect"
|
|
11331
|
+
*
|
|
11332
|
+
* const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
|
|
11333
|
+
* console.log(result._tag) // "Some"
|
|
11334
|
+
* ```
|
|
11335
|
+
*
|
|
9167
11336
|
* @category JSON
|
|
9168
11337
|
* @since 4.0.0
|
|
9169
11338
|
*/
|
|
@@ -9176,12 +11345,16 @@ export const Json: Codec<Json> = make(AST.Json)
|
|
|
9176
11345
|
export type MutableJson = null | number | boolean | string | MutableJsonArray | MutableJsonObject
|
|
9177
11346
|
|
|
9178
11347
|
/**
|
|
11348
|
+
* A mutable array of {@link MutableJson} values.
|
|
11349
|
+
*
|
|
9179
11350
|
* @category JSON
|
|
9180
11351
|
* @since 4.0.0
|
|
9181
11352
|
*/
|
|
9182
11353
|
export interface MutableJsonArray extends Array<MutableJson> {}
|
|
9183
11354
|
|
|
9184
11355
|
/**
|
|
11356
|
+
* A mutable record whose values are {@link MutableJson} values.
|
|
11357
|
+
*
|
|
9185
11358
|
* @category JSON
|
|
9186
11359
|
* @since 4.0.0
|
|
9187
11360
|
*/
|
|
@@ -9190,6 +11363,9 @@ export interface MutableJsonObject {
|
|
|
9190
11363
|
}
|
|
9191
11364
|
|
|
9192
11365
|
/**
|
|
11366
|
+
* Schema that accepts any mutable JSON-compatible value. See {@link Json} for
|
|
11367
|
+
* the immutable variant.
|
|
11368
|
+
*
|
|
9193
11369
|
* @category JSON
|
|
9194
11370
|
* @since 4.0.0
|
|
9195
11371
|
*/
|
|
@@ -9210,6 +11386,13 @@ export function resolveInto<S extends Top>(schema: S): S["~annotate.in"] | undef
|
|
|
9210
11386
|
}
|
|
9211
11387
|
|
|
9212
11388
|
/**
|
|
11389
|
+
* The `Annotations` namespace groups all annotation interfaces used to attach
|
|
11390
|
+
* metadata to schemas. Annotations control documentation, validation messages,
|
|
11391
|
+
* JSON Schema generation, equivalence, arbitrary generation, and more.
|
|
11392
|
+
*
|
|
11393
|
+
* Use {@link resolveInto} to read the annotations attached to a schema at
|
|
11394
|
+
* runtime.
|
|
11395
|
+
*
|
|
9213
11396
|
* @since 4.0.0
|
|
9214
11397
|
*/
|
|
9215
11398
|
export declare namespace Annotations {
|
|
@@ -9259,6 +11442,9 @@ export declare namespace Annotations {
|
|
|
9259
11442
|
}
|
|
9260
11443
|
|
|
9261
11444
|
/**
|
|
11445
|
+
* Annotations shared by all schema nodes. These map to common JSON Schema /
|
|
11446
|
+
* OpenAPI fields: `title`, `description`, `format`, etc.
|
|
11447
|
+
*
|
|
9262
11448
|
* @since 4.0.0
|
|
9263
11449
|
*/
|
|
9264
11450
|
export interface Augment extends Annotations {
|
|
@@ -9274,6 +11460,8 @@ export declare namespace Annotations {
|
|
|
9274
11460
|
}
|
|
9275
11461
|
|
|
9276
11462
|
/**
|
|
11463
|
+
* Extends {@link Augment} with type-parametric `default` and `examples` fields.
|
|
11464
|
+
*
|
|
9277
11465
|
* @since 4.0.0
|
|
9278
11466
|
*/
|
|
9279
11467
|
export interface Documentation<T> extends Augment {
|
|
@@ -9282,6 +11470,10 @@ export declare namespace Annotations {
|
|
|
9282
11470
|
}
|
|
9283
11471
|
|
|
9284
11472
|
/**
|
|
11473
|
+
* Annotations for struct property schemas. Extends {@link Documentation}
|
|
11474
|
+
* with an optional `messageMissingKey` to override the error message when
|
|
11475
|
+
* the property key is absent during decoding.
|
|
11476
|
+
*
|
|
9285
11477
|
* @category Model
|
|
9286
11478
|
* @since 4.0.0
|
|
9287
11479
|
*/
|
|
@@ -9293,6 +11485,11 @@ export declare namespace Annotations {
|
|
|
9293
11485
|
}
|
|
9294
11486
|
|
|
9295
11487
|
/**
|
|
11488
|
+
* Base annotations shared by all composite schema nodes. Extends
|
|
11489
|
+
* {@link Documentation} with error messages, branding, parse options, and
|
|
11490
|
+
* arbitrary generation hooks. {@link Declaration} and other annotation
|
|
11491
|
+
* interfaces build on top of this.
|
|
11492
|
+
*
|
|
9296
11493
|
* @category Model
|
|
9297
11494
|
* @since 4.0.0
|
|
9298
11495
|
*/
|
|
@@ -9339,6 +11536,12 @@ export declare namespace Annotations {
|
|
|
9339
11536
|
}
|
|
9340
11537
|
|
|
9341
11538
|
/**
|
|
11539
|
+
* Full annotation set for `Declaration` schema nodes — used when defining
|
|
11540
|
+
* custom, opaque schema types via `Schema.declare`. Extends {@link Bottom}
|
|
11541
|
+
* with optional codec, arbitrary, equivalence, and formatter hooks so that
|
|
11542
|
+
* derived capabilities (JSON encoding, property testing, etc.) can be
|
|
11543
|
+
* provided for the custom type.
|
|
11544
|
+
*
|
|
9342
11545
|
* @category Model
|
|
9343
11546
|
* @since 4.0.0
|
|
9344
11547
|
*/
|
|
@@ -9375,10 +11578,9 @@ export declare namespace Annotations {
|
|
|
9375
11578
|
}
|
|
9376
11579
|
|
|
9377
11580
|
/**
|
|
9378
|
-
*
|
|
9379
|
-
*
|
|
9380
|
-
*
|
|
9381
|
-
* invariant
|
|
11581
|
+
* Annotations for filter schema nodes (created via `Schema.filter`). Extends
|
|
11582
|
+
* {@link Augment} with an optional error message, identifier, and metadata.
|
|
11583
|
+
* Filters are intentionally non-parametric to keep them covariant.
|
|
9382
11584
|
*
|
|
9383
11585
|
* @category Model
|
|
9384
11586
|
* @since 4.0.0
|