Skip to content

Instantly share code, notes, and snippets.

@vbsteven
Created April 8, 2019 16:27
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vbsteven/b2311c51b2686cbacf037b0fffb2eb06 to your computer and use it in GitHub Desktop.
Save vbsteven/b2311c51b2686cbacf037b0fffb2eb06 to your computer and use it in GitHub Desktop.
Custom Spring annotation and validator in Kotlin
package io.license.core.validation
import javax.validation.Constraint
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext
import javax.validation.Payload
import kotlin.reflect.KClass
@Target(AnnotationTarget.FIELD)
@MustBeDocumented
@Constraint(validatedBy = [NullableNotBlankValidator::class])
annotation class NullableNotBlank(
val message: String = "{javax.validation.constraints.NotBlank.message}",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
)
class NullableNotBlankValidator : ConstraintValidator<NullableNotBlank, String> {
override fun isValid(value: String?, context: ConstraintValidatorContext?): Boolean {
if (value == null) return true
return value.isNotBlank()
}
}
@arolfes
Copy link

arolfes commented Dec 8, 2020

Hi @vbsteven,

thanks for sharing your Validator and Annotation.

I changed the isValid Method in NullableNotBlankValidator a bit.

class NullableNotBlankValidator : ConstraintValidator<NullableNotBlank, String> {
    override fun isValid(value: String?, context: ConstraintValidatorContext?) = value?.isNotBlank() ?: true
}

The related tests stay successful

package io.license.core.validation

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

internal class NullableNotBlankValidatorTest {

    private val validator = NullableNotBlankValidator()

    @Test
    fun `should find no validation error, if value is null`() {
        assertTrue(validator.isValid(value = null, context = null))
    }

    @Test
    fun `should return validation error, if value is empty`() {
        assertFalse(validator.isValid(value = "", context = null))
    }

    @ParameterizedTest
    @ValueSource(strings = [" ", "  ", "   "])
    fun `should return validation error, if value contains only spaces`(value: String) {
        assertFalse(validator.isValid(value = value, context = null))
    }

    @ParameterizedTest
    @ValueSource(strings = ["a", " a", "a ", " a "])
    fun `should find no validation error, if value contains at least one character`(value: String) {
        assertTrue(validator.isValid(value = value, context = null))
    }
}

@pmarquez
Copy link

pmarquez commented Mar 4, 2023

Thanks for sharing this Kotlin gist. They are hard to come by and deeply appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment