@@ -20,16 +20,6 @@ import type {
20
20
PerseusWidgetsMap ,
21
21
} from "@khanacademy/perseus-core" ;
22
22
23
- function mapWidgetIdsToEmptyScore ( widgetIds : ReadonlyArray < string > ) : {
24
- [ widgetId : string ] : PerseusScore ;
25
- } {
26
- const emptyResult : { [ widgetId : string ] : PerseusScore } = { } ;
27
- widgetIds . forEach ( ( widgetId ) => {
28
- emptyResult [ widgetId ] = { type : "invalid" , message : null } ;
29
- } ) ;
30
- return emptyResult ;
31
- }
32
-
33
23
export function getUpgradedWidgetOptions (
34
24
oldWidgetOptions : PerseusWidgetsMap ,
35
25
) : PerseusWidgetsMap {
@@ -122,18 +112,6 @@ export function scoreWidgetsFunctional(
122
112
) : { [ widgetId : string ] : PerseusScore } {
123
113
const upgradedWidgets = getUpgradedWidgetOptions ( widgets ) ;
124
114
125
- // Do empty check first
126
- const emptyWidgets = emptyWidgetsFunctional (
127
- widgets ,
128
- widgetIds ,
129
- userInputMap ,
130
- strings ,
131
- locale ,
132
- ) ;
133
- if ( emptyWidgets . length > 0 ) {
134
- return mapWidgetIdsToEmptyScore ( emptyWidgets ) ;
135
- }
136
-
137
115
const gradedWidgetIds = widgetIds . filter ( ( id ) => {
138
116
const props = upgradedWidgets [ id ] ;
139
117
const widgetIsGraded : boolean = props ?. graded == null || props . graded ;
@@ -150,13 +128,14 @@ export function scoreWidgetsFunctional(
150
128
}
151
129
152
130
const userInput = userInputMap [ id ] ;
131
+ const validator = getWidgetValidator ( widget . type ) ;
153
132
const scorer = getWidgetScorer ( widget . type ) ;
154
- const score = scorer ?. (
155
- userInput as UserInput ,
156
- widget . options ,
157
- strings ,
158
- locale ,
159
- ) ;
133
+
134
+ // We do validation (empty checks) first and then scoring. If
135
+ // validation fails, it's result is itself a PerseusScore.
136
+ const score =
137
+ validator ?. ( userInput , widget . options , strings , locale ) ??
138
+ scorer ?. ( userInput , widget . options , strings , locale ) ;
160
139
if ( score != null ) {
161
140
widgetScores [ id ] = score ;
162
141
}
0 commit comments