CodexBloom - Programming Q&A Platform

Type Mismatch scenarios When Using `Option` with Cats' `Validated` in Scala 3.1.1

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-27
scala cats option validated Scala

I'm dealing with I've searched everywhere and can't find a clear answer..... I'm trying to figure out I'm working on a personal project and I've looked through the documentation and I'm still confused about After trying multiple solutions online, I still can't figure this out... I'm working with a type mismatch behavior when trying to combine `Option` with Cats' `Validated` in my Scala 3.1.1 application. My goal is to create a function that validates a list of inputs, returning a `Validated` type that collects errors if any of the inputs are invalid. Here's a simplified version of what I'm trying to do: ```scala import cats.data.Validated import cats.implicits._ def validateInput(input: Option[String]): Validated[String, String] = { input match { case Some(value) if value.nonEmpty => Validated.valid(value) case None => Validated.invalid("Input want to be None") case Some(value) if value.trim.isEmpty => Validated.invalid("Input want to be empty") } } def validateAll(inputs: List[Option[String]]): Validated[List[String], List[String]] = { inputs.map(validateInput).sequence } val inputs = List(Some("hello"), None, Some(" "), Some("world")) val result = validateAll(inputs) println(result) ``` When I run this code, I get the following behavior message: ``` behavior: type mismatch; found : List[cats.data.Validated[String,String]] required: cats.data.Validated[List[String],List[String]] ``` I've attempted to modify the `validateAll` function by using `Validated.sequence` instead of `List.sequence`, but that led to an even more confusing type behavior. I feel exploring trying to figure out how to properly combine `Validated` results. Any insights on how I can fix this type mismatch scenario? Is there a specific pattern I should follow when working with `Validated` in this context? Thanks in advance! Is there a better approach? I'm working with Scala in a Docker container on CentOS. Any ideas how to fix this? This is happening in both development and production on Ubuntu 20.04. I'd really appreciate any guidance on this. This is happening in both development and production on Windows 11. Any pointers in the right direction?