Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/effect/src/SchemaGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,10 @@ type StringifyJsonOptions = {
* **Details**
*
* - Skips `None` inputs.
* - On thrown stringify failures, such as circular references, fails with
* - If `JSON.stringify` throws or returns `undefined`, fails with
* `SchemaIssue.InvalidValue`.
* - Supports optional `replacer` and `space` options, matching
* `JSON.stringify`.
* - If `JSON.stringify` returns `undefined`, such as for `undefined`,
* functions, symbols, or a replacer that removes the root value, that
* `undefined` result is returned rather than converted into an `Issue`.
*
* **Example** (Stringifying JSON)
*
Expand All @@ -1040,7 +1037,13 @@ type StringifyJsonOptions = {
export function stringifyJson(options?: StringifyJsonOptions): Getter<string, unknown> {
return onSome((input) =>
Effect.try({
try: () => Option.some(JSON.stringify(input, options?.replacer, options?.space)),
try: () => {
const output = JSON.stringify(input, options?.replacer, options?.space)
if (output === undefined) {
throw new TypeError("Value cannot be represented as JSON")
}
return Option.some(output)
},
catch: (e) => new SchemaIssue.InvalidValue(Option.some(input), { message: globalThis.String(e) })
})
)
Expand Down
9 changes: 7 additions & 2 deletions packages/effect/test/schema/SchemaGetter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert } from "@effect/vitest"
import { assert, describe, it } from "@effect/vitest"
import { DateTime, Effect, Option, Result, SchemaGetter } from "effect"
import { describe, it } from "vitest"
import { assertSome, deepStrictEqual } from "../utils/assert.ts"

function makeAsserts<T, E>(getter: SchemaGetter.Getter<T, E>) {
Expand All @@ -16,6 +15,12 @@ function makeAsserts<T, E>(getter: SchemaGetter.Getter<T, E>) {
}

describe("SchemaGetter", () => {
it.effect("stringifyJson fails when JSON.stringify returns undefined", () =>
SchemaGetter.stringifyJson().run(Option.some(undefined), {}).pipe(
Effect.flip,
Effect.map((issue) => assert.strictEqual(issue._tag, "InvalidValue"))
))

Comment thread
coderabbitai[bot] marked this conversation as resolved.
it("map", () => {
const getter = SchemaGetter.succeed(1).map((t) => t + 1)
const result = Effect.runSync(getter.run(Option.some(1), {}))
Expand Down
Loading