Skip to content

Commit

Permalink
dave -added convienience validators
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed May 27, 2016
1 parent 94f0a03 commit cba8959
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/main/scala/io/github/daviddenton/crossyfield/Validator.scala
@@ -1,5 +1,8 @@
package io.github.daviddenton.crossyfield

import java.time.{LocalDate, LocalDateTime, ZonedDateTime}
import java.util.UUID

import io.github.daviddenton.crossyfield.Validator.ValidationError

import scala.language.implicitConversions
Expand Down Expand Up @@ -145,3 +148,34 @@ case class Invalid(invalid: Seq[ValidationError]) extends Validation[Nothing] {
object Invalid {
def apply(p: ValidationError): Invalid = Invalid(Seq(p))
}

/**
* Convenience Validators
*/
object Validators {

object string {
def int(id: Symbol, msg: String = "invalid int") = Validator.mk(id, msg, (s: String) => s.toInt)

def double(id: Symbol, msg: String = "invalid double") = Validator.mk(id, msg, (s: String) => s.toDouble)

def long(id: Symbol, msg: String = "invalid long") = Validator.mk(id, msg, (s: String) => s.toLong)

def float(id: Symbol, msg: String = "invalid float") = Validator.mk(id, msg, (s: String) => s.toFloat)

def uuid(id: Symbol, msg: String = "invalid uuid") = Validator.mk(id, msg, UUID.fromString)

def bigDecimal(id: Symbol, msg: String = "invalid bigDecimal") = Validator.mk(id, msg, (s: String) => BigDecimal(s))

def boolean(id: Symbol, msg: String = "invalid boolean") = Validator.mk(id, "invalid float", (s: String) => s.toBoolean)

def char(id: Symbol, msg: String = "invalid boolean") = Validator.mk(id, msg, (s: String) => s.charAt(0))

def localDateTime(id: Symbol, msg: String = "invalid localDateTime") = Validator.mk(id, msg, LocalDateTime.parse)

def localDate(id: Symbol, msg: String = "invalid localDate") = Validator.mk(id, msg, LocalDate.parse)

def zonedDateTime(id: Symbol, msg: String = "invalid zonedDateTime") = Validator.mk(id, msg, ZonedDateTime.parse)
}

}
@@ -1,5 +1,8 @@
package io.github.daviddenton.crossyfield

import java.time.{LocalDate, LocalDateTime}
import java.util.UUID

import org.scalatest._

class ValidatorTest extends FunSpec with ShouldMatchers {
Expand Down Expand Up @@ -81,18 +84,18 @@ class ValidatorTest extends FunSpec with ShouldMatchers {
}

it("handles cross field validation failure") {
val ex = Validator.mk('ex, "a reason" , (s: String) => s )
val ex = Validator.mk('ex, "a reason", (s: String) => s)
ex <--?("bob", "reason", _ != "bob") shouldBe Invalid('ex, "reason")
ex validate("bob", "reason", _ != "bob") shouldBe Invalid('ex, "reason")
}

it("handles cross field validation success") {
val ex = Validator.mk('ex, "a reason" , (s: String) => s )
val ex = Validator.mk('ex, "a reason", (s: String) => s)
ex <--?("bob", "reason", _ == "bob") shouldBe Validated("bob")
}

it("validation failure - function throws") {
val ex = Validator.mk('ex, "a reason" , (s: String) => throw new RuntimeException() )
val ex = Validator.mk('ex, "a reason", (s: String) => throw new RuntimeException())
ex <--?("bob", "reason", _ == "bob") shouldBe Invalid('ex, "a reason")
}

Expand Down Expand Up @@ -136,5 +139,23 @@ class ValidatorTest extends FunSpec with ShouldMatchers {
Validation.<--?(Seq(Ignored, Validated(1), Invalid('ex -> "missing"), Invalid('ex -> "invalid"))) shouldBe Invalid(Seq('ex -> "missing", 'ex -> "invalid"))
}
}

describe("Validators") {
it("string") {
val uuid = UUID.randomUUID()
Validators.string.int('id) <--? "1" shouldBe Validated(1)
Validators.string.long('id) <--? "1" shouldBe Validated(1)
Validators.string.double('id) <--? "1" shouldBe Validated(1.0d)
Validators.string.float('id) <--? "1" shouldBe Validated(1.0f)
(Validators.string.boolean('id) <--? "true") shouldBe Validated(true)
Validators.string.char('id) <--? "1.2345" shouldBe Validated('1')
Validators.string.localDate('id) <--? "2000-01-01" shouldBe Validated(LocalDate.of(2000, 1, 1))
Validators.string.localDateTime('id) <--? "1970-01-01T00:00:00" shouldBe Validated(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0))
(Validators.string.zonedDateTime('id) <--? "1970-01-01T00:00:00-00:00").toString shouldBe "Validated(1970-01-01T00:00Z)"
Validators.string.bigDecimal('id) <--? "1.2345" shouldBe Validated(BigDecimal("1.2345"))
Validators.string.uuid('id) <--? uuid.toString shouldBe Validated(uuid)
}
}

}
}

0 comments on commit cba8959

Please sign in to comment.