Issue with Composing Schema.Option with PhoneNumber Schema in Effect Typescript Library

I'm trying to model a domain schema in order to compose with Schema.Option* when needed.

This schema works as expected:
const PhoneNumber = pipe(
    Schema.NonEmptyTrimmedString,
    Schema.filter((i) => isValidPhoneNumber(i, "IT")),
    Schema.brand("Field/PhoneNumber"),
);

but it fails if used in combination with Schema.Option (this is the content of an HOF; instead of PhoneNumber there is schema parameter) as it looks the filter error takes precedence:
pipe(
    Schema.OptionFromNonEmptyTrimmedString,
    Schema.compose(Schema.OptionFromSelf(Schema.typeSchema(PhoneNumber))),
)


So I tried using a transformation, but it looks Schema.typeSchema skips decode and only Schema.String is checked against. Same using Schema.is as it always returns true even if should return
false
as the predicate yields
false
.
const PhoneNumber = pipe(
    Schema.NonEmptyTrimmedString,
    Schema.transformOrFail(pipe(Schema.String, Schema.brand("Field/PhoneNumber")), {
        strict: true,
        decode: (i, _, ast) =>
            Either.liftPredicate(
                i,
                () => isValidPhoneNumber(i, "IT"),
                () => new ParseResult.Transformation(ast, i, "Transformation", new ParseResult.Type(ast, i))
            ),
        encode: (i) => Either.right(i)
    }),
);

using Schema.decode w/o Schema.typeSchema works as expected and fails correctly:
ParseError: (NonEmptyTrimmedString <-> string & Brand<"Field/PhoneNumber">)
└─ Transformation process failure
   └─ (NonEmptyTrimmedString <-> string & Brand<"Field/PhoneNumber">)
      └─ Transformation process failure
         └─ Expected (NonEmptyTrimmedString <-> string & Brand<"Field/PhoneNumber">), actual "123456789"
Was this page helpful?