Skip to content

Commit 016f1df

Browse files
authored
feat: add config to ValidateFunctionContext (#2483)
Allows to access the config in custom validators
1 parent 6542785 commit 016f1df

File tree

13 files changed

+235
-92
lines changed

13 files changed

+235
-92
lines changed

packages/angular-material/src/library/layouts/categorization-layout.renderer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
defaultJsonFormsI18nState,
3131
deriveLabelForUISchemaElement,
3232
getAjv,
33+
getConfig,
3334
isVisible,
3435
JsonFormsState,
3536
Labelable,
@@ -87,7 +88,13 @@ export class CategorizationTabLayoutRenderer
8788
this.hidden = !props.visible;
8889
this.visibleCategories = this.uischema.elements.filter(
8990
(category: Category | Categorization) =>
90-
isVisible(category, props.data, undefined, getAjv(state))
91+
isVisible(
92+
category,
93+
props.data,
94+
undefined,
95+
getAjv(state),
96+
getConfig(state)
97+
)
9198
);
9299
this.categoryLabels = this.visibleCategories.map((element) =>
93100
deriveLabelForUISchemaElement(

packages/angular-material/src/library/other/util.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import {
2626
ControlElement,
2727
getAjv,
28+
getConfig,
2829
getData,
2930
isVisible,
3031
JsonFormsState,
@@ -44,7 +45,13 @@ export const mapStateToVisible = (
4445
const visible =
4546
ownProps.visible !== undefined
4647
? ownProps.visible
47-
: isVisible(ownProps.uischema, getData(state), undefined, getAjv(state));
48+
: isVisible(
49+
ownProps.uischema,
50+
getData(state),
51+
undefined,
52+
getAjv(state),
53+
getConfig(state)
54+
);
4855

4956
return {
5057
visible,

packages/core/src/mappers/cell.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ export const mapStateToCellProps = (
112112
): StatePropsOfCell => {
113113
const { id, schema, path, uischema, renderers, cells } = ownProps;
114114
const rootData = getData(state);
115+
const config = getConfig(state);
115116
const visible =
116117
ownProps.visible !== undefined
117118
? ownProps.visible
118-
: isVisible(uischema, rootData, undefined, getAjv(state));
119+
: isVisible(uischema, rootData, undefined, getAjv(state), config);
119120

120121
const rootSchema = getSchema(state);
121-
const config = getConfig(state);
122122

123123
/* When determining the enabled state of cells we take a shortcut: At the
124124
* moment it's only possible to configure enablement and disablement at the

packages/core/src/mappers/renderer.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,11 @@ export const mapStateToControlProps = (
582582
const { uischema } = ownProps;
583583
const rootData = getData(state);
584584
const path = composeWithUi(uischema, ownProps.path);
585+
const config = getConfig(state);
586+
585587
const visible: boolean =
586588
ownProps.visible === undefined || hasShowRule(uischema)
587-
? isVisible(uischema, rootData, ownProps.path, getAjv(state))
589+
? isVisible(uischema, rootData, ownProps.path, getAjv(state), config)
588590
: ownProps.visible;
589591
const controlElement = uischema as ControlElement;
590592
const id = ownProps.id;
@@ -604,7 +606,6 @@ export const mapStateToControlProps = (
604606
const data = Resolve.data(rootData, path);
605607
const labelDesc = createLabelDescriptionFrom(uischema, resolvedSchema);
606608
const label = labelDesc.show ? labelDesc.text : '';
607-
const config = getConfig(state);
608609
const enabled: boolean = isInherentlyEnabled(
609610
state,
610611
ownProps,
@@ -1042,7 +1043,13 @@ export const mapStateToLayoutProps = (
10421043
const { uischema } = ownProps;
10431044
const visible: boolean =
10441045
ownProps.visible === undefined || hasShowRule(uischema)
1045-
? isVisible(ownProps.uischema, rootData, ownProps.path, getAjv(state))
1046+
? isVisible(
1047+
ownProps.uischema,
1048+
rootData,
1049+
ownProps.path,
1050+
getAjv(state),
1051+
getConfig(state)
1052+
)
10461053
: ownProps.visible;
10471054

10481055
const data = Resolve.data(rootData, ownProps.path);
@@ -1280,7 +1287,13 @@ export const mapStateToLabelProps = (
12801287

12811288
const visible: boolean =
12821289
props.visible === undefined || hasShowRule(uischema)
1283-
? isVisible(props.uischema, getData(state), props.path, getAjv(state))
1290+
? isVisible(
1291+
props.uischema,
1292+
getData(state),
1293+
props.path,
1294+
getAjv(state),
1295+
getConfig(state)
1296+
)
12841297
: props.visible;
12851298

12861299
const text = uischema.text;

packages/core/src/mappers/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const isInherentlyEnabled = (
1919
return false;
2020
}
2121
if (uischema && hasEnableRule(uischema)) {
22-
return isEnabled(uischema, rootData, ownProps?.path, getAjv(state));
22+
return isEnabled(uischema, rootData, ownProps?.path, getAjv(state), config);
2323
}
2424
if (typeof uischema?.options?.readonly === 'boolean') {
2525
return !uischema.options.readonly;

packages/core/src/models/uischema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ export interface ValidateFunctionContext {
169169
path: string | undefined;
170170
/** The `UISchemaElement` containing the rule that uses the ValidateFunctionCondition, e.g. a `ControlElement` */
171171
uischemaElement: UISchemaElement;
172+
/** The form config */
173+
config: unknown;
172174
}
173175

174176
/**

packages/core/src/util/runtime.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,19 @@ const evaluateCondition = (
6767
uischema: UISchemaElement,
6868
condition: Condition,
6969
path: string,
70-
ajv: Ajv
70+
ajv: Ajv,
71+
config: unknown
7172
): boolean => {
7273
if (isAndCondition(condition)) {
7374
return condition.conditions.reduce(
74-
(acc, cur) => acc && evaluateCondition(data, uischema, cur, path, ajv),
75+
(acc, cur) =>
76+
acc && evaluateCondition(data, uischema, cur, path, ajv, config),
7577
true
7678
);
7779
} else if (isOrCondition(condition)) {
7880
return condition.conditions.reduce(
79-
(acc, cur) => acc || evaluateCondition(data, uischema, cur, path, ajv),
81+
(acc, cur) =>
82+
acc || evaluateCondition(data, uischema, cur, path, ajv, config),
8083
false
8184
);
8285
} else if (isLeafCondition(condition)) {
@@ -95,6 +98,7 @@ const evaluateCondition = (
9598
fullData: data,
9699
path,
97100
uischemaElement: uischema,
101+
config,
98102
};
99103
return condition.validate(context);
100104
} else {
@@ -107,19 +111,21 @@ const isRuleFulfilled = (
107111
uischema: UISchemaElement,
108112
data: any,
109113
path: string,
110-
ajv: Ajv
114+
ajv: Ajv,
115+
config: unknown
111116
): boolean => {
112117
const condition = uischema.rule.condition;
113-
return evaluateCondition(data, uischema, condition, path, ajv);
118+
return evaluateCondition(data, uischema, condition, path, ajv, config);
114119
};
115120

116121
export const evalVisibility = (
117122
uischema: UISchemaElement,
118123
data: any,
119124
path: string = undefined,
120-
ajv: Ajv
125+
ajv: Ajv,
126+
config: unknown
121127
): boolean => {
122-
const fulfilled = isRuleFulfilled(uischema, data, path, ajv);
128+
const fulfilled = isRuleFulfilled(uischema, data, path, ajv, config);
123129

124130
switch (uischema.rule.effect) {
125131
case RuleEffect.HIDE:
@@ -136,9 +142,10 @@ export const evalEnablement = (
136142
uischema: UISchemaElement,
137143
data: any,
138144
path: string = undefined,
139-
ajv: Ajv
145+
ajv: Ajv,
146+
config: unknown
140147
): boolean => {
141-
const fulfilled = isRuleFulfilled(uischema, data, path, ajv);
148+
const fulfilled = isRuleFulfilled(uischema, data, path, ajv, config);
142149

143150
switch (uischema.rule.effect) {
144151
case RuleEffect.DISABLE:
@@ -177,10 +184,11 @@ export const isVisible = (
177184
uischema: UISchemaElement,
178185
data: any,
179186
path: string = undefined,
180-
ajv: Ajv
187+
ajv: Ajv,
188+
config: unknown
181189
): boolean => {
182190
if (uischema.rule) {
183-
return evalVisibility(uischema, data, path, ajv);
191+
return evalVisibility(uischema, data, path, ajv, config);
184192
}
185193

186194
return true;
@@ -190,10 +198,11 @@ export const isEnabled = (
190198
uischema: UISchemaElement,
191199
data: any,
192200
path: string = undefined,
193-
ajv: Ajv
201+
ajv: Ajv,
202+
config: unknown
194203
): boolean => {
195204
if (uischema.rule) {
196-
return evalEnablement(uischema, data, path, ajv);
205+
return evalEnablement(uischema, data, path, ajv, config);
197206
}
198207

199208
return true;

packages/core/src/util/util.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,20 @@ export const Paths = {
163163

164164
// Runtime --
165165
export const Runtime = {
166-
isEnabled(uischema: UISchemaElement, data: any, ajv: Ajv): boolean {
167-
return isEnabled(uischema, data, undefined, ajv);
166+
isEnabled(
167+
uischema: UISchemaElement,
168+
data: any,
169+
ajv: Ajv,
170+
config: unknown
171+
): boolean {
172+
return isEnabled(uischema, data, undefined, ajv, config);
168173
},
169-
isVisible(uischema: UISchemaElement, data: any, ajv: Ajv): boolean {
170-
return isVisible(uischema, data, undefined, ajv);
174+
isVisible(
175+
uischema: UISchemaElement,
176+
data: any,
177+
ajv: Ajv,
178+
config: unknown
179+
): boolean {
180+
return isVisible(uischema, data, undefined, ajv, config);
171181
},
172182
};

0 commit comments

Comments
 (0)