Skip to content

Fix Maybe and implement optional schema - #10

Merged
lazamar merged 6 commits into
mainfrom
optional
Nov 20, 2025
Merged

Fix Maybe and implement optional schema#10
lazamar merged 6 commits into
mainfrom
optional

Conversation

@lazamar

@lazamar lazamar commented Nov 20, 2025

Copy link
Copy Markdown
Contributor

This PR:

  1. Fixes the encoding of Maybe values. Previously we could not correctly encode nested maybe values (Maybe<Maybe<T>>).
  2. Introduces optionalNullable and optionalMaybe to represent object fields that may be omitted.
  3. Add lots of tests for the json library.

@lowly2005 lowly2005 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛳️

@lazamar
lazamar merged commit 4758122 into main Nov 20, 2025
1 check passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

const maybe = <A>(s: Schema<A>): Schema<Maybe<A>> =>
new Schema(D.maybe(s.decoder), E.maybe(s.encoder));

// An object field that may be absent.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Optional schema cannot decode its own encoded Just values

The optional schema still wires D.optional (which only accepts a raw value or undefined) to E.maybe, but E.maybe now emits { nothing: {} }/{ just: ... } objects. After this change, encoding s.optional(s.string) with Just('x') produces { just: 'x' }, which D.optional rejects because it no longer matches either branch, so the schema can no longer roundtrip even non-empty optionals. Any use of s.optional will now fail to decode values it encodes.

Useful? React with 👍 / 👎.

@ctuncay
ctuncay deleted the optional branch January 29, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants