-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expression: Extract validation from scorer (#1878)
## Summary: Arguably, this validator is quite useless in that it does nothing. However, I was thinking that we'd be better off if all of our widgets had a validator, even if it does nothing, rather than have some widgets have a validator and some not. In my mind, we're better off with the same convention everywhere. This PR introduces the concept of "validation data" that we discussed earlier today (November 18). This represents _only_ the data needed by the validator. It intersects that type into the ScoringData type (nee Rubric) to ensure that the scoring function has all the data it needs to score (but also validate). * `PerseusExpressionRubric` - the data needed to score the `expression` user input (Note: this will be renamed to `PerseusExpressionScoringData` in the near future) * `PerseusExpressionValidationData` - the data needed to validate the `expression` user input * `ExpressionWidgetOptions` - the data needed to render the `expression` widget In the `expression` widget case, the validation data is empty, but in other widgets, the data in this type would also need to be in both the rubric and widget options (as the widget will call the validator on the client with only the widget options in hand and the scorer will call the validator on the server with only the rubric in hand). Issue: LEMS-2598 ## Test plan: `yarn test` `yarn typecheck` Author: jeremywiebe Reviewers: handeyeco, jeremywiebe, Myranae Required Reviewers: Approved By: handeyeco Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1878
- Loading branch information
1 parent
0afb1a4
commit a27f23b
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": minor | ||
--- | ||
|
||
Add expression validator function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/perseus/src/widgets/expression/validate-expression.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import validateExpression from "./validate-expression"; | ||
|
||
describe("expression validation", () => { | ||
it("should return invalid for empty user input", () => { | ||
const result = validateExpression(""); | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should return null for non-empty user input", () => { | ||
const result = validateExpression("x+1"); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/perseus/src/widgets/expression/validate-expression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusExpressionUserInput} from "../../validation.types"; | ||
|
||
/** | ||
* Checks user input from the expression widget to see if it is scorable. | ||
* | ||
* Note: Most of the expression widget's validation requires the Rubric because | ||
* of its use of KhanAnswerTypes as a core part of scoring. | ||
* | ||
* @see `scoreExpression()` for more details. | ||
*/ | ||
function validateExpression( | ||
userInput: PerseusExpressionUserInput, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
if (userInput === "") { | ||
return {type: "invalid", message: null}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateExpression; |