Skip to content

feat(core): Add validate function based rule condition #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/core/src/models/uischema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ export interface SchemaBasedCondition extends BaseCondition, Scoped {
failWhenUndefined?: boolean;
}

/** A condition using a validation function to determine its fulfillment. */
export interface ValidateFunctionCondition extends BaseCondition, Scoped {
/**
* Validates whether the condition is fulfilled.
*
* @param data The data as resolved via the scope.
* @returns `true` if the condition is fulfilled */
validate: (context: ValidateFunctionContext) => boolean;
}

export interface ValidateFunctionContext {
/** The resolved data scoped to the `ValidateFunctionCondition`'s scope. */
data: unknown;
/** The full data of the form. */
fullData: unknown;
/** Optional instance path. Necessary when the actual data path can not be inferred via the scope alone as it is the case with nested controls. */
path: string | undefined;
/** The `UISchemaElement` containing the rule that uses the ValidateFunctionCondition, e.g. a `ControlElement` */
uischemaElement: UISchemaElement;
}

/**
* A composable condition.
*/
Expand Down Expand Up @@ -179,7 +200,8 @@ export type Condition =
| LeafCondition
| OrCondition
| AndCondition
| SchemaBasedCondition;
| SchemaBasedCondition
| ValidateFunctionCondition;

/**
* Common base interface for any UI schema element.
Expand Down
23 changes: 20 additions & 3 deletions packages/core/src/util/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
SchemaBasedCondition,
Scopable,
UISchemaElement,
ValidateFunctionCondition,
} from '../models';
import { resolveData } from './resolvers';
import type Ajv from 'ajv';
Expand All @@ -51,24 +52,31 @@ const isSchemaCondition = (
condition: Condition
): condition is SchemaBasedCondition => has(condition, 'schema');

const isValidateFunctionCondition = (
condition: Condition
): condition is ValidateFunctionCondition =>
has(condition, 'validate') &&
typeof (condition as ValidateFunctionCondition).validate === 'function';

const getConditionScope = (condition: Scopable, path: string): string => {
return composeWithUi(condition, path);
};

const evaluateCondition = (
data: any,
uischema: UISchemaElement,
condition: Condition,
path: string,
ajv: Ajv
): boolean => {
if (isAndCondition(condition)) {
return condition.conditions.reduce(
(acc, cur) => acc && evaluateCondition(data, cur, path, ajv),
(acc, cur) => acc && evaluateCondition(data, uischema, cur, path, ajv),
true
);
} else if (isOrCondition(condition)) {
return condition.conditions.reduce(
(acc, cur) => acc || evaluateCondition(data, cur, path, ajv),
(acc, cur) => acc || evaluateCondition(data, uischema, cur, path, ajv),
false
);
} else if (isLeafCondition(condition)) {
Expand All @@ -80,6 +88,15 @@ const evaluateCondition = (
return false;
}
return ajv.validate(condition.schema, value) as boolean;
} else if (isValidateFunctionCondition(condition)) {
const value = resolveData(data, getConditionScope(condition, path));
const context = {
data: value,
fullData: data,
path,
uischemaElement: uischema,
};
return condition.validate(context);
} else {
// unknown condition
return true;
Expand All @@ -93,7 +110,7 @@ const isRuleFulfilled = (
ajv: Ajv
): boolean => {
const condition = uischema.rule.condition;
return evaluateCondition(data, condition, path, ajv);
return evaluateCondition(data, uischema, condition, path, ajv);
};

export const evalVisibility = (
Expand Down
115 changes: 115 additions & 0 deletions packages/core/test/util/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
OrCondition,
RuleEffect,
SchemaBasedCondition,
ValidateFunctionCondition,
ValidateFunctionContext,
} from '../../src';
import { evalEnablement, evalVisibility } from '../../src/util/runtime';

Expand Down Expand Up @@ -491,6 +493,119 @@ test('evalEnablement disable valid case', (t) => {
t.is(evalEnablement(uischema, data, undefined, createAjv()), false);
});

// Add test case for ValidateFunctionCondition with evalEnablement (valid enable case)
test('evalEnablement enable valid case based on ValidateFunctionCondition', (t) => {
const condition: ValidateFunctionCondition = {
scope: '#/properties/ruleValue',
validate: (context: ValidateFunctionContext) => context.data === 'bar',
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/value',
rule: {
effect: RuleEffect.ENABLE,
condition: condition,
},
};
const data = {
value: 'foo',
ruleValue: 'bar',
};
t.is(evalEnablement(uischema, data, undefined, createAjv()), true);
});

// Add test case for ValidateFunctionCondition with evalEnablement (invalid enable case)
test('evalEnablement enable invalid case based on ValidateFunctionCondition', (t) => {
const condition: ValidateFunctionCondition = {
scope: '#/properties/ruleValue',
validate: (context: ValidateFunctionContext) => context.data === 'bar',
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/value',
rule: {
effect: RuleEffect.ENABLE,
condition: condition,
},
};
const data = {
value: 'foo',
ruleValue: 'foobar',
};
t.is(evalEnablement(uischema, data, undefined, createAjv()), false);
});

// Add test case for ValidateFunctionCondition with evalEnablement (valid disable case)
test('evalEnablement disable valid case based on ValidateFunctionCondition', (t) => {
const condition: ValidateFunctionCondition = {
scope: '#/properties/ruleValue',
validate: (context: ValidateFunctionContext) => context.data === 'bar',
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/value',
rule: {
effect: RuleEffect.DISABLE,
condition: condition,
},
};
const data = {
value: 'foo',
ruleValue: 'bar',
};
t.is(evalEnablement(uischema, data, undefined, createAjv()), false);
});

// Add test case for ValidateFunctionCondition with evalEnablement (invalid disable case)
test('evalEnablement disable invalid case based on ValidateFunctionCondition', (t) => {
const condition: ValidateFunctionCondition = {
scope: '#/properties/ruleValue',
validate: (context: ValidateFunctionContext) => context.data === 'bar',
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/value',
rule: {
effect: RuleEffect.DISABLE,
condition: condition,
},
};
const data = {
value: 'foo',
ruleValue: 'foobar',
};
t.is(evalEnablement(uischema, data, undefined, createAjv()), true);
});

// Test context properties for ValidateFunctionCondition
test('ValidateFunctionCondition correctly passes context parameters', (t) => {
const condition: ValidateFunctionCondition = {
scope: '#/properties/ruleValue',
validate: (context: ValidateFunctionContext) => {
// Verify all context properties are passed correctly
return (
context.data === 'bar' &&
(context.fullData as any).value === 'foo' &&
context.path === undefined &&
(context.uischemaElement as any).scope === '#/properties/value'
);
},
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/value',
rule: {
effect: RuleEffect.ENABLE,
condition: condition,
},
};
const data = {
value: 'foo',
ruleValue: 'bar',
};
t.is(evalEnablement(uischema, data, undefined, createAjv()), true);
});

test('evalEnablement disable invalid case', (t) => {
const leafCondition: LeafCondition = {
type: 'LEAF',
Expand Down
22 changes: 22 additions & 0 deletions packages/examples/src/examples/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { ValidateFunctionContext } from '@jsonforms/core';
import { registerExamples } from '../register';

export const schema = {
Expand All @@ -44,6 +45,10 @@ export const schema = {
type: 'string',
enum: ['All', 'Some', 'Only potatoes'],
},
vitaminDeficiency: {
type: 'string',
enum: ['None', 'Vitamin A', 'Vitamin B', 'Vitamin C'],
},
},
};

Expand Down Expand Up @@ -101,6 +106,23 @@ export const uischema = {
},
},
},
{
type: 'Control',
label: 'Vitamin deficiency?',
scope: '#/properties/vitaminDeficiency',
rule: {
effect: 'SHOW',
condition: {
scope: '#',
validate: (context: ValidateFunctionContext) => {
return (
!(context.data as any).dead &&
(context.data as any).kindOfVegetables !== 'All'
);
},
},
},
},
],
},
],
Expand Down