Skip to content

Commit

Permalink
Add non-companion way of doing validate
Browse files Browse the repository at this point in the history
  • Loading branch information
howyp committed Dec 20, 2017
1 parent bb93a9d commit d59f2d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package eu.timepit.refined.cats

import cats.data.ValidatedNel
import cats.syntax.either._
import eu.timepit.refined.api.RefinedTypeOps
import eu.timepit.refined.api.{Refined, RefinedType, RefinedTypeOps}

object validation {
implicit class CatsValidateOps[FTP, T](companion: RefinedTypeOps[FTP, T]) {
implicit class CatsValidateCompanionOps[FTP, T](companion: RefinedTypeOps[FTP, T]) {
def validate(value: T): ValidatedNel[String, FTP] = companion.from(value).toValidatedNel
}
implicit class CatsValidateSyntax[T](value: T) {
def validate[P](
implicit refined: RefinedType.AuxT[T Refined P, T]): ValidatedNel[String, T Refined P] =
refined.refine(value).toValidatedNel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eu.timepit.refined.cats

import cats.data.{NonEmptyList, Validated}
import cats.implicits._
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
Expand Down Expand Up @@ -34,4 +35,10 @@ class CatsSpec extends Properties("cats") {
import validation._
PosInt.validate(0) ?= Validated.Invalid(NonEmptyList("Predicate failed: (0 > 0).", Nil))
}

property("Validate as syntax on the raw type") = secure {
import validation._
val x = 5
x.validate[Positive] ?= Validated.Valid(PosInt.unsafeFrom(5))
}
}

0 comments on commit d59f2d8

Please sign in to comment.