Skip to content

Commit

Permalink
Add validation for empty user input
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremywiebe committed Nov 19, 2024
1 parent b5f972d commit bf6577c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
28 changes: 27 additions & 1 deletion packages/perseus/src/widgets/matrix/validate-matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ import validateMatrix from "./validate-matrix";
import type {PerseusMatrixUserInput} from "../../validation.types";

describe("matrixValidator", () => {
it("is null always", () => {
it("should return invalid when answers is completely empty", () => {
// Arrange
const userInput: PerseusMatrixUserInput = {
answers: [],
};

// Act
const result = validateMatrix(userInput, {});

// 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, {});

// Assert
expect(result).toHaveInvalidInput();
});

it("should return null for non-empty user input", () => {
// Arrange
const userInput: PerseusMatrixUserInput = {
answers: [
Expand Down
10 changes: 9 additions & 1 deletion packages/perseus/src/widgets/matrix/validate-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
/**
* Checks user input from the matrix widget to see if it is scorable.
*
* Note: The matrix widget cannot do any validation without the Scoring
* 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.
Expand All @@ -16,6 +16,14 @@ function validateMatrix(
userInput: PerseusMatrixUserInput,
rubric: PerseusMatrixValidationData,
): Extract<PerseusScore, {type: "invalid"}> | null {
// Very basic check: did we get 0 rows or any rows with 0 answers? If so,
// then the user input is not gradable.
if (
userInput.answers.length === 0 ||
userInput.answers.some((row) => row.length === 0)
) {
return {type: "invalid", message: null};
}
return null;
}

Expand Down

0 comments on commit bf6577c

Please sign in to comment.