Reference: validation helpers¶
Two tiers. The rule builder (rule(…) { cases }) states whole rules
mirroring the data model — one rule per ValidationRule, its cases
stating what they check — and is the recommended way to write rule sets;
the case factories (checkLowerBound & co.) produce single
ValidationRuleCases with autogenerated messages, useful for quick
prototyping inside a hand-built rule.
Rule builder¶
Inside the DSL's rule-set blocks (validationRuleSet,
nonInteractiveValidationRuleSet, dataSchemaValidationRuleSet,
metadataSchemaValidationRuleSet, and the per-input validationRuleSet of
a workflow type's dataInterface) the
builder scope is already active — declare rules directly. Machine
identifiers are derived (and overridable): the rule code from the
anchored value's reference path (/edgeLength → edge_length), bound-case
codes from the case's shape and severity (too_small_error). User-facing
text — name, every message, their multiLanguage* variants — is always
explicit, like schema labels. Every declaration produces plain
ValidationRule data: the serialized rule set is indistinguishable from a
hand-assembled one.
validationRuleSet {
rule(configValueModel.edgeLength, name = "Edge length check") {
error(above = 1.millimeter, message = "Too small value specified for edge length")
error(below = 500.millimeter, message = "Too big value specified for edge length")
warning(above = 5.millimeter, message = "Very small value specified for edge length")
warning(below = 50.millimeter, message = "Very big value specified for edge length")
}
count(configValueModel.presets, name = "Presets check") {
error(atLeast = 1, code = "empty", message = "Define at least one preset")
}
}
Outside the DSL — utility sources, tests — enter
the same scope with rules { … }, which returns the built
ValidationRuleSet (a body that is itself a ValidationRuleSet expression
is absorbed unchanged, so both styles coexist).
rule(value, name, …) { cases }¶
One validation rule anchored on a value. Each case states its requirement with exactly one of:
above(strict lower bound),atLeast(inclusive lower),below(strict upper),atMost(inclusive upper) — available when the anchor is anIntegerValue,RealValue, orDimensionalScalarValue, typed accordingly (Int/Double/DimensionalScalar). Case codes derive astoo_small_<severity>/too_big_<severity>.condition— a free-formBooleanValueexpression that must hold, for everything bounds can't say (list predicates, multi-field conditions, enum checks). Available for any anchor (lists, compounds, …); an explicit casecodeis required since there is nothing mechanical to derive it from.
Bounds take either a constant or another value of the anchored kind, so cross-field requirements read the same way:
rule(configValueModel.maxSize, name = "Size range check") {
error(atLeast = configValueModel.minSize, message = "Maximum size must not be smaller than minimum size")
}
Severity picks the case function: error(…) / warning(…). Optional per
case: code, multiLanguageMessage (expression-valued messages like
"Too small: ".asValueModel() + configValueModel.edgeLength work), and
details. Optional per rule: code, description, multiLanguageName,
multiLanguageDescription.
Conditional rules: appliesWhen¶
Every rule-level builder takes an optional appliesWhen gate — the rule is
enforced only in configurations where the gate holds. This is the home for
"required/valid only when feature X is on" requirements that cross-cutting
toggles create:
count(configValueModel.emissivitySurfaces, name = "Emissivity surfaces",
appliesWhen = configValueModel.enableRadiation) {
error(atLeast = 1, message = "Assign at least one surface when radiation is enabled")
}
The gate composes into each case condition as a plain boolean implication —
the serialized rule set carries no dedicated conditionality construct, and
an inactive configuration passes regardless of the gated requirement.
(Show/hide of the gated fields themselves is UI configuration —
visibleWhen — not validation.)
count(list, name, …) { cases }¶
Cardinality of a list, with the same case vocabulary applied to the list's
size: error(atMost = 50, …) is a length cap, and value-typed bounds allow
cross-field limits (atMost = configValueModel.maxPresets).
For the plain "must not be empty" check prefer the schema's nonEmpty
flag instead of a rule: compoundList("presets") { nonEmpty = true }
exposes the requirement to user interfaces and is enforced by structural
validation (empty_value), like optional is — also for entity
assignments. A count rule is for finer cardinality (at least two, at
most fifty, cross-field limits), or when the message must be the author's.
eachElement(list, elementModelFactory, name, …) { cases }¶
A requirement every element of a list must meet — the per-row checks
applied to tables and preset lists. Each case gives a per-element condition
(the lambda receives the element model) and an explicit code:
eachElement(configValueModel.presets, PresetsValueModel::fromReference, name = "Preset scales") {
error(code = "non_positive_scale", message = "Every preset scale must be positive") {
it.scale greaterThan 0.0
}
}
elementModelFactory creates the element model from a reference — for
compound elements pass the generated …ValueModel::fromReference (the same
idiom options bindings use), for plain elements the matching reference
constructor (e.g. { RealReference(it) }).
The rule is evaluated per element: every failing element produces its
own result, located at the element's indexed path (/presets/2) and, for
compound elements with a stable id, its element reference — so a UI can
navigate to the offending row. Inside the block, element is the element
under evaluation, usable in messages and details:
eachElement(configValueModel.presets, PresetsValueModel::fromReference, name = "Preset scales") {
error(
code = "non_positive_scale",
message = "Scale ".asValueModel() + element.scale.toStringValue() + " must be positive",
details = mapOf("scale" to element.scale),
) { it.scale greaterThan 0.0 }
}
A detail resolves to its bare JSON. Wrap it in typed(…) to report it tagged
with its value type — details = mapOf("scale" to typed(element.scale))
reports { "type": "dimensional_scalar", "value": { … } } — when a consumer
needs to know how to read it.
unique(list, elementModelFactory, by, name, message, …)¶
No two list elements may share a key — the duplicate-name check for probe
points, materials, result fields. by selects the string key per element
(e.g. by = { it.name }). Case code duplicate.
unique, monotonic and disjointAssignments are aggregate rules: they are
reported once, at the list, and name the offending elements in
details.offendingElementIndices — a list of element indices, typed as
{ "type": "list", "value": [1, 2] }.
monotonic(list, elementModelFactory, by, name, message, …)¶
The selected column must be ordered. by selects the compared real value
(for dimensional columns select it.temperature.toRealValue(); comparison
happens in SI units, which preserves ordering). Direction and strictness
via increasing / strictly; the derived case code names them
(not_strictly_increasing, …).
A table does not need this rule: declaring its axes with
compoundTable already requires the rows to be
ordered by those columns and to carry each combination once, checked
structurally (unordered_table_row, repeated_table_row). Reach for
monotonic where a list must be ordered by something that does not index
it — a decreasing tolerance sequence, say.
sumsTo(list, elementModelFactory, by, target, name, message, …)¶
The selected column must sum to target within tolerance (default
1e-9) — sum-to-one constraints on factor and fraction lists. Case code
sum_mismatch.
exactlyOneOf(first, second, code, name, message, …)¶
Exactly one of two data inputs must be provided. The message lambda
receives the already-built provided-input count:
exactlyOneOf(
inputMeshAndFields, inputRampsMeshAndFields,
code = "mutually_exclusive_inputs",
name = "Mutually exclusive inputs",
message = { provided -> "Exactly 1 input must be provided. You provided ".asValueModel() + provided.toStringValue() + ".".asValueModel() },
)
disjointAssignments(assignments, anchor, code, name, message, …)¶
No entity may appear in more than one of the given assignment lists — the
pairwise-disjoint check for CAD-region-style assignments. anchor is the
configuration value the reported result attaches to:
disjointAssignments(
configValueModel.materials.fluids.map { it.regions } + configValueModel.materials.solids.map { it.regions },
anchor = configValueModel.materials,
code = "unique_regions_across_materials",
name = "Check assignments of CAD regions to materials",
message = "Each region must be uniquely assigned to a single material",
)
rule(validationRule)¶
Adds a hand-assembled ValidationRule — the fully explicit form for the
rare shape even condition cases don't cover, mixing freely with the
declared ones.
Case factories¶
Pre-built ValidationRuleCase factories for the most common numeric
checks. Generated by the framework's
BoundCheckValidationFunctionsGenerator; available for
IntegerValue, RealValue, and DimensionalScalarValue. Their codes
and messages are autogenerated — fine for prototyping; shipping
components should prefer the rule builders above, whose messages are
explicit.
Imports:
import com.simscale.workflows.framework.values.validation.checkLowerBound
import com.simscale.workflows.framework.values.validation.checkUpperBound
import com.simscale.workflows.framework.values.validation.checkRange
import com.simscale.workflows.framework.values.validation.inOptions
(The unified-DSL build pipeline injects these automatically inside
validationRuleSet { … } blocks.)
checkLowerBound¶
fun checkLowerBound(
value: IntegerValue | RealValue | DimensionalScalarValue,
lowerBound: Int | Double | DimensionalScalar<*>,
includeBound: Boolean,
severityLevel: SeverityLevel = SeverityLevel.ERROR,
): ValidationRuleCase
Returns a ValidationRuleCase whose condition is value > lowerBound
(or value >= lowerBound if includeBound = true). Default
severity is ERROR.
The case's code is autogenerated as <value-type>_number_too_low
(e.g. real_number_too_low); the message is autogenerated to
include the bound value.
Example:
checkUpperBound¶
fun checkUpperBound(
value: IntegerValue | RealValue | DimensionalScalarValue,
upperBound: Int | Double | DimensionalScalar<*>,
includeBound: Boolean,
severityLevel: SeverityLevel = SeverityLevel.ERROR,
): ValidationRuleCase
Returns a ValidationRuleCase whose condition is value < upperBound
(or value <= upperBound if includeBound = true). Default severity
is ERROR.
code autogenerated as <value-type>_number_too_high; message
autogenerated.
Example:
checkRange¶
fun checkRange(
lowerBound: IntegerValue | RealValue | DimensionalScalarValue,
upperBound: IntegerValue | RealValue | DimensionalScalarValue,
includeBound: Boolean,
): ValidationRuleCase
For when both bounds are themselves configuration values and you
want to assert lowerBound <= upperBound (or < if
includeBound = false). Different from checkLowerBound / checkUpperBound
in that it doesn't validate a single value against a constant — it
validates two configuration values against each other.
code autogenerated as
<value-type>_number_lower_bound_is_higher_than_upper_bound; severity
is always ERROR.
Example:
checkRange(
lowerBound = configValueModel.minSize,
upperBound = configValueModel.maxSize,
includeBound = false,
)
Options pickers need no validator¶
There is no inOptions helper. Fields driven by an
options binding
store a reference to the picked source-list element rather than a
copy of its value, so "is the selection still one of the options?"
reduces to referential integrity — does the referenced element still
exist? Configuration validation checks that automatically; a reference
to a deleted element is reported as a validation error on the field,
like a missing or unknown value. You don't pair the dropdown with a
validation rule, and there's nothing to keep "in lockstep".
Beyond bounds: condition cases¶
Multi-field conditions ("if A then B must hold"), enum and boolean checks,
and list predicates (any { } / all { } / none { }) have no dedicated
bound parameter — state them as condition cases over the value-model
operators. For the operator surface, see
reference: value-model catalogue.
Cross-references¶
- Concepts: validation rules.
- How-to: add a validation rule.
- Reference: value-model catalogue.
- Reference: CAD validation helpers — the geometry-aware blocks, evaluable in non-interactive rule sets only.