Skip to content

Commit

Permalink
Add Validation.ifPresent and Validation.required (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoepelman authored Nov 11, 2024
1 parent d921b3a commit 7fb9b8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/konform.api
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public abstract interface class io/konform/validation/ValidationErrors : java/ut

public final class io/konform/validation/ValidationKt {
public static final fun flatten (Ljava/util/List;)Lio/konform/validation/Validation;
public static final fun ifPresent (Lio/konform/validation/Validation;)Lio/konform/validation/Validation;
public static final fun required (Lio/konform/validation/Validation;)Lio/konform/validation/Validation;
}

public abstract class io/konform/validation/ValidationResult {
Expand Down
7 changes: 7 additions & 0 deletions src/commonMain/kotlin/io/konform/validation/Validation.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.konform.validation

import io.konform.validation.types.EmptyValidation
import io.konform.validation.types.NullableValidation
import io.konform.validation.types.ValidateAll

public interface Validation<in T> {
Expand All @@ -20,3 +21,9 @@ public fun <T> List<Validation<T>>.flatten(): Validation<T> =
1 -> first()
else -> ValidateAll(this)
}

/** Run a validation only if the actual value is not-null. */
public fun <T : Any> Validation<T>.ifPresent(): Validation<T?> = NullableValidation(required = false, validation = this)

/** Require a nullable value to actually be present. */
public fun <T : Any> Validation<T>.required(): Validation<T?> = NullableValidation(required = true, validation = this)

0 comments on commit 7fb9b8e

Please sign in to comment.