All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icerockdev.validation.NoNullElements.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * 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