-
Notifications
You must be signed in to change notification settings - Fork 352
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
Matrix: Extract validation from scorer #1883
Changes from all commits
bcb1e82
eaa1492
718fc62
6109412
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": minor | ||
--- | ||
|
||
Introduces a validation function for the matrix widget (extracted from matrix scoring function). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {mockStrings} from "../../strings"; | ||
|
||
import validateMatrix from "./validate-matrix"; | ||
|
||
import type {PerseusMatrixUserInput} from "../../validation.types"; | ||
|
||
describe("matrixValidator", () => { | ||
it("should return invalid when answers is completely empty", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [[]], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should return invalid when any answer row is empty", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [[1, 2, 3], [], [7, 8, 9]], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput(); | ||
}); | ||
|
||
it("should return null for non-empty user input", () => { | ||
// Arrange | ||
const userInput: PerseusMatrixUserInput = { | ||
answers: [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9], | ||
], | ||
}; | ||
|
||
// Act | ||
const result = validateMatrix(userInput, {}, mockStrings); | ||
|
||
// Assert | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import _ from "underscore"; | ||
|
||
import {getMatrixSize} from "./matrix"; | ||
|
||
import type {PerseusStrings} from "../../strings"; | ||
import type {PerseusScore} from "../../types"; | ||
import type { | ||
PerseusMatrixUserInput, | ||
PerseusMatrixValidationData, | ||
} from "../../validation.types"; | ||
|
||
/** | ||
* Checks user input from the matrix widget to see if it is scorable. | ||
* | ||
jeremywiebe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Note: The matrix widget cannot do much validation without the Scoring | ||
* Data because of its use of KhanAnswerTypes as a core part of scoring. | ||
* | ||
* @see `scoreMatrix()` for more details. | ||
*/ | ||
function validateMatrix( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Did you decide just to leave all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's all left in the scoring function because there is no logic that can be moved into the validator that doesn't require the scoring data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It kind of looked like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. I need the solution size for part of these check but the |
||
userInput: PerseusMatrixUserInput, | ||
validationData: PerseusMatrixValidationData, | ||
strings: PerseusStrings, | ||
): Extract<PerseusScore, {type: "invalid"}> | null { | ||
const supplied = userInput.answers; | ||
const suppliedSize = getMatrixSize(supplied); | ||
|
||
for (let row = 0; row < suppliedSize[0]; row++) { | ||
for (let col = 0; col < suppliedSize[1]; col++) { | ||
if ( | ||
supplied[row][col] == null || | ||
supplied[row][col].toString().length === 0 | ||
) { | ||
return { | ||
type: "invalid", | ||
message: strings.fillAllCells, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default validateMatrix; |
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 mentioned this in the other PRs, but just to be extra annoying: I'm not sure we really need a bunch of aliases for
Empty
.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'll remove it (and others that were
Empty
). I've added a doc comment to the top of this file in another PR.