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/dist/Schema.js
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
|
import * as Arr from "./Array.js";
|
|
@@ -45,9 +127,51 @@ import * as Struct_ from "./Struct.js";
|
|
|
45
127
|
import * as FastCheck from "./testing/FastCheck.js";
|
|
46
128
|
const TypeId = InternalSchema.TypeId;
|
|
47
129
|
/**
|
|
48
|
-
*
|
|
130
|
+
* Creates a schema for a **parametric** type (a generic container such as
|
|
131
|
+
* `Array<A>`, `Option<A>`, etc.) by accepting a list of type-parameter schemas
|
|
132
|
+
* and a decoder factory.
|
|
133
|
+
*
|
|
134
|
+
* The outer call `declareConstructor<T, E, Iso>()` fixes the decoded type `T`,
|
|
135
|
+
* the encoded type `E`, and the optional iso type. The inner call receives:
|
|
136
|
+
* - `typeParameters` — the concrete schemas for each type variable
|
|
137
|
+
* - `run` — a factory that, given resolved codecs for each type parameter,
|
|
138
|
+
* returns a parsing function `(u, ast, options) => Effect<T, Issue>`
|
|
139
|
+
* - `annotations` — optional metadata
|
|
140
|
+
*
|
|
141
|
+
* @see {@link declare} for creating schemas for non-parametric types.
|
|
142
|
+
*
|
|
143
|
+
* **Example** (Schema for a parametric `Box<A>` type)
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { Effect, Schema } from "effect"
|
|
147
|
+
* import * as SchemaParser from "effect/SchemaParser"
|
|
148
|
+
* import * as Issue from "effect/SchemaIssue"
|
|
149
|
+
* import * as Option from "effect/Option"
|
|
150
|
+
*
|
|
151
|
+
* interface Box<A> {
|
|
152
|
+
* readonly value: A
|
|
153
|
+
* }
|
|
49
154
|
*
|
|
50
|
-
*
|
|
155
|
+
* const isBox = (u: unknown): u is Box<unknown> =>
|
|
156
|
+
* typeof u === "object" && u !== null && "value" in u
|
|
157
|
+
*
|
|
158
|
+
* const Box = <A extends Schema.Top>(item: A) =>
|
|
159
|
+
* Schema.declareConstructor<Box<A["Type"]>, Box<A["Encoded"]>>()(
|
|
160
|
+
* [item],
|
|
161
|
+
* ([itemCodec]) =>
|
|
162
|
+
* (u, ast, options) => {
|
|
163
|
+
* if (!isBox(u)) {
|
|
164
|
+
* return Effect.fail(new Issue.InvalidType(ast, Option.some(u)))
|
|
165
|
+
* }
|
|
166
|
+
* return Effect.map(
|
|
167
|
+
* SchemaParser.decodeUnknownEffect(itemCodec)(u.value, options),
|
|
168
|
+
* (value) => ({ value })
|
|
169
|
+
* )
|
|
170
|
+
* }
|
|
171
|
+
* )
|
|
172
|
+
*
|
|
173
|
+
* const schema = Box(Schema.Number)
|
|
174
|
+
* ```
|
|
51
175
|
*
|
|
52
176
|
* @category Constructors
|
|
53
177
|
* @since 4.0.0
|
|
@@ -58,18 +182,60 @@ export function declareConstructor() {
|
|
|
58
182
|
};
|
|
59
183
|
}
|
|
60
184
|
/**
|
|
61
|
-
*
|
|
185
|
+
* Creates a schema for a **non-parametric** opaque type using a type-guard
|
|
186
|
+
* function. The schema accepts any unknown value and succeeds when `is` returns
|
|
187
|
+
* `true`, failing with an `InvalidType` issue otherwise.
|
|
188
|
+
*
|
|
189
|
+
* Use this when the type has no type parameters. For parametric types such as
|
|
190
|
+
* `Option<A>` or `Array<A>`, use {@link declareConstructor} instead.
|
|
191
|
+
*
|
|
192
|
+
* **Example** (Schema for a custom `UserId` branded type)
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* import { Schema } from "effect"
|
|
196
|
+
*
|
|
197
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
198
|
+
*
|
|
199
|
+
* const isUserId = (u: unknown): u is UserId =>
|
|
200
|
+
* typeof u === "string" && u.startsWith("user_")
|
|
201
|
+
*
|
|
202
|
+
* const UserId = Schema.declare<UserId>(isUserId, {
|
|
203
|
+
* title: "UserId",
|
|
204
|
+
* description: "A user identifier starting with 'user_'"
|
|
205
|
+
* })
|
|
206
|
+
* ```
|
|
62
207
|
*
|
|
63
208
|
* @see {@link declareConstructor} for creating schemas for parametric types.
|
|
64
209
|
*
|
|
210
|
+
* @category Constructors
|
|
65
211
|
* @since 4.0.0
|
|
66
212
|
*/
|
|
67
213
|
export function declare(is, annotations) {
|
|
68
214
|
return declareConstructor()([], () => (input, ast) => is(input) ? Effect.succeed(input) : Effect.fail(new Issue.InvalidType(ast, Option_.some(input))), annotations);
|
|
69
215
|
}
|
|
70
216
|
/**
|
|
71
|
-
*
|
|
72
|
-
* parameters.
|
|
217
|
+
* Widens a schema's type to the fully-parameterized {@link Bottom} interface,
|
|
218
|
+
* making all 14 type parameters visible to TypeScript.
|
|
219
|
+
*
|
|
220
|
+
* Normally, concrete schema interfaces (e.g. `Schema<string>`) hide most type
|
|
221
|
+
* parameters. `revealBottom` is useful when writing generic utilities that need
|
|
222
|
+
* to inspect or propagate the complete set of type parameters.
|
|
223
|
+
*
|
|
224
|
+
* **Example** (Inspecting all type parameters of a schema)
|
|
225
|
+
*
|
|
226
|
+
* ```ts
|
|
227
|
+
* import { Schema } from "effect"
|
|
228
|
+
*
|
|
229
|
+
* const schema = Schema.String
|
|
230
|
+
*
|
|
231
|
+
* // Widen to Bottom to access all 14 type parameters
|
|
232
|
+
* const bottom = Schema.revealBottom(schema)
|
|
233
|
+
*
|
|
234
|
+
* // `bottom` now exposes Type, Encoded, DecodingServices, EncodingServices,
|
|
235
|
+
* // ast, ~rebuild.out, ~type.make.in, Iso, ~type.parameters, etc.
|
|
236
|
+
* type T = typeof bottom["Type"] // string
|
|
237
|
+
* type E = typeof bottom["Encoded"] // string
|
|
238
|
+
* ```
|
|
73
239
|
*
|
|
74
240
|
* @since 4.0.0
|
|
75
241
|
*/
|
|
@@ -78,8 +244,24 @@ export function revealBottom(bottom) {
|
|
|
78
244
|
}
|
|
79
245
|
/**
|
|
80
246
|
* Adds metadata annotations to a schema without changing its runtime behavior.
|
|
81
|
-
*
|
|
82
|
-
*
|
|
247
|
+
* This is the pipeable (curried) counterpart of the `.annotate` method.
|
|
248
|
+
*
|
|
249
|
+
* Annotations provide extra context used by documentation generators, JSON
|
|
250
|
+
* Schema converters, error formatters, and other tooling. Common keys include
|
|
251
|
+
* `title`, `description`, `examples`, `message`, and `identifier`.
|
|
252
|
+
*
|
|
253
|
+
* **Example** (Adding a title and description)
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* import { Schema } from "effect"
|
|
257
|
+
*
|
|
258
|
+
* const Age = Schema.Number.pipe(
|
|
259
|
+
* Schema.annotate({
|
|
260
|
+
* title: "Age",
|
|
261
|
+
* description: "A non-negative integer representing age in years"
|
|
262
|
+
* })
|
|
263
|
+
* )
|
|
264
|
+
* ```
|
|
83
265
|
*
|
|
84
266
|
* @category Annotations
|
|
85
267
|
* @since 4.0.0
|
|
@@ -90,9 +272,29 @@ export function annotate(annotations) {
|
|
|
90
272
|
};
|
|
91
273
|
}
|
|
92
274
|
/**
|
|
93
|
-
* Adds key-
|
|
94
|
-
*
|
|
95
|
-
*
|
|
275
|
+
* Adds key-level annotations to a schema field. This is the pipeable
|
|
276
|
+
* (curried) counterpart of the `.annotateKey` method.
|
|
277
|
+
*
|
|
278
|
+
* Key annotations apply to a field's position inside a `Struct` or `Tuple`
|
|
279
|
+
* rather than to the field's value type. They can carry a
|
|
280
|
+
* `messageMissingKey` to customise the error shown when the field is absent,
|
|
281
|
+
* as well as standard documentation fields such as `title`, `description`,
|
|
282
|
+
* and `examples`.
|
|
283
|
+
*
|
|
284
|
+
* **Example** (Custom missing-key message for a required field)
|
|
285
|
+
*
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { Schema } from "effect"
|
|
288
|
+
*
|
|
289
|
+
* const schema = Schema.Struct({
|
|
290
|
+
* username: Schema.String.pipe(
|
|
291
|
+
* Schema.annotateKey({
|
|
292
|
+
* description: "The username used to log in",
|
|
293
|
+
* messageMissingKey: "Username is required"
|
|
294
|
+
* })
|
|
295
|
+
* )
|
|
296
|
+
* })
|
|
297
|
+
* ```
|
|
96
298
|
*
|
|
97
299
|
* @category Annotations
|
|
98
300
|
* @since 4.0.0
|
|
@@ -103,6 +305,25 @@ export function annotateKey(annotations) {
|
|
|
103
305
|
};
|
|
104
306
|
}
|
|
105
307
|
/**
|
|
308
|
+
* Identity function that widens a value to the full {@link Codec} interface,
|
|
309
|
+
* prompting TypeScript to infer all four type parameters (`T`, `E`, `RD`, `RE`).
|
|
310
|
+
*
|
|
311
|
+
* When a schema is stored in a variable typed as `Schema<T>` or `Top`, the
|
|
312
|
+
* encoded type and service requirements are erased. Passing the value through
|
|
313
|
+
* `revealCodec` recovers those parameters without any runtime cost.
|
|
314
|
+
*
|
|
315
|
+
* **Example** (Recovering encoded type from a schema variable)
|
|
316
|
+
*
|
|
317
|
+
* ```ts
|
|
318
|
+
* import { Schema } from "effect"
|
|
319
|
+
*
|
|
320
|
+
* const schema: Schema.Schema<number> = Schema.NumberFromString
|
|
321
|
+
*
|
|
322
|
+
* // Without revealCodec, Encoded is unknown
|
|
323
|
+
* const codec = Schema.revealCodec(schema)
|
|
324
|
+
* type Enc = typeof codec["Encoded"] // string
|
|
325
|
+
* ```
|
|
326
|
+
*
|
|
106
327
|
* @since 4.0.0
|
|
107
328
|
*/
|
|
108
329
|
export function revealCodec(codec) {
|
|
@@ -110,12 +331,30 @@ export function revealCodec(codec) {
|
|
|
110
331
|
}
|
|
111
332
|
const SchemaErrorTypeId = "~effect/Schema/SchemaError";
|
|
112
333
|
/**
|
|
113
|
-
*
|
|
334
|
+
* Error thrown (or returned as the error channel value) when schema decoding
|
|
335
|
+
* or encoding fails.
|
|
336
|
+
*
|
|
337
|
+
* The `issue` field contains a structured {@link Issue.Issue} tree describing
|
|
338
|
+
* every validation failure, including the path to the problematic value,
|
|
339
|
+
* expected types, and actual values received. `message` renders the issue tree
|
|
340
|
+
* as a human-readable string.
|
|
341
|
+
*
|
|
342
|
+
* Use {@link isSchemaError} to narrow an unknown value to `SchemaError`.
|
|
343
|
+
*
|
|
344
|
+
* **Example** (Catching a SchemaError)
|
|
345
|
+
*
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { Schema } from "effect"
|
|
114
348
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
349
|
+
* try {
|
|
350
|
+
* Schema.decodeUnknownSync(Schema.Number)("not a number")
|
|
351
|
+
* } catch (err) {
|
|
352
|
+
* if (Schema.isSchemaError(err)) {
|
|
353
|
+
* console.log(err.message)
|
|
354
|
+
* // Expected number, actual "not a number"
|
|
355
|
+
* }
|
|
356
|
+
* }
|
|
357
|
+
* ```
|
|
119
358
|
*
|
|
120
359
|
* @since 4.0.0
|
|
121
360
|
*/
|
|
@@ -135,6 +374,22 @@ export class SchemaError {
|
|
|
135
374
|
}
|
|
136
375
|
}
|
|
137
376
|
/**
|
|
377
|
+
* Returns `true` if `u` is a {@link SchemaError}.
|
|
378
|
+
*
|
|
379
|
+
* **Example** (Type guard in a catch block)
|
|
380
|
+
*
|
|
381
|
+
* ```ts
|
|
382
|
+
* import { Schema } from "effect"
|
|
383
|
+
*
|
|
384
|
+
* try {
|
|
385
|
+
* Schema.decodeUnknownSync(Schema.Number)("oops")
|
|
386
|
+
* } catch (err) {
|
|
387
|
+
* if (Schema.isSchemaError(err)) {
|
|
388
|
+
* console.log(err._tag) // "SchemaError"
|
|
389
|
+
* }
|
|
390
|
+
* }
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
138
393
|
* @since 4.0.0
|
|
139
394
|
*/
|
|
140
395
|
export function isSchemaError(u) {
|
|
@@ -374,6 +629,11 @@ export const is = Parser.is;
|
|
|
374
629
|
*/
|
|
375
630
|
export const asserts = Parser.asserts;
|
|
376
631
|
/**
|
|
632
|
+
* Decodes an `unknown` input against a schema, returning an `Effect` that
|
|
633
|
+
* succeeds with the decoded value or fails with a {@link SchemaError}. Use this
|
|
634
|
+
* when the input type is not statically known. Prefer {@link decodeEffect} when
|
|
635
|
+
* the input is already typed as the schema's `Encoded` type.
|
|
636
|
+
*
|
|
377
637
|
* @category Decoding
|
|
378
638
|
* @since 4.0.0
|
|
379
639
|
*/
|
|
@@ -384,11 +644,22 @@ export function decodeUnknownEffect(schema) {
|
|
|
384
644
|
};
|
|
385
645
|
}
|
|
386
646
|
/**
|
|
647
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
648
|
+
* returning an `Effect` that succeeds with the decoded value or fails with a
|
|
649
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
650
|
+
* input use {@link decodeUnknownEffect}.
|
|
651
|
+
*
|
|
387
652
|
* @category Decoding
|
|
388
653
|
* @since 4.0.0
|
|
389
654
|
*/
|
|
390
655
|
export const decodeEffect = decodeUnknownEffect;
|
|
391
656
|
/**
|
|
657
|
+
* Decodes an `unknown` input against a schema synchronously, returning an
|
|
658
|
+
* `Exit` that is either a `Success` with the decoded value or a `Failure` with
|
|
659
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
660
|
+
* `DecodingServices` requirement. Prefer {@link decodeExit} when the input is
|
|
661
|
+
* already typed as the schema's `Encoded` type.
|
|
662
|
+
*
|
|
392
663
|
* @category Decoding
|
|
393
664
|
* @since 4.0.0
|
|
394
665
|
*/
|
|
@@ -399,41 +670,110 @@ export function decodeUnknownExit(schema) {
|
|
|
399
670
|
};
|
|
400
671
|
}
|
|
401
672
|
/**
|
|
673
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
674
|
+
* synchronously, returning an `Exit` that is either a `Success` with the
|
|
675
|
+
* decoded value or a `Failure` with a {@link SchemaError}. Only usable with
|
|
676
|
+
* schemas that have no `DecodingServices` requirement. For `unknown` input use
|
|
677
|
+
* {@link decodeUnknownExit}.
|
|
678
|
+
*
|
|
402
679
|
* @category Decoding
|
|
403
680
|
* @since 4.0.0
|
|
404
681
|
*/
|
|
405
682
|
export const decodeExit = decodeUnknownExit;
|
|
406
683
|
/**
|
|
684
|
+
* Decodes an `unknown` input against a schema, returning an `Option` that is
|
|
685
|
+
* `Some` with the decoded value on success or `None` on failure. Prefer this
|
|
686
|
+
* over {@link decodeUnknownExit} or {@link decodeUnknownEffect} when you only
|
|
687
|
+
* need to know whether decoding succeeded and don't need error details. For
|
|
688
|
+
* typed input use {@link decodeOption}.
|
|
689
|
+
*
|
|
407
690
|
* @category Decoding
|
|
408
691
|
* @since 4.0.0
|
|
409
692
|
*/
|
|
410
693
|
export const decodeUnknownOption = Parser.decodeUnknownOption;
|
|
411
694
|
/**
|
|
695
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
696
|
+
* returning an `Option` that is `Some` with the decoded value on success or
|
|
697
|
+
* `None` on failure. For `unknown` input use {@link decodeUnknownOption}.
|
|
698
|
+
*
|
|
412
699
|
* @category Decoding
|
|
413
700
|
* @since 4.0.0
|
|
414
701
|
*/
|
|
415
702
|
export const decodeOption = Parser.decodeOption;
|
|
416
703
|
/**
|
|
704
|
+
* Decodes an `unknown` input against a schema, returning a `Promise` that
|
|
705
|
+
* resolves with the decoded value or rejects with a {@link SchemaError}. Useful
|
|
706
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
707
|
+
* {@link decodePromise}.
|
|
708
|
+
*
|
|
417
709
|
* @category Decoding
|
|
418
710
|
* @since 4.0.0
|
|
419
711
|
*/
|
|
420
712
|
export const decodeUnknownPromise = Parser.decodeUnknownPromise;
|
|
421
713
|
/**
|
|
714
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
715
|
+
* returning a `Promise` that resolves with the decoded value or rejects with a
|
|
716
|
+
* {@link SchemaError}. For `unknown` input use {@link decodeUnknownPromise}.
|
|
717
|
+
*
|
|
422
718
|
* @category Decoding
|
|
423
719
|
* @since 4.0.0
|
|
424
720
|
*/
|
|
425
721
|
export const decodePromise = Parser.decodePromise;
|
|
426
722
|
/**
|
|
723
|
+
* Decodes an `unknown` input against a schema synchronously, throwing a
|
|
724
|
+
* {@link SchemaError} on failure. Use this when you want to validate data at a
|
|
725
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
726
|
+
* non-throwing alternatives see {@link decodeUnknownOption},
|
|
727
|
+
* {@link decodeUnknownExit}, or {@link decodeUnknownEffect}. For typed input
|
|
728
|
+
* use {@link decodeSync}.
|
|
729
|
+
*
|
|
730
|
+
* **Example** (Decoding with a transformation schema)
|
|
731
|
+
*
|
|
732
|
+
* ```ts
|
|
733
|
+
* import { Schema } from "effect"
|
|
734
|
+
*
|
|
735
|
+
* const NumberFromString = Schema.NumberFromString
|
|
736
|
+
*
|
|
737
|
+
* console.log(Schema.decodeUnknownSync(NumberFromString)("42"))
|
|
738
|
+
* // Output: 42
|
|
739
|
+
*
|
|
740
|
+
* Schema.decodeUnknownSync(NumberFromString)("not a number")
|
|
741
|
+
* // throws SchemaError: NumberFromString
|
|
742
|
+
* // └─ Encoded side transformation failure
|
|
743
|
+
* // └─ NumberFromString
|
|
744
|
+
* // └─ Expected a numeric string, actual "not a number"
|
|
745
|
+
* ```
|
|
746
|
+
*
|
|
427
747
|
* @category Decoding
|
|
428
748
|
* @since 4.0.0
|
|
429
749
|
*/
|
|
430
750
|
export const decodeUnknownSync = Parser.decodeUnknownSync;
|
|
431
751
|
/**
|
|
752
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
753
|
+
* synchronously, throwing a {@link SchemaError} on failure. For `unknown` input
|
|
754
|
+
* use {@link decodeUnknownSync}.
|
|
755
|
+
*
|
|
432
756
|
* @category Decoding
|
|
433
757
|
* @since 4.0.0
|
|
434
758
|
*/
|
|
435
759
|
export const decodeSync = Parser.decodeSync;
|
|
436
760
|
/**
|
|
761
|
+
* Encodes an `unknown` input against a schema, returning an `Effect` that
|
|
762
|
+
* succeeds with the encoded value or fails with a {@link SchemaError}. Use this
|
|
763
|
+
* when the input type is not statically known. Prefer {@link encodeEffect} when
|
|
764
|
+
* the input is already typed as the schema's `Type`.
|
|
765
|
+
*
|
|
766
|
+
* **Example** (Encoding a value to a string)
|
|
767
|
+
*
|
|
768
|
+
* ```ts
|
|
769
|
+
* import { Effect, Schema } from "effect"
|
|
770
|
+
*
|
|
771
|
+
* const NumberFromString = Schema.NumberFromString
|
|
772
|
+
*
|
|
773
|
+
* Effect.runPromise(Schema.encodeUnknownEffect(NumberFromString)(42)).then(console.log)
|
|
774
|
+
* // Output: "42"
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
437
777
|
* @category Encoding
|
|
438
778
|
* @since 4.0.0
|
|
439
779
|
*/
|
|
@@ -444,11 +784,22 @@ export function encodeUnknownEffect(schema) {
|
|
|
444
784
|
};
|
|
445
785
|
}
|
|
446
786
|
/**
|
|
787
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
788
|
+
* `Effect` that succeeds with the encoded value or fails with a
|
|
789
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
790
|
+
* input use {@link encodeUnknownEffect}.
|
|
791
|
+
*
|
|
447
792
|
* @category Encoding
|
|
448
793
|
* @since 4.0.0
|
|
449
794
|
*/
|
|
450
795
|
export const encodeEffect = encodeUnknownEffect;
|
|
451
796
|
/**
|
|
797
|
+
* Encodes an `unknown` input against a schema synchronously, returning an
|
|
798
|
+
* `Exit` that is either a `Success` with the encoded value or a `Failure` with
|
|
799
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
800
|
+
* `EncodingServices` requirement. Prefer {@link encodeExit} when the input is
|
|
801
|
+
* already typed as the schema's `Type`.
|
|
802
|
+
*
|
|
452
803
|
* @category Encoding
|
|
453
804
|
* @since 4.0.0
|
|
454
805
|
*/
|
|
@@ -459,36 +810,72 @@ export function encodeUnknownExit(schema) {
|
|
|
459
810
|
};
|
|
460
811
|
}
|
|
461
812
|
/**
|
|
813
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
814
|
+
* returning an `Exit` that is either a `Success` with the encoded value or a
|
|
815
|
+
* `Failure` with a {@link SchemaError}. Only usable with schemas that have no
|
|
816
|
+
* `EncodingServices` requirement. For `unknown` input use
|
|
817
|
+
* {@link encodeUnknownExit}.
|
|
818
|
+
*
|
|
462
819
|
* @category Encoding
|
|
463
820
|
* @since 4.0.0
|
|
464
821
|
*/
|
|
465
822
|
export const encodeExit = encodeUnknownExit;
|
|
466
823
|
/**
|
|
824
|
+
* Encodes an `unknown` input against a schema, returning an `Option` that is
|
|
825
|
+
* `Some` with the encoded value on success or `None` on failure. Prefer this
|
|
826
|
+
* over {@link encodeUnknownExit} or {@link encodeUnknownEffect} when you only
|
|
827
|
+
* need to know whether encoding succeeded and don't need error details. For
|
|
828
|
+
* typed input use {@link encodeOption}.
|
|
829
|
+
*
|
|
467
830
|
* @category Encoding
|
|
468
831
|
* @since 4.0.0
|
|
469
832
|
*/
|
|
470
833
|
export const encodeUnknownOption = Parser.encodeUnknownOption;
|
|
471
834
|
/**
|
|
835
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
836
|
+
* `Option` that is `Some` with the encoded value on success or `None` on
|
|
837
|
+
* failure. For `unknown` input use {@link encodeUnknownOption}.
|
|
838
|
+
*
|
|
472
839
|
* @category Encoding
|
|
473
840
|
* @since 4.0.0
|
|
474
841
|
*/
|
|
475
842
|
export const encodeOption = Parser.encodeOption;
|
|
476
843
|
/**
|
|
844
|
+
* Encodes an `unknown` input against a schema, returning a `Promise` that
|
|
845
|
+
* resolves with the encoded value or rejects with a {@link SchemaError}. Useful
|
|
846
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
847
|
+
* {@link encodePromise}.
|
|
848
|
+
*
|
|
477
849
|
* @category Encoding
|
|
478
850
|
* @since 4.0.0
|
|
479
851
|
*/
|
|
480
852
|
export const encodeUnknownPromise = Parser.encodeUnknownPromise;
|
|
481
853
|
/**
|
|
854
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning a
|
|
855
|
+
* `Promise` that resolves with the encoded value or rejects with a
|
|
856
|
+
* {@link SchemaError}. For `unknown` input use {@link encodeUnknownPromise}.
|
|
857
|
+
*
|
|
482
858
|
* @category Encoding
|
|
483
859
|
* @since 4.0.0
|
|
484
860
|
*/
|
|
485
861
|
export const encodePromise = Parser.encodePromise;
|
|
486
862
|
/**
|
|
863
|
+
* Encodes an `unknown` input against a schema synchronously, throwing a
|
|
864
|
+
* {@link SchemaError} on failure. Use this when you want to serialize data at a
|
|
865
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
866
|
+
* non-throwing alternatives see {@link encodeUnknownOption},
|
|
867
|
+
* {@link encodeUnknownExit}, or {@link encodeUnknownEffect}. For typed input
|
|
868
|
+
* use {@link encodeSync}.
|
|
869
|
+
*
|
|
487
870
|
* @category Encoding
|
|
488
871
|
* @since 4.0.0
|
|
489
872
|
*/
|
|
490
873
|
export const encodeUnknownSync = Parser.encodeUnknownSync;
|
|
491
874
|
/**
|
|
875
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
876
|
+
* throwing a {@link SchemaError} on failure. For `unknown` input use
|
|
877
|
+
* {@link encodeUnknownSync}.
|
|
878
|
+
*
|
|
492
879
|
* @category Encoding
|
|
493
880
|
* @since 4.0.0
|
|
494
881
|
*/
|
|
@@ -543,57 +930,74 @@ export const optionalKey = /*#__PURE__*/Struct_.lambda(schema => make(AST.option
|
|
|
543
930
|
schema
|
|
544
931
|
}));
|
|
545
932
|
/**
|
|
933
|
+
* Reverses {@link optionalKey}, returning the inner required schema. Only
|
|
934
|
+
* applicable to schemas already wrapped with `optionalKey`.
|
|
935
|
+
*
|
|
546
936
|
* @since 4.0.0
|
|
547
937
|
*/
|
|
548
938
|
export const requiredKey = /*#__PURE__*/Struct_.lambda(self => self.schema);
|
|
549
939
|
/**
|
|
550
|
-
*
|
|
940
|
+
* Marks a struct field as optional, allowing the key to be absent or
|
|
551
941
|
* `undefined`.
|
|
552
942
|
*
|
|
553
|
-
*
|
|
554
|
-
* that:
|
|
555
|
-
* - Can be omitted from the object entirely
|
|
556
|
-
* - Can be explicitly set to `undefined`
|
|
557
|
-
* - Can contain the specified schema type
|
|
943
|
+
* explicitly set to `undefined`. Equivalent to `optionalKey(UndefinedOr(S))`.
|
|
558
944
|
*
|
|
559
|
-
*
|
|
945
|
+
* Use {@link optionalKey} instead if you want exact optional semantics (absent
|
|
946
|
+
* only, not `undefined`).
|
|
947
|
+
*
|
|
948
|
+
* **Example** (Optional field accepting undefined)
|
|
560
949
|
*
|
|
561
950
|
* ```ts
|
|
562
951
|
* import { Schema } from "effect"
|
|
563
952
|
*
|
|
564
953
|
* const schema = Schema.Struct({
|
|
565
954
|
* name: Schema.String,
|
|
566
|
-
* age: Schema.
|
|
955
|
+
* age: Schema.optional(Schema.Number)
|
|
567
956
|
* })
|
|
568
957
|
*
|
|
569
|
-
* //
|
|
570
|
-
* type Person = typeof schema
|
|
958
|
+
* // { readonly name: string; readonly age?: number | undefined }
|
|
959
|
+
* type Person = typeof schema.Type
|
|
571
960
|
* ```
|
|
572
961
|
*
|
|
573
962
|
* @since 4.0.0
|
|
574
963
|
*/
|
|
575
964
|
export const optional = /*#__PURE__*/Struct_.lambda(self => optionalKey(UndefinedOr(self)));
|
|
576
965
|
/**
|
|
966
|
+
* Reverses {@link optional}, returning the inner schema (unwrapping `UndefinedOr`).
|
|
967
|
+
* Only applicable to schemas already wrapped with `optional`.
|
|
968
|
+
*
|
|
577
969
|
* @since 4.0.0
|
|
578
970
|
*/
|
|
579
971
|
export const required = /*#__PURE__*/Struct_.lambda(self => self.schema.members[0]);
|
|
580
972
|
/**
|
|
973
|
+
* Makes a struct field mutable (removes the `readonly` modifier on the property).
|
|
974
|
+
* Use {@link readonlyKey} to reverse.
|
|
975
|
+
*
|
|
581
976
|
* @since 4.0.0
|
|
582
977
|
*/
|
|
583
978
|
export const mutableKey = /*#__PURE__*/Struct_.lambda(schema => make(AST.mutableKey(schema.ast), {
|
|
584
979
|
schema
|
|
585
980
|
}));
|
|
586
981
|
/**
|
|
982
|
+
* Reverses {@link mutableKey}, returning the inner schema as readonly again.
|
|
983
|
+
* Only applicable to schemas already wrapped with `mutableKey`.
|
|
984
|
+
*
|
|
587
985
|
* @since 4.0.0
|
|
588
986
|
*/
|
|
589
987
|
export const readonlyKey = /*#__PURE__*/Struct_.lambda(self => self.schema);
|
|
590
988
|
/**
|
|
989
|
+
* Extracts the type-side schema: sets `Encoded` to equal the decoded `Type`,
|
|
990
|
+
* discarding the encoding transformation path.
|
|
991
|
+
*
|
|
591
992
|
* @since 4.0.0
|
|
592
993
|
*/
|
|
593
994
|
export const toType = /*#__PURE__*/Struct_.lambda(schema => make(AST.toType(schema.ast), {
|
|
594
995
|
schema
|
|
595
996
|
}));
|
|
596
997
|
/**
|
|
998
|
+
* Extracts the encoded-side schema: sets `Type` to equal the `Encoded`,
|
|
999
|
+
* discarding the decoding transformation path.
|
|
1000
|
+
*
|
|
597
1001
|
* @since 4.0.0
|
|
598
1002
|
*/
|
|
599
1003
|
export const toEncoded = /*#__PURE__*/Struct_.lambda(schema => make(AST.toEncoded(schema.ast), {
|
|
@@ -613,6 +1017,16 @@ export function flip(schema) {
|
|
|
613
1017
|
});
|
|
614
1018
|
}
|
|
615
1019
|
/**
|
|
1020
|
+
* Creates a schema for a single literal value (string, number, bigint, boolean, or null).
|
|
1021
|
+
*
|
|
1022
|
+
* **Example** (String literal)
|
|
1023
|
+
* ```ts
|
|
1024
|
+
* import { Schema } from "effect"
|
|
1025
|
+
*
|
|
1026
|
+
* const schema = Schema.Literal("hello")
|
|
1027
|
+
* // Type: Schema.Literal<"hello">
|
|
1028
|
+
* ```
|
|
1029
|
+
*
|
|
616
1030
|
* @see {@link Literals} for a schema that represents a union of literals.
|
|
617
1031
|
* @see {@link tag} for a schema that represents a literal value that can be
|
|
618
1032
|
* used as a discriminator field in tagged unions and has a constructor default.
|
|
@@ -634,6 +1048,18 @@ function templateLiteralFromParts(parts) {
|
|
|
634
1048
|
return new AST.TemplateLiteral(parts.map(part => isSchema(part) ? part.ast : new AST.Literal(part)));
|
|
635
1049
|
}
|
|
636
1050
|
/**
|
|
1051
|
+
* Creates a schema that validates strings matching a template literal pattern. Each part can be
|
|
1052
|
+
* a literal string/number/bigint or a schema whose encoded type is a string, number, or bigint.
|
|
1053
|
+
*
|
|
1054
|
+
* **Example** (URL path pattern)
|
|
1055
|
+
* ```ts
|
|
1056
|
+
* import { Schema } from "effect"
|
|
1057
|
+
*
|
|
1058
|
+
* const schema = Schema.TemplateLiteral(["/user/", Schema.Number])
|
|
1059
|
+
* // matches strings like "/user/123", "/user/42", etc.
|
|
1060
|
+
* ```
|
|
1061
|
+
*
|
|
1062
|
+
* @see {@link TemplateLiteralParser} for a schema that also parses matched parts into a tuple.
|
|
637
1063
|
* @since 4.0.0
|
|
638
1064
|
*/
|
|
639
1065
|
export function TemplateLiteral(parts) {
|
|
@@ -642,6 +1068,18 @@ export function TemplateLiteral(parts) {
|
|
|
642
1068
|
});
|
|
643
1069
|
}
|
|
644
1070
|
/**
|
|
1071
|
+
* Like {@link TemplateLiteral} but decodes the matched string into a readonly tuple of typed values,
|
|
1072
|
+
* one element per schema part.
|
|
1073
|
+
*
|
|
1074
|
+
* **Example** (Parse path parameters)
|
|
1075
|
+
* ```ts
|
|
1076
|
+
* import { Schema } from "effect"
|
|
1077
|
+
*
|
|
1078
|
+
* const schema = Schema.TemplateLiteralParser(["/user/", Schema.NumberFromString])
|
|
1079
|
+
* // decodes "/user/42" => readonly ["/user/", 42]
|
|
1080
|
+
* ```
|
|
1081
|
+
*
|
|
1082
|
+
* @see {@link TemplateLiteral} for a validation-only version that keeps the string encoded.
|
|
645
1083
|
* @since 4.0.0
|
|
646
1084
|
*/
|
|
647
1085
|
export function TemplateLiteralParser(parts) {
|
|
@@ -650,6 +1088,21 @@ export function TemplateLiteralParser(parts) {
|
|
|
650
1088
|
});
|
|
651
1089
|
}
|
|
652
1090
|
/**
|
|
1091
|
+
* Creates a schema from a TypeScript enum object. Validates that the input is one of the enum's values.
|
|
1092
|
+
*
|
|
1093
|
+
* **Example** (Direction enum)
|
|
1094
|
+
* ```ts
|
|
1095
|
+
* import { Schema } from "effect"
|
|
1096
|
+
*
|
|
1097
|
+
* enum Direction {
|
|
1098
|
+
* Up = "Up",
|
|
1099
|
+
* Down = "Down"
|
|
1100
|
+
* }
|
|
1101
|
+
*
|
|
1102
|
+
* const schema = Schema.Enum(Direction)
|
|
1103
|
+
* // accepts "Up" or "Down"
|
|
1104
|
+
* ```
|
|
1105
|
+
*
|
|
653
1106
|
* @since 4.0.0
|
|
654
1107
|
*/
|
|
655
1108
|
export function Enum(enums) {
|
|
@@ -658,84 +1111,102 @@ export function Enum(enums) {
|
|
|
658
1111
|
});
|
|
659
1112
|
}
|
|
660
1113
|
/**
|
|
1114
|
+
* Schema for the `never` type. Always fails validation — no value satisfies it.
|
|
1115
|
+
*
|
|
661
1116
|
* @since 4.0.0
|
|
662
1117
|
*/
|
|
663
1118
|
export const Never = /*#__PURE__*/make(AST.never);
|
|
664
1119
|
/**
|
|
1120
|
+
* Schema for the `any` type. Accepts any value without validation.
|
|
1121
|
+
*
|
|
1122
|
+
* @see {@link Unknown} for a safer alternative that uses `unknown`.
|
|
665
1123
|
* @since 4.0.0
|
|
666
1124
|
*/
|
|
667
1125
|
export const Any = /*#__PURE__*/make(AST.any);
|
|
668
1126
|
/**
|
|
1127
|
+
* Schema for the `unknown` type. Accepts any value without validation.
|
|
1128
|
+
*
|
|
1129
|
+
* @see {@link Any} for the `any` variant.
|
|
669
1130
|
* @since 4.0.0
|
|
670
1131
|
*/
|
|
671
1132
|
export const Unknown = /*#__PURE__*/make(AST.unknown);
|
|
672
1133
|
/**
|
|
1134
|
+
* Schema for the `null` literal. Validates that the input is strictly `null`.
|
|
1135
|
+
*
|
|
1136
|
+
* @see {@link NullOr} for a union with another schema.
|
|
673
1137
|
* @since 4.0.0
|
|
674
1138
|
*/
|
|
675
1139
|
export const Null = /*#__PURE__*/make(AST.null);
|
|
676
1140
|
/**
|
|
1141
|
+
* Schema for the `undefined` literal. Validates that the input is strictly `undefined`.
|
|
1142
|
+
*
|
|
1143
|
+
* @see {@link UndefinedOr} for a union with another schema.
|
|
677
1144
|
* @since 4.0.0
|
|
678
1145
|
*/
|
|
679
1146
|
export const Undefined = /*#__PURE__*/make(AST.undefined);
|
|
680
1147
|
/**
|
|
681
|
-
*
|
|
1148
|
+
* Schema for `string` values. Validates that the input is `typeof` `"string"`.
|
|
682
1149
|
*
|
|
683
1150
|
* @since 4.0.0
|
|
684
1151
|
*/
|
|
685
1152
|
export const String = /*#__PURE__*/make(AST.string);
|
|
686
1153
|
/**
|
|
687
|
-
*
|
|
1154
|
+
* Schema for `number` values, including `NaN`, `Infinity`, and `-Infinity`.
|
|
688
1155
|
*
|
|
689
1156
|
* **Default Json Serializer**
|
|
690
1157
|
*
|
|
691
|
-
* -
|
|
692
|
-
* -
|
|
1158
|
+
* - Finite numbers are serialized as numbers.
|
|
1159
|
+
* - Non-finite values are serialized as strings (`"NaN"`, `"Infinity"`, `"-Infinity"`).
|
|
693
1160
|
*
|
|
1161
|
+
* @see {@link Finite} for a schema that excludes non-finite values.
|
|
694
1162
|
* @since 4.0.0
|
|
695
1163
|
*/
|
|
696
1164
|
export const Number = /*#__PURE__*/make(AST.number);
|
|
697
1165
|
/**
|
|
698
|
-
*
|
|
1166
|
+
* Schema for `boolean` values. Validates that the input is `typeof` `"boolean"`.
|
|
699
1167
|
*
|
|
700
1168
|
* @category Boolean
|
|
701
1169
|
* @since 4.0.0
|
|
702
1170
|
*/
|
|
703
1171
|
export const Boolean = /*#__PURE__*/make(AST.boolean);
|
|
704
1172
|
/**
|
|
705
|
-
*
|
|
1173
|
+
* Schema for `symbol` values. Validates that the input is `typeof` `"symbol"`.
|
|
706
1174
|
*
|
|
1175
|
+
* @see {@link UniqueSymbol} for a schema that matches a specific symbol.
|
|
707
1176
|
* @since 4.0.0
|
|
708
1177
|
*/
|
|
709
1178
|
export const Symbol = /*#__PURE__*/make(AST.symbol);
|
|
710
1179
|
/**
|
|
711
|
-
*
|
|
1180
|
+
* Schema for `bigint` values. Validates that the input is `typeof` `"bigint"`.
|
|
712
1181
|
*
|
|
713
1182
|
* @since 4.0.0
|
|
714
1183
|
*/
|
|
715
1184
|
export const BigInt = /*#__PURE__*/make(AST.bigInt);
|
|
716
1185
|
/**
|
|
717
|
-
*
|
|
1186
|
+
* Schema for the `void` type. Accepts `undefined` as the encoded value.
|
|
718
1187
|
*
|
|
719
1188
|
* @since 4.0.0
|
|
720
1189
|
*/
|
|
721
1190
|
export const Void = /*#__PURE__*/make(AST.void);
|
|
722
1191
|
/**
|
|
723
|
-
*
|
|
1192
|
+
* Schema for the `object` type. Validates that the input is a non-null object or function
|
|
1193
|
+
* (i.e. `typeof value === "object" && value !== null || typeof value === "function"`).
|
|
724
1194
|
*
|
|
725
1195
|
* @since 4.0.0
|
|
726
1196
|
*/
|
|
727
1197
|
export const ObjectKeyword = /*#__PURE__*/make(AST.objectKeyword);
|
|
728
1198
|
/**
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* **Example**
|
|
1199
|
+
* Creates a schema for a specific symbol. Only that exact symbol satisfies the schema.
|
|
732
1200
|
*
|
|
1201
|
+
* **Example** (Specific symbol)
|
|
733
1202
|
* ```ts
|
|
734
1203
|
* import { Schema } from "effect"
|
|
735
1204
|
*
|
|
736
|
-
* const
|
|
737
|
-
* const schema = Schema.UniqueSymbol(
|
|
1205
|
+
* const mySymbol = Symbol.for("mySymbol")
|
|
1206
|
+
* const schema = Schema.UniqueSymbol(mySymbol)
|
|
738
1207
|
* ```
|
|
1208
|
+
*
|
|
1209
|
+
* @see {@link Symbol} for a schema that accepts any symbol.
|
|
739
1210
|
* @since 4.0.0
|
|
740
1211
|
*/
|
|
741
1212
|
export function UniqueSymbol(symbol) {
|
|
@@ -751,6 +1222,34 @@ function makeStruct(ast, fields) {
|
|
|
751
1222
|
});
|
|
752
1223
|
}
|
|
753
1224
|
/**
|
|
1225
|
+
* Defines a struct schema from a map of field schemas.
|
|
1226
|
+
*
|
|
1227
|
+
* Each field value is a schema. Use {@link optionalKey} or {@link optional} to
|
|
1228
|
+
* mark fields as optional, and {@link mutableKey} to mark them as mutable.
|
|
1229
|
+
*
|
|
1230
|
+
* The resulting schema's `Type` is a readonly object type with the fields'
|
|
1231
|
+
* decoded types. The `Encoded` form mirrors the field schemas' encoded types.
|
|
1232
|
+
*
|
|
1233
|
+
* **Example** (Basic struct)
|
|
1234
|
+
*
|
|
1235
|
+
* ```ts
|
|
1236
|
+
* import { Schema } from "effect"
|
|
1237
|
+
*
|
|
1238
|
+
* const Person = Schema.Struct({
|
|
1239
|
+
* name: Schema.String,
|
|
1240
|
+
* age: Schema.Number,
|
|
1241
|
+
* email: Schema.optionalKey(Schema.String)
|
|
1242
|
+
* })
|
|
1243
|
+
*
|
|
1244
|
+
* // { readonly name: string; readonly age: number; readonly email?: string }
|
|
1245
|
+
* type Person = typeof Person.Type
|
|
1246
|
+
*
|
|
1247
|
+
* const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
|
|
1248
|
+
* console.log(alice)
|
|
1249
|
+
* // { name: 'Alice', age: 30 }
|
|
1250
|
+
* ```
|
|
1251
|
+
*
|
|
1252
|
+
* @category Constructors
|
|
754
1253
|
* @since 4.0.0
|
|
755
1254
|
*/
|
|
756
1255
|
export function Struct(fields) {
|
|
@@ -778,6 +1277,26 @@ export function fieldsAssign(fields) {
|
|
|
778
1277
|
return Struct_.lambda(struct => struct.mapFields(Struct_.assign(fields)));
|
|
779
1278
|
}
|
|
780
1279
|
/**
|
|
1280
|
+
* Renames struct keys in the encoded form without changing the decoded type.
|
|
1281
|
+
*
|
|
1282
|
+
* Takes a partial mapping `{ decodedKey: encodedKey }` and produces a
|
|
1283
|
+
* transformation schema that decodes from the renamed keys and encodes back to
|
|
1284
|
+
* the renamed keys. Keys not present in the mapping are left unchanged.
|
|
1285
|
+
*
|
|
1286
|
+
* **Example** (Rename `name` to `full_name` in the encoded form)
|
|
1287
|
+
*
|
|
1288
|
+
* ```ts
|
|
1289
|
+
* import { Schema } from "effect"
|
|
1290
|
+
*
|
|
1291
|
+
* const Person = Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
1292
|
+
* const Encoded = Person.pipe(Schema.encodeKeys({ name: "full_name" }))
|
|
1293
|
+
*
|
|
1294
|
+
* // Decodes { full_name: "Alice", age: 30 } → { name: "Alice", age: 30 }
|
|
1295
|
+
* const alice = Schema.decodeUnknownSync(Encoded)({ full_name: "Alice", age: 30 })
|
|
1296
|
+
* console.log(alice)
|
|
1297
|
+
* // { name: 'Alice', age: 30 }
|
|
1298
|
+
* ```
|
|
1299
|
+
*
|
|
781
1300
|
* @category Struct transformations
|
|
782
1301
|
* @since 4.0.0
|
|
783
1302
|
*/
|
|
@@ -801,6 +1320,31 @@ export function encodeKeys(mapping) {
|
|
|
801
1320
|
};
|
|
802
1321
|
}
|
|
803
1322
|
/**
|
|
1323
|
+
* Adds derived fields to a struct schema during decoding.
|
|
1324
|
+
*
|
|
1325
|
+
* Each new field is derived from the decoded struct value via a function that
|
|
1326
|
+
* returns `Option`. On encoding the derived fields are stripped. This allows
|
|
1327
|
+
* computed or enriched fields to live in the decoded type without appearing in
|
|
1328
|
+
* the encoded form.
|
|
1329
|
+
*
|
|
1330
|
+
* **Example** (Add a computed `fullName` field)
|
|
1331
|
+
*
|
|
1332
|
+
* ```ts
|
|
1333
|
+
* import { Option, Schema } from "effect"
|
|
1334
|
+
*
|
|
1335
|
+
* const Person = Schema.Struct({ first: Schema.String, last: Schema.String })
|
|
1336
|
+
* const Extended = Person.pipe(
|
|
1337
|
+
* Schema.extendTo(
|
|
1338
|
+
* { fullName: Schema.String },
|
|
1339
|
+
* { fullName: (p) => Option.some(`${p.first} ${p.last}`) }
|
|
1340
|
+
* )
|
|
1341
|
+
* )
|
|
1342
|
+
*
|
|
1343
|
+
* const alice = Schema.decodeUnknownSync(Extended)({ first: "Alice", last: "Smith" })
|
|
1344
|
+
* console.log(alice.fullName)
|
|
1345
|
+
* // Alice Smith
|
|
1346
|
+
* ```
|
|
1347
|
+
*
|
|
804
1348
|
* @since 4.0.0
|
|
805
1349
|
* @experimental
|
|
806
1350
|
*/
|
|
@@ -840,6 +1384,24 @@ derive) {
|
|
|
840
1384
|
};
|
|
841
1385
|
}
|
|
842
1386
|
/**
|
|
1387
|
+
* Defines a record (dictionary) schema with typed keys and values.
|
|
1388
|
+
*
|
|
1389
|
+
* **Example** (String-keyed record of numbers)
|
|
1390
|
+
*
|
|
1391
|
+
* ```ts
|
|
1392
|
+
* import { Schema } from "effect"
|
|
1393
|
+
*
|
|
1394
|
+
* const schema = Schema.Record(Schema.String, Schema.Number)
|
|
1395
|
+
*
|
|
1396
|
+
* // { readonly [x: string]: number }
|
|
1397
|
+
* type R = typeof schema.Type
|
|
1398
|
+
*
|
|
1399
|
+
* const result = Schema.decodeUnknownSync(schema)({ a: 1, b: 2 })
|
|
1400
|
+
* console.log(result)
|
|
1401
|
+
* // { a: 1, b: 2 }
|
|
1402
|
+
* ```
|
|
1403
|
+
*
|
|
1404
|
+
* @category Constructors
|
|
843
1405
|
* @since 4.0.0
|
|
844
1406
|
*/
|
|
845
1407
|
export function Record(key, value, options) {
|
|
@@ -850,6 +1412,24 @@ export function Record(key, value, options) {
|
|
|
850
1412
|
});
|
|
851
1413
|
}
|
|
852
1414
|
/**
|
|
1415
|
+
* Extends a struct schema with one or more record (index-signature) schemas,
|
|
1416
|
+
* producing a schema whose decoded type intersects the struct and all records.
|
|
1417
|
+
*
|
|
1418
|
+
* **Example** (Struct with string-indexed extra keys)
|
|
1419
|
+
*
|
|
1420
|
+
* ```ts
|
|
1421
|
+
* import { Schema } from "effect"
|
|
1422
|
+
*
|
|
1423
|
+
* const schema = Schema.StructWithRest(
|
|
1424
|
+
* Schema.Struct({ id: Schema.Number }),
|
|
1425
|
+
* [Schema.Record(Schema.String, Schema.String)]
|
|
1426
|
+
* )
|
|
1427
|
+
*
|
|
1428
|
+
* // { readonly id: number } & { readonly [x: string]: string }
|
|
1429
|
+
* type T = typeof schema.Type
|
|
1430
|
+
* ```
|
|
1431
|
+
*
|
|
1432
|
+
* @category Constructors
|
|
853
1433
|
* @since 4.0.0
|
|
854
1434
|
*/
|
|
855
1435
|
export function StructWithRest(schema, records) {
|
|
@@ -868,6 +1448,20 @@ function makeTuple(ast, elements) {
|
|
|
868
1448
|
});
|
|
869
1449
|
}
|
|
870
1450
|
/**
|
|
1451
|
+
* Defines a fixed-length tuple schema from an array of element schemas.
|
|
1452
|
+
*
|
|
1453
|
+
* **Example** (Pair of string and number)
|
|
1454
|
+
*
|
|
1455
|
+
* ```ts
|
|
1456
|
+
* import { Schema } from "effect"
|
|
1457
|
+
*
|
|
1458
|
+
* const schema = Schema.Tuple([Schema.String, Schema.Number])
|
|
1459
|
+
*
|
|
1460
|
+
* const pair = Schema.decodeUnknownSync(schema)(["hello", 42])
|
|
1461
|
+
* console.log(pair)
|
|
1462
|
+
* // [ 'hello', 42 ]
|
|
1463
|
+
* ```
|
|
1464
|
+
*
|
|
871
1465
|
* @category Constructors
|
|
872
1466
|
* @since 4.0.0
|
|
873
1467
|
*/
|
|
@@ -875,6 +1469,26 @@ export function Tuple(elements) {
|
|
|
875
1469
|
return makeTuple(AST.tuple(elements), elements);
|
|
876
1470
|
}
|
|
877
1471
|
/**
|
|
1472
|
+
* Extends a fixed-length tuple schema with rest elements, creating a variadic
|
|
1473
|
+
* tuple that starts with the fixed elements and ends with zero or more rest
|
|
1474
|
+
* elements.
|
|
1475
|
+
*
|
|
1476
|
+
* **Example** (Tuple with rest)
|
|
1477
|
+
*
|
|
1478
|
+
* ```ts
|
|
1479
|
+
* import { Schema } from "effect"
|
|
1480
|
+
*
|
|
1481
|
+
* // [string, number, ...boolean[]]
|
|
1482
|
+
* const schema = Schema.TupleWithRest(
|
|
1483
|
+
* Schema.Tuple([Schema.String, Schema.Number]),
|
|
1484
|
+
* [Schema.Boolean]
|
|
1485
|
+
* )
|
|
1486
|
+
*
|
|
1487
|
+
* const result = Schema.decodeUnknownSync(schema)(["hello", 1, true, false])
|
|
1488
|
+
* console.log(result)
|
|
1489
|
+
* // [ 'hello', 1, true, false ]
|
|
1490
|
+
* ```
|
|
1491
|
+
*
|
|
878
1492
|
* @category Constructors
|
|
879
1493
|
* @since 4.0.0
|
|
880
1494
|
*/
|
|
@@ -885,6 +1499,20 @@ export function TupleWithRest(schema, rest) {
|
|
|
885
1499
|
});
|
|
886
1500
|
}
|
|
887
1501
|
/**
|
|
1502
|
+
* Defines a `ReadonlyArray` schema for a given element schema.
|
|
1503
|
+
*
|
|
1504
|
+
* **Example** (Array of strings)
|
|
1505
|
+
*
|
|
1506
|
+
* ```ts
|
|
1507
|
+
* import { Schema } from "effect"
|
|
1508
|
+
*
|
|
1509
|
+
* const schema = Schema.Array(Schema.String)
|
|
1510
|
+
*
|
|
1511
|
+
* const result = Schema.decodeUnknownSync(schema)(["a", "b", "c"])
|
|
1512
|
+
* console.log(result)
|
|
1513
|
+
* // [ 'a', 'b', 'c' ]
|
|
1514
|
+
* ```
|
|
1515
|
+
*
|
|
888
1516
|
* @category Constructors
|
|
889
1517
|
* @since 4.0.0
|
|
890
1518
|
*/
|
|
@@ -892,6 +1520,20 @@ export const Array = /*#__PURE__*/Struct_.lambda(schema => make(new AST.Arrays(f
|
|
|
892
1520
|
schema
|
|
893
1521
|
}));
|
|
894
1522
|
/**
|
|
1523
|
+
* Defines a non-empty `ReadonlyArray` schema — at least one element required.
|
|
1524
|
+
* Type is `readonly [T, ...T[]]`.
|
|
1525
|
+
*
|
|
1526
|
+
* **Example** (Non-empty array of numbers)
|
|
1527
|
+
*
|
|
1528
|
+
* ```ts
|
|
1529
|
+
* import { Schema } from "effect"
|
|
1530
|
+
*
|
|
1531
|
+
* const schema = Schema.NonEmptyArray(Schema.Number)
|
|
1532
|
+
*
|
|
1533
|
+
* Schema.decodeUnknownSync(schema)([1, 2, 3]) // ok
|
|
1534
|
+
* Schema.decodeUnknownSync(schema)([]) // throws
|
|
1535
|
+
* ```
|
|
1536
|
+
*
|
|
895
1537
|
* @category Constructors
|
|
896
1538
|
* @since 4.0.0
|
|
897
1539
|
*/
|
|
@@ -911,7 +1553,18 @@ export function UniqueArray(item) {
|
|
|
911
1553
|
return Array(item).check(isUnique());
|
|
912
1554
|
}
|
|
913
1555
|
/**
|
|
914
|
-
* Makes
|
|
1556
|
+
* Makes an array or tuple schema mutable, removing the `readonly` modifier.
|
|
1557
|
+
*
|
|
1558
|
+
* **Example** (Mutable array)
|
|
1559
|
+
*
|
|
1560
|
+
* ```ts
|
|
1561
|
+
* import { Schema } from "effect"
|
|
1562
|
+
*
|
|
1563
|
+
* const schema = Schema.mutable(Schema.Array(Schema.Number))
|
|
1564
|
+
*
|
|
1565
|
+
* // number[] (mutable)
|
|
1566
|
+
* type T = typeof schema.Type
|
|
1567
|
+
* ```
|
|
915
1568
|
*
|
|
916
1569
|
* @since 4.0.0
|
|
917
1570
|
*/
|
|
@@ -930,12 +1583,23 @@ function makeUnion(ast, members) {
|
|
|
930
1583
|
});
|
|
931
1584
|
}
|
|
932
1585
|
/**
|
|
933
|
-
* Creates a schema
|
|
1586
|
+
* Creates a union schema from an array of member schemas. Members are tested in
|
|
1587
|
+
* order; the first match is returned.
|
|
1588
|
+
*
|
|
1589
|
+
* Optionally, specify `mode`:
|
|
1590
|
+
* - `"anyOf"` (default) — matches if any member matches.
|
|
1591
|
+
* - `"oneOf"` — matches if exactly one member matches.
|
|
1592
|
+
*
|
|
1593
|
+
* **Example** (String or number union)
|
|
1594
|
+
*
|
|
1595
|
+
* ```ts
|
|
1596
|
+
* import { Schema } from "effect"
|
|
934
1597
|
*
|
|
935
|
-
*
|
|
1598
|
+
* const schema = Schema.Union([Schema.String, Schema.Number])
|
|
936
1599
|
*
|
|
937
|
-
*
|
|
938
|
-
*
|
|
1600
|
+
* Schema.decodeUnknownSync(schema)("hello") // "hello"
|
|
1601
|
+
* Schema.decodeUnknownSync(schema)(42) // 42
|
|
1602
|
+
* ```
|
|
939
1603
|
*
|
|
940
1604
|
* @category Constructors
|
|
941
1605
|
* @since 4.0.0
|
|
@@ -944,6 +1608,16 @@ export function Union(members, options) {
|
|
|
944
1608
|
return makeUnion(AST.union(members, options?.mode ?? "anyOf", undefined), members);
|
|
945
1609
|
}
|
|
946
1610
|
/**
|
|
1611
|
+
* Creates a union schema from an array of literal values.
|
|
1612
|
+
*
|
|
1613
|
+
* **Example** (Status codes)
|
|
1614
|
+
* ```ts
|
|
1615
|
+
* import { Schema } from "effect"
|
|
1616
|
+
*
|
|
1617
|
+
* const schema = Schema.Literals(["active", "inactive", "pending"])
|
|
1618
|
+
* // accepts "active", "inactive", or "pending"
|
|
1619
|
+
* ```
|
|
1620
|
+
*
|
|
947
1621
|
* @see {@link Literal} for a schema that represents a single literal.
|
|
948
1622
|
* @category Constructors
|
|
949
1623
|
* @since 4.0.0
|
|
@@ -965,16 +1639,21 @@ export function Literals(literals) {
|
|
|
965
1639
|
});
|
|
966
1640
|
}
|
|
967
1641
|
/**
|
|
1642
|
+
* Creates a union schema of `S | null`.
|
|
1643
|
+
*
|
|
968
1644
|
* @category Constructors
|
|
969
1645
|
* @since 4.0.0
|
|
970
1646
|
*/
|
|
971
1647
|
export const NullOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Null]));
|
|
972
1648
|
/**
|
|
1649
|
+
* Creates a union schema of `S | undefined`.
|
|
1650
|
+
*
|
|
973
1651
|
* @category Constructors
|
|
974
1652
|
* @since 4.0.0
|
|
975
1653
|
*/
|
|
976
1654
|
export const UndefinedOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Undefined]));
|
|
977
1655
|
/**
|
|
1656
|
+
* Creates a union schema of `S | null | undefined`.
|
|
978
1657
|
* @category Constructors
|
|
979
1658
|
* @since 4.0.0
|
|
980
1659
|
*/
|
|
@@ -984,6 +1663,21 @@ export const NullishOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Null,
|
|
|
984
1663
|
* essential for creating recursive schemas where a schema references itself,
|
|
985
1664
|
* preventing infinite recursion during schema definition.
|
|
986
1665
|
*
|
|
1666
|
+
* **Example** (Recursive tree schema)
|
|
1667
|
+
* ```ts
|
|
1668
|
+
* import { Schema } from "effect"
|
|
1669
|
+
*
|
|
1670
|
+
* interface Tree {
|
|
1671
|
+
* readonly value: number
|
|
1672
|
+
* readonly children: ReadonlyArray<Tree>
|
|
1673
|
+
* }
|
|
1674
|
+
*
|
|
1675
|
+
* const Tree = Schema.Struct({
|
|
1676
|
+
* value: Schema.Number,
|
|
1677
|
+
* children: Schema.Array(Schema.suspend((): Schema.Codec<Tree> => Tree))
|
|
1678
|
+
* })
|
|
1679
|
+
* ```
|
|
1680
|
+
*
|
|
987
1681
|
* @category Constructors
|
|
988
1682
|
* @since 4.0.0
|
|
989
1683
|
*/
|
|
@@ -991,6 +1685,18 @@ export function suspend(f) {
|
|
|
991
1685
|
return make(new AST.Suspend(() => f().ast));
|
|
992
1686
|
}
|
|
993
1687
|
/**
|
|
1688
|
+
* Pipeable function that attaches one or more filter checks to a schema without
|
|
1689
|
+
* changing the TypeScript type.
|
|
1690
|
+
*
|
|
1691
|
+
* **Example** (Adding checks to a schema)
|
|
1692
|
+
* ```ts
|
|
1693
|
+
* import { Schema } from "effect"
|
|
1694
|
+
*
|
|
1695
|
+
* const AgeSchema = Schema.Number.pipe(
|
|
1696
|
+
* Schema.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(120))
|
|
1697
|
+
* )
|
|
1698
|
+
* ```
|
|
1699
|
+
*
|
|
994
1700
|
* @category Filtering
|
|
995
1701
|
* @since 4.0.0
|
|
996
1702
|
*/
|
|
@@ -998,6 +1704,9 @@ export function check(...checks) {
|
|
|
998
1704
|
return self => self.check(...checks);
|
|
999
1705
|
}
|
|
1000
1706
|
/**
|
|
1707
|
+
* Narrows the TypeScript type of a schema's output via a type guard predicate,
|
|
1708
|
+
* attaching the guard as a runtime filter check.
|
|
1709
|
+
*
|
|
1001
1710
|
* @category Filtering
|
|
1002
1711
|
* @since 4.0.0
|
|
1003
1712
|
*/
|
|
@@ -1007,7 +1716,8 @@ export function refine(refinement, annotations) {
|
|
|
1007
1716
|
});
|
|
1008
1717
|
}
|
|
1009
1718
|
/**
|
|
1010
|
-
* Adds a brand to a schema
|
|
1719
|
+
* Adds a nominal brand to a schema, intersecting the output type with
|
|
1720
|
+
* `Brand.Brand<B>` to prevent accidental mixing of structurally identical types.
|
|
1011
1721
|
*
|
|
1012
1722
|
* @category Branding
|
|
1013
1723
|
* @since 4.0.0
|
|
@@ -1019,7 +1729,10 @@ export function brand(identifier) {
|
|
|
1019
1729
|
});
|
|
1020
1730
|
}
|
|
1021
1731
|
/**
|
|
1022
|
-
* @
|
|
1732
|
+
* Creates a branded schema from a {@link Brand.Constructor}, applying the
|
|
1733
|
+
* constructor's checks and brand tag to the underlying schema.
|
|
1734
|
+
*
|
|
1735
|
+
* @category Branding
|
|
1023
1736
|
* @since 4.0.0
|
|
1024
1737
|
*/
|
|
1025
1738
|
export function fromBrand(identifier, ctor) {
|
|
@@ -1028,6 +1741,25 @@ export function fromBrand(identifier, ctor) {
|
|
|
1028
1741
|
};
|
|
1029
1742
|
}
|
|
1030
1743
|
/**
|
|
1744
|
+
* Intercepts the decoding pipeline of a schema.
|
|
1745
|
+
*
|
|
1746
|
+
* The provided function receives the current decoding `Effect` and `ParseOptions`,
|
|
1747
|
+
* and returns a new `Effect` — potentially adding service requirements (`RD`),
|
|
1748
|
+
* recovering from errors, or augmenting the result.
|
|
1749
|
+
*
|
|
1750
|
+
* **Example** (Logging decode failures)
|
|
1751
|
+
*
|
|
1752
|
+
* ```ts
|
|
1753
|
+
* import { Effect, Schema } from "effect"
|
|
1754
|
+
*
|
|
1755
|
+
* const Logged = Schema.String.pipe(
|
|
1756
|
+
* Schema.middlewareDecoding((effect) =>
|
|
1757
|
+
* Effect.tapError(effect, (issue) => Effect.log("decode failed", issue))
|
|
1758
|
+
* )
|
|
1759
|
+
* )
|
|
1760
|
+
* ```
|
|
1761
|
+
*
|
|
1762
|
+
* @see {@link catchDecoding} for a simpler error-recovery variant
|
|
1031
1763
|
* @since 4.0.0
|
|
1032
1764
|
*/
|
|
1033
1765
|
export function middlewareDecoding(decode) {
|
|
@@ -1036,6 +1768,25 @@ export function middlewareDecoding(decode) {
|
|
|
1036
1768
|
});
|
|
1037
1769
|
}
|
|
1038
1770
|
/**
|
|
1771
|
+
* Intercepts the encoding pipeline of a schema.
|
|
1772
|
+
*
|
|
1773
|
+
* The provided function receives the current encoding `Effect` and `ParseOptions`,
|
|
1774
|
+
* and returns a new `Effect` — potentially adding service requirements (`RE`),
|
|
1775
|
+
* recovering from errors, or augmenting the result.
|
|
1776
|
+
*
|
|
1777
|
+
* **Example** (Logging encode failures)
|
|
1778
|
+
*
|
|
1779
|
+
* ```ts
|
|
1780
|
+
* import { Effect, Schema } from "effect"
|
|
1781
|
+
*
|
|
1782
|
+
* const Logged = Schema.String.pipe(
|
|
1783
|
+
* Schema.middlewareEncoding((effect) =>
|
|
1784
|
+
* Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
|
|
1785
|
+
* )
|
|
1786
|
+
* )
|
|
1787
|
+
* ```
|
|
1788
|
+
*
|
|
1789
|
+
* @see {@link catchEncoding} for a simpler error-recovery variant
|
|
1039
1790
|
* @since 4.0.0
|
|
1040
1791
|
*/
|
|
1041
1792
|
export function middlewareEncoding(encode) {
|
|
@@ -1044,24 +1795,50 @@ export function middlewareEncoding(encode) {
|
|
|
1044
1795
|
});
|
|
1045
1796
|
}
|
|
1046
1797
|
/**
|
|
1798
|
+
* Recovers from a decoding error by providing a fallback value.
|
|
1799
|
+
*
|
|
1800
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
1801
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
1802
|
+
*
|
|
1803
|
+
* **Example** (Returning a default on decode failure)
|
|
1804
|
+
*
|
|
1805
|
+
* ```ts
|
|
1806
|
+
* import { Effect, Option, Schema } from "effect"
|
|
1807
|
+
*
|
|
1808
|
+
* const schema = Schema.Number.pipe(
|
|
1809
|
+
* Schema.catchDecoding((_issue) => Effect.succeed(Option.some(0)))
|
|
1810
|
+
* )
|
|
1811
|
+
* ```
|
|
1812
|
+
*
|
|
1813
|
+
* @see {@link catchDecodingWithContext} to add service requirements to the handler
|
|
1047
1814
|
* @since 4.0.0
|
|
1048
1815
|
*/
|
|
1049
1816
|
export function catchDecoding(f) {
|
|
1050
1817
|
return catchDecodingWithContext(f);
|
|
1051
1818
|
}
|
|
1052
1819
|
/**
|
|
1820
|
+
* Like {@link catchDecoding}, but the handler may require Effect services (`R`).
|
|
1821
|
+
*
|
|
1053
1822
|
* @since 4.0.0
|
|
1054
1823
|
*/
|
|
1055
1824
|
export function catchDecodingWithContext(f) {
|
|
1056
1825
|
return self => self.pipe(middlewareDecoding(Effect.catchEager(f)));
|
|
1057
1826
|
}
|
|
1058
1827
|
/**
|
|
1828
|
+
* Recovers from an encoding error by providing a fallback value.
|
|
1829
|
+
*
|
|
1830
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
1831
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
1832
|
+
*
|
|
1833
|
+
* @see {@link catchEncodingWithContext} to add service requirements to the handler
|
|
1059
1834
|
* @since 4.0.0
|
|
1060
1835
|
*/
|
|
1061
1836
|
export function catchEncoding(f) {
|
|
1062
1837
|
return catchEncodingWithContext(f);
|
|
1063
1838
|
}
|
|
1064
1839
|
/**
|
|
1840
|
+
* Like {@link catchEncoding}, but the handler may require Effect services (`R`).
|
|
1841
|
+
*
|
|
1065
1842
|
* @since 4.0.0
|
|
1066
1843
|
*/
|
|
1067
1844
|
export function catchEncodingWithContext(f) {
|
|
@@ -1123,6 +1900,25 @@ export function encodeTo(to, transformation) {
|
|
|
1123
1900
|
};
|
|
1124
1901
|
}
|
|
1125
1902
|
/**
|
|
1903
|
+
* Applies a transformation to a schema's encoded type, creating a new schema where encoding/decoding
|
|
1904
|
+
* operate on `S["Encoded"]` rather than `S["Type"]`.
|
|
1905
|
+
*
|
|
1906
|
+
* The `decode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during decoding),
|
|
1907
|
+
* and the `encode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during encoding).
|
|
1908
|
+
*
|
|
1909
|
+
* **Example** (Upper-casing encoded strings)
|
|
1910
|
+
*
|
|
1911
|
+
* ```ts
|
|
1912
|
+
* import { Schema, SchemaGetter } from "effect"
|
|
1913
|
+
*
|
|
1914
|
+
* const UpperFromLower = Schema.String.pipe(
|
|
1915
|
+
* Schema.encode({
|
|
1916
|
+
* decode: SchemaGetter.transform((s: string) => s.toLowerCase()),
|
|
1917
|
+
* encode: SchemaGetter.transform((s: string) => s.toUpperCase())
|
|
1918
|
+
* })
|
|
1919
|
+
* )
|
|
1920
|
+
* ```
|
|
1921
|
+
*
|
|
1126
1922
|
* @since 4.0.0
|
|
1127
1923
|
*/
|
|
1128
1924
|
export function encode(transformation) {
|
|
@@ -1131,6 +1927,32 @@ export function encode(transformation) {
|
|
|
1131
1927
|
};
|
|
1132
1928
|
}
|
|
1133
1929
|
/**
|
|
1930
|
+
* Attaches a constructor default value to a schema field.
|
|
1931
|
+
*
|
|
1932
|
+
* The `defaultValue` function receives `Option.some(undefined)` when the field is
|
|
1933
|
+
* explicitly set to `undefined`, or `Option.none()` when the field is absent.
|
|
1934
|
+
* Return `Option.some(value)` to supply a default, or `Option.none()` to leave the
|
|
1935
|
+
* field required. An `Effect` may be returned for async or service-dependent defaults.
|
|
1936
|
+
*
|
|
1937
|
+
* Constructor defaults are applied only during `makeUnsafe` / `make`, not during
|
|
1938
|
+
* decoding or encoding.
|
|
1939
|
+
*
|
|
1940
|
+
* **Example** (Optional field with a static default)
|
|
1941
|
+
*
|
|
1942
|
+
* ```ts
|
|
1943
|
+
* import { Option, Schema } from "effect"
|
|
1944
|
+
*
|
|
1945
|
+
* const MySchema = Schema.Struct({
|
|
1946
|
+
* name: Schema.String.pipe(
|
|
1947
|
+
* Schema.optionalKey,
|
|
1948
|
+
* Schema.withConstructorDefault(() => Option.some("anonymous"))
|
|
1949
|
+
* )
|
|
1950
|
+
* })
|
|
1951
|
+
*
|
|
1952
|
+
* const value = MySchema.makeUnsafe({})
|
|
1953
|
+
* // value: { name: "anonymous" }
|
|
1954
|
+
* ```
|
|
1955
|
+
*
|
|
1134
1956
|
* @since 4.0.0
|
|
1135
1957
|
*/
|
|
1136
1958
|
export function withConstructorDefault(defaultValue) {
|
|
@@ -1147,6 +1969,27 @@ export function withConstructorDefault(defaultValue) {
|
|
|
1147
1969
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
1148
1970
|
* - `omit`: Omit the value from the output.
|
|
1149
1971
|
*
|
|
1972
|
+
* Provides a default value for a missing key during decoding.
|
|
1973
|
+
*
|
|
1974
|
+
* When the key is absent from the input, `defaultValue()` is called to supply a value.
|
|
1975
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
1976
|
+
* - `"passthrough"` (default): include the value in the output
|
|
1977
|
+
* - `"omit"`: omit the key from the output
|
|
1978
|
+
*
|
|
1979
|
+
* **Example** (Default for a missing struct key)
|
|
1980
|
+
*
|
|
1981
|
+
* ```ts
|
|
1982
|
+
* import { Schema } from "effect"
|
|
1983
|
+
*
|
|
1984
|
+
* const MySchema = Schema.Struct({
|
|
1985
|
+
* name: Schema.String.pipe(Schema.withDecodingDefaultKey(() => "anonymous"))
|
|
1986
|
+
* })
|
|
1987
|
+
*
|
|
1988
|
+
* const result = Schema.decodeUnknownSync(MySchema)({})
|
|
1989
|
+
* // result: { name: "anonymous" }
|
|
1990
|
+
* ```
|
|
1991
|
+
*
|
|
1992
|
+
* @see {@link withDecodingDefault} for the `optional` (value-level) variant
|
|
1150
1993
|
* @since 4.0.0
|
|
1151
1994
|
*/
|
|
1152
1995
|
export function withDecodingDefaultKey(defaultValue, options) {
|
|
@@ -1165,6 +2008,30 @@ export function withDecodingDefaultKey(defaultValue, options) {
|
|
|
1165
2008
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
1166
2009
|
* - `omit`: Omit the value from the output.
|
|
1167
2010
|
*
|
|
2011
|
+
* Provides a default value for an `optional` field during decoding.
|
|
2012
|
+
*
|
|
2013
|
+
* Similar to {@link withDecodingDefaultKey} but works on `optional` (value-level optional)
|
|
2014
|
+
* rather than `optionalKey` (key-level optional).
|
|
2015
|
+
*
|
|
2016
|
+
* When the value is `undefined` or absent, `defaultValue()` is called to supply a value.
|
|
2017
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
2018
|
+
* - `"passthrough"` (default): include the value in the output
|
|
2019
|
+
* - `"omit"`: omit the value from the output
|
|
2020
|
+
*
|
|
2021
|
+
* **Example** (Default for an optional field value)
|
|
2022
|
+
*
|
|
2023
|
+
* ```ts
|
|
2024
|
+
* import { Schema } from "effect"
|
|
2025
|
+
*
|
|
2026
|
+
* const MySchema = Schema.Struct({
|
|
2027
|
+
* name: Schema.String.pipe(Schema.optional, Schema.withDecodingDefault(() => "anonymous"))
|
|
2028
|
+
* })
|
|
2029
|
+
*
|
|
2030
|
+
* const result = Schema.decodeUnknownSync(MySchema)({ name: undefined })
|
|
2031
|
+
* // result: { name: "anonymous" }
|
|
2032
|
+
* ```
|
|
2033
|
+
*
|
|
2034
|
+
* @see {@link withDecodingDefaultKey} for the key-level variant
|
|
1168
2035
|
* @since 4.0.0
|
|
1169
2036
|
*/
|
|
1170
2037
|
export function withDecodingDefault(defaultValue, options) {
|
|
@@ -1177,23 +2044,50 @@ export function withDecodingDefault(defaultValue, options) {
|
|
|
1177
2044
|
};
|
|
1178
2045
|
}
|
|
1179
2046
|
/**
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
2047
|
+
* Combines a {@link Literal} schema with {@link withConstructorDefault}, making it ideal
|
|
2048
|
+
* for discriminator fields in tagged unions. When constructing via `makeUnsafe`, the
|
|
2049
|
+
* `_tag` field can be omitted and will be filled automatically.
|
|
2050
|
+
*
|
|
2051
|
+
* **Example** (Discriminated union tag)
|
|
1182
2052
|
*
|
|
1183
|
-
*
|
|
1184
|
-
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
2053
|
+
* ```ts
|
|
2054
|
+
* import { Schema } from "effect"
|
|
2055
|
+
*
|
|
2056
|
+
* const A = Schema.Struct({ _tag: Schema.tag("A"), value: Schema.Number })
|
|
2057
|
+
*
|
|
2058
|
+
* // _tag is optional in makeUnsafe, auto-filled to "A"
|
|
2059
|
+
* const a = A.makeUnsafe({ value: 42 })
|
|
2060
|
+
* // a: { _tag: "A", value: 42 }
|
|
2061
|
+
* ```
|
|
1187
2062
|
*
|
|
2063
|
+
* @see {@link tagDefaultOmit} to also omit the tag during encoding
|
|
2064
|
+
* @see {@link TaggedStruct} for a shorthand that adds `_tag` automatically
|
|
1188
2065
|
* @since 4.0.0
|
|
1189
2066
|
*/
|
|
1190
2067
|
export function tag(literal) {
|
|
1191
2068
|
return Literal(literal).pipe(withConstructorDefault(() => Option_.some(literal)));
|
|
1192
2069
|
}
|
|
1193
2070
|
/**
|
|
1194
|
-
*
|
|
1195
|
-
* the
|
|
2071
|
+
* Like {@link tag}, but additionally omits the tag field from the encoded output.
|
|
2072
|
+
* Useful when the encoded form (e.g. JSON) does not include the discriminator key,
|
|
2073
|
+
* but the decoded type and constructor still need it.
|
|
2074
|
+
*
|
|
2075
|
+
* **Example** (Tag omitted during encoding)
|
|
2076
|
+
*
|
|
2077
|
+
* ```ts
|
|
2078
|
+
* import { Schema } from "effect"
|
|
2079
|
+
*
|
|
2080
|
+
* const A = Schema.Struct({
|
|
2081
|
+
* _tag: Schema.tagDefaultOmit("A"),
|
|
2082
|
+
* value: Schema.Number
|
|
2083
|
+
* })
|
|
2084
|
+
*
|
|
2085
|
+
* // Encode strips the _tag field
|
|
2086
|
+
* const encoded = Schema.encodeUnknownSync(A)({ _tag: "A", value: 1 })
|
|
2087
|
+
* // encoded: { value: 1 }
|
|
2088
|
+
* ```
|
|
1196
2089
|
*
|
|
2090
|
+
* @see {@link tag} for the variant that keeps the tag during encoding
|
|
1197
2091
|
* @since 4.0.0
|
|
1198
2092
|
*/
|
|
1199
2093
|
export function tagDefaultOmit(literal) {
|
|
@@ -1250,6 +2144,26 @@ export function TaggedStruct(value, fields) {
|
|
|
1250
2144
|
});
|
|
1251
2145
|
}
|
|
1252
2146
|
/**
|
|
2147
|
+
* Augments an existing {@link Union} of tagged structs with utility methods keyed by the discriminant field.
|
|
2148
|
+
*
|
|
2149
|
+
* **Example** (Adding tagged-union utilities to an existing union)
|
|
2150
|
+
*
|
|
2151
|
+
* ```ts
|
|
2152
|
+
* import { Schema } from "effect"
|
|
2153
|
+
*
|
|
2154
|
+
* const A = Schema.TaggedStruct("A", { value: Schema.Number })
|
|
2155
|
+
* const B = Schema.TaggedStruct("B", { name: Schema.String })
|
|
2156
|
+
*
|
|
2157
|
+
* const MyUnion = Schema.Union([A, B]).pipe(Schema.toTaggedUnion("_tag"))
|
|
2158
|
+
*
|
|
2159
|
+
* // Pattern-match on the union
|
|
2160
|
+
* const result = MyUnion.match({ _tag: "A", value: 1 }, {
|
|
2161
|
+
* A: (a) => `number: ${a.value}`,
|
|
2162
|
+
* B: (b) => `name: ${b.name}`
|
|
2163
|
+
* })
|
|
2164
|
+
* ```
|
|
2165
|
+
*
|
|
2166
|
+
* @see {@link TaggedUnion} for a shorthand that builds the union from scratch
|
|
1253
2167
|
* @since 4.0.0
|
|
1254
2168
|
* @experimental
|
|
1255
2169
|
*/
|
|
@@ -1295,6 +2209,28 @@ export function toTaggedUnion(tag) {
|
|
|
1295
2209
|
};
|
|
1296
2210
|
}
|
|
1297
2211
|
/**
|
|
2212
|
+
* Builds a discriminated union from a record of field sets, one per variant.
|
|
2213
|
+
* Each key becomes the `_tag` literal and the value is passed to {@link TaggedStruct}.
|
|
2214
|
+
* The result includes `cases`, `guards`, `isAnyOf`, and `match` utilities.
|
|
2215
|
+
*
|
|
2216
|
+
* **Example** (Discriminated union with pattern matching)
|
|
2217
|
+
*
|
|
2218
|
+
* ```ts
|
|
2219
|
+
* import { Schema } from "effect"
|
|
2220
|
+
*
|
|
2221
|
+
* const Shape = Schema.TaggedUnion({
|
|
2222
|
+
* Circle: { radius: Schema.Number },
|
|
2223
|
+
* Rectangle: { width: Schema.Number, height: Schema.Number }
|
|
2224
|
+
* })
|
|
2225
|
+
*
|
|
2226
|
+
* // Pattern-match on a decoded value
|
|
2227
|
+
* const area = Shape.match({ _tag: "Circle", radius: 5 }, {
|
|
2228
|
+
* Circle: (c) => Math.PI * c.radius ** 2,
|
|
2229
|
+
* Rectangle: (r) => r.width * r.height
|
|
2230
|
+
* })
|
|
2231
|
+
* ```
|
|
2232
|
+
*
|
|
2233
|
+
* @see {@link toTaggedUnion} to augment an existing union instead
|
|
1298
2234
|
* @category Constructors
|
|
1299
2235
|
* @since 4.0.0
|
|
1300
2236
|
*/
|
|
@@ -1318,6 +2254,23 @@ export function TaggedUnion(casesByTag) {
|
|
|
1318
2254
|
});
|
|
1319
2255
|
}
|
|
1320
2256
|
/**
|
|
2257
|
+
* Wraps a schema so that its decoded `Type` becomes a nominally distinct type `Self`.
|
|
2258
|
+
* Useful for creating opaque types that are structurally identical to a base schema
|
|
2259
|
+
* but type-incompatible with it.
|
|
2260
|
+
*
|
|
2261
|
+
* **Example** (Opaque user ID)
|
|
2262
|
+
*
|
|
2263
|
+
* ```ts
|
|
2264
|
+
* import { Schema } from "effect"
|
|
2265
|
+
*
|
|
2266
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
2267
|
+
* const UserId = Schema.Opaque<UserId>()(Schema.String)
|
|
2268
|
+
*
|
|
2269
|
+
* // Decoded value is UserId, not plain string
|
|
2270
|
+
* const id = Schema.decodeUnknownSync(UserId)("abc")
|
|
2271
|
+
* // id: UserId
|
|
2272
|
+
* ```
|
|
2273
|
+
*
|
|
1321
2274
|
* @since 4.0.0
|
|
1322
2275
|
*/
|
|
1323
2276
|
export function Opaque() {
|
|
@@ -1329,7 +2282,19 @@ export function Opaque() {
|
|
|
1329
2282
|
};
|
|
1330
2283
|
}
|
|
1331
2284
|
/**
|
|
1332
|
-
* Creates a schema that validates
|
|
2285
|
+
* Creates a schema that validates values using `instanceof`.
|
|
2286
|
+
* Decoding and encoding pass the value through unchanged.
|
|
2287
|
+
*
|
|
2288
|
+
* **Example** (Schema for a built-in class)
|
|
2289
|
+
*
|
|
2290
|
+
* ```ts
|
|
2291
|
+
* import { Schema } from "effect"
|
|
2292
|
+
*
|
|
2293
|
+
* const DateSchema = Schema.instanceOf(Date)
|
|
2294
|
+
*
|
|
2295
|
+
* const decoded = Schema.decodeUnknownSync(DateSchema)(new Date("2024-01-01"))
|
|
2296
|
+
* // decoded: Date
|
|
2297
|
+
* ```
|
|
1333
2298
|
*
|
|
1334
2299
|
* @category Constructors
|
|
1335
2300
|
* @since 4.0.0
|
|
@@ -1338,6 +2303,9 @@ export function instanceOf(constructor, annotations) {
|
|
|
1338
2303
|
return declare(u => u instanceof constructor, annotations);
|
|
1339
2304
|
}
|
|
1340
2305
|
/**
|
|
2306
|
+
* Constructs an `AST.Link` that describes how a value of type `T` encodes to and decodes from a `To` schema.
|
|
2307
|
+
* Used when building low-level AST transformations that bridge two schema types.
|
|
2308
|
+
*
|
|
1341
2309
|
* @since 4.0.0
|
|
1342
2310
|
* @experimental
|
|
1343
2311
|
*/
|
|
@@ -1350,11 +2318,30 @@ export function link() {
|
|
|
1350
2318
|
// Checks
|
|
1351
2319
|
// -----------------------------------------------------------------------------
|
|
1352
2320
|
/**
|
|
2321
|
+
* Creates a custom filter check from a predicate function. The predicate
|
|
2322
|
+
* receives the input value, the schema's AST, and parse options, and returns
|
|
2323
|
+
* `true`/`undefined` on success or a failure description on error.
|
|
2324
|
+
*
|
|
2325
|
+
* **Example** (Custom filter check)
|
|
2326
|
+
* ```ts
|
|
2327
|
+
* import { Schema } from "effect"
|
|
2328
|
+
*
|
|
2329
|
+
* // Check that a number is even
|
|
2330
|
+
* const isEven = Schema.makeFilter(
|
|
2331
|
+
* (n: number) => n % 2 === 0 || "expected an even number"
|
|
2332
|
+
* )
|
|
2333
|
+
*
|
|
2334
|
+
* const EvenNumber = Schema.Number.check(isEven)
|
|
2335
|
+
* ```
|
|
2336
|
+
*
|
|
1353
2337
|
* @category Checks Constructors
|
|
1354
2338
|
* @since 4.0.0
|
|
1355
2339
|
*/
|
|
1356
2340
|
export const makeFilter = AST.makeFilter;
|
|
1357
2341
|
/**
|
|
2342
|
+
* Groups multiple checks into a single {@link AST.FilterGroup}, applying
|
|
2343
|
+
* optional shared annotations to the group as a whole.
|
|
2344
|
+
*
|
|
1358
2345
|
* @category Checks Constructors
|
|
1359
2346
|
* @since 4.0.0
|
|
1360
2347
|
*/
|
|
@@ -1842,6 +2829,9 @@ export function isFinite(annotations) {
|
|
|
1842
2829
|
});
|
|
1843
2830
|
}
|
|
1844
2831
|
/**
|
|
2832
|
+
* Generic factory for creating a "greater than" (`>`) check for any ordered
|
|
2833
|
+
* type by supplying an {@link Order.Order} instance.
|
|
2834
|
+
*
|
|
1845
2835
|
* @category Order checks
|
|
1846
2836
|
* @since 4.0.0
|
|
1847
2837
|
*/
|
|
@@ -1857,6 +2847,9 @@ export function makeIsGreaterThan(options) {
|
|
|
1857
2847
|
};
|
|
1858
2848
|
}
|
|
1859
2849
|
/**
|
|
2850
|
+
* Generic factory for creating a ">=" check for any ordered type by supplying
|
|
2851
|
+
* an {@link Order.Order} instance.
|
|
2852
|
+
*
|
|
1860
2853
|
* @category Order checks
|
|
1861
2854
|
* @since 4.0.0
|
|
1862
2855
|
*/
|
|
@@ -1872,6 +2865,9 @@ export function makeIsGreaterThanOrEqualTo(options) {
|
|
|
1872
2865
|
};
|
|
1873
2866
|
}
|
|
1874
2867
|
/**
|
|
2868
|
+
* Generic factory for creating a "<" check for any ordered type by supplying
|
|
2869
|
+
* an {@link Order.Order} instance.
|
|
2870
|
+
*
|
|
1875
2871
|
* @category Order checks
|
|
1876
2872
|
* @since 4.0.0
|
|
1877
2873
|
*/
|
|
@@ -1887,6 +2883,9 @@ export function makeIsLessThan(options) {
|
|
|
1887
2883
|
};
|
|
1888
2884
|
}
|
|
1889
2885
|
/**
|
|
2886
|
+
* Generic factory for creating a "<=" check for any ordered type by supplying
|
|
2887
|
+
* an {@link Order.Order} instance.
|
|
2888
|
+
*
|
|
1890
2889
|
* @category Order checks
|
|
1891
2890
|
* @since 4.0.0
|
|
1892
2891
|
*/
|
|
@@ -1902,6 +2901,9 @@ export function makeIsLessThanOrEqualTo(options) {
|
|
|
1902
2901
|
};
|
|
1903
2902
|
}
|
|
1904
2903
|
/**
|
|
2904
|
+
* Generic factory for creating an inclusive/exclusive range check for any
|
|
2905
|
+
* ordered type by supplying an {@link Order.Order} instance.
|
|
2906
|
+
*
|
|
1905
2907
|
* @category Order checks
|
|
1906
2908
|
* @since 4.0.0
|
|
1907
2909
|
*/
|
|
@@ -1922,6 +2924,9 @@ export function makeIsBetween(deriveOptions) {
|
|
|
1922
2924
|
};
|
|
1923
2925
|
}
|
|
1924
2926
|
/**
|
|
2927
|
+
* Generic factory for creating a divisibility check for any numeric type by
|
|
2928
|
+
* supplying a remainder function and a zero value.
|
|
2929
|
+
*
|
|
1925
2930
|
* @category Numeric checks
|
|
1926
2931
|
* @since 4.0.0
|
|
1927
2932
|
*/
|
|
@@ -2590,6 +3595,14 @@ export const isBetweenBigDecimal = /*#__PURE__*/makeIsBetween({
|
|
|
2590
3595
|
* constraint to ensure generated strings or arrays have at least the required
|
|
2591
3596
|
* length.
|
|
2592
3597
|
*
|
|
3598
|
+
* **Example** (Minimum length check)
|
|
3599
|
+
* ```ts
|
|
3600
|
+
* import { Schema } from "effect"
|
|
3601
|
+
*
|
|
3602
|
+
* const NonEmptyStringSchema = Schema.String.check(Schema.isMinLength(1))
|
|
3603
|
+
* const NonEmptyArraySchema = Schema.Array(Schema.Number).check(Schema.isMinLength(1))
|
|
3604
|
+
* ```
|
|
3605
|
+
*
|
|
2593
3606
|
* @category Length checks
|
|
2594
3607
|
* @since 4.0.0
|
|
2595
3608
|
*/
|
|
@@ -2982,6 +3995,7 @@ export function isPropertyNames(keySchema, annotations) {
|
|
|
2982
3995
|
* constraint using the provided equivalence function to ensure generated arrays
|
|
2983
3996
|
* contain only unique items.
|
|
2984
3997
|
*
|
|
3998
|
+
* @category Array checks
|
|
2985
3999
|
* @since 4.0.0
|
|
2986
4000
|
*/
|
|
2987
4001
|
export function isUnique(annotations) {
|
|
@@ -3016,6 +4030,8 @@ export const NonEmptyString = /*#__PURE__*/String.check(/*#__PURE__*/isNonEmpty(
|
|
|
3016
4030
|
*/
|
|
3017
4031
|
export const Char = /*#__PURE__*/String.check(/*#__PURE__*/isLengthBetween(1, 1));
|
|
3018
4032
|
/**
|
|
4033
|
+
* Creates a schema for `Option<A>`. See {@link Option} for details.
|
|
4034
|
+
*
|
|
3019
4035
|
* @category Option
|
|
3020
4036
|
* @since 4.0.0
|
|
3021
4037
|
*/
|
|
@@ -3159,6 +4175,8 @@ export function OptionFromOptional(schema) {
|
|
|
3159
4175
|
return optional(schema).pipe(decodeTo(Option(toType(schema)), Transformation.optionFromOptional()));
|
|
3160
4176
|
}
|
|
3161
4177
|
/**
|
|
4178
|
+
* Creates a schema for `Result<A, E>`. See {@link Result} for details.
|
|
4179
|
+
*
|
|
3162
4180
|
* @category Result
|
|
3163
4181
|
* @since 4.0.0
|
|
3164
4182
|
*/
|
|
@@ -3287,6 +4305,9 @@ export function Redacted(value, options) {
|
|
|
3287
4305
|
});
|
|
3288
4306
|
}
|
|
3289
4307
|
/**
|
|
4308
|
+
* Middleware that wraps decoded errors in `Redacted`, preventing sensitive
|
|
4309
|
+
* schema details from leaking in error messages.
|
|
4310
|
+
*
|
|
3290
4311
|
* @category Redacted
|
|
3291
4312
|
* @since 4.0.0
|
|
3292
4313
|
*/
|
|
@@ -3294,6 +4315,10 @@ export function redact(schema) {
|
|
|
3294
4315
|
return schema.pipe(middlewareDecoding(Effect.mapErrorEager(Issue.redact)));
|
|
3295
4316
|
}
|
|
3296
4317
|
/**
|
|
4318
|
+
* Decodes a value and wraps it in `Redacted<A>`. Unlike {@link Redacted} which
|
|
4319
|
+
* expects the input to already be a `Redacted` instance, this schema decodes
|
|
4320
|
+
* the raw value and wraps it.
|
|
4321
|
+
*
|
|
3297
4322
|
* @category Redacted
|
|
3298
4323
|
* @since 4.0.0
|
|
3299
4324
|
*/
|
|
@@ -3306,6 +4331,8 @@ export function RedactedFromValue(value, options) {
|
|
|
3306
4331
|
}));
|
|
3307
4332
|
}
|
|
3308
4333
|
/**
|
|
4334
|
+
* Creates a schema for `Cause.Reason<E>`. See {@link CauseReason} for details.
|
|
4335
|
+
*
|
|
3309
4336
|
* @category CauseReason
|
|
3310
4337
|
* @since 4.0.0
|
|
3311
4338
|
*/
|
|
@@ -3405,6 +4432,8 @@ function causeReasonToFormatter(error, defect) {
|
|
|
3405
4432
|
};
|
|
3406
4433
|
}
|
|
3407
4434
|
/**
|
|
4435
|
+
* Creates a schema for `Cause<E>`. See {@link Cause} for details.
|
|
4436
|
+
*
|
|
3408
4437
|
* @category Cause
|
|
3409
4438
|
* @since 4.0.0
|
|
3410
4439
|
*/
|
|
@@ -3538,6 +4567,8 @@ export const DefectWithStack = /*#__PURE__*/Union([/*#__PURE__*/ErrorJsonEncoded
|
|
|
3538
4567
|
toArbitrary: () => fc => fc.json()
|
|
3539
4568
|
}), defectTransformation))]);
|
|
3540
4569
|
/**
|
|
4570
|
+
* Creates a schema for `Exit<A, E>`. See {@link Exit} for details.
|
|
4571
|
+
*
|
|
3541
4572
|
* @category Exit
|
|
3542
4573
|
* @since 4.0.0
|
|
3543
4574
|
*/
|
|
@@ -3882,6 +4913,11 @@ export function Chunk(value) {
|
|
|
3882
4913
|
});
|
|
3883
4914
|
}
|
|
3884
4915
|
/**
|
|
4916
|
+
* Schema for JavaScript `RegExp` objects.
|
|
4917
|
+
*
|
|
4918
|
+
* The default JSON serializer encodes a `RegExp` as `{ source, flags }`.
|
|
4919
|
+
*
|
|
4920
|
+
* @category Schemas
|
|
3885
4921
|
* @since 4.0.0
|
|
3886
4922
|
*/
|
|
3887
4923
|
export const RegExp = /*#__PURE__*/instanceOf(globalThis.RegExp, {
|
|
@@ -3959,8 +4995,19 @@ export const URLFromString = /*#__PURE__*/String.annotate({
|
|
|
3959
4995
|
* A schema for JavaScript `Date` objects.
|
|
3960
4996
|
*
|
|
3961
4997
|
* This schema accepts any `Date` instance, including invalid dates (e.g., `new
|
|
3962
|
-
* Date("invalid")`). For validating only valid dates, use
|
|
4998
|
+
* Date("invalid")`). For validating only valid dates, use {@link DateValid}
|
|
4999
|
+
* instead. The default JSON serializer encodes `Date` as an ISO 8601 string.
|
|
5000
|
+
*
|
|
5001
|
+
* **Example** (Date schema)
|
|
3963
5002
|
*
|
|
5003
|
+
* ```ts
|
|
5004
|
+
* import { Schema } from "effect"
|
|
5005
|
+
*
|
|
5006
|
+
* Schema.decodeUnknownSync(Schema.Date)(new Date("2024-01-01"))
|
|
5007
|
+
* // => Date { 2024-01-01T00:00:00.000Z }
|
|
5008
|
+
* ```
|
|
5009
|
+
*
|
|
5010
|
+
* @category Schemas
|
|
3964
5011
|
* @since 4.0.0
|
|
3965
5012
|
*/
|
|
3966
5013
|
export const Date = /*#__PURE__*/instanceOf(globalThis.Date, {
|
|
@@ -3992,9 +5039,20 @@ export const DateValid = /*#__PURE__*/Date.check(/*#__PURE__*/isDateValid());
|
|
|
3992
5039
|
/**
|
|
3993
5040
|
* A schema for `Duration` values.
|
|
3994
5041
|
*
|
|
3995
|
-
*
|
|
5042
|
+
* The default JSON serializer encodes `Duration` as a tagged object with the
|
|
5043
|
+
* duration type and value.
|
|
5044
|
+
*
|
|
5045
|
+
* **Example** (Duration schema)
|
|
3996
5046
|
*
|
|
3997
|
-
*
|
|
5047
|
+
* ```ts
|
|
5048
|
+
* import { Schema } from "effect"
|
|
5049
|
+
* import { Duration } from "effect"
|
|
5050
|
+
*
|
|
5051
|
+
* Schema.decodeUnknownSync(Schema.Duration)(Duration.seconds(5))
|
|
5052
|
+
* // => Duration(5s)
|
|
5053
|
+
* ```
|
|
5054
|
+
*
|
|
5055
|
+
* @category Duration
|
|
3998
5056
|
*
|
|
3999
5057
|
* @since 4.0.0
|
|
4000
5058
|
*/
|
|
@@ -4212,6 +5270,12 @@ export function fromJsonString(schema) {
|
|
|
4212
5270
|
}).pipe(decodeTo(schema, Transformation.fromJsonString));
|
|
4213
5271
|
}
|
|
4214
5272
|
/**
|
|
5273
|
+
* Schema for JavaScript `File` objects.
|
|
5274
|
+
*
|
|
5275
|
+
* The default JSON serializer encodes a `File` as `{ data, type, name, lastModified }`
|
|
5276
|
+
* where `data` is base64-encoded.
|
|
5277
|
+
*
|
|
5278
|
+
* @category Schemas
|
|
4215
5279
|
* @since 4.0.0
|
|
4216
5280
|
*/
|
|
4217
5281
|
export const File = /*#__PURE__*/instanceOf(globalThis.File, {
|
|
@@ -4258,6 +5322,12 @@ export const File = /*#__PURE__*/instanceOf(globalThis.File, {
|
|
|
4258
5322
|
}))
|
|
4259
5323
|
});
|
|
4260
5324
|
/**
|
|
5325
|
+
* Schema for JavaScript `FormData` objects.
|
|
5326
|
+
*
|
|
5327
|
+
* The default JSON serializer encodes a `FormData` as an array of `[key, entry]`
|
|
5328
|
+
* pairs where each entry is tagged as `"String"` or `"File"`.
|
|
5329
|
+
*
|
|
5330
|
+
* @category Schemas
|
|
4261
5331
|
* @since 4.0.0
|
|
4262
5332
|
*/
|
|
4263
5333
|
export const FormData = /*#__PURE__*/instanceOf(globalThis.FormData, {
|
|
@@ -4387,6 +5457,11 @@ export function fromFormData(schema) {
|
|
|
4387
5457
|
return FormData.pipe(decodeTo(schema, Transformation.fromFormData));
|
|
4388
5458
|
}
|
|
4389
5459
|
/**
|
|
5460
|
+
* Schema for JavaScript `URLSearchParams` objects.
|
|
5461
|
+
*
|
|
5462
|
+
* The default JSON serializer encodes a `URLSearchParams` as a query string.
|
|
5463
|
+
*
|
|
5464
|
+
* @category Schemas
|
|
4390
5465
|
* @since 4.0.0
|
|
4391
5466
|
*/
|
|
4392
5467
|
export const URLSearchParams = /*#__PURE__*/instanceOf(globalThis.URLSearchParams, {
|
|
@@ -4544,6 +5619,9 @@ export const Trim = /*#__PURE__*/String.annotate({
|
|
|
4544
5619
|
expected: "a string that will be decoded as a trimmed string"
|
|
4545
5620
|
}).pipe(/*#__PURE__*/decodeTo(Trimmed, /*#__PURE__*/Transformation.trim()));
|
|
4546
5621
|
/**
|
|
5622
|
+
* A union schema for JavaScript property keys: `number | symbol | string`.
|
|
5623
|
+
*
|
|
5624
|
+
* @category Schemas
|
|
4547
5625
|
* @since 4.0.0
|
|
4548
5626
|
*/
|
|
4549
5627
|
export const PropertyKey = /*#__PURE__*/Union([Finite, Symbol, String]);
|
|
@@ -4948,6 +6026,46 @@ function isStruct(schema) {
|
|
|
4948
6026
|
return isSchema(schema);
|
|
4949
6027
|
}
|
|
4950
6028
|
/**
|
|
6029
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
6030
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
6031
|
+
* input (unless `disableValidation` is set in the options).
|
|
6032
|
+
*
|
|
6033
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
6034
|
+
* type parameter can be used to add nominal brands.
|
|
6035
|
+
*
|
|
6036
|
+
* **Example** (Basic class)
|
|
6037
|
+
*
|
|
6038
|
+
* ```ts
|
|
6039
|
+
* import { Schema } from "effect"
|
|
6040
|
+
*
|
|
6041
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
6042
|
+
* name: Schema.String,
|
|
6043
|
+
* age: Schema.Number
|
|
6044
|
+
* }) {}
|
|
6045
|
+
*
|
|
6046
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
6047
|
+
* console.log(alice.name) // "Alice"
|
|
6048
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
6049
|
+
* ```
|
|
6050
|
+
*
|
|
6051
|
+
* **Example** (Extending a class)
|
|
6052
|
+
*
|
|
6053
|
+
* ```ts
|
|
6054
|
+
* import { Schema } from "effect"
|
|
6055
|
+
*
|
|
6056
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
6057
|
+
* name: Schema.String
|
|
6058
|
+
* }) {}
|
|
6059
|
+
*
|
|
6060
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
6061
|
+
* breed: Schema.String
|
|
6062
|
+
* }) {}
|
|
6063
|
+
*
|
|
6064
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
6065
|
+
* console.log(dog.name) // "Rex"
|
|
6066
|
+
* console.log(dog.breed) // "Labrador"
|
|
6067
|
+
* ```
|
|
6068
|
+
*
|
|
4951
6069
|
* @category Constructors
|
|
4952
6070
|
* @since 4.0.0
|
|
4953
6071
|
*/
|
|
@@ -4956,6 +6074,27 @@ export const Class = identifier => (schema, annotations) => {
|
|
|
4956
6074
|
return makeClass(Data.Class, identifier, struct, annotations);
|
|
4957
6075
|
};
|
|
4958
6076
|
/**
|
|
6077
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
6078
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
6079
|
+
* discrimination patterns.
|
|
6080
|
+
*
|
|
6081
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
6082
|
+
* it defaults to the `tag` value.
|
|
6083
|
+
*
|
|
6084
|
+
* **Example** (Tagged class)
|
|
6085
|
+
*
|
|
6086
|
+
* ```ts
|
|
6087
|
+
* import { Schema } from "effect"
|
|
6088
|
+
*
|
|
6089
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
6090
|
+
* radius: Schema.Number
|
|
6091
|
+
* }) {}
|
|
6092
|
+
*
|
|
6093
|
+
* const c = new Circle({ radius: 5 })
|
|
6094
|
+
* console.log(c._tag) // "Circle"
|
|
6095
|
+
* console.log(c.radius) // 5
|
|
6096
|
+
* ```
|
|
6097
|
+
*
|
|
4959
6098
|
* @category Constructors
|
|
4960
6099
|
* @since 4.0.0
|
|
4961
6100
|
*/
|
|
@@ -4970,6 +6109,25 @@ export const TaggedClass = identifier => {
|
|
|
4970
6109
|
};
|
|
4971
6110
|
};
|
|
4972
6111
|
/**
|
|
6112
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
6113
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
6114
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
6115
|
+
* `Effect.gen`.
|
|
6116
|
+
*
|
|
6117
|
+
* **Example** (Schema-backed error)
|
|
6118
|
+
*
|
|
6119
|
+
* ```ts
|
|
6120
|
+
* import { Effect, Schema } from "effect"
|
|
6121
|
+
*
|
|
6122
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
6123
|
+
* id: Schema.Number
|
|
6124
|
+
* }) {}
|
|
6125
|
+
*
|
|
6126
|
+
* const program = Effect.gen(function*() {
|
|
6127
|
+
* yield* new NotFound({ id: 1 })
|
|
6128
|
+
* })
|
|
6129
|
+
* ```
|
|
6130
|
+
*
|
|
4973
6131
|
* @category Constructors
|
|
4974
6132
|
* @since 4.0.0
|
|
4975
6133
|
*/
|
|
@@ -4980,6 +6138,24 @@ export const ErrorClass = identifier => (schema, annotations) => {
|
|
|
4980
6138
|
return self;
|
|
4981
6139
|
};
|
|
4982
6140
|
/**
|
|
6141
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
6142
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
6143
|
+
* union member.
|
|
6144
|
+
*
|
|
6145
|
+
* **Example** (Tagged error class)
|
|
6146
|
+
*
|
|
6147
|
+
* ```ts
|
|
6148
|
+
* import { Effect, Schema } from "effect"
|
|
6149
|
+
*
|
|
6150
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
6151
|
+
* id: Schema.Number
|
|
6152
|
+
* }) {}
|
|
6153
|
+
*
|
|
6154
|
+
* const program = Effect.gen(function*() {
|
|
6155
|
+
* yield* new NotFound({ id: 42 })
|
|
6156
|
+
* })
|
|
6157
|
+
* ```
|
|
6158
|
+
*
|
|
4983
6159
|
* @category Constructors
|
|
4984
6160
|
* @since 4.0.0
|
|
4985
6161
|
*/
|
|
@@ -4994,6 +6170,11 @@ export const TaggedErrorClass = identifier => {
|
|
|
4994
6170
|
};
|
|
4995
6171
|
};
|
|
4996
6172
|
/**
|
|
6173
|
+
* Derives a {@link LazyArbitrary} from a schema. The result is memoized so
|
|
6174
|
+
* repeated calls with the same schema are cheap.
|
|
6175
|
+
*
|
|
6176
|
+
* Prefer {@link toArbitrary} when you just need the arbitrary directly.
|
|
6177
|
+
*
|
|
4997
6178
|
* @category Arbitrary
|
|
4998
6179
|
* @since 4.0.0
|
|
4999
6180
|
*/
|
|
@@ -5002,6 +6183,24 @@ export function toArbitraryLazy(schema) {
|
|
|
5002
6183
|
return fc => lawc(fc, {});
|
|
5003
6184
|
}
|
|
5004
6185
|
/**
|
|
6186
|
+
* Derives a `fast-check` `Arbitrary` from a schema for property-based
|
|
6187
|
+
* testing. The derived arbitrary generates values that satisfy the schema.
|
|
6188
|
+
*
|
|
6189
|
+
* **Example** (Generating arbitrary values)
|
|
6190
|
+
*
|
|
6191
|
+
* ```ts
|
|
6192
|
+
* import { Schema } from "effect"
|
|
6193
|
+
* import * as FastCheck from "fast-check"
|
|
6194
|
+
*
|
|
6195
|
+
* const PersonArb = Schema.toArbitrary(
|
|
6196
|
+
* Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
6197
|
+
* )
|
|
6198
|
+
*
|
|
6199
|
+
* // Sample a random value
|
|
6200
|
+
* const sample = FastCheck.sample(PersonArb, 1)[0]
|
|
6201
|
+
* console.log(typeof sample.name) // "string"
|
|
6202
|
+
* ```
|
|
6203
|
+
*
|
|
5005
6204
|
* @category Arbitrary
|
|
5006
6205
|
* @since 4.0.0
|
|
5007
6206
|
*/
|
|
@@ -5028,6 +6227,13 @@ export function overrideToFormatter(toFormatter) {
|
|
|
5028
6227
|
};
|
|
5029
6228
|
}
|
|
5030
6229
|
/**
|
|
6230
|
+
* Derives a string formatter function from a schema. The formatter converts
|
|
6231
|
+
* a value to its human-readable string representation, recursing into structs,
|
|
6232
|
+
* arrays, and unions.
|
|
6233
|
+
*
|
|
6234
|
+
* The optional `onBefore` hook lets you intercept specific AST nodes before
|
|
6235
|
+
* the default formatting logic runs.
|
|
6236
|
+
*
|
|
5031
6237
|
* @category Formatter
|
|
5032
6238
|
* @since 4.0.0
|
|
5033
6239
|
*/
|
|
@@ -5166,10 +6372,9 @@ export function toFormatter(schema, options) {
|
|
|
5166
6372
|
// Equivalence
|
|
5167
6373
|
// -----------------------------------------------------------------------------
|
|
5168
6374
|
/**
|
|
5169
|
-
*
|
|
5170
|
-
*
|
|
5171
|
-
*
|
|
5172
|
-
* the schema invariant.
|
|
6375
|
+
* Overrides the equivalence derivation for a schema by supplying a custom
|
|
6376
|
+
* `Equivalence`. Use this when the default structural equivalence derived by
|
|
6377
|
+
* {@link toEquivalence} is not appropriate for a type.
|
|
5173
6378
|
*
|
|
5174
6379
|
* @category Equivalence
|
|
5175
6380
|
* @since 4.0.0
|
|
@@ -5180,6 +6385,21 @@ export function overrideToEquivalence(toEquivalence) {
|
|
|
5180
6385
|
});
|
|
5181
6386
|
}
|
|
5182
6387
|
/**
|
|
6388
|
+
* Derives an `Equivalence` from a schema. Two values are considered equal when
|
|
6389
|
+
* every field (and nested field) compares equal according to the schema
|
|
6390
|
+
* structure.
|
|
6391
|
+
*
|
|
6392
|
+
* **Example** (Struct equivalence)
|
|
6393
|
+
*
|
|
6394
|
+
* ```ts
|
|
6395
|
+
* import { Schema } from "effect"
|
|
6396
|
+
*
|
|
6397
|
+
* const eq = Schema.toEquivalence(Schema.Struct({ id: Schema.Number, name: Schema.String }))
|
|
6398
|
+
*
|
|
6399
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 1, name: "Alice" })) // true
|
|
6400
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 2, name: "Alice" })) // false
|
|
6401
|
+
* ```
|
|
6402
|
+
*
|
|
5183
6403
|
* @category Equivalence
|
|
5184
6404
|
* @since 4.0.0
|
|
5185
6405
|
*/
|
|
@@ -5190,6 +6410,10 @@ export function toEquivalence(schema) {
|
|
|
5190
6410
|
// Representation
|
|
5191
6411
|
// -----------------------------------------------------------------------------
|
|
5192
6412
|
/**
|
|
6413
|
+
* Derives an intermediate `SchemaRepresentation.Document` from a schema. This
|
|
6414
|
+
* document is used internally by {@link toJsonSchemaDocument} and related
|
|
6415
|
+
* functions to produce JSON Schema output.
|
|
6416
|
+
*
|
|
5193
6417
|
* @category Representation
|
|
5194
6418
|
* @since 4.0.0
|
|
5195
6419
|
*/
|
|
@@ -5217,6 +6441,9 @@ export function toJsonSchemaDocument(schema, options) {
|
|
|
5217
6441
|
// Canonical Codecs
|
|
5218
6442
|
// -----------------------------------------------------------------------------
|
|
5219
6443
|
/**
|
|
6444
|
+
* Derives a canonical JSON codec from a schema. The encoded form is `unknown`
|
|
6445
|
+
* (any JSON-compatible value), decoded to the schema's `Type`.
|
|
6446
|
+
*
|
|
5220
6447
|
* @category Canonical Codecs
|
|
5221
6448
|
* @since 4.0.0
|
|
5222
6449
|
*/
|
|
@@ -5224,6 +6451,9 @@ export function toCodecJson(schema) {
|
|
|
5224
6451
|
return make(InternalToCodec.toCodecJson(schema.ast));
|
|
5225
6452
|
}
|
|
5226
6453
|
/**
|
|
6454
|
+
* Derives an isomorphism codec from a schema. The encoded form is the
|
|
6455
|
+
* schema's `Iso` type — the intermediate representation used for round-tripping.
|
|
6456
|
+
*
|
|
5227
6457
|
* @category Canonical Codecs
|
|
5228
6458
|
* @since 4.0.0
|
|
5229
6459
|
*/
|
|
@@ -5238,6 +6468,10 @@ export function toCodecStringTree(schema, options) {
|
|
|
5238
6468
|
}
|
|
5239
6469
|
}
|
|
5240
6470
|
/**
|
|
6471
|
+
* Derives an XML encoder from a codec. Encodes a value to an XML string by
|
|
6472
|
+
* first converting it through {@link toCodecStringTree}, then serializing the
|
|
6473
|
+
* resulting tree to XML.
|
|
6474
|
+
*
|
|
5241
6475
|
* @category Canonical Codecs
|
|
5242
6476
|
* @since 4.0.0
|
|
5243
6477
|
*/
|
|
@@ -5450,6 +6684,9 @@ function onSerializerEnsureArray(ast) {
|
|
|
5450
6684
|
// Optic APIs
|
|
5451
6685
|
// -----------------------------------------------------------------------------
|
|
5452
6686
|
/**
|
|
6687
|
+
* Derives an `Iso` optic from a schema that isomorphically converts between
|
|
6688
|
+
* the schema's `Type` and its `Iso` (intermediate / serialized form).
|
|
6689
|
+
*
|
|
5453
6690
|
* @category Optic
|
|
5454
6691
|
* @since 4.0.0
|
|
5455
6692
|
*/
|
|
@@ -5458,6 +6695,8 @@ export function toIso(schema) {
|
|
|
5458
6695
|
return Optic_.makeIso(Parser.encodeSync(serializer), Parser.decodeSync(serializer));
|
|
5459
6696
|
}
|
|
5460
6697
|
/**
|
|
6698
|
+
* Returns an identity `Iso` over the schema's source (`Type`) side.
|
|
6699
|
+
*
|
|
5461
6700
|
* @category Optic
|
|
5462
6701
|
* @since 4.0.0
|
|
5463
6702
|
*/
|
|
@@ -5465,6 +6704,8 @@ export function toIsoSource(_) {
|
|
|
5465
6704
|
return Optic_.id();
|
|
5466
6705
|
}
|
|
5467
6706
|
/**
|
|
6707
|
+
* Returns an identity `Iso` over the schema's focus (`Iso`) side.
|
|
6708
|
+
*
|
|
5468
6709
|
* @category Optic
|
|
5469
6710
|
* @since 4.0.0
|
|
5470
6711
|
*/
|
|
@@ -5472,10 +6713,11 @@ export function toIsoFocus(_) {
|
|
|
5472
6713
|
return Optic_.id();
|
|
5473
6714
|
}
|
|
5474
6715
|
/**
|
|
5475
|
-
*
|
|
5476
|
-
*
|
|
5477
|
-
*
|
|
5478
|
-
* the
|
|
6716
|
+
* Overrides the ISO codec derivation for a schema by providing a target codec
|
|
6717
|
+
* and explicit `decode`/`encode` getters. The resulting schema carries a
|
|
6718
|
+
* custom `Iso` type, which changes the schema's type parameter — use
|
|
6719
|
+
* {@link overrideToCodecIso} when the default ISO transformation is not
|
|
6720
|
+
* appropriate.
|
|
5479
6721
|
*
|
|
5480
6722
|
* @category Optic
|
|
5481
6723
|
* @since 4.0.0
|
|
@@ -5493,11 +6735,15 @@ export function overrideToCodecIso(to, transformation) {
|
|
|
5493
6735
|
// Differ APIs
|
|
5494
6736
|
// -----------------------------------------------------------------------------
|
|
5495
6737
|
/**
|
|
6738
|
+
* Derives a JSON Patch differ from a codec. Serializes values to JSON (via
|
|
6739
|
+
* {@link toCodecJson}), computes RFC 6902 JSON Patch operations between old
|
|
6740
|
+
* and new values, and can apply patches back to the typed value.
|
|
6741
|
+
*
|
|
5496
6742
|
* @category JsonPatch
|
|
5497
6743
|
* @since 4.0.0
|
|
5498
6744
|
*/
|
|
5499
6745
|
export function toDifferJsonPatch(schema) {
|
|
5500
|
-
const serializer = toCodecJson(schema);
|
|
6746
|
+
const serializer = toCodecJson(schema);
|
|
5501
6747
|
const get = Parser.encodeSync(serializer);
|
|
5502
6748
|
const set = Parser.decodeSync(serializer);
|
|
5503
6749
|
return {
|
|
@@ -5512,6 +6758,10 @@ export function toDifferJsonPatch(schema) {
|
|
|
5512
6758
|
};
|
|
5513
6759
|
}
|
|
5514
6760
|
/**
|
|
6761
|
+
* Creates a recursive schema for a {@link Tree} of values described by `node`.
|
|
6762
|
+
* The resulting schema accepts a single node value, an array of trees, or an
|
|
6763
|
+
* object whose values are trees.
|
|
6764
|
+
*
|
|
5515
6765
|
* @category Tree
|
|
5516
6766
|
* @since 4.0.0
|
|
5517
6767
|
*/
|
|
@@ -5521,11 +6771,25 @@ export function Tree(node) {
|
|
|
5521
6771
|
return Tree;
|
|
5522
6772
|
}
|
|
5523
6773
|
/**
|
|
6774
|
+
* Schema that accepts and validates any immutable JSON-compatible value.
|
|
6775
|
+
*
|
|
6776
|
+
* **Example** (Validating a JSON value)
|
|
6777
|
+
*
|
|
6778
|
+
* ```ts
|
|
6779
|
+
* import { Schema } from "effect"
|
|
6780
|
+
*
|
|
6781
|
+
* const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
|
|
6782
|
+
* console.log(result._tag) // "Some"
|
|
6783
|
+
* ```
|
|
6784
|
+
*
|
|
5524
6785
|
* @category JSON
|
|
5525
6786
|
* @since 4.0.0
|
|
5526
6787
|
*/
|
|
5527
6788
|
export const Json = /*#__PURE__*/make(AST.Json);
|
|
5528
6789
|
/**
|
|
6790
|
+
* Schema that accepts any mutable JSON-compatible value. See {@link Json} for
|
|
6791
|
+
* the immutable variant.
|
|
6792
|
+
*
|
|
5529
6793
|
* @category JSON
|
|
5530
6794
|
* @since 4.0.0
|
|
5531
6795
|
*/
|