-
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
Numeric Input: Treat unrecognized simplify
values as "required"
#2308
base: main
Are you sure you want to change the base?
Changes from all commits
6a0e6dc
0f8adfb
1e34d5f
18d1332
4c784d8
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 |
---|---|---|
|
@@ -16,7 +16,10 @@ import {defaulted} from "../general-purpose-parsers/defaulted"; | |
|
||
import {parseWidget} from "./widget"; | ||
|
||
import type {NumericInputWidget} from "../../data-schema"; | ||
import type { | ||
NumericInputWidget, | ||
PerseusNumericInputSimplify, | ||
} from "../../data-schema"; | ||
import type {Parser} from "../parser-types"; | ||
|
||
const parseMathFormat = enumeration( | ||
|
@@ -29,12 +32,41 @@ const parseMathFormat = enumeration( | |
"pi", | ||
); | ||
|
||
const parseSimplify = enumeration( | ||
"required", | ||
"correct", | ||
"enforced", | ||
"optional", | ||
); | ||
const parseSimplify = pipeParsers( | ||
union(constant(null)) | ||
.or(constant(undefined)) | ||
.or(boolean) | ||
.or(constant("required")) | ||
.or(constant("correct")) | ||
.or(constant("enforced")) | ||
.or(constant("optional")) | ||
.or(constant("accepted")).parser, | ||
).then(convert(deprecatedSimplifyValuesToRequired)).parser; | ||
|
||
function deprecatedSimplifyValuesToRequired( | ||
simplify: | ||
| "required" | ||
| "correct" | ||
| "enforced" | ||
| "optional" | ||
| "accepted" | ||
| null | ||
| undefined | ||
| boolean, | ||
): PerseusNumericInputSimplify { | ||
switch (simplify) { | ||
case "enforced": | ||
case "required": | ||
case "optional": | ||
return simplify; | ||
// NOTE(benchristel): "accepted", "correct", true, false, undefined, and | ||
// null are all treated the same as "required" during scoring, so we | ||
// convert them to "required" here to preserve behavior. See the tests | ||
// in score-numeric-input.test.ts | ||
default: | ||
return "required"; | ||
} | ||
} | ||
|
||
export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget( | ||
constant("numeric-input"), | ||
|
@@ -53,20 +85,7 @@ export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget( | |
// TODO(benchristel): simplify should never be a boolean, but we | ||
// have some content where it is anyway. If we ever backfill | ||
// the data, we should simplify `simplify`. | ||
simplify: optional( | ||
nullable( | ||
union(parseSimplify).or( | ||
pipeParsers(boolean).then( | ||
convert((value) => { | ||
if (typeof value === "boolean") { | ||
return value ? "required" : "optional"; | ||
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. @SonicScrewdriver I changed the behavior here to match what the code was doing in production before this parser existed. It seems like the scoring logic treats both |
||
} | ||
return value; | ||
}), | ||
).parser, | ||
).parser, | ||
), | ||
), | ||
simplify: parseSimplify, | ||
}), | ||
), | ||
labelText: optional(string), | ||
|
@@ -78,16 +97,7 @@ export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget( | |
array( | ||
object({ | ||
name: parseMathFormat, | ||
simplify: optional( | ||
nullable( | ||
enumeration( | ||
"required", | ||
"correct", | ||
"enforced", | ||
"optional", | ||
), | ||
), | ||
), | ||
simplify: parseSimplify, | ||
}), | ||
), | ||
), | ||
|
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 still need to add tests for this.