Issue with Custom Message in `Schema.optional` vs `Schema.optionalWith`

I don't know if this behaviour is intended or not. I noticed that when I use Schema.optional I can't provide a custom message.

For example, here I get the message Expected undefined, actual "" when decoding the input { foo: '' } with IgnoredMessage even though it defines a custom message.

I don't have this issue when using Schema.optionalWith(Foo, { exact: true }), so I suppose the problem comes from some Schema.Union(Foo, Schema.Undefined), that is used internally by Schema.optional and which message is not overridden by the user annotation.

const Foo = Schema.NumberFromString.annotations({
  message: () => ({ message: 'not a number', override: true })
})

const IgnoredMessage = Schema.Struct({
  foo: Schema.optional(Foo).annotations({
    message: () => ({ message: 'BOOM!', override: true })
  })
})

const Exact = Schema.Struct({
  foo: Schema.optionalWith(Foo, { exact: true })
})

const program = Schema.decodeUnknown(IgnoredMessage)({ foo: '' })


optionalWith is probably fine in my use case, but my program does not actually care if the value is missing or
undefined
, so it breaks the robustness principle.
Was this page helpful?