ComputedSchema

interface ComputedSchema<T> : Schema<T> (source)

A schema representing a computed value of type T: a form value that should equal the result of evaluating a given computation over other values of the form. A common example of a computed value would be a field whose value should equal the sum of other form values.

Example usage:

data class ItemPurchase(
val price: Double?,
val amount: Int?,
val tax: Float?,
val total: Double
)

object Total : ComputedValue<Double> {
private val ComputedValueContext.price: Double? by dependency("../price")
private val ComputedValueContext.amount: Int? by dependency("../amount")
private val ComputedValueContext.tax: Float? by dependency("../tax")

override suspend fun ComputedValueContext.compute(): Double {
val totalPrice = (price ?: 0.0) * (amount ?: 0)
return totalPrice + totalPrice * (tax ?: 0f)
}
}

val itemPurchaseSchema = ClassSchema {
ItemPurchase::price { NullableSchema { DoubleSchema() } }
ItemPurchase::amount { NullableSchema { IntSchema() } }
ItemPurchase::tax { NullableSchema { FloatSchema() } }
ItemPurchase::total { ComputedSchema(Total) { DoubleSchema() } }
}

This schema will defer its implementation to the provided inner innerSchema.

This schema automatically includes a validation which checks that the form value equals the result of evaluating the provided computed value. A valueMismatchCode and valueMismatchSeverity may be provided to specify the code and severity of the issue emitted when the form value doesn't match the result of the computation. The code defaults to MatchesComputedValue.DEFAULT_CODE and the severity to ValidationIssueSeverity.Error.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Computed value represented by this schema.

Link copied to clipboard
abstract val initialValue: T

Initial value for a value of this schema.

Link copied to clipboard
abstract val innerSchema: Schema<T>

Inner schema of the value being computed.

Link copied to clipboard
abstract val typeInfo: TypeInfo

Information about the type of value represented by this schema.

Link copied to clipboard
abstract val validations: List<Validation<T>>

List of validations used to validate this schema.

Link copied to clipboard

Validation issue code to emit when the value doesn't match the result of the computation.

Link copied to clipboard

Severity of the validation issue to emit when the value doesn't match the result of the computation.

Functions

Link copied to clipboard
open fun assignableTo(type: KType): Boolean

Whether a value of this schema can be assigned to a variable with the provided type.

Link copied to clipboard
abstract suspend fun change(path: AbsolutePath, value: T, intoValue: Any?, eventsBus: SchemaEventsBus, setValue: suspend (value: T) -> Unit)

Changes value, a value of this schema stored by the form manager into the given value intoValue, sending events about the change through eventsBus. The provided path represents the path of the value being changed within the form manager. The value must be set via setValue, even if it hasn't changed; for parent schemas, it should typically be the same instance of value, but mutated to resemble intoValue.

Link copied to clipboard
abstract suspend fun clone(value: T): T

Returns a clone (deep copy) of value.

Link copied to clipboard
fun Schema<*>.comparePaths(path1: String, path2: String): Int
fun Schema<*>.comparePaths(path1: Path, path2: Path): Int

Compares two paths of a schema according to the order in which children are defined in said schema. Returns a value > 0 if path1 is greater than path2, < 0 if path2 if greater than path1, or 0 otherwise.

Link copied to clipboard
abstract suspend fun destroy(path: AbsolutePath, value: T, eventsBus: SchemaEventsBus, removeValue: suspend (value: T) -> Unit)

Destroys and removes value, a value of this schema stored by the form manager, sending events about the destruction through eventsBus. The provided path represents the path of the value being destroyed within the form manager. removeValue should always be called to remove the value.

Link copied to clipboard
abstract suspend fun init(path: AbsolutePath, fromValue: Any?, eventsBus: SchemaEventsBus, setValue: suspend (value: T) -> Unit)

Initialises and sets a value of this schema from a given value fromValue to be stored by the form manager, sending events about the initialisation of the value through eventsBus. The provided path represents the path of the value being initialised within the form manager. The value must be set via setValue, which should always be called.