Skip to content

Commit 73a7882

Browse files
committed
fix custom validation, add jsdoc annotations
1 parent 2fcb1c6 commit 73a7882

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/process/validation/rules/validateCustom.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const shouldValidate = (context: ValidationContext) => {
1616
};
1717

1818
export const validateCustomSync: RuleFnSync = (context: ValidationContext) => {
19-
const { component, index, instance } = context;
19+
const { component, index, instance, value, data, row, submission } = context;
2020
const customValidation = component.validate?.custom;
2121
try {
2222
if (!shouldValidate(context) || !customValidation) {
@@ -25,9 +25,16 @@ export const validateCustomSync: RuleFnSync = (context: ValidationContext) => {
2525

2626
const validationContext: any = instance?.evalContext ? instance.evalContext() : context;
2727

28+
// We have to augment some of the evalContext values here if the evalContext comes from the instance
2829
const isValid = evaluate(customValidation, validationContext, 'valid', true, (context) => {
30+
context.component = component;
31+
context.data = data;
32+
context.row = row;
2933
context.rowIndex = typeof index === 'number' ? index : validationContext.rowIndex;
34+
context.instance = instance;
3035
context.valid = true;
36+
context.input = value;
37+
context.submission = submission;
3138
});
3239

3340
if (isValid === null || isValid === true) {

src/utils/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ type EvaluatorContext = {
109109
};
110110
export type EvaluatorFn = (context: EvaluatorContext) => any;
111111

112+
/**
113+
* A convenience function that wraps Evaluator.evaluate and normalizes context values
114+
* @param evaluation - The code string to evaluate
115+
* @param context - The processor context
116+
* @param ret - The return value
117+
* @param interpolate - Whether or not to interpolate the code string before evaluating
118+
* @param evalContextFn - A callback to mutate the context value after it has been normalized
119+
* @param options - Options to pass to the Evaluator
120+
* @returns {*} - Returns the result of the evaluation
121+
*/
112122
export function evaluate(
113123
evaluation: string,
114124
context: EvaluatorContext,

0 commit comments

Comments
 (0)