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

jvmMain.AsyncFormUtils.kt Maven / Gradle / Ivy

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

package io.kform

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 launching futures in the context of a form validator. */
private val defaultScope = CoroutineScope(Dispatchers.Default + CoroutineName("Async form util"))

/**
 * Returns a future that completes with a list of information about the parts of the form value
 * [formValue] (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas.
 */
@JvmOverloads
@JvmName("valueInfo")
public fun  valueInfoAsync(
    formSchema: Schema,
    formValue: T,
    path: Path = AbsolutePath.MATCH_ALL,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture>> =
    coroutineScope.future { valueInfo(formSchema, formValue, path).toList() }

/**
 * Returns a future that completes with a list of information about the parts of the form value
 * [formValue] (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas.
 */
@JvmOverloads
@JvmName("valueInfo")
public fun  valueInfoAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture>> =
    valueInfoAsync(formSchema, formValue, AbsolutePath(path), coroutineScope)

/**
 * Returns a future that completes with whether there exists a part of the form value [formValue]
 * (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas.
 */
@JvmOverloads
@JvmName("has")
public fun  hasAsync(
    formSchema: Schema,
    formValue: T,
    path: Path,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = coroutineScope.future { has(formSchema, formValue, path) }

/**
 * Returns a future that completes with whether there exists a part of the form value [formValue]
 * (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] matches no schemas.
 */
@JvmOverloads
@JvmName("has")
public fun  hasAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = hasAsync(formSchema, formValue, path, coroutineScope)

/**
 * Returns a future that completes with the single part of the form value [formValue] (with schema
 * [formSchema]) matching [path].
 *
 * To get information about multiple parts of a form value at once, use [valueInfoAsync] instead.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] contains wildcards
 * or matches no schemas and with [NoSuchElementException] if no part of [formValue] matches [path].
 */
@JvmOverloads
@JvmName("get")
public fun  getAsync(
    formSchema: Schema,
    formValue: T,
    path: Path,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = coroutineScope.future { get(formSchema, formValue, path) }

/**
 * Returns a future that completes with the single part of the form value [formValue] (with schema
 * [formSchema]) matching [path].
 *
 * To get information about multiple parts of a form value at once, use [valueInfoAsync] instead.
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] contains wildcards
 * or matches no schemas and with [NoSuchElementException] if no part of [formValue] matches [path].
 */
@JvmOverloads
@JvmName("get")
public fun  getAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = getAsync(formSchema, formValue, AbsolutePath(path), coroutineScope)

/**
 * Returns a future that completes with a clone (deep copy) of the single part of the form value
 * [formValue] (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] contains wildcards
 * or matches no schemas and with [NoSuchElementException] if no part of [formValue] matches [path].
 */
@JvmOverloads
@JvmName("getClone")
public fun  getCloneAsync(
    formSchema: Schema,
    formValue: T,
    path: Path = AbsolutePath.ROOT,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = coroutineScope.future { getClone(formSchema, formValue, path) }

/**
 * Returns a future that completes with a clone (deep copy) of the single part of the form value
 * [formValue] (with schema [formSchema]) matching [path].
 *
 * The future will complete exceptionally with [InvalidPathException] if [path] contains wildcards
 * or matches no schemas and with [NoSuchElementException] if no part of [formValue] matches [path].
 */
@JvmOverloads
@JvmName("getClone")
public fun  getCloneAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture = getCloneAsync(formSchema, formValue, path, coroutineScope)

/**
 * Validates the parts of the form value [formValue] matching [path] against [formSchema]. 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
@JvmName("validate")
public fun  validateAsync(
    formSchema: Schema,
    formValue: T,
    path: Path = AbsolutePath.MATCH_ALL,
    externalContexts: Map? = null,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    coroutineScope.future {
        validate(formSchema, formValue, path, externalContexts, externalValidations).toList()
    }

/**
 * Validates the parts of the form value [formValue] matching [path] against [formSchema]. 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
@JvmName("validate")
public fun  validateAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    externalContexts: Map? = null,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    validateAsync(
        formSchema,
        formValue,
        AbsolutePath(path),
        externalContexts,
        externalValidations,
        coroutineScope
    )

/**
 * Validates all parts of the form value [formValue] against [formSchema]. 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
@JvmName("validate")
public fun  validateAsync(
    formSchema: Schema,
    formValue: T,
    externalContexts: Map?,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture> =
    validateAsync(
        formSchema,
        formValue,
        AbsolutePath.MATCH_ALL,
        externalContexts,
        externalValidations,
        coroutineScope
    )

/**
 * Returns a future that completes with whether the parts of the form value [formValue] (with schema
 * [formSchema]) 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
@JvmName("isValid")
public fun  isValidAsync(
    formSchema: Schema,
    formValue: T,
    path: Path = AbsolutePath.MATCH_ALL,
    externalContexts: Map? = null,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture =
    coroutineScope.future {
        isValid(formSchema, formValue, path, externalContexts, externalValidations)
    }

/**
 * Returns a future that completes with whether the parts of the form value [formValue] (with schema
 * [formSchema]) 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
@JvmName("isValid")
public fun  isValidAsync(
    formSchema: Schema,
    formValue: T,
    path: String,
    externalContexts: Map? = null,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture =
    isValidAsync(
        formSchema,
        formValue,
        AbsolutePath(path),
        externalContexts,
        externalValidations,
        coroutineScope
    )

/**
 * Returns a future that completes with whether all parts of the form value [formValue] (with schema
 * [formSchema]) 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
@JvmName("isValid")
public fun  isValidAsync(
    formSchema: Schema,
    formValue: T,
    externalContexts: Map?,
    externalValidations: Map>>? = null,
    coroutineScope: CoroutineScope = defaultScope
): CompletableFuture =
    isValidAsync(
        formSchema,
        formValue,
        AbsolutePath.MATCH_ALL,
        externalContexts,
        externalValidations,
        coroutineScope
    )




© 2015 - 2024 Weber Informatics LLC | Privacy Policy