Skip to content

Commit

Permalink
SSS: Introduce ValidationResult type so things are standardized (#1993)
Browse files Browse the repository at this point in the history
## Summary:

This is a tiny PR to introduce a validation result which is a subset of the `PerseusScore`, but since we use it all over the validation code, it seemed useful to create a type for it. 

Issue: LEMS-2561

## Test plan:

`yarn typecheck`
`yarn test`

Author: jeremywiebe

Reviewers: handeyeco

Required Reviewers:

Approved By: handeyeco

Checks: ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x)

Pull Request URL: #1993
  • Loading branch information
jeremywiebe authored Dec 13, 2024
1 parent 8ca54d0 commit 99e55b6
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-zoos-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Internal: introduce `ValidationResult` for use in validation functions
2 changes: 2 additions & 0 deletions packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@ export type WidgetTransform = (
problemNumber?: number,
) => any;

export type ValidationResult = Extract<PerseusScore, {type: "invalid"}> | null;

export type WidgetScorerFunction = (
// The user data needed to score
userInput: UserInput,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {PerseusStrings} from "../../strings";
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {
PerseusCategorizerUserInput,
PerseusCategorizerValidationData,
Expand All @@ -17,7 +17,7 @@ function validateCategorizer(
userInput: PerseusCategorizerUserInput,
validationData: PerseusCategorizerValidationData,
strings: PerseusStrings,
): Extract<PerseusScore, {type: "invalid"}> | null {
): ValidationResult {
const incomplete = validationData.items.some(
(_, i) => userInput.values[i] == null,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/dropdown/validate-dropdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusDropdownUserInput} from "../../validation.types";

/**
Expand All @@ -7,7 +7,7 @@ import type {PerseusDropdownUserInput} from "../../validation.types";
*/
function validateDropdown(
userInput: PerseusDropdownUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
): ValidationResult {
if (userInput.value === 0) {
return {
type: "invalid",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusExpressionUserInput} from "../../validation.types";

/**
Expand All @@ -11,7 +11,7 @@ import type {PerseusExpressionUserInput} from "../../validation.types";
*/
function validateExpression(
userInput: PerseusExpressionUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
): ValidationResult {
if (userInput === "") {
return {type: "invalid", message: null};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/matrix/validate-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from "underscore";
import {getMatrixSize} from "./matrix";

import type {PerseusStrings} from "../../strings";
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {
PerseusMatrixUserInput,
PerseusMatrixValidationData,
Expand All @@ -21,7 +21,7 @@ function validateMatrix(
userInput: PerseusMatrixUserInput,
validationData: PerseusMatrixValidationData,
strings: PerseusStrings,
): Extract<PerseusScore, {type: "invalid"}> | null {
): ValidationResult {
const supplied = userInput.answers;
const suppliedSize = getMatrixSize(supplied);

Expand Down
6 changes: 2 additions & 4 deletions packages/perseus/src/widgets/orderer/validate-orderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusOrdererUserInput} from "../../validation.types";

/**
Expand All @@ -7,9 +7,7 @@ import type {PerseusOrdererUserInput} from "../../validation.types";
* @param userInput
* @see `scoreOrderer` for more details.
*/
function validateOrderer(
userInput: PerseusOrdererUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
function validateOrderer(userInput: PerseusOrdererUserInput): ValidationResult {
if (userInput.current.length === 0) {
return {
type: "invalid",
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/plotter/validate-plotter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Util from "../../util";

import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {
PerseusPlotterUserInput,
PerseusPlotterValidationData,
Expand All @@ -17,7 +17,7 @@ const {deepEq} = Util;
function validatePlotter(
userInput: PerseusPlotterUserInput,
validationData: PerseusPlotterValidationData,
): Extract<PerseusScore, {type: "invalid"}> | null {
): ValidationResult {
if (deepEq(userInput, validationData.starting)) {
return {
type: "invalid",
Expand Down
6 changes: 2 additions & 4 deletions packages/perseus/src/widgets/radio/validate-radio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusRadioUserInput} from "../../validation.types";

/**
Expand All @@ -9,9 +9,7 @@ import type {PerseusRadioUserInput} from "../../validation.types";
* @param userInput
* @see `scoreRadio` for the additional validation logic and the scoring logic.
*/
function validateRadio(
userInput: PerseusRadioUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
function validateRadio(userInput: PerseusRadioUserInput): ValidationResult {
const numSelected = userInput.choicesSelected.reduce((sum, selected) => {
return sum + (selected ? 1 : 0);
}, 0);
Expand Down
6 changes: 2 additions & 4 deletions packages/perseus/src/widgets/sorter/validate-sorter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusSorterUserInput} from "../../validation.types";

/**
Expand All @@ -8,9 +8,7 @@ import type {PerseusSorterUserInput} from "../../validation.types";
* @see 'scoreSorter' in 'packages/perseus/src/widgets/sorter/score-sorter.ts'
* for more details on how the sorter widget is scored.
*/
function validateSorter(
userInput: PerseusSorterUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
function validateSorter(userInput: PerseusSorterUserInput): ValidationResult {
// If the sorter widget hasn't been changed yet, we treat it as "empty" which
// prevents the "Check" button from becoming active. We want the user
// to make a change before trying to move forward. This makes an
Expand Down
6 changes: 2 additions & 4 deletions packages/perseus/src/widgets/table/validate-table.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {filterNonEmpty} from "./utils";

import type {PerseusScore} from "../../types";
import type {ValidationResult} from "../../types";
import type {PerseusTableUserInput} from "../../validation.types";

function validateTable(
userInput: PerseusTableUserInput,
): Extract<PerseusScore, {type: "invalid"}> | null {
function validateTable(userInput: PerseusTableUserInput): ValidationResult {
const supplied = filterNonEmpty(userInput);

const hasEmptyCell = supplied.some(function (row) {
Expand Down

0 comments on commit 99e55b6

Please sign in to comment.