ComputedSchema
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.
Properties
Computed value represented by this schema.
Initial value for a value of this schema.
Inner schema of the value being computed.
List of validations used to validate this schema.
Validation issue code to emit when the value doesn't match the result of the computation.
Severity of the validation issue to emit when the value doesn't match the result of the computation.
Functions
Whether a value of this schema can be assigned to a variable with the provided type.
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.
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.
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.