Skip to content

Schema.transform does not preserve branding through encode/decode #5953

@SimonVadier

Description

@SimonVadier

What version of Effect is running?

3.19.5

What steps can reproduce the bug?

When using Schema.transform with branded types, the brand is lost during the encode operation. The From/To branding is not preserved when encoding back from the transformed type.

import * as Schema from "effect/Schema";

const BrandedBool = Schema.Boolean.pipe(Schema.brand("bool"));
const BrandedString = Schema.String.pipe(Schema.brand("string"));

const BoolFromString = Schema.transform(BrandedString, BrandedBool, {
  strict: false,
  decode: (i) => i === "on",
  encode: (bool) => (bool ? "on" : "off"),
});

const bool = BrandedBool.make(false); // boolean & Brand<"bool">
const stringFromBool = Schema.encodeSync(BoolFromString)(bool); // Expected: string & Brand<"string">, Actual: string
const boolFromString = Schema.decodeSync(BoolFromString)(stringFromBool); // boolean & Brand<"bool">

What is the expected behavior?

Schema.encodeSync(BoolFromString)(bool) should return string & Brand<"string"> because BrandedString is used as the From type in the transformation.

What do you see instead?

Schema.encodeSync(BoolFromString)(bool) returns string without the brand. The Brand<"string"> is stripped during encoding.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions