title: Scala: How to fix type mismatch error trying to decode JSON with Circe Decoder
body: <p>I need some help with the following situation. I have a JSON API with a registration request. The request takes 4 parameters: Standard phone, password, email, and a person parameter where a customer chooses a type. Based on the chosen type, they have to specify one more parameter: Name or age.</p>
  <p>When I get an HTTP request, I need to decode it into my request object. Doing it with the code below results in a compilation error of type:</p>
  <pre class="lang-scala prettyprint-override"><code>type mismatch;
   found   : Unit
   required: io.circe.Decoder[RegistrationReq]
  </code></pre>
  <p>Any ideas why that can be the case and how to fix it? Thanks in advance!</p>
  <pre class="lang-scala prettyprint-override"><code>import cats.effect.Sync
  import cats.syntax.either._
  import circe.jsonOf
  import io.circe.{Decoder, DecodingFailure, HCursor}
  import io.circe.generic.semiauto._
  import io.circe.refined._
  import io.circe.Decoder.Result
  import org.http4s.EntityDecoder
  
  trait RegJsonCodec {
  
    import JsonCodec._ //all common encoders and decoders come from there
  
    implicit val RegistrationReqDecoder: Decoder[RegistrationReq] = {
  
      def apply(c: HCursor): Result[RegistrationReq] =
        for {
          phone &lt;- getPhone(c)
          password &lt;- c.downField(&quot;password&quot;).as[Password]
          email &lt;- c.downField(&quot;email&quot;).as[Email] //Password and Email are refined Strings
          person &lt;- getPerson(c)
        } yield RegistrationReq(phone, password, email, person)
    }
  
    private def getPhone(c: HCursor): Either[DecodingFailure, Long] =
      c.downField(&quot;phone&quot;).as[Long].orElse {
        c.downField(&quot;phone&quot;).as[String].flatMap {
          str =&gt;
            Either.catchNonFatal(str.toLong)
              .leftMap(e =&gt; DecodingFailure(e.getMessage, c.history))
        }
      }.flatMap(phone =&gt; Either.right(phone))
  
    private def getPerson(c: HCursor): Either[DecodingFailure, Person] = {
      c.downField(&quot;person&quot;).downField(&quot;type&quot;).as[String].flatMap { type =&gt;
  
        c.downField(&quot;person&quot;).downField(&quot;name&quot;).as[String]
          .orElse(c.downField(&quot;person&quot;).downField(&quot;age&quot;).as[String]).flatMap { extra =&gt;
  
          Either.catchNonFatal(Person.fromPair(type, extra))
            .leftMap(e =&gt; DecodingFailure(e.getMessage, c.history))
            .flatMap(person =&gt; Either.right(person))
        }
      }
    }
  
    implicit def requestEntityDecoder[F[_] : Sync]: EntityDecoder[F, RegistrationReq] = jsonOf[F, RegistrationReq]
  
  }
  
  object RegJsonCodec extends RegJsonCodec
  </code></pre>