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

jvmMain.AsyncFormValidator.kt Maven / Gradle / Ivy

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

package io.kform

import java.util.concurrent.CompletableFuture
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.future.future

/**
 * This class is a simple [FormValidator] wrapper for usage from Java, providing methods returning
 * completable futures instead of using suspend functions or Kotlin flows.
 *
 * Use this class to validate form values. Form values are of type [T] and represent the content of
 * a form with the provided form schema.
 *
 * External validations may be provided to further validate the form against validations not present
 * in the schema.
 *
 * Once instantiated with a given form schema, the validator can be used to validate values
 * according to all [validations][Validation] of said schema, as well as the validator's external
 * validations.
 */
public class AsyncFormValidator
@JvmOverloads
constructor(
    formSchema: Schema,
    externalValidations: ExternalValidations = emptyMap(),
    coroutineContext: CoroutineContext = Dispatchers.Default,
) {
    private val validator = FormValidator(formSchema, externalValidations)
    private val scope: CoroutineScope =
        CoroutineScope(CoroutineName("Async form validator") + coroutineContext)

    /**
     * Validates the parts of the form value [formValue] matching [path] against the validator's
     * schema. Returns a future that completes with a list of found [validation issues]
     * [LocatedValidationIssue].
     *
     * A map of [externalContexts] may be provided for validations that depend on them.
     *
     * The future will complete exceptionally with [InvalidPathException] if [path] matches no
     * schemas.
     */
    @JvmOverloads
    public fun validate(
        formValue: T,
        path: Path,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture> =
        scope.future { validator.validate(formValue, path, externalContexts).toList() }

    /**
     * Validates the parts of the form value [formValue] matching [path] against the validator's
     * schema. Returns a future that completes with a list of found [validation issues]
     * [LocatedValidationIssue].
     *
     * A map of [externalContexts] may be provided for validations that depend on them.
     *
     * The future will complete exceptionally with [InvalidPathException] if [path] matches no
     * schemas.
     */
    @JvmOverloads
    public fun validate(
        formValue: T,
        path: String,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture> =
        validate(formValue, AbsolutePath(path), externalContexts)

    /**
     * Validates all parts of the form value [formValue] against the validator's schema. Returns a
     * future that completes with a list of found [validation issues] [LocatedValidationIssue].
     *
     * A map of [externalContexts] should be provided for validations that depend on them.
     */
    @JvmOverloads
    public fun validate(
        formValue: T,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture> =
        validate(formValue, AbsolutePath.MATCH_ALL, externalContexts)

    /**
     * Returns a future that completes with whether the parts of the form value [formValue] matching
     * [path] are valid according to the validator's schema.
     *
     * These parts are said to be valid if they contain no validation errors.
     *
     * A map of [externalContexts] may be provided for validations that depend on them.
     *
     * The future will complete exceptionally with [InvalidPathException] if [path] matches no
     * schemas.
     */
    @JvmOverloads
    public fun isValid(
        formValue: T,
        path: Path,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture =
        scope.future { validator.isValid(formValue, path, externalContexts) }

    /**
     * Returns a future that completes with whether the parts of the form value [formValue] matching
     * [path] are valid according to the validator's schema.
     *
     * These parts are said to be valid if they contain no validation errors.
     *
     * A map of [externalContexts] may be provided for validations that depend on them.
     *
     * The future will complete exceptionally with [InvalidPathException] if [path] matches no
     * schemas.
     */
    @JvmOverloads
    public fun isValid(
        formValue: T,
        path: String,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture = isValid(formValue, AbsolutePath(path), externalContexts)

    /**
     * Returns a future that completes with whether all parts of the form value [formValue] are
     * valid according to the validator's schema.
     *
     * These parts are said to be valid if they contain no validation errors.
     *
     * A map of [externalContexts] should be provided for validations that depend on them.
     */
    @JvmOverloads
    public fun isValid(
        formValue: T,
        externalContexts: ExternalContexts? = null
    ): CompletableFuture = isValid(formValue, AbsolutePath.MATCH_ALL, externalContexts)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy