Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema: Add Support for Infinity in Duration #4307

Merged
merged 2 commits into from
Jan 21, 2025
Merged

Conversation

gcanti
Copy link
Contributor

@gcanti gcanti commented Jan 21, 2025

Fix 1

Schema: Add Support for Infinity in Duration.

This update adds support for encoding Duration.infinity in Schema.Duration.

Before

Attempting to encode Duration.infinity resulted in a ParseError due to the lack of support for Infinity in Schema.Duration:

import { Duration, Schema } from "effect"

console.log(Schema.encodeUnknownSync(Schema.Duration)(Duration.infinity))
/*
throws:
ParseError: Duration
└─ Encoded side transformation failure
   └─ HRTime
      └─ [0]
         └─ NonNegativeInt
            └─ Predicate refinement failure
               └─ Expected an integer, actual Infinity
*/

After

The updated behavior successfully encodes Duration.infinity as [ -1, 0 ]:

import { Duration, Schema } from "effect"

console.log(Schema.encodeUnknownSync(Schema.Duration)(Duration.infinity))
// Output: [ -1, 0 ]

Fix 2

Schema: Enhance error messages for discriminated unions.

Before

import { Schema } from "effect"

const schema = Schema.Union(
  Schema.Tuple(Schema.Literal(-1), Schema.Literal(0)).annotations({
    identifier: "A"
  }),
  Schema.Tuple(Schema.NonNegativeInt, Schema.NonNegativeInt).annotations({
    identifier: "B"
  })
).annotations({ identifier: "AB" })

Schema.decodeUnknownSync(schema)([-500, 0])
/*
throws:
ParseError: AB
├─ { readonly 0: -1 }
│  └─ ["0"]
│     └─ Expected -1, actual -500
└─ B
   └─ [0]
      └─ NonNegativeInt
         └─ From side refinement failure
            └─ NonNegative
               └─ Predicate refinement failure
                  └─ Expected a non-negative number, actual -500
*/

After

import { Schema } from "effect"

const schema = Schema.Union(
  Schema.Tuple(Schema.Literal(-1), Schema.Literal(0)).annotations({
    identifier: "A"
  }),
  Schema.Tuple(Schema.NonNegativeInt, Schema.NonNegativeInt).annotations({
    identifier: "B"
  })
).annotations({ identifier: "AB" })

Schema.decodeUnknownSync(schema)([-500, 0])
/*
throws:
ParseError: AB
-├─ { readonly 0: -1 }
+├─ A
│  └─ ["0"]
│     └─ Expected -1, actual -500
└─ B
   └─ [0]
      └─ NonNegativeInt
         └─ From side refinement failure
            └─ NonNegative
               └─ Predicate refinement failure
                  └─ Expected a non-negative number, actual -500
*/

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@gcanti gcanti added the schema label Jan 21, 2025
Copy link

changeset-bot bot commented Jan 21, 2025

🦋 Changeset detected

Latest commit: f63d926

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 35 packages
Name Type
effect Patch
@effect/cli Patch
@effect/cluster-browser Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-kysely Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql Patch
@effect/typeclass Patch
@effect/vitest Patch
@effect/ai Patch
@effect/ai-openai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gcanti gcanti merged commit 289c13b into main Jan 21, 2025
12 checks passed
@gcanti gcanti deleted the fix-schema-Duration branch January 21, 2025 08:27
@github-actions github-actions bot mentioned this pull request Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

1 participant