File tree Expand file tree Collapse file tree 5 files changed +55
-152
lines changed
internal/codersdkvalidator Expand file tree Collapse file tree 5 files changed +55
-152
lines changed Original file line number Diff line number Diff line change 1
1
package codersdkvalidator
2
2
3
3
import (
4
- "context"
5
-
6
4
"github.com/coder/coder/v2/codersdk"
7
- "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
8
5
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9
6
)
10
7
11
- type displayNameValidator struct {
12
- err error
13
- }
14
-
15
8
func DisplayName () validator.String {
16
- return displayNameValidator {}
17
- }
18
-
19
- var _ validator.String = displayNameValidator {}
20
-
21
- func (v displayNameValidator ) ValidateString (ctx context.Context , req validator.StringRequest , resp * validator.StringResponse ) {
22
- if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
23
- return
24
- }
25
-
26
- name := req .ConfigValue .ValueString ()
27
- if v .err = codersdk .DisplayNameValid (name ); v .err != nil {
28
- resp .Diagnostics .Append (validatordiag .InvalidAttributeValueDiagnostic (
29
- req .Path ,
30
- v .Description (ctx ),
31
- name ,
32
- ))
33
- }
34
- }
35
-
36
- var _ validator.Describer = displayNameValidator {}
37
-
38
- func (v displayNameValidator ) Description (_ context.Context ) string {
39
- if v .err != nil {
40
- return v .err .Error ()
41
- }
42
- return "value must be a valid display name"
43
- }
44
-
45
- func (v displayNameValidator ) MarkdownDescription (ctx context.Context ) string {
46
- return v .Description (ctx )
9
+ return validatorFromFunc (codersdk .DisplayNameValid , "value must be a valid display name" )
47
10
}
Original file line number Diff line number Diff line change 1
1
package codersdkvalidator
2
2
3
3
import (
4
- "context"
5
-
6
4
"github.com/coder/coder/v2/codersdk"
7
- "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
8
5
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9
6
)
10
7
11
- type nameValidator struct {
12
- err error
13
- }
14
-
15
8
func Name () validator.String {
16
- return nameValidator {}
17
- }
18
-
19
- var _ validator.String = nameValidator {}
20
-
21
- func (v nameValidator ) ValidateString (ctx context.Context , req validator.StringRequest , resp * validator.StringResponse ) {
22
- if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
23
- return
24
- }
25
-
26
- name := req .ConfigValue .ValueString ()
27
- if v .err = codersdk .NameValid (name ); v .err != nil {
28
- resp .Diagnostics .Append (validatordiag .InvalidAttributeValueDiagnostic (
29
- req .Path ,
30
- v .Description (ctx ),
31
- name ,
32
- ))
33
- }
34
- }
35
-
36
- var _ validator.Describer = nameValidator {}
37
-
38
- func (v nameValidator ) Description (_ context.Context ) string {
39
- if v .err != nil {
40
- return v .err .Error ()
41
- }
42
- return "value must be a valid name"
43
- }
44
-
45
- func (v nameValidator ) MarkdownDescription (ctx context.Context ) string {
46
- return v .Description (ctx )
9
+ return validatorFromFunc (codersdk .NameValid , "value must be a valid name" )
47
10
}
Original file line number Diff line number Diff line change 1
1
package codersdkvalidator
2
2
3
3
import (
4
- "context"
5
-
6
4
"github.com/coder/coder/v2/codersdk"
7
- "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
8
5
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9
6
)
10
7
11
- type templateVersionNameValidator struct {
12
- err error
13
- }
14
-
15
8
func TemplateVersionName () validator.String {
16
- return templateVersionNameValidator {}
17
- }
18
-
19
- var _ validator.String = templateVersionNameValidator {}
20
-
21
- func (v templateVersionNameValidator ) ValidateString (ctx context.Context , req validator.StringRequest , resp * validator.StringResponse ) {
22
- if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
23
- return
24
- }
25
-
26
- name := req .ConfigValue .ValueString ()
27
- if v .err = codersdk .TemplateVersionNameValid (name ); v .err != nil {
28
- resp .Diagnostics .Append (validatordiag .InvalidAttributeValueDiagnostic (
29
- req .Path ,
30
- v .Description (ctx ),
31
- name ,
32
- ))
33
- }
34
- }
35
-
36
- var _ validator.Describer = templateVersionNameValidator {}
37
-
38
- func (v templateVersionNameValidator ) Description (_ context.Context ) string {
39
- if v .err != nil {
40
- return v .err .Error ()
41
- }
42
- return "value must be a valid template version name"
43
- }
44
-
45
- func (v templateVersionNameValidator ) MarkdownDescription (ctx context.Context ) string {
46
- return v .Description (ctx )
9
+ return validatorFromFunc (codersdk .TemplateVersionNameValid , "value must be a valid template version name" )
47
10
}
Original file line number Diff line number Diff line change 1
1
package codersdkvalidator
2
2
3
3
import (
4
- "context"
5
-
6
4
"github.com/coder/coder/v2/codersdk"
7
- "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
8
5
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9
6
)
10
7
11
- type userRealNameValidator struct {
12
- err error
13
- }
14
-
15
8
func UserRealName () validator.String {
16
- return userRealNameValidator {}
17
- }
18
-
19
- var _ validator.String = userRealNameValidator {}
20
-
21
- func (v userRealNameValidator ) ValidateString (ctx context.Context , req validator.StringRequest , resp * validator.StringResponse ) {
22
- if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
23
- return
24
- }
25
-
26
- name := req .ConfigValue .ValueString ()
27
- if v .err = codersdk .UserRealNameValid (name ); v .err != nil {
28
- resp .Diagnostics .Append (validatordiag .InvalidAttributeValueDiagnostic (
29
- req .Path ,
30
- v .Description (ctx ),
31
- name ,
32
- ))
33
- }
34
- }
35
-
36
- var _ validator.Describer = userRealNameValidator {}
37
-
38
- func (v userRealNameValidator ) Description (_ context.Context ) string {
39
- if v .err != nil {
40
- return v .err .Error ()
41
- }
42
- return "value must be a valid name for a user"
43
- }
44
-
45
- func (v userRealNameValidator ) MarkdownDescription (ctx context.Context ) string {
46
- return v .Description (ctx )
9
+ return validatorFromFunc (codersdk .UserRealNameValid , "value must be a valid name for a user" )
47
10
}
Original file line number Diff line number Diff line change
1
+ package codersdkvalidator
2
+
3
+ import (
4
+ "context"
5
+
6
+ "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
7
+ "github.com/hashicorp/terraform-plugin-framework/schema/validator"
8
+ )
9
+
10
+ type functionValidator struct {
11
+ check func (string ) error
12
+ defaultMessage string
13
+ err error
14
+ }
15
+
16
+ func validatorFromFunc (check func (string ) error , defaultMessage string ) functionValidator {
17
+ return functionValidator {
18
+ check : check ,
19
+ defaultMessage : defaultMessage ,
20
+ }
21
+ }
22
+
23
+ var _ validator.String = functionValidator {}
24
+
25
+ func (v functionValidator ) ValidateString (ctx context.Context , req validator.StringRequest , resp * validator.StringResponse ) {
26
+ if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
27
+ return
28
+ }
29
+
30
+ name := req .ConfigValue .ValueString ()
31
+ if v .err = v .check (name ); v .err != nil {
32
+ resp .Diagnostics .Append (validatordiag .InvalidAttributeValueDiagnostic (
33
+ req .Path ,
34
+ v .Description (ctx ),
35
+ name ,
36
+ ))
37
+ }
38
+ }
39
+
40
+ var _ validator.Describer = functionValidator {}
41
+
42
+ func (v functionValidator ) Description (_ context.Context ) string {
43
+ if v .err != nil {
44
+ return v .err .Error ()
45
+ }
46
+ return "value must be a valid name"
47
+ }
48
+
49
+ func (v functionValidator ) MarkdownDescription (ctx context.Context ) string {
50
+ return v .Description (ctx )
51
+ }
You can’t perform that action at this time.
0 commit comments