All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.test.AsyncValidationRunner.kt Maven / Gradle / Ivy

There is a newer version: 0.23.0
Show newest version
@file:JvmName("AsyncValidationRunner")

package io.kform.test

import io.kform.*
import io.kform.schemas.AnySchema
import java.util.concurrent.CompletableFuture
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.future.future

/** Default coroutine scope for running validations within futures. */
private val defaultScope =
    CoroutineScope(Dispatchers.Default + CoroutineName("Async validation runner"))

/**
 * Runs a given [validation] within a form with schema [formSchema] and returns a future that
 * completes with a list of its issues.
 *
 * It is necessary to provide all information required to run the validation: the [path] of the
 * value being validated, the [value] itself, the [values of the dependencies][dependencyValues] (by
 * dependency name), and the values of the [external contexts][externalContexts] that the validation
 * depends on.
 *
 * If the validation depends on values within the value being validated, then these may be omitted
 * from the provided [dependencyValues].
 *
 * Otherwise, missing dependencies or external contexts will be passed to the validation as `null`,
 * in which case the validation may or may not throw depending on its implementation.
 *
 * Exceptions thrown while running the validation will not be caught by this function. As opposed to
 * how a [form validator][FormValidator] or [form manager][FormManager] operate, exceptions are
 * **not** wrapped in a [ValidationExceptionError] nor emitted as part of the issues flow.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas,
 * with [InvalidDependencyPathException] if [validation] contains an invalid dependency, or with
 * [IllegalArgumentException] when providing unknown names for [dependencyValues] or
 * [externalContexts] or when providing mismatching dependency values for dependencies within
 * [value].
 */
@JvmOverloads
@JvmName("runValidation")
public fun  runValidationAsync(
    formSchema: Schema<*>,
    validation: Validation,
    value: T,
    path: Path = AbsolutePath.ROOT,
    dependencyValues: Map? = null,
    externalContexts: ExternalContexts? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    coroutineScope.future {
        runValidation(formSchema, validation, value, path, dependencyValues, externalContexts)
            .toList()
    }

/**
 * Runs a given [validation] within a form with schema [formSchema] and returns a future that
 * completes with a list of its issues.
 *
 * It is necessary to provide all information required to run the validation: the [path] of the
 * value being validated, the [value] itself, the [values of the dependencies][dependencyValues] (by
 * dependency name), and the values of the [external contexts][externalContexts] that the validation
 * depends on.
 *
 * If the validation depends on values within the value being validated, then these may be omitted
 * from the provided [dependencyValues].
 *
 * Otherwise, missing dependencies or external contexts will be passed to the validation as `null`,
 * in which case the validation may or may not throw depending on its implementation.
 *
 * Exceptions thrown while running the validation will not be caught by this function. As opposed to
 * how a [form validator][FormValidator] or [form manager][FormManager] operate, exceptions are
 * **not** wrapped in a [ValidationExceptionError] nor emitted as part of the issues flow.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas,
 * with [InvalidDependencyPathException] if [validation] contains an invalid dependency, or with
 * [IllegalArgumentException] when providing unknown names for [dependencyValues] or
 * [externalContexts] or when providing mismatching dependency values for dependencies within
 * [value].
 */
@JvmOverloads
@JvmName("runValidation")
public fun  runValidationAsync(
    formSchema: Schema<*>,
    validation: Validation,
    value: T,
    path: String,
    dependencyValues: Map? = null,
    externalContexts: ExternalContexts? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    runValidationAsync(
        formSchema,
        validation,
        value,
        AbsolutePath(path),
        dependencyValues,
        externalContexts,
        coroutineScope
    )

/**
 * Runs a given [validation] within a form with a schema of type [AnySchema] and returns a future
 * that completes with a list of its issues.
 *
 * It is necessary to provide all information required to run the validation: the [path] of the
 * value being validated, the [value] itself, the [values of the dependencies][dependencyValues] (by
 * dependency name), and the values of the [external contexts][externalContexts] that the validation
 * depends on.
 *
 * If the validation depends on values within the value being validated, then these may be omitted
 * from the provided [dependencyValues].
 *
 * Otherwise, missing dependencies or external contexts will be passed to the validation as `null`,
 * in which case the validation may or may not throw depending on its implementation.
 *
 * Exceptions thrown while running the validation will not be caught by this function. As opposed to
 * how a [form validator][FormValidator] or [form manager][FormManager] operate, exceptions are
 * **not** wrapped in a [ValidationExceptionError] nor emitted as part of the issues flow.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas,
 * with [InvalidDependencyPathException] if [validation] contains an invalid dependency, or with
 * [IllegalArgumentException] when providing unknown names for [dependencyValues] or
 * [externalContexts] or when providing mismatching dependency values for dependencies within
 * [value].
 */
@JvmOverloads
@JvmName("runValidation")
public fun  runValidationAsync(
    validation: Validation,
    value: T,
    path: Path = AbsolutePath.ROOT,
    dependencyValues: Map? = null,
    externalContexts: ExternalContexts? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    coroutineScope.future {
        runValidation(validation, value, path, dependencyValues, externalContexts).toList()
    }

/**
 * Runs a given [validation] within a form with a schema of type [AnySchema] and returns a future
 * that completes with a list of its issues.
 *
 * It is necessary to provide all information required to run the validation: the [path] of the
 * value being validated, the [value] itself, the [values of the dependencies][dependencyValues] (by
 * dependency name), and the values of the [external contexts][externalContexts] that the validation
 * depends on.
 *
 * If the validation depends on values within the value being validated, then these may be omitted
 * from the provided [dependencyValues].
 *
 * Otherwise, missing dependencies or external contexts will be passed to the validation as `null`,
 * in which case the validation may or may not throw depending on its implementation.
 *
 * Exceptions thrown while running the validation will not be caught by this function. As opposed to
 * how a [form validator][FormValidator] or [form manager][FormManager] operate, exceptions are
 * **not** wrapped in a [ValidationExceptionError] nor emitted as part of the issues flow.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas,
 * with [InvalidDependencyPathException] if [validation] contains an invalid dependency, or with
 * [IllegalArgumentException] when providing unknown names for [dependencyValues] or
 * [externalContexts] or when providing mismatching dependency values for dependencies within
 * [value].
 */
@JvmOverloads
@JvmName("runValidation")
public fun  runValidationAsync(
    validation: Validation,
    value: T,
    path: String,
    dependencyValues: Map? = null,
    externalContexts: ExternalContexts? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    runValidationAsync(
        validation,
        value,
        AbsolutePath(path),
        dependencyValues,
        externalContexts,
        coroutineScope
    )




© 2015 - 2024 Weber Informatics LLC | Privacy Policy