Skip to content

Commit 061909b

Browse files
committed
move requiring a single value further in the process where more context exists'
1 parent d68435d commit 061909b

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

coderd/httpapi/queryparams.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,16 @@ func ParseCustomList[T any](parser *QueryParamParser, vals url.Values, def []T,
297297
func parseQueryParam[T any](parser *QueryParamParser, vals url.Values, parse func(v string) (T, error), def T, queryParam string) (T, error) {
298298
setParse := func(set []string) (T, error) {
299299
if len(set) > 1 {
300-
return def, xerrors.Errorf("multiple values provided for the query param %q, only 1 is supported", queryParam)
300+
// Set as a parser.Error rather than return an error.
301+
// Returned errors are errors from the passed in `parse` function, and
302+
// imply the query param value had attempted to be parsed.
303+
// By raising the error this way, we can also more easily control how it
304+
// is presented to the user. A returned error is wrapped with more text.
305+
parser.Errors = append(parser.Errors, codersdk.ValidationError{
306+
Field: queryParam,
307+
Detail: fmt.Sprintf("Query param %q provided more than once, found %d times. Only provide 1 instance of this query param.", queryParam, len(set)),
308+
})
309+
return def, nil
301310
}
302311
return parse(set[0])
303312
}

coderd/httpapi/queryparams_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type queryParamTestCase[T any] struct {
3232

3333
func TestParseQueryParams(t *testing.T) {
3434
t.Parallel()
35-
const multipleValuesError = "multiple values provided"
35+
const multipleValuesError = "provided more than once"
3636

3737
t.Run("Enum", func(t *testing.T) {
3838
t.Parallel()

coderd/searchquery/search.go

-11
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,6 @@ func searchTerms(query string, defaultKey func(term string, values url.Values) e
163163
}
164164
}
165165

166-
for k := range searchValues {
167-
if len(searchValues[k]) > 1 {
168-
return nil, []codersdk.ValidationError{
169-
{
170-
Field: "q",
171-
Detail: fmt.Sprintf("Query parameter %q provided more than once, found %d times", k, len(searchValues[k])),
172-
},
173-
}
174-
}
175-
}
176-
177166
return searchValues, nil
178167
}
179168

0 commit comments

Comments
 (0)