diff options
Diffstat (limited to 'src/main/scala/xyz/driver/common/validation')
-rw-r--r-- | src/main/scala/xyz/driver/common/validation/ValidationError.scala | 3 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/common/validation/Validators.scala | 41 |
2 files changed, 0 insertions, 44 deletions
diff --git a/src/main/scala/xyz/driver/common/validation/ValidationError.scala b/src/main/scala/xyz/driver/common/validation/ValidationError.scala deleted file mode 100644 index 6db445d..0000000 --- a/src/main/scala/xyz/driver/common/validation/ValidationError.scala +++ /dev/null @@ -1,3 +0,0 @@ -package xyz.driver.common.validation - -final case class ValidationError(message: String) diff --git a/src/main/scala/xyz/driver/common/validation/Validators.scala b/src/main/scala/xyz/driver/common/validation/Validators.scala deleted file mode 100644 index 8d807f4..0000000 --- a/src/main/scala/xyz/driver/common/validation/Validators.scala +++ /dev/null @@ -1,41 +0,0 @@ -package xyz.driver.common.validation - -import xyz.driver.common.logging._ -import xyz.driver.common.utils.JsonSerializer - -import scala.util.control.NonFatal - -object Validators extends PhiLogging { - - type Validator[Input, Refined] = Input => Either[ValidationError, Refined] - - def generic[T, R](message: String)(f: PartialFunction[T, R]): Validator[T, R] = { value => - if (f.isDefinedAt(value)) Right(f(value)) - else Left(ValidationError(message)) - } - - def nonEmpty[T](field: String): Validator[Option[T], T] = generic(s"$field is empty") { - case Some(x) => x - } - - def nonEmptyString(field: String): Validator[String, String] = generic(s"$field is empty") { - case x if x.nonEmpty => x - } - - def deserializableTo[Refined](field: String) - (value: String) - (implicit m: Manifest[Refined]): Either[ValidationError, Refined] = { - try { - Right(JsonSerializer.deserialize[Refined](value)) - } catch { - case NonFatal(e) => - logger.error(phi"Can not deserialize the ${Unsafe(field)}: $e") - Left(ValidationError(s"$field is invalid")) - } - } - - def success[T](result: T): Either[Nothing, T] = Right(result) - - def fail(message: String): Either[ValidationError, Nothing] = Left(ValidationError(message)) - -} |