commonMain.neat.internal.RequiredValidator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neat-validation-jvm Show documentation
Show all versions of neat-validation-jvm Show documentation
A collection library that is built with interoperability in mind
The newest version!
package neat.internal
import neat.CompoundValidators
import neat.Invalid
import neat.Validator
import neat.Validators
import neat.Validity
import neat.aggregate
@PublishedApi
internal class RequiredValidator(val validators: Validators
, val message: String) : Validator
{
override val label: String = validators.label
override fun validate(value: P?): Validity
= buildList {
if (value == null) {
add(Invalid(value, listOf(message)))
return@buildList
}
if (validators is CompoundValidators) {
addAll(
validateRecursively(
validators = validators as CompoundValidators,
value = value
)
)
} else {
addAll(validators.functions.values.map {
it.function(value)
})
}
}.aggregate(value) as Validity
}