mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-05 04:56:02 +01:00
Initial experimental implementation of the validation framework.
This commit is contained in:
@@ -1,11 +1,36 @@
|
||||
package app
|
||||
|
||||
import org.scalatra._
|
||||
import org.scalatra.json._
|
||||
import org.json4s._
|
||||
import org.json4s.jackson._
|
||||
|
||||
/**
|
||||
* Provides generic features for ScalatraServlet implementations.
|
||||
*/
|
||||
trait ServletBase {
|
||||
abstract class ServletBase extends ScalatraServlet with JacksonJsonSupport {
|
||||
|
||||
implicit val jsonFormats = DefaultFormats
|
||||
|
||||
// TODO get from session
|
||||
val LoginUser = System.getProperty("user.name")
|
||||
|
||||
protected def withValidation(validator: Map[String, String] => ValidationResult, params: Map[String, String])(action: => Any): Any = {
|
||||
validator(params).valid match {
|
||||
case true => action
|
||||
case false => throw new RuntimeException("Invalid Request") // TODO show error page?
|
||||
}
|
||||
}
|
||||
|
||||
case class ValidationResult(valid: Boolean, errors: Map[String, String]){
|
||||
def toJSON(): JObject = {
|
||||
JObject(
|
||||
"valid" -> JBool(valid),
|
||||
"errors" -> JObject(errors.map { case (key, value) =>
|
||||
JField(key, JString(value))
|
||||
}.toList)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user