Skip to content

Commit e6afe73

Browse files
committed
address review comments related to valid/validate functions
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent a5ad58b commit e6afe73

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

coderd/templates.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"sort"
10+
"strings"
1011
"time"
1112

1213
"github.com/go-chi/chi/v5"
@@ -29,6 +30,7 @@ import (
2930
"github.com/coder/coder/v2/coderd/searchquery"
3031
"github.com/coder/coder/v2/coderd/telemetry"
3132
"github.com/coder/coder/v2/coderd/util/ptr"
33+
"github.com/coder/coder/v2/coderd/util/slice"
3234
"github.com/coder/coder/v2/coderd/workspacestats"
3335
"github.com/coder/coder/v2/codersdk"
3436
"github.com/coder/coder/v2/examples"
@@ -352,13 +354,17 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
352354
}
353355
}
354356

355-
if createTemplate.CORSBehavior != nil && *createTemplate.CORSBehavior != "" {
356-
val := createTemplate.CORSBehavior
357-
if err := val.Validate(); err != nil {
358-
validErrs = append(validErrs, codersdk.ValidationError{Field: "cors_behavior", Detail: err.Error()})
359-
} else {
360-
corsBehavior = database.CorsBehavior(*val)
361-
}
357+
// Default the CORS behavior here to Simple so we don't break all existing templates.
358+
val := database.CorsBehaviorSimple
359+
if createTemplate.CORSBehavior != nil {
360+
val = database.CorsBehavior(*createTemplate.CORSBehavior)
361+
}
362+
if !val.Valid() {
363+
validErrs = append(validErrs, codersdk.ValidationError{Field: "cors_behavior",
364+
Detail: fmt.Sprintf("Invalid CORS behavior %q. Must be one of [%s]", *createTemplate.CORSBehavior, strings.Join(slice.ToStrings(database.AllCorsBehaviorValues()), ", ")),
365+
})
366+
} else {
367+
corsBehavior = val
362368
}
363369

364370
if autostopRequirementWeeks < 0 {
@@ -738,11 +744,13 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
738744

739745
corsBehavior := template.CorsBehavior
740746
if req.CORSBehavior != nil && *req.CORSBehavior != "" {
741-
val := req.CORSBehavior
742-
if err := val.Validate(); err != nil {
743-
validErrs = append(validErrs, codersdk.ValidationError{Field: "cors_behavior", Detail: err.Error()})
747+
val := database.CorsBehavior(*req.CORSBehavior)
748+
if !val.Valid() {
749+
validErrs = append(validErrs, codersdk.ValidationError{Field: "cors_behavior",
750+
Detail: fmt.Sprintf("Invalid CORS behavior %q. Must be one of [%s]", *req.CORSBehavior, strings.Join(slice.ToStrings(database.AllCorsBehaviorValues()), ", ")),
751+
})
744752
} else {
745-
corsBehavior = database.CorsBehavior(*val)
753+
corsBehavior = val
746754
}
747755
}
748756

codersdk/cors_behavior.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
package codersdk
22

3-
import (
4-
"golang.org/x/xerrors"
5-
)
6-
73
type CORSBehavior string
84

95
const (
106
CORSBehaviorSimple CORSBehavior = "simple"
117
CORSBehaviorPassthru CORSBehavior = "passthru"
128
)
13-
14-
func (c CORSBehavior) Validate() error {
15-
if c != CORSBehaviorSimple && c != CORSBehaviorPassthru {
16-
return xerrors.New("Invalid CORS behavior.")
17-
}
18-
return nil
19-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy