FHIR Chat · Add constraint via Ruleset · shorthand

Stream: shorthand

Topic: Add constraint via Ruleset


view this post on Zulip Simone Heckmann (Jul 02 2021 at 16:43):

We have a situation where we need to add Constraints that do length checks to a lot of fields. The length varies, so we'd need to create a lot of different invariants for a lot of different length.
So the idea was to just use a RuleSet instead an let it create inline invariants with dynamic length like this:

RuleSet: assertLength (length)
* ^constraint.human = "Value must be shorter than {length}"
* ^constraint.expression = "length({length})"
* ^constraint.severity = #error

Profile: Test
Parent: Patient
Id: Test
Title: "Test"
Description: ""
* name.family
  * insert assertLength(10)

However, this gives an error:
A rule that does not use a path cannot be indented to indicate context. The rule will be processed as if it is not indented.
Adding the indentation to the RuleSet instead returns
Errors parsing insert rule with parameterized RuleSet assertLength
- The first rule of a definition cannot be indented. The rule will be processed as if it is not indented.

I'm at a loss. How can I create a RuleSet that adds lines relative to the current path of where the RuleSet is inserted...?

view this post on Zulip Nick Freiter (Jul 02 2021 at 17:26):

Support for creating a RuleSet that adds rules relative to the path of an insert rule was only added in the most recent release of SUSHI, SUSHI 2.0.0-beta.2. In that version, you can indent an insert rule to give it context.

However, there is actually a bug we've just learned of today in this thread which prevents application of an insert rules path when the rules that path is being applied to are ^ rules without a path (as is the case in your example). So unfortunately SUSHI 2.0.0-beta.2 won't quite work in that case just yet. We have a fix for it in progress here, but it likely won't be until next week that we can release a new version of SUSHI that fixes this bug.

There is a workaround you can use that @Martin Höcker displays in this thread, and that is to treat the path as another input to the RuleSet. So here is how that looks for the example you gave:

RuleSet: assertLength (path, length)
* {path} ^constraint.human = "Value must be shorter than {length}"
* {path} ^constraint.expression = "length({length})"
* {path} ^constraint.severity = #error

Profile: Test
Parent: Patient
Id: Test
Title: "Test"
Description: ""
* insert assertLength(name.family, 10)

Last updated: Apr 12 2022 at 19:14 UTC