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

spice.http.server.rest.RestfulValidation.scala Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package spice.http.server.rest

import spice.ValidationError

trait RestfulValidation[Request] {
  def validate(request: Request): Either[ValidationError, Request]
}

object RestfulValidation {
  def nonEmpty[Request, T](f: Request => Option[T],
                           error: => ValidationError): RestfulValidation[Request] = apply(r => f(r).isEmpty, error)

  def apply[Request, T](f: Request => Boolean, error: => ValidationError): RestfulValidation[Request] = {
    new RestfulValidation[Request] {
      override def validate(request: Request): Either[ValidationError, Request] = {
        if (f(request)) {
          Right(request)
        } else {
          Left(error)
        }
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy