All Downloads are FREE. Search and download functionalities are using the official Maven repository.

validation.ValidationError.scala Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
package jap
package validation

sealed abstract class ValidationError(
    val error: String,
    val description: Option[String] = None,
)(implicit val path: FieldPath) {
  override def toString = path.full + ":" + error + description.fold("")("(" + _ + ")")
}
object ValidationError {
  case class Custom(
      override val error: String,
      override val description: Option[String] = None,
  )(implicit path: FieldPath)
      extends ValidationError(error, description)

  case class Compare(op: Compare.Op, compared: String)(implicit path: FieldPath)
      extends ValidationError("compare", Some(s"${path.full} should be ${Compare.messageByOp(op)} $compared"))

  object Compare {
    sealed trait Op
    case object >   extends Op
    case object >=  extends Op
    case object <   extends Op
    case object <=  extends Op
    case object === extends Op
    case object !== extends Op

    def messageByOp(op: Op) =
      op match {
        case >   => "more than"
        case >=  => "more than or equal to"
        case <   => "less than"
        case <=  => "less than or equal to"
        case === => "equal to"
        case !== => "not equal to"
      }
  }

  case class MinSize(size: Int)(implicit path: FieldPath)
      extends ValidationError("min-length", Some(s"${path.full} min size should be $size"))

  case class MaxSize(size: Int)(implicit path: FieldPath)
      extends ValidationError("max-length", Some(s"${path.full} max should be $size"))

  case class Empty()(implicit path: FieldPath)
      extends ValidationError("empty", Some(s"${path.full} should not be empty"))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy