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

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

The newest version!
package io.github.cdimascio.swagger

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

operator fun Regex.contains(text: CharSequence): Boolean = this.matches(text)
data class ValidationError(val code: Int, val message: String)

typealias ErrorHandler = (status: HttpStatus, List) -> T
class Validate internal constructor(
        swaggerJsonPath: String,
        errorHandler: ErrorHandler) {

    /**
     * Returns an instance of Validate
     */
    companion object Instance {
        private val defaultErrorHandler: ErrorHandler =
                { status, messages -> ValidationError(status.value(), messages[0]) }

        /**
         * Configure the validator by specifying the path to a Swagger v2 specification
         * @param swaggerJsonPath path to a json formatted v2 swagger specification
         * @return the validate object
         */
        fun configure(swaggerJsonPath: String) = configure(swaggerJsonPath, defaultErrorHandler)

        /**
         * Configure the validator by specifying the path to a Swagger v2 JSON specification and a custom error handler
         * @param swaggerJsonPath path to a json formatted v2 swagger specification
         * @param errorHandler custom validation error handler
         * @return the validate object
         */
        fun  configure(swaggerJsonPath: String, errorHandler: ErrorHandler) = Validate(swaggerJsonPath, errorHandler)
    }

    private val validator = Validator(swaggerJsonPath, errorHandler)

    /**
     * @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) = validator.validate(request) ?: handler()

    inner class Request(val request: ServerRequest) {
        /**
         * @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)
        }
    }

    inner 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) ?: handler(success(it)) }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy