0

I'd like to use the Cats ValidatedNec data type similarly to the example in the Cats documentation for Validated in the section Meeting applicative-- in my case, I'm parsing Strings from a file, validating against an appropriate regex for the field, and then (for several fields) converting to a different data type (assuming the regex matched). However, instead of using case objects which extend a common trait for the invalid results (as in the example), I'd like to use case classes (which extend a common trait) so I can include contextual information in the case of failure. Can this be done as simply as calling all the validation methods (putting the results in a tuple), as in the validateForm example, and calling mapN? I'm getting conflicting errors from Intellij (from IntelliJ, it's telling me the expected and actual parameters to mapN are the same (though it is still marking it as an error); when running sbt on the command line, it doesn't resolve the mapN method. I'm using Scala 2.12.8 and cats 2.0.0-M1. Any help would be appreciated!

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Timothy Perrigo
  • 723
  • 4
  • 18

1 Answers1

0

https://gist.github.com/DmytroMitin/6cf51c3563dba62307a2a256eeb4ebb5


"IntelliJ errors" are irrelevant.

Regarding "sbt errors", in order to make mapN work, check that you have

import cats.syntax.apply._

If this still doesn't work for you provide your MCVE.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • I do have `cats.syntax.apply._` imported; the error I'm getting from sbt after calling all the validation methods (with the results in a tuple) is `possible cause: maybe a semicolon is missing before `value mapN'?`. I will submit an MCVE as soon as I have a chance. Thanks for replying! – Timothy Perrigo May 28 '19 at 17:34
  • I apologize for taking so long to reply, but here is a link to a [gist](https://gist.github.com/tperrigo/adc9dc4d3775ff235da47b19e2e20dd6) which recreates the compile error I've been experiencing (`ValidationExample.scala:47:35: value mapN is not a member of (ValidatorNec.this.ValidationResult[java.time.LocalDateTime], ValidatorNec.this.ValidationResult[Double]) [error] possible cause: maybe a semicolon is missing before `value mapN'?`) – Timothy Perrigo Jun 02 '19 at 15:57
  • 1
    @TimothyPerrigo If you imported `cats.implicits._` you shouldn't import `cats.syntax.apply._`, otherwise you make ambiguity. If you remove the import the error is `type mismatch; found : App.Reading.type required: (java.time.LocalDateTime, Double) => App.Reading`. So for some reason compiler doesn't recognize `Reading` as a function. – Dmytro Mitin Jun 02 '19 at 16:51
  • @TimothyPerrigo If you replace `Reading` with `Reading(_,_)` you'll have `type mismatch; found : java.time.LocalDateTime required: java.time.LocalDate` and you'll see that your definition of case class `Reading` is incorrect. – Dmytro Mitin Jun 02 '19 at 16:51
  • @TimothyPerrigo https://gist.github.com/DmytroMitin/6cf51c3563dba62307a2a256eeb4ebb5 – Dmytro Mitin Jun 02 '19 at 16:51
  • Oh man...I apologize for missing something so simple! Thanks for your help! – Timothy Perrigo Jun 03 '19 at 11:24