zio-schema icon indicating copy to clipboard operation
zio-schema copied to clipboard

Rationalize handling of nested Optional schemas

Open thinkharderdev opened this issue 3 years ago • 0 comments

Currently if we have a nested Optional schema such as Schema.Optional(Schema.Optional(Schema.primtive(StandardType.StringType)) we cannot distinguish between the JSONprotobuf encodings of Some(None) and None which will both be encoded to null in JSON (and similarly for protobuf).

As a first order issue this will cause test failures when we do a "round-trip" encoding/decoding of Some(None which will encode to null and then decode to just plain None instead of Some(None).

A couple of options for handling this:

  1. Use a type constraint to prevent "nesting" of optional schemas
trait Schema[A] {. self =>
  def optional(implicit ev: A <:!< Option[Any]): Schema.Optional[Option[A]]
} 

This generalized type constraint doesn't exist in Scala so we'd have to create it

  1. Allow nested optional schemas but make it so Some(None) == None in the specs.

thinkharderdev avatar May 22 '21 12:05 thinkharderdev