10

I already know how to receive a JSON object and automatically deserialize it into the required format (e.g. with a data class). Also look here: How to receive JSON object in Ktor?

My problem now is that I want to validate the JSON request and return BadRequest if it's not in the desired format, something like that in Django: https://stackoverflow.com/a/44085405/5005715

How can I do that in Ktor/Kotlin? Unfortunately, I couldn't find a solution in the docs. Also, required/optional fields would be nice.

Aliquis
  • 2,091
  • 4
  • 21
  • 40
  • 1
    What's wrong with validating the object after decode? BTW the Django example looks like [JSR-303](https://beanvalidation.org/), but I haven't seen something like this in Ktor – Omar Mainegra Jan 16 '19 at 05:14
  • 1
    the problem is that if a mandatory parameter is missing, Jackson will send an Exception because it cannot deserialize the JSON. I have the same issue and I haven't found in the docs how to fail with a BadRequestException – Rytek Mar 18 '20 at 15:51
  • @Rytek did you find anything which solved your issue? – VishalDevgire Apr 06 '21 at 19:38

5 Answers5

13

You can use hibernate-validator for input validations. Refer below:

Add Dependency (Gradle):

compile "org.hibernate.validator:hibernate-validator:6.1.1.Final"

Annotate your data class (DTO):

data class SampleDto(
    @field:Min(value=100)
    val id: Int,
    @field:Max(value=99)
    val age: Int
)

Add Validator in Routing:

import javax.validation.Validation

fun Application.module() {

    val service = SampleService()
    val validator = Validation.buildDefaultValidatorFactory().validator

    routing {
        post("/sample/resource/") {
            val sampleDto = call.receive<SampleDto>()
            sampleDto.validate(validator)
            service.process(sampleDto)
            call.respond(HttpStatusCode.OK)
        }
    }
}

@Throws(BadRequestException::class)
fun <T : Any> T.validate(validator: Validator) {
    validator.validate(this)
        .takeIf { it.isNotEmpty() }
        ?.let { throw BadRequestException(it.first().messageWithFieldName()) }
}

fun <T : Any> ConstraintViolation<T>.messageWithFieldName() = "${this.propertyPath} ${this.message}"

Bonus Step (Optional) - Add Exception Handler:

fun Application.exceptionHandler() {

    install(StatusPages) {
        exception<BadRequestException> { e ->
            call.respond(HttpStatusCode.BadRequest, ErrorDto(e.message, HttpStatusCode.BadRequest.value))
            throw e
        }
    }

}

data class ErrorDto(val message: String, val errorCode: Int)
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
5

Here is a quick example of how to validate and respond with 400 if needed.

fun main(args: Array<String>) {
    embeddedServer(Netty, 5000) {
        install(CallLogging)
        install(ContentNegotiation) { gson { } }
        install(Routing) {
            post("test") {
                val sample = call.receive<Sample>()
                if (!sample.validate()) {
                    call.respond(HttpStatusCode.BadRequest, "Sample did not pass validation")
                }
                call.respond("Ok")
            }
        }
    }.start()
}

fun Sample.validate(): Boolean = id > 5

data class Sample(val id: Int)

Did you have something else in mind?

There are no inbuilt annotations or the like.

avolkmann
  • 2,962
  • 2
  • 19
  • 27
1

Taking the answer of Andreas a step further you can return a list of errors when the request is invalid like so:

    post {
        val postDog = call.receive<PostDog>()
        val validationErrors = postDog.validate()
        if (validationErrors.isEmpty()) {

            // Save to database

        } else {
            call.respond(HttpStatusCode.BadRequest, validationErrors)
        }

    }

    fun PostDog.validate() : List<Error> {
        var validationErrors : MutableList<Error> = mutableListOf()
        if(name == null || name.isBlank())
            validationErrors.add(Error(code = "dog.name.required", message = "Dog requires a name"))
        if(color == null || color.isBlank())
            validationErrors.add(Error(code = "dog.color.required", message = "Dog requires a color"))            
        return validationErrors
    }

    data class PostDog(
      val name: String,
      val color: String          
    )

    data class Error(
        val code : String,
        val message : String
    )
34m0
  • 5,755
  • 1
  • 30
  • 22
1

use this library

https://github.com/valiktor/valiktor

data class Employee(val id: Int, val name: String, val email: String) {
        init {
            validate(this) {
                validate(Employee::id).isPositive()
                validate(Employee::name).hasSize(min = 3, max = 80)
                validate(Employee::email).isNotBlank().isEmail()
            }
        }
    }
0

Am not sure Ktor has already something for that.Spring handles it well with @Valid annotation. I was also looking for something like this to make validation whether of the json or the object. I found this framework https://github.com/making/yavi. It looks interesting. I will make a try