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

Merged
merged 3 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion packages/core/src/models/uischema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ 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: (data: unknown) => boolean;
}

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

/**
* Common base interface for any UI schema element.
Expand Down
10 changes: 10 additions & 0 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,6 +52,12 @@ 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);
};
Expand Down Expand Up @@ -80,6 +87,9 @@ const evaluateCondition = (
return false;
}
return ajv.validate(condition.schema, value) as boolean;
} else if (isValidateFunctionCondition(condition)) {
const value = resolveData(data, getConditionScope(condition, path));
return condition.validate(value);
} else {
// unknown condition
return true;
Expand Down
85 changes: 85 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,7 @@ import {
OrCondition,
RuleEffect,
SchemaBasedCondition,
ValidateFunctionCondition,
} from '../../src';
import { evalEnablement, evalVisibility } from '../../src/util/runtime';

Expand Down Expand Up @@ -491,6 +492,90 @@ 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: (data) => 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: (data) => 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: (data) => 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: (data) => 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('evalEnablement disable invalid case', (t) => {
const leafCondition: LeafCondition = {
type: 'LEAF',
Expand Down
18 changes: 18 additions & 0 deletions packages/examples/src/examples/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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 +105,20 @@ export const uischema = {
},
},
},
{
type: 'Control',
label: 'Vitamin deficiency?',
scope: '#/properties/vitaminDeficiency',
rule: {
effect: 'SHOW',
condition: {
scope: '#',
validate: (data: any) => {
return !data.dead && data.kindOfVegetables !== 'All';
},
},
},
},
],
},
],
Expand Down