![JAR search and dependency download from the Maven repository](/logo.png)
com.icerockdev.validation.NoNullElements.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-utils Show documentation
Show all versions of web-utils Show documentation
Ktor based web-server with validation and internationalization
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package com.icerockdev.validation
import javax.validation.Constraint
import javax.validation.Payload
import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS
import kotlin.annotation.AnnotationTarget.CONSTRUCTOR
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
import kotlin.reflect.KClass
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext
/**
* Annotation to validate each list element (is not empty).
*
* Example, validate field
* @field:NoNullElements
*/
@MustBeDocumented
@Constraint(validatedBy = [NoNullElementsValidator::class])
@Target(allowedTargets = [FUNCTION, FIELD, ANNOTATION_CLASS, CONSTRUCTOR, VALUE_PARAMETER, TYPE_PARAMETER])
@Retention(AnnotationRetention.RUNTIME)
annotation class NoNullElements(
val message: String = "must not contain null elements",
val groups: Array> = [],
val payload: Array> = []
)
class NoNullElementsValidator : ConstraintValidator> {
override fun isValid(value: Collection?, context: ConstraintValidatorContext): Boolean {
if (value == null) {
return true
}
return value.stream().noneMatch {
it == null
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy