mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
add decodingFallback migration example (#727)
This commit is contained in:
@@ -4302,39 +4302,70 @@ They are similar to transformations, but they are able to catch errors and modif
|
||||
|
||||
### Fallbacks
|
||||
|
||||
You can use `Schema.catchDecoding` to return a fallback value when decoding fails.
|
||||
This API uses an Effect without a context. If you need a fallback value that depends on a service, use `Schema.catchDecodingWithContext`.
|
||||
|
||||
**Example** (Returning a simple fallback value)
|
||||
|
||||
```ts
|
||||
import { Effect } from "effect"
|
||||
import { Schema } from "effect/schema"
|
||||
|
||||
const fallback = Effect.succeedSome("b")
|
||||
const schema = Schema.String.pipe(Schema.catchDecoding(() => fallback))
|
||||
// Provide a fallback string when decoding does not succeed
|
||||
const schema = Schema.String.pipe(Schema.catchDecoding(() => Effect.succeedSome("b")))
|
||||
|
||||
console.log(String(Schema.decodeUnknownExit(schema)(null)))
|
||||
// Success("b")
|
||||
```
|
||||
|
||||
### Providing a Service
|
||||
You can also return `Option.none()` to omit a field from the output.
|
||||
This is useful when working with optional fields.
|
||||
|
||||
**Example** (Omitting a field when decoding fails)
|
||||
|
||||
```ts
|
||||
import { Effect } from "effect"
|
||||
import { Schema } from "effect/schema"
|
||||
|
||||
// Omit the field when decoding does not succeed
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.optionalKey(Schema.String).pipe(Schema.catchDecoding(() => Effect.succeedNone))
|
||||
})
|
||||
|
||||
console.log(String(Schema.decodeUnknownExit(schema)({ a: null })))
|
||||
// Success({})
|
||||
```
|
||||
|
||||
#### Using a Service to provide a fallback value
|
||||
|
||||
You can use `Schema.catchDecodingWithContext` to get a fallback value from a service.
|
||||
|
||||
**Example** (Retrieving a fallback value from a service)
|
||||
|
||||
```ts
|
||||
import { Effect, ServiceMap } from "effect"
|
||||
import { Option } from "effect/data"
|
||||
import { Schema } from "effect/schema"
|
||||
|
||||
// Define a service that provides a fallback value
|
||||
class Service extends ServiceMap.Service<Service, { fallback: Effect.Effect<string> }>()("Service") {}
|
||||
|
||||
// ┌─── Codec<string, string, Service, never>
|
||||
// ▼
|
||||
const schema = Schema.revealCodec(
|
||||
Schema.String.pipe(
|
||||
Schema.catchDecodingWithContext(() =>
|
||||
Effect.gen(function* () {
|
||||
const service = yield* Service
|
||||
return Option.some(yield* service.fallback)
|
||||
})
|
||||
Schema.revealCodec(
|
||||
Schema.String.pipe(
|
||||
Schema.catchDecodingWithContext(() =>
|
||||
Effect.gen(function* () {
|
||||
const service = yield* Service
|
||||
return Option.some(yield* service.fallback)
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
// Provide the service during decoding
|
||||
// ┌─── Codec<string, string, never, never>
|
||||
// ▼
|
||||
const provided = Schema.revealCodec(
|
||||
@@ -7478,6 +7509,33 @@ console.log(Schema.encodeSync(DiscriminatedShape)({ kind: "circle", radius: 10 }
|
||||
// { radius: 10 }
|
||||
```
|
||||
|
||||
### decodingFallback
|
||||
|
||||
v3
|
||||
|
||||
```ts
|
||||
import { Effect, Schema } from "effect"
|
||||
|
||||
const schema = Schema.String.annotations({
|
||||
decodingFallback: () => Effect.succeed("a")
|
||||
})
|
||||
|
||||
console.log(Schema.decodeUnknownSync(schema)(1))
|
||||
// "a"
|
||||
```
|
||||
|
||||
v4
|
||||
|
||||
```ts
|
||||
import { Effect } from "effect"
|
||||
import { Schema } from "effect/schema"
|
||||
|
||||
const schema = Schema.String.pipe(Schema.catchDecoding(() => Effect.succeedSome("a")))
|
||||
|
||||
console.log(Schema.decodeUnknownSync(schema)(1))
|
||||
// "a"
|
||||
```
|
||||
|
||||
### annotations
|
||||
|
||||
v3
|
||||
|
||||
Reference in New Issue
Block a user