-
Notifications
You must be signed in to change notification settings - Fork 166
validation added to NumericalInput to just accept numerical values on block creation form #2615
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
base: master
Are you sure you want to change the base?
validation added to NumericalInput to just accept numerical values on block creation form #2615
Conversation
|
Thanks for the pull request, @jesusbalderramawgu! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. Where can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
|
Suggestion: In the description of the PR you can add |
src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/messages.js
Outdated
Show resolved
Hide resolved
src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/messages.js
Outdated
Show resolved
Hide resolved
|
You need to follow the conventional commits messages |
|
Thanks! Let me know once the other reviewers have approved it, or if you need me to approve some more test runs. |
kdmccormick
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work so far @jesusbalderramawgu !
src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/messages.js
Outdated
Show resolved
Hide resolved
| ); | ||
| } | ||
| if (problemType !== ProblemTypeKeys.NUMERIC || !answer.isAnswerRange) { | ||
| const isValidValue = validateAnswerNumeric(answer.title) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example formula provided in the "Select Problem Type" modal (m*c^2) is not valid for numeric input:
Here is a valid formula I typed into a numeric problem, and then took a screenshot of:
Unfortunately, it's not visually consistent with the rest of the example problem images in the modal. I'll reach out to see if we can get our hands on the original images... ( @marcotuts , any chance you have access to them?). In the meantime, feel free to use my screenshot, even if it looks a little off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marco's working on a new image to put here, will post it here soon~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| */ | ||
| export const answerRangeFormatRegex = /^[([]\s*-?(?:\d+(?:\.\d+)?|\d+\/\d+)\s*,\s*-?(?:\d+(?:\.\d+)?|\d+\/\d+)\s*[)\]]$/m; | ||
|
|
||
| export const numericRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numeric input problems can also include sin, pi, and a few other functions.
I would ask you to reference the docs (https://docs.openedx.org/en/latest/educators/references/course_development/exercise_tools/numerical_input.html), but those docs aren't entirely correct either 🤦🏻 In particular, the docs say that g is allows, but this does not work in practice.
I'll have to do some digging in the code to figure out what's really allowed; I'll circle back. In the meantime, can you try updating the regex to include pi, sqrt, sin, arcscin, cos, arccos, tan, arctan, log2, and parenthases ( ) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, here's the function which perform the numeric evaluation: https://github.com/openedx/openedx-calc/blob/master/calc/calc.py#L239
And here's the call site: https://github.com/openedx/edx-platform/blob/f4f14a69874d5804ef33778486f7d6cab400e206/xmodule/capa/responsetypes.py#L1563
Note that both variables and unary_functions are passed in as {}.
@jesusbalderramawgu , would you or your team be able to reverse engineer this function in order to figure out exactly what is allowed in a numeric input formula?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I will check this with the team
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, just to follow up on the ticket, I'm still checking because now needs more work the validation part.
After this feedback, the frontend shouldn't validate if is a valid math expression, it should be on the backend. After some review I found an endpoint to do this validation but it's really attached to django views, so I'm looking for a way of request this from react and validate the input component.
I'll keep you posted, thanks in advance!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also thinking that the validation might need to happen on the backend, glad to see you came to the same conclusion.
Perhaps we need to factor out a new REST API for the validation? Curious to hear what you find.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree on the new rest API for this validation, but gonna check with the team first in case they know something about how to reuse it.
this is where the validation is handled inside edx platform
xmodule/capa/inputtypes.py
in this function, line 1277
def preview_formcalc(self, get):
it handles every formula as latex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a solution, I would like your thoughts on this.
after some digging and checking with @holaontiveros
we found that is possible to request the validation endpoint in this view, but it is required to save the numeric input first.
Basically when we select "problem" in the options and the modal opens up
a block is created with this endpoint
http://studio.local.openedx.io:8001/xblock/
returning a blockId which is required to validate the formula, but the input is not created because is not saved yet.
so we can't validate the formula without the creation of the input because even though we have a blockId it is like a temporary id until we create the input. We need to save the input first to get a valid id and be able to validate.
my proposal is to save an empty response (numerical input) when the modal opens up, that way we would have a valid id as reference and when the user types in the answer we can validate successfully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @jesusbalderramawgu , thanks for looking into this. That particular approach is not viable, because we need the editor to be able to work without saving the Problem first. You can see the importance of this by going into Libraries (Beta), creating a Problem, and hitting "Cancel" instead of saving: notice that no Problem is created.
(In Courses, on the other hand, if you being creating a Problem and hit "Cancel", you will end up with a blank problem which you have to then delete. This is an undesirable legacy behavior which we'll want to fix one day.)
I think we'll need a new endpoint, maybe one that just takes an answer string and validates it for a particular response type (formula, numeric, etc.)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your feedback @kdmccormick, I checked what you said about the flow and I agree.
a new endpoint is required for this then, it should receive a string and return a boolean to know if is valid or not.
do you know who can help with the creation of this endpoint?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2615 +/- ##
==========================================
+ Coverage 94.81% 94.83% +0.02%
==========================================
Files 1225 1234 +9
Lines 27481 27771 +290
Branches 6187 6267 +80
==========================================
+ Hits 26056 26338 +282
+ Misses 1367 1362 -5
- Partials 58 71 +13 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
842e6a2 to
14d2c72
Compare
thanks, I made the change in the current commit messages and I'll keep this in mind for future PR |

Description
There's a bug with Numerical Input Component which Incorrectly Handles Formulas as numericalresponse.
after some research and talking with the community we agreed to validate the author block form to just accept numeric values in the answers.
this is how the form will show the error if the value is not numeric.