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

invirt.http4k.views.validation.kt Maven / Gradle / Ivy

There is a newer version: 0.10.11
Show newest version
package invirt.http4k.views

import invirt.http4k.AppRequestContexts
import io.validk.ValidationErrors
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.lens.RequestContextKey
import org.http4k.template.ViewModel

/**
 * Used strictly to provide these errors within the context of a macro
 */
internal val validationErrorContextKey = RequestContextKey.optional(AppRequestContexts.contexts)

internal fun Request.setErrors(errors: ValidationErrors) {
    validationErrorContextKey[this] = errors
}

class ErrorResponseView(
    val model: Any,
    val errors: ValidationErrors,
    val view: String
) : ViewResponse(view)

fun ViewModel.errorResponse(errors: ValidationErrors, status: Status = Status.UNPROCESSABLE_ENTITY): Response {
    return ErrorResponseView(this, errors, this.template()).status(status)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy