# \`oneOfVariant\` and Scala 3 \`enum\`

**URL:** <https://softwaremill.community/t/oneofvariant-and-scala-3-enum/199>\
**Category:** tapir\
**Created:** [May 10, 2023, 3:12pm UTC](https://softwaremill.community/t/oneofvariant-and-scala-3-enum/199 "2023-05-10T15:12:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [May 10, 2023, 3:12pm UTC](https://softwaremill.community/t/oneofvariant-and-scala-3-enum/199/1 "2023-05-10T15:12:39Z")

</div>

Hello again! 👋

I’m using zio-json and tapir, and trying to use `.errorOut(oneOf[QueryValidationError](oneOfVariant[SyntaxError](...), ...))`.

I have this code:

```plaintext
  enum QueryValidationError(val message: String) {
    case InvalidRequestError(msg: String) extends QueryValidationError(msg)
    case SyntaxError(msg: String) extends QueryValidationError(msg)
    case CompilationError(msg: String) extends QueryValidationError(msg)
  }

  object QueryValidationError {
    given JsonCodec[QueryValidationError] = DeriveJsonCodec.gen
  }

```

and the my code on the tapir side doesn’t compile because there are no given instances for the members of the `enum`. I’m not sure if this is a tapir thing or zio-json thing, and would really like to know how to do this 🙂

Thank you very much! ❤

---

<div class="post-metadata">

**Author:** ![kciesielski](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/kciesielski/32/133_2.png) [@kciesielski](https://softwaremill.community/u/kciesielski)\
**Post date:** [May 10, 2023, 3:39pm UTC](https://softwaremill.community/t/oneofvariant-and-scala-3-enum/199/2 "2023-05-10T15:39:40Z")

</div>

Hello, Ben!

In case of enums you need json codecs for each variant. Here’s a mini example of a working endpoint (based on the Adopt Tapir template):

```auto
  enum QueryValidationError(val message: String) {
    case InvalidRequestError(msg: String) extends QueryValidationError(msg)
    case SyntaxError(msg: String) extends QueryValidationError(msg)
    case CompilationError(msg: String) extends QueryValidationError(msg)
  }

  object QueryValidationError {
    given validationError: zio.json.JsonCodec[SyntaxError] = DeriveJsonCodec.gen[SyntaxError]
    given compilationError: zio.json.JsonCodec[CompilationError] = DeriveJsonCodec.gen[CompilationError]
    given invalidRequestError: zio.json.JsonCodec[InvalidRequestError] = DeriveJsonCodec.gen[InvalidRequestError]
  }

  import QueryValidationError._

  val booksListing: PublicEndpoint[Unit, QueryValidationError, List[Book], Any] = endpoint.get
    .in("books" / "list" / "all")
    .out(jsonBody[List[Book]])
    .errorOut(
      oneOf(
        oneOfVariant(statusCode(StatusCode.BadRequest).and(jsonBody[SyntaxError])),
        oneOfVariant(
          statusCode(StatusCode.Unauthorized).and(jsonBody[InvalidRequestError])
        ),
        oneOfDefaultVariant(statusCode(StatusCode.InternalServerError).and(jsonBody[CompilationError]))
      )
    )
  val booksListingServerEndpoint: ZServerEndpoint[Any, Any] = booksListing.serverLogic(_ => ZIO.succeed(Left(SyntaxError("oh no!"))))

```

---

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [May 10, 2023, 3:41pm UTC](https://softwaremill.community/t/oneofvariant-and-scala-3-enum/199/3 "2023-05-10T15:41:08Z")

</div>

Thank you so much! 🤩
