Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ValidationErrors #95

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Deprecate ValidationErrors in favour of List<ValidationError>
  • Loading branch information
dhoepelman committed Apr 2, 2024
commit 108a01e1d7079f291244797f77b6cc1ebdcb5cf4
28 changes: 11 additions & 17 deletions src/commonMain/kotlin/io/konform/validation/ValidationResult.kt
Original file line number Diff line number Diff line change
@@ -16,17 +16,11 @@ internal data class PropertyValidationError(
}
}

@Deprecated("Replace with directly using List<ValidationError>", ReplaceWith("List<ValidationError>"))
public interface ValidationErrors : List<ValidationError>

internal object NoValidationErrors : ValidationErrors, List<ValidationError> by emptyList()

internal class DefaultValidationErrors(private val errors: List<ValidationError>) : ValidationErrors, List<ValidationError> by errors {
override fun toString(): String {
return errors.toString()
}
}

public sealed class ValidationResult<out T> {
/** Get the validation errors at a specific path. Will return null for a valid result. */
public abstract operator fun get(vararg propertyPath: Any): List<String>?

/** If this is a valid result, returns the result of applying the given [transform] function to the value. Otherwise, return the original error. */
@@ -36,7 +30,7 @@ public sealed class ValidationResult<out T> {
is Invalid -> this
}

public abstract val errors: ValidationErrors
public abstract val errors: List<ValidationError>
}

public data class Invalid(
@@ -52,12 +46,10 @@ public data class Invalid(
}
}

override val errors: ValidationErrors by lazy {
DefaultValidationErrors(
internalErrors.flatMap { (path, errors) ->
errors.map { PropertyValidationError(path, it) }
},
)
override val errors: List<ValidationError> by lazy {
internalErrors.flatMap { (path, errors) ->
errors.map { PropertyValidationError(path, it) }
}
}

override fun toString(): String {
@@ -66,8 +58,10 @@ public data class Invalid(
}

public data class Valid<T>(val value: T) : ValidationResult<T>() {
@Deprecated("It is not useful to index a valid result, it will always return null", ReplaceWith("null"))
override fun get(vararg propertyPath: Any): List<String>? = null

override val errors: ValidationErrors
get() = DefaultValidationErrors(emptyList())
@Deprecated("It is not useful to call errors on a valid result, it will always return an empty list.", ReplaceWith("emptyList()"))
override val errors: List<ValidationError>
get() = emptyList()
}