Skip to content

Conversation

@Maria-Golomb
Copy link
Contributor

@Maria-Golomb Maria-Golomb commented Apr 11, 2025

Link to Jira Ticket

https://formio.atlassian.net/browse/FIO-9942

Description

Added a static registerEvaluator method to work properly with extended evaluator classes by using this instead of referencing the base Evaluator class directly.
This ensures that any subclass extending the Evaluator can successfully register additional methods or properties using registerEvaluator.

Dependencies

How has this PR been tested?

manually during the process of connecting @formio/protected-eval to 5.x renderer.

Checklist:

  • I have completed the above PR template
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings
  • My changes include tests that prove my fix is effective (or that my feature works as intended)
  • New and existing unit/integration tests pass locally with my changes
  • Any dependent changes have corresponding PRs that are listed above

Copy link
Contributor

@brendanbond brendanbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are classes with almost exclusively static methods, I'm not sure this change will function like we think. Can you confirm?

@Maria-Golomb
Copy link
Contributor Author

Maria-Golomb commented Apr 15, 2025

The registerEvaluator method allows external modules to extend the Evaluator class:
public static registerEvaluator(evaluator: any) { Object.keys(evaluator).forEach((key) => { (Evaluator as any)[key] = evaluator[key]; }); }

However, using (Evaluator as any)[key] = evaluator[key]; assigns properties directly to the Evaluator class where the method is defined — not to the subclass that might be calling it.

In our case, when we register a protected evaluator using Formio.use(ProtectedEval), we are working with the Evaluator class from formio/js, which extends JSONLogicEvaluator from formio/core, and that in turn extends Evaluator.

The registerEvaluator method exists in Evaluator. So when we use the line (Evaluator as any)[key] = evaluator[key]; it assigns the evaluator to the Evaluator class inside formio/core, not to the Evaluator class from formio/js.

This causes problems when we later try to access the registerEvaluator method or the registered evaluator from Evaluator on formio/js, such as in this line:
https://github.com/formio/formio.js/blob/master/src/formio.form.js#L94

At that point, registerEvaluator is undefined, because it's not present on the actual Evaluator class.

My fix:
(this as any)[key] = evaluator[key];
here we’re referring to the class that actually called registerEvaluator (this in a static method refers to the class it's called on).
So our Evaluator (from formio/js) have access to registerEvaluator and we don’t have this problem.
This way, the Evaluator class from formio/js retains access to the registered evaluator, and everything works as expected.

@brendanbond brendanbond merged commit 65e0262 into master Apr 15, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants