Sequence of Validation and Computation in Backend

Hi there,

in the JavaDoc of IDocumentRtService, it is stated, that a document shall be validated before computations are executed. But what, if one or more computations draw a document invalid. Assume something like a computed age field and a validation rule, which verifies, that the age is, for instance, greater than 18. Age is computed by a birthday.

So validation won’t fail, since the age field is not computed yet. After the computation, the age field can contain values < 18, which would result in an validation error…

Is this assumed as bad modeling, since either the computation should verify itself, that age values < 18 are invalid? But this mixes validation into computation, which feels also not clean. Or the validation should make sure, that age is filled (which would fail always, since the value is computed after validation).

Is there a reason why validation is recommanded before computation?

Hi @felix-sharp-beam,

I’ve asked the Kernel team to look into this into more detail but here’s a bit of information from a Modeler’s perspective:

Validation before Computation

Well… not exactly. The compute method for IDocumentRtService in the JavaDoc actually says:
" Before performing any computations, all non-computed fields in the given IDocument that are operands of any computation will be formally validated:"

There is a difference between formal validation and the validateFull method. As the documentation goes on to say, formal validation checks for invalid values and empty required fields. These checks do not include all the Validation Rules that you have modeled.

A different way of thinking about formal validation is that it is checking the settings that you made on the Field Editor in the SME. Formal validation of “TotalPrice” would check:

  • That the Field Value only uses numbers and a deciaml point
  • The there are two digits after the decimal point

There’s a brief description of Formal Errors in the Kernel Language documentation.

In my opinion, validation is recommended before computation so that all Validation Rules in your Document Model are considered. For example a Validation Rule with the following Error Condition:

;; Only allow orders for next day delivery that are over 100€
DifferenceInDays(OrderingDate,DeliveryDate) == 1 and [TotalPrice] < 100

Would not be triggered in the formal validation. This would, however, be evaluated using the validateFull method. This information is essential if you are going to use the TotalPrice Value in a Computation Rule.

Regarding,

This validation would not fail when calling the compute Method as only non-computed Fields are formally validated.

Validation After Computation

Well, maybe… again it depends on the type of error that you are prevoking. To quote the compute method again:
" If a computation results in a value that is not valid (e.g. a number value outside the valid range), the erroneous IComputedFieldInstance will be included in IDocumentComputationResult.getComputedFieldInstancesWithErrors() with a corresponding IComputedFieldInstance.getErrorMessage()."

As you can see a further Formal Validation takes place. Based on your example, if the Field, “Age”, was modeled with a Minimumn Value = 18, then, to use your words, the computation verifies itself. This field will be included in IDocumentComputationResult.getComputedFieldInstancesWithErrors().

If, however, you have modeled a Validation Rule with the following Error Condition:

;; Main Account Holder must be over 18
[AccountHolderType] == "Main" And [Age] < 18

This would not be triggered by the compute Method.

In my opinion, this means that you also need to validate after computations too. For example, in the Frontend the following happens:

  1. Change Field Value triggers Formal Validation of this Field and validatePart with only this Field in the set of entity instances
  2. Re-computation of all computed Fields

As the entire Document has not been validated, we then model a full validation that is triggered before we send this data to the server. This matches up with the recommendations and documentation above.

@malcolm-silver-ice: Thank you very much for your detailed answer. It is quite much what the Kernel team would have answered.
@felix-sharp-beam: In summary, the general suggestion from the Kernel team is to validate before computing and also to validate after. Validating before computing ensures that you are computing on a valid state (from the business point of view) and validate after computing ensures that the resulting data is still valid, see @malcolm-silver-ice’s answer.