src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt

changeset 209
c9c6abf167c7
parent 207
479dd7993ef9
child 210
37fbdcb422b7
--- a/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Tue Aug 03 12:22:10 2021 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt	Tue Aug 03 13:41:32 2021 +0200
@@ -37,6 +37,10 @@
 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
 typealias PathParameters = Map<String, String>
 
+sealed interface ValidationResult<T>
+class ValidationError<T>(val message: String): ValidationResult<T>
+class ValidatedValue<T>(val result: T): ValidationResult<T>
+
 class HttpRequest(
     val request: HttpServletRequest,
     val response: HttpServletResponse,
@@ -155,6 +159,18 @@
     fun param(name: String): String? = request.getParameter(name)
     fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray()
 
+    fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), errorMessages: MutableList<String>): T? {
+        return when (val result = validator(param(name))) {
+            is ValidationError -> {
+                errorMessages.add(i18n(result.message))
+                null
+            }
+            is ValidatedValue -> {
+                result.result
+            }
+        }
+    }
+
     private fun forward(jsp: String) {
         request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
     }

mercurial