Skip to content

Commit 89b281d

Browse files
committed
s/Required/RequiredNotEmpty
1 parent cbce34a commit 89b281d

File tree

9 files changed

+24
-24
lines changed

9 files changed

+24
-24
lines changed

coderd/coderdtest/oidctest/idp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
10521052
f.logger.Info(r.Context(), "http call device auth")
10531053

10541054
p := httpapi.NewQueryParamParser()
1055-
p.Required("client_id")
1055+
p.RequiredNotEmpty("client_id")
10561056
clientID := p.String(r.URL.Query(), "", "client_id")
10571057
_ = p.String(r.URL.Query(), "", "scopes")
10581058
if len(p.Errors) > 0 {

coderd/httpapi/queryparams.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ type QueryParamParser struct {
2323
// Parsed is a map of all query params that were parsed. This is useful
2424
// for checking if extra query params were passed in.
2525
Parsed map[string]bool
26-
// RequiredParams is a map of all query params that are required. This is useful
26+
// RequiredNotEmptyParams is a map of all query params that are required. This is useful
2727
// for forcing a value to be provided.
28-
RequiredParams map[string]bool
28+
RequiredNotEmptyParams map[string]bool
2929
}
3030

3131
func NewQueryParamParser() *QueryParamParser {
3232
return &QueryParamParser{
33-
Errors: []codersdk.ValidationError{},
34-
Parsed: map[string]bool{},
35-
RequiredParams: map[string]bool{},
33+
Errors: []codersdk.ValidationError{},
34+
Parsed: map[string]bool{},
35+
RequiredNotEmptyParams: map[string]bool{},
3636
}
3737
}
3838

@@ -90,9 +90,9 @@ func (p *QueryParamParser) Boolean(vals url.Values, def bool, queryParam string)
9090
return v
9191
}
9292

93-
func (p *QueryParamParser) Required(queryParam ...string) *QueryParamParser {
93+
func (p *QueryParamParser) RequiredNotEmpty(queryParam ...string) *QueryParamParser {
9494
for _, q := range queryParam {
95-
p.RequiredParams[q] = true
95+
p.RequiredNotEmptyParams[q] = true
9696
}
9797
return p
9898
}
@@ -246,10 +246,10 @@ func ParseCustomList[T any](parser *QueryParamParser, vals url.Values, def []T,
246246
func parseQueryParam[T any](parser *QueryParamParser, vals url.Values, parse func(v string) (T, error), def T, queryParam string) (T, error) {
247247
parser.addParsed(queryParam)
248248
// If the query param is required and not present, return an error.
249-
if parser.RequiredParams[queryParam] && (!vals.Has(queryParam) || vals.Get(queryParam) == "") {
249+
if parser.RequiredNotEmptyParams[queryParam] && (!vals.Has(queryParam) || vals.Get(queryParam) == "") {
250250
parser.Errors = append(parser.Errors, codersdk.ValidationError{
251251
Field: queryParam,
252-
Detail: fmt.Sprintf("Query param %q is required", queryParam),
252+
Detail: fmt.Sprintf("Query param %q is required and cannot be empty", queryParam),
253253
})
254254
return def, nil
255255
}

coderd/httpapi/queryparams_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,12 @@ func TestParseQueryParams(t *testing.T) {
320320
t.Parallel()
321321

322322
parser := httpapi.NewQueryParamParser()
323-
parser.Required("test_value")
323+
parser.RequiredNotEmpty("test_value")
324324
parser.UUID(url.Values{}, uuid.New(), "test_value")
325325
require.Len(t, parser.Errors, 1)
326326

327327
parser = httpapi.NewQueryParamParser()
328-
parser.Required("test_value")
328+
parser.RequiredNotEmpty("test_value")
329329
parser.String(url.Values{"test_value": {""}}, "", "test_value")
330330
require.Len(t, parser.Errors, 1)
331331
})

coderd/insights.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func (api *API) insightsUserActivity(rw http.ResponseWriter, r *http.Request) {
7272
ctx := r.Context()
7373

7474
p := httpapi.NewQueryParamParser().
75-
Required("start_time").
76-
Required("end_time")
75+
RequiredNotEmpty("start_time").
76+
RequiredNotEmpty("end_time")
7777
vals := r.URL.Query()
7878
var (
7979
// The QueryParamParser does not preserve timezone, so we need
@@ -161,8 +161,8 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
161161
ctx := r.Context()
162162

163163
p := httpapi.NewQueryParamParser().
164-
Required("start_time").
165-
Required("end_time")
164+
RequiredNotEmpty("start_time").
165+
RequiredNotEmpty("end_time")
166166
vals := r.URL.Query()
167167
var (
168168
// The QueryParamParser does not preserve timezone, so we need
@@ -253,8 +253,8 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
253253
ctx := r.Context()
254254

255255
p := httpapi.NewQueryParamParser().
256-
Required("start_time").
257-
Required("end_time")
256+
RequiredNotEmpty("start_time").
257+
RequiredNotEmpty("end_time")
258258
vals := r.URL.Query()
259259
var (
260260
// The QueryParamParser does not preserve timezone, so we need

coderd/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ func (api *API) userByName(rw http.ResponseWriter, r *http.Request) {
583583
func (api *API) userAutofillParameters(rw http.ResponseWriter, r *http.Request) {
584584
user := httpmw.UserParam(r)
585585

586-
p := httpapi.NewQueryParamParser().Required("template_id")
586+
p := httpapi.NewQueryParamParser().RequiredNotEmpty("template_id")
587587
templateID := p.UUID(r.URL.Query(), uuid.UUID{}, "template_id")
588588
p.ErrorExcessParams(r.URL.Query())
589589
if len(p.Errors) > 0 {

coderd/workspaceapps/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ func (s *Server) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {
636636

637637
values := r.URL.Query()
638638
parser := httpapi.NewQueryParamParser()
639-
reconnect := parser.Required("reconnect").UUID(values, uuid.New(), "reconnect")
639+
reconnect := parser.RequiredNotEmpty("reconnect").UUID(values, uuid.New(), "reconnect")
640640
height := parser.UInt(values, 80, "height")
641641
width := parser.UInt(values, 80, "width")
642642
if len(parser.Errors) > 0 {

enterprise/coderd/identityprovider/authorize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func extractAuthorizeParams(r *http.Request, callbackURL string) (authorizeParam
3232
p := httpapi.NewQueryParamParser()
3333
vals := r.URL.Query()
3434

35-
p.Required("state", "response_type", "client_id")
35+
p.RequiredNotEmpty("state", "response_type", "client_id")
3636

3737
// TODO: Can we make this a URL straight out of the database?
3838
cb, err := url.Parse(callbackURL)

enterprise/coderd/identityprovider/tokens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func extractTokenParams(r *http.Request, callbackURL string) (tokenParams, []cod
4646
return tokenParams{}, nil, err
4747
}
4848

49-
p.Required("grant_type", "client_secret", "client_id", "code")
49+
p.RequiredNotEmpty("grant_type", "client_secret", "client_id", "code")
5050

5151
vals := r.Form
5252
params := tokenParams{

enterprise/coderd/jfrog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (api *API) jFrogXrayScan(rw http.ResponseWriter, r *http.Request) {
6767
ctx = r.Context()
6868
vals = r.URL.Query()
6969
p = httpapi.NewQueryParamParser()
70-
wsID = p.Required("workspace_id").UUID(vals, uuid.UUID{}, "workspace_id")
71-
agentID = p.Required("agent_id").UUID(vals, uuid.UUID{}, "agent_id")
70+
wsID = p.RequiredNotEmpty("workspace_id").UUID(vals, uuid.UUID{}, "workspace_id")
71+
agentID = p.RequiredNotEmpty("agent_id").UUID(vals, uuid.UUID{}, "agent_id")
7272
)
7373

7474
if len(p.Errors) > 0 {

0 commit comments

Comments
 (0)