You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/uischema/rules.mdx
+92-9Lines changed: 92 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -10,30 +10,113 @@ import ApiLink from '../../../src/components/common/ApiLink';
10
10
11
11
Rules allow for dynamic aspects for a form, e.g. by hiding or disabling UI schema elements.
12
12
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:
15
14
16
15
```js
17
16
"rule": {
18
-
"effect":"HIDE",
17
+
"effect":"HIDE"|"SHOW"|"ENABLE"|"DISABLE",
19
18
"condition": {
20
-
"scope":"#/properties/name",
21
-
"schema":{ enum: ["foo"] }
19
+
"scope":"<UI Schema scope>",
20
+
"schema":JSON Schema
22
21
}
23
22
}
24
23
```
25
24
26
25
A rule basically works by first evaluating the `condition` property and in case it evaluates to true, executing the associated `effect`.
27
26
28
-
## RuleEffect
29
27
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:
31
32
32
33
-`HIDE`: hide the UI schema element
33
34
-`SHOW`: show the UI schema element
34
35
-`DISABLE`: disable the UI schema element
35
36
-`ENABLE`: enable the UI schema element
36
37
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
+
```
38
120
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