| 230 | // Build lazily when decoding or redacted encoding fails. |
| 231 | let redacted: Document | undefined |
| 232 | const example = (i: number): Effect.Effect<Json> => { |
| 233 | const name = tags[i] |
| 234 | const none = (invalid: string): Json => ({ |
| 235 | ...(typeof name === "string" ? { tag: name } : {}), |
| 236 | invalid |
| 237 | }) |
| 238 | const candidate = exampleOf(members[i], document.definitions) |
| 239 | if (candidate === undefined) { |
| 240 | return Effect.succeed(none("No value fits this schema.")) |
| 241 | } |
| 242 | // Validate the JSON round-trip too: serialization turns -0 into 0. |
| 243 | const message: Json = JSON.parse(JSON.stringify(candidate)) |
| 244 | const entry = (shown: unknown, status?: { invalid: string }): Json => |
| 245 | shown === undefined |
| 246 | ? none( |
| 247 | "No safe example: a Schema.Redacted's value here comes from an " + |
| 248 | "example or default that can't be redacted." |
| 249 | ) |
| 250 | : holdsPlaceholder(shown) |
| 251 | ? { message: shown as Json, redacted: true, ...status } |
| 252 | : { message, ...status } |
| 253 | // Fall back to the redacting schema if decoding or encoding fails. |
| 254 | const walked = (status?: { invalid: string }): Json => { |
| 255 | if (encodeRedacted === undefined) return { message, ...status } |
| 256 | redacted ??= documentOf(redactedSchema) |
| 257 | return entry( |
| 258 | exampleOf(membersOf(redacted)[i], redacted.definitions, true), |
| 259 | status |
| 260 | ) |
| 261 | } |
| 262 | return decode(message, { errors: "all" }).pipe( |
| 263 | Effect.matchEffect({ |
| 264 | onFailure: (error) => Effect.succeed(walked({ invalid: error.message })), |
| 265 | onSuccess: (value) => |
| 266 | encodeRedacted === undefined |
| 267 | ? Effect.succeed({ message }) |
| 268 | : Effect.match(encodeRedacted(value), { |
| 269 | onFailure: () => walked(), |
| 270 | onSuccess: (shown) => entry(shown) |
| 271 | }) |
| 272 | }) |
| 273 | ) |
| 274 | } |
| 275 | return Effect.map( |
| 276 | Effect.forEach( |
| 277 | tag === undefined ? members.map((_, i) => i) : [index], |