如何同时使用Kotlin数据类和Spring Boot验证来验证Null类型?

huangapple go评论81阅读模式
英文:

how to use kotlin data class and springboot-Validation to verify Null type at the same time?

问题

data class GetSourceListRequest(   
    @NotNull(message = "category not allow null")
    val category: List<Int>    
)

// Then use `@ControllerAdvice` to handle exceptions.
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
fun <T> httpMessageNotReadableExceptionHandler(e: HttpMessageNotReadableException): String {          
    return e.message
}

// The error message obtained is as follows:
// > JSON parse error: Instantiation of [simple type, class com.sage.feature.source.database.request.GetSourceListRequest] value failed for JSON property category due to missing (therefore NULL) value for creator parameter category which is a non-nullable type
//
// But it's not what I expected, `@NotNull` didn't work because the json conversion had an error before that.
//
// I found a related [question](https://stackoverflow.com/questions/49896933/kotlin-data-class-and-bean-validation-notnull-on-long-fields-does-not-work), but the solution it gives is to make the property a nullable type, which is not the perfect solution.
//
// How to use springboot-validation in this case?
英文:
data class GetSourceListRequest(   
    @NotNull(message = &quot;category not allow null&quot;)
    val category: List&lt;Int&gt;    
)

Then use @ControllerAdvice to handle exceptions.

@ExceptionHandler(value = [HttpMessageNotReadableException::class])
    fun &lt;T&gt; httpMessageNotReadableExceptionHandler(e: HttpMessageNotReadableException): String {          
        return e.message
    }

The error message obtained is as follows:
> JSON parse error: Instantiation of [simple type, class com.sage.feature.source.database.request.GetSourceListRequest] value failed for JSON property category due to missing (therefore NULL) value for creator parameter category which is a non-nullable type

But it's not what I expected, @NotNull didn't work because the json conversion had an error before that.

I found a related question, but the solution it gives is to make the property a nullable type, which is not the perfect solution.

How to use springboot-validation in this case?

答案1

得分: 1

Have you tried making the category field nullable?:

data class GetSourceListRequest(   
    @NotNull(message = "category not allow null")
    val category: List<Int>?
)

I know this functionally changes your Request object but this is the general problem I have found when you want to use later-firing validation - the "carrier" object needs to allow for nulls.

The problem goes further with enum values where you cannot necessarily get a good error message if the type only permits valid enum values.

An alternate suggestion for you is to consider a Kotlin-native validation framework that works alongside Spring: Konform (we made our own annotation to invoke the Konform validation automatically on Controllers where we wanted this validation framework).

Even using Konform, we end up with the same thing - request objects whose fields are all nullable and then either using accessor methods with the !! syntax because we know by the same the object is accessed the validation has occurred, or transforming to a strongly typed object.

英文:

Have you tried making the category field nullable?:

data class GetSourceListRequest(   
    @NotNull(message = &quot;category not allow null&quot;)
    val category: List&lt;Int&gt;?
)

I know this functionally changes your Request object but this is the general problem I have found when you want to use later-firing validation - the "carrier" object needs to allow for nulls.

The problem goes further with enum values where you cannot necessarily get a good error message if the type only permits valid enum values.

An alternate suggestion for you is to consider a Kotlin-native validation framework that works alongside Spring : Konform (we made our own annotation to invoke the Konform validation automatically on Controllers where we wanted this validation framework).

Even using Konform, we end up with the same thing - request objects whose fields are all nullable and then either using accessor methods with the !! syntax because we know by the same the object is accessed the validation has occurred, or transforming to a strongly typed object.

huangapple
  • 本文由 发表于 2023年5月11日 10:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定