commonMain.neat.internal.OptionalValidator.kt Maven / Gradle / Ivy
package neat.internal
import neat.CompoundValidators
import neat.Valid
import neat.Validator
import neat.Validators
import neat.Validity
import neat.aggregate
@PublishedApi
internal class OptionalValidator(val validators: Validators
) : Validator
{
override val label: String = validators.label
override fun validate(value: P?): Validity
{
if (value == null) return Valid(value)
if (validators is CompoundValidators) {
return validateRecursively(
validators = validators as CompoundValidators,
value = value
).aggregate(value)
}
return validators.functions.values.map {
it.function(value)
}.aggregate(value)
}
}