Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite6afe73

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

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-
ifcreateTemplate.CORSBehavior!=nil&&*createTemplate.CORSBehavior!="" {
356-
val:=createTemplate.CORSBehavior
357-
iferr:=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+
ifcreateTemplate.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
ifautostopRequirementWeeks<0 {
@@ -738,11 +744,13 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
738744

739745
corsBehavior:=template.CorsBehavior
740746
ifreq.CORSBehavior!=nil&&*req.CORSBehavior!="" {
741-
val:=req.CORSBehavior
742-
iferr:=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
typeCORSBehaviorstring
84

95
const (
106
CORSBehaviorSimpleCORSBehavior="simple"
117
CORSBehaviorPassthruCORSBehavior="passthru"
128
)
13-
14-
func (cCORSBehavior)Validate()error {
15-
ifc!=CORSBehaviorSimple&&c!=CORSBehaviorPassthru {
16-
returnxerrors.New("Invalid CORS behavior.")
17-
}
18-
returnnil
19-
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp