commonMain.neat.internal.RequiredValidator.kt Maven / Gradle / Ivy
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
}