Skip to content

Commit 5ed032f

Browse files
authored
Simplify and fix some proto validations (#57)
* Simplify and fix some proto validations * Simplify and fix some proto validations
1 parent 04c0d4c commit 5ed032f

File tree

19 files changed

+258
-208
lines changed

19 files changed

+258
-208
lines changed

doc/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ <h2>Table of Contents</h2>
299299
<a href="#metalstack%2fapi%2fv2%2fpredefined_rules.proto-extensions"><span class="badge">X</span>File-level Extensions</a>
300300
</li>
301301

302+
<li>
303+
<a href="#metalstack%2fapi%2fv2%2fpredefined_rules.proto-extensions"><span class="badge">X</span>File-level Extensions</a>
304+
</li>
305+
302306

303307
</ul>
304308
</li>
@@ -2519,6 +2523,14 @@ <h3 id="metalstack/api/v2/predefined_rules.proto-extensions">File-level Extensio
25192523
<td><p>IsPrefix validates if the given string is a valid prefix</p></td>
25202524
</tr>
25212525

2526+
<tr>
2527+
<td>is_uri</td>
2528+
<td><a href="#bool">bool</a></td>
2529+
<td><a href="#buf.validate.StringRules">.buf.validate.StringRules</a></td>
2530+
<td>80048956</td>
2531+
<td><p>IsUri validates if the given string is a valid URI</p></td>
2532+
</tr>
2533+
25222534
<tr>
25232535
<td>macaddress</td>
25242536
<td><a href="#bool">bool</a></td>

go/metalstack/admin/v2/image.pb.go

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/metalstack/api/v2/image.pb.go

Lines changed: 8 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/metalstack/api/v2/partition.pb.go

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/metalstack/api/v2/predefined_rules.pb.go

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/metalstack/api/v2/token.pb.go

Lines changed: 8 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/validation/image_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"testing"
55

66
"buf.build/go/protovalidate"
7+
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
78
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
89
"github.com/stretchr/testify/require"
10+
"google.golang.org/protobuf/proto"
911
)
1012

1113
func TestValidateImage(t *testing.T) {
@@ -32,7 +34,7 @@ func TestValidateImage(t *testing.T) {
3234
},
3335
wantErr: true,
3436
wantErrorMessage: `validation error:
35-
- url: url must be a valid URI [valid_url]`,
37+
- url: given uri must be valid [string.uri]`,
3638
},
3739
{
3840
name: "Invalid Image, no features",
@@ -43,7 +45,44 @@ func TestValidateImage(t *testing.T) {
4345
},
4446
wantErr: true,
4547
wantErrorMessage: `validation error:
46-
- features[0]: feature must be valid [features]`,
48+
- features[0]: value must be one of the defined enum values [enum.defined_only]`,
49+
},
50+
{
51+
name: "Valid ImageUpdate minimal config",
52+
msg: &adminv2.ImageServiceUpdateRequest{
53+
Id: "debian:12.0.20250101",
54+
Name: proto.String("debian"),
55+
UpdateMeta: &apiv2.UpdateMeta{},
56+
Features: []apiv2.ImageFeature{apiv2.ImageFeature_IMAGE_FEATURE_MACHINE},
57+
Classification: apiv2.ImageClassification_IMAGE_CLASSIFICATION_PREVIEW,
58+
},
59+
wantErr: false,
60+
},
61+
{
62+
name: "InValid ImageUpdate duplicate Features",
63+
msg: &adminv2.ImageServiceUpdateRequest{
64+
Id: "debian:12.0.20250101",
65+
Name: proto.String("debian"),
66+
UpdateMeta: &apiv2.UpdateMeta{},
67+
Features: []apiv2.ImageFeature{apiv2.ImageFeature_IMAGE_FEATURE_MACHINE, apiv2.ImageFeature_IMAGE_FEATURE_MACHINE},
68+
Classification: apiv2.ImageClassification_IMAGE_CLASSIFICATION_PREVIEW,
69+
},
70+
wantErr: true,
71+
wantErrorMessage: `validation error:
72+
- features: repeated value must contain unique items [repeated.unique]`,
73+
},
74+
{
75+
name: "InValid ImageUpdate invalid Features",
76+
msg: &adminv2.ImageServiceUpdateRequest{
77+
Id: "debian:12.0.20250101",
78+
Name: proto.String("debian"),
79+
UpdateMeta: &apiv2.UpdateMeta{},
80+
Features: []apiv2.ImageFeature{apiv2.ImageFeature_IMAGE_FEATURE_MACHINE, 3},
81+
Classification: apiv2.ImageClassification_IMAGE_CLASSIFICATION_PREVIEW,
82+
},
83+
wantErr: true,
84+
wantErrorMessage: `validation error:
85+
- features[1]: value must be one of the defined enum values [enum.defined_only]`,
4786
},
4887
}
4988

go/tests/validation/token_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package validation
2+
3+
import (
4+
"testing"
5+
6+
"buf.build/go/protovalidate"
7+
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestValidateToken(t *testing.T) {
12+
validator, err := protovalidate.New()
13+
require.NoError(t, err)
14+
15+
tests := prototests{
16+
{
17+
name: "Valid Token Create Request",
18+
msg: &apiv2.TokenServiceCreateRequest{
19+
Description: "A Token",
20+
ProjectRoles: map[string]apiv2.ProjectRole{
21+
"p12": apiv2.ProjectRole_PROJECT_ROLE_EDITOR,
22+
"p22": apiv2.ProjectRole_PROJECT_ROLE_VIEWER,
23+
"p32": apiv2.ProjectRole_PROJECT_ROLE_OWNER,
24+
},
25+
TenantRoles: map[string]apiv2.TenantRole{
26+
"t12": apiv2.TenantRole_TENANT_ROLE_EDITOR,
27+
},
28+
},
29+
wantErr: false,
30+
},
31+
{
32+
name: "InValid Token Create Request",
33+
msg: &apiv2.TokenServiceCreateRequest{
34+
Description: "B Token",
35+
ProjectRoles: map[string]apiv2.ProjectRole{
36+
"p42": 5,
37+
},
38+
},
39+
wantErr: true,
40+
wantErrorMessage: `validation error:
41+
- project_roles["p42"]: value must be one of the defined enum values [enum.defined_only]`,
42+
},
43+
}
44+
45+
validateProtos(t, tests, validator)
46+
}

proto/metalstack/admin/v2/image.proto

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,16 @@ message ImageServiceUpdateRequest {
5454
// UpdateMeta contains the timestamp and strategy to be used in this update request
5555
metalstack.api.v2.UpdateMeta update_meta = 2 [(buf.validate.field).required = true];
5656
// URL where this image is located
57-
optional string url = 3 [(buf.validate.field).cel = {
58-
id: "valid_url"
59-
message: "url must be a valid URI"
60-
// `isUri` validates that a string is an absolute URI.
61-
// This expression validates that the uri field is an absolute URI.
62-
// Note: to allow relative URI, use `isUriRef`.
63-
expression: "this.isUri()"
64-
}];
57+
optional string url = 3 [(buf.validate.field).string.(metalstack.api.v2.is_uri) = true];
6558
// Name of this imageLayout
6659
optional string name = 4 [(buf.validate.field).string.(metalstack.api.v2.is_name) = true];
6760
// Description of this imageLayout
6861
optional string description = 5 [(buf.validate.field).string.(metalstack.api.v2.is_description) = true];
6962
// Features of this image
7063
repeated metalstack.api.v2.ImageFeature features = 6 [(buf.validate.field).repeated = {
71-
min_items: 1
64+
unique: true
7265
items: {
73-
cel: [
74-
{
75-
id: "features"
76-
message: "feature must be valid"
77-
expression: "this >= 0 && this <= 2"
78-
}
79-
]
66+
enum: {defined_only: true}
8067
}
8168
}];
8269
// Classification of this image

0 commit comments

Comments
 (0)