0

I have rest controller with token creation call. Here inside ObjectNode I get big json data. The database column is varchar2(4000) nad I want limit this ObjectNode size to 4000 adding validation at controller level. Not sure how to do this?

data class TokenRequest(
    @NotEmpty(message = "id is mandatory")
    open val id: String,
    @NotEmpty(message = "gameId is mandatory")
    open val game: String,
    @NotEmpty(message = "gameType is mandatory")
    open val type: String,
    @NotEmpty(message = "gameDate is mandatory")
    open val date: String,
    @NotEmpty(message = "coupon is mandatory")
    open val token: ObjectNode,
)
class TokenController {
fun createToken(@Valid @RequestBody request: TokenRequest): Token {
        val now = Token.generateNowTimestamp()
        val token = Token.fromTokenRequest(request, now, now, request.teamId)
        return tokenService.create(token)
    }
}
Pramod
  • 69
  • 1
  • 10

1 Answers1

2

It sounds like you're trying to cap the size of the JSON data contained in the 'token' field of your request. You want it to be no more than 4000 characters, right? There's actually a way to handle this in Kotlin by creating your own validation annotation. Here's how:

First, you need to create the annotation itself:

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Constraint(validatedBy = [JsonNodeLengthValidator::class])
annotation class MaxJsonLength(
    val message: String = "JSON Object is too big",
    val groups: Array<KClass<*>> = [],
    val payload: Array<KClass<out Payload>> = [],
    val value: Int = 4000
)

Then, we'll make a custom validator for it:

import com.fasterxml.jackson.databind.node.ObjectNode
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext

class JsonNodeLengthValidator : ConstraintValidator<MaxJsonLength, ObjectNode> {

    private var maxLength: Int = 0

    override fun initialize(annotation: MaxJsonLength) {
        this.maxLength = annotation.value
    }

    override fun isValid(node: ObjectNode?, context: ConstraintValidatorContext): Boolean {
        return node?.toString()?.length ?: 0 <= maxLength
    }
}

Finally, we'll use our shiny new validator annotation in your data class:

data class TokenRequest(
    @NotEmpty(message = "id is mandatory")
    open val id: String,
    @NotEmpty(message = "gameId is mandatory")
    open val game: String,
    @NotEmpty(message = "gameType is mandatory")
    open val type: String,
    @NotEmpty(message = "gameDate is mandatory")
    open val date: String,
    @NotEmpty(message = "coupon is mandatory")
    @MaxJsonLength(value = 4000, message = "Token JSON object is too big")
    open val token: ObjectNode,
)

So there you have it! This makes sure that your TokenRequest validation will fail if the JSON string of token goes beyond 4000 characters. If it does, you'll get a validation error. Hope this helps!

  • This answer, along with many others you've posted in the last few days, appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with this answer, I would encourage you to delete it. – NotTheDr01ds Jun 02 '23 at 13:26
  • Since it appears likely to have been written by AI, **readers should review this answer carefully and critically, as it *may* contain fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 02 '23 at 13:26