Skip to content

Commit 3c24ad8

Browse files
Improve rule documentation
Adds a generic rule description and multiple rule examples to the rule documentation.
1 parent 4126e2a commit 3c24ad8

File tree

1 file changed

+92
-9
lines changed

1 file changed

+92
-9
lines changed

content/docs/uischema/rules.mdx

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,113 @@ import ApiLink from '../../../src/components/common/ApiLink';
1010

1111
Rules allow for dynamic aspects for a form, e.g. by hiding or disabling UI schema elements.
1212

13-
A rule may be attached to any UI schema element and can be defined with the `rule` property.
14-
We'll first look at an example definition of a rule and then explain it in detail.
13+
A rule may be attached to any UI schema element and can be defined with the `rule` property which looks like:
1514

1615
```js
1716
"rule": {
18-
"effect": "HIDE",
17+
"effect": "HIDE" | "SHOW" | "ENABLE" | "DISABLE",
1918
"condition": {
20-
"scope": "#/properties/name",
21-
"schema": { enum: ["foo"] }
19+
"scope": "<UI Schema scope>",
20+
"schema": JSON Schema
2221
}
2322
}
2423
```
2524

2625
A rule basically works by first evaluating the `condition` property and in case it evaluates to true, executing the associated `effect`.
2726

28-
## RuleEffect
2927

30-
The `effect` property determines what should happen to the attached UI schema element once the `condition` is met. In the example above, if the `name` property has the value of `foo`, we'll hide the UI schema element the rule is attached to. Current effects include:
28+
## Rule Effect
29+
30+
The `effect` property determines what should happen to the attached UI schema element once the `condition` is met.
31+
Current effects include:
3132

3233
- `HIDE`: hide the UI schema element
3334
- `SHOW`: show the UI schema element
3435
- `DISABLE`: disable the UI schema element
3536
- `ENABLE`: enable the UI schema element
3637

37-
The `condition` property describes what value should be observed and validates that value with against the JSON schema that is specified with `schema`. If validation succeeds the condition is fulfilled and the associated `effect` will be triggered.
38+
## Rule Condition
39+
40+
The rule `condition` object contains a `scope` and a `schema` property.
41+
The `schema` property is a standard JSON schema object.
42+
This means, everything that can be specified using JSON schema can be used in the rule condition.
43+
The `schema` is validated against the data specified in the `scope` property.
44+
If the `scope` data matches the `schema` the rule evaluates to true and the rule effect is applied.
45+
46+
Note, `SchemaBasedCondition`s have been introduced with version 2.0.6 and have become the new default.
47+
The previous format via `type` and `expectedValue` properties is still supported for the time being.
48+
49+
## Examples
50+
51+
Below are some common rule examples.
52+
53+
To match a scope variable to a specific value, "const" can be used:
54+
55+
```js
56+
"rule": {
57+
"effect": "HIDE",
58+
"condition": {
59+
"scope": "#/properties/counter",
60+
"schema": { const: 10 }
61+
}
62+
}
63+
```
64+
Here, the control is hidden when the `counter` property is equal to `10`.
65+
66+
Similar, to match multiple values, `enum` can be used:
67+
68+
```js
69+
"rule": {
70+
"effect": "HIDE",
71+
"condition": {
72+
"scope": "#/properties/name",
73+
"schema": { enum: ["foo", "bar"] }
74+
}
75+
}
76+
```
77+
The rule evaluates to true if the `scope` property `name` is either "foo" or "bar".
78+
79+
A rule can be negated using "not":
80+
81+
```js
82+
"rule": {
83+
"effect": "SHOW",
84+
"condition": {
85+
"scope": "#/properties/counter",
86+
"schema": { not: { const: 10 } }
87+
}
88+
}
89+
```
90+
91+
The following rule evaluates to true if the `counter` property is `1 <= counter < 10`:
92+
93+
```js
94+
"rule": {
95+
"effect": "SHOW",
96+
"condition": {
97+
"scope": "#/properties/counter",
98+
"schema": { minimum: 1, exclusiveMaximum: 10 }
99+
}
100+
}
101+
```
102+
103+
104+
A rule can even operate on the full form data object and over multiple properties:
105+
106+
```js
107+
"rule": {
108+
"effect": "SHOW",
109+
"condition": {
110+
"scope": "#",
111+
"schema": {
112+
"properties": {
113+
"stringArray": { "contains": { "const": "Foo" } }
114+
},
115+
"required": ["stringArray", "otherProperty"]
116+
}
117+
}
118+
}
119+
```
38120

39-
`SchemaBasedCondition`s have been introduced with version 2.0.6 and have become the new default. The previous format via `type` and `expectedValue` properties is still supported for the time being.
121+
In this example, the condition is true if the properties "stringArray" and "otherProperty" are set in the form data and the "stringArray" property contains an element "Foo".
122+
Note, that the schema rule in this example looks more like a normal JSON schema as it is commonly used.

0 commit comments

Comments
 (0)