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

feat: use validation error while parsingcoder_parameter#86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 2 commits intocoder:mainfrommtojek:5574-validation-rules-fix-1
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionexamples/resources/coder_app/resource.tf
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,7 @@ resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
slug = "code-server"
display_name = "VS Code"
icon = data.coder_workspace.me.access_url + "/icon/code.svg"
icon ="${data.coder_workspace.me.access_url}/icon/code.svg"
url = "http://localhost:13337"
share = "owner"
subdomain = false
Expand Down
3 changes: 3 additions & 0 deletionsexamples/resources/coder_parameter/resource.tf
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,16 +28,19 @@ data "coder_parameter" "is_public_instance" {
name = "Is public instance?"
icon = "/icon/docker.svg"
type = "bool"
default = false
}

data "coder_parameter" "cores" {
name = "CPU Cores"
icon = "/icon/"
default = 3
}

data "coder_parameter" "disk_size" {
name = "Disk Size"
type = "number"
default = "9"
validation {
# This can apply to number and string types.
min = 0
Expand Down
33 changes: 33 additions & 0 deletionsprovider/examples_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
package provider_test

import (
"os"
"testing"

"github.com/coder/terraform-provider-coder/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/require"
)

func TestExamples(t *testing.T) {
t.Parallel()

t.Run("coder_parameter", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: mustReadFile(t, "../examples/resources/coder_parameter/resource.tf"),
}},
})
})
}

func mustReadFile(t *testing.T, path string) string {
content, err := os.ReadFile(path)
require.NoError(t, err)
return string(content)
}
8 changes: 4 additions & 4 deletionsprovider/parameter.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -300,13 +300,13 @@ func (v *Validation) Valid(typ, value string) error {
if err != nil {
return fmt.Errorf("compile regex %q: %s", regex, err)
}
matched := regex.MatchString(value)
if !matched {
return fmt.Errorf("value %q does not match %q", value, regex)
}
if v.Error == "" {
return fmt.Errorf("an error must be specified with a regex validation")
}
matched := regex.MatchString(value)
if !matched {
return fmt.Errorf("%s (value %q does not match %q)", v.Error, value, regex)
}
case "number":
num, err := strconv.Atoi(value)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletionprovider/parameter_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -267,7 +267,8 @@ func TestValueValidatesType(t *testing.T) {
Name,
Type,
Value,
Regex string
Regex,
RegexError string
Min,
Max int
Error *regexp.Regexp
Expand DownExpand Up@@ -312,6 +313,13 @@ func TestValueValidatesType(t *testing.T) {
Type: "bool",
Value: "cat",
Error: regexp.MustCompile("boolean value can be either"),
}, {
Name: "BadStringWithRegex",
Type: "string",
Regex: "banana",
RegexError: "bad fruit",
Value: "apple",
Error: regexp.MustCompile(`bad fruit`),
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand All@@ -320,6 +328,7 @@ func TestValueValidatesType(t *testing.T) {
Min: tc.Min,
Max: tc.Max,
Regex: tc.Regex,
Error: tc.RegexError,
}
err := v.Valid(tc.Type, tc.Value)
if tc.Error != nil {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp