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

io.github.cdimascio.swagger.Validate.kt Maven / Gradle / Ivy

package io.github.cdimascio.swagger

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.web.reactive.function.BodyExtractors
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.body
import reactor.core.publisher.Mono


object Validate {
    class BodyValidator(val request: ServerRequest, val bodyType: Class) {
        fun validate(handler: (T) -> Mono): Mono {
            val success = { json: String -> jacksonObjectMapper().readValue(json, bodyType) }
            val json = request.body(BodyExtractors.toMono(String::class.java))
            return json.flatMap {
                Validator.validate(request, it)?.let {
                    ServerResponse.badRequest().body(Mono.just(it))
                } ?: handler(success(it))
            }
        }
    }


    /**
     * @param request the server request, typically used withBody
     * @return Request
     */
    fun request(request: ServerRequest) = Request(request)


    /**
     * @param handler a function which returns a server response
     * @returns a server response
     */
    fun request(request: ServerRequest, handler: () -> Mono): Mono {
        val error = Validator.validate(request)
        return if (error != null) ServerResponse.badRequest().body(Mono.just(error))
        else handler()
    }

    class Request(val request: ServerRequest /* error handler but have a default */) {

        /**
         * @param bodyType the type to deserialize
         * @param handler a function which recieves the body and returns a server response
         * @returns a server response
         */
        fun  withBody(bodyType: Class, handler: (T) -> Mono): Mono {
            return BodyValidator(request, bodyType).validate(handler)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy