Skip to content

Commit a8619e3

Browse files
authoredMar 10, 2025··
fix(validation tags): handle spaces in enum values (#268)
This commit introduces management of spaces within enum values when using validation tags. Spaces are handled by adding single quotes around the enum value Relates to #267
1 parent 3efdc5b commit a8619e3

15 files changed

+1183
-9
lines changed
 

‎pkg/codegen/generators/helpers.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ func GenerateValidateTags[T any](schema asyncapi.Validations[T], isPointer bool,
6666
}
6767
}
6868

69+
// singleQuote prepends and appends a single quote to the provided string.
70+
func singleQuote(s string) string {
71+
return "'" + s + "'"
72+
}
73+
6974
func appendEnumDirectives[T any](schema asyncapi.Validations[T], directives []string) []string {
7075
if len(schema.Enum) > 0 {
7176
var enumsStr []string
7277
for _, e := range schema.Enum {
7378
if eStr, ok := e.(string); ok {
74-
enumsStr = append(enumsStr, eStr)
79+
// single quotes are mandatory in order to handle values with spaces
80+
enumsStr = append(enumsStr, singleQuote(eStr))
7581
}
7682
}
7783

‎test/v2/issues/131/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v2/issues/137/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v2/issues/137/suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func TestValidateOneOfExist(t *testing.T) {
2424
}
2525
}
2626

27-
assert.Equal(t, "oneof=API0 API1 API2 API3 API4", oneOfTag)
27+
assert.Equal(t, "oneof='API0' 'API1' 'API2' 'API3' 'API4'", oneOfTag)
2828
}

‎test/v2/issues/245/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v2/issues/267/asyncapi.gen.go

+507
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v2/issues/267/asyncapi.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
asyncapi: 2.6.0
2+
info:
3+
title: Sample App
4+
version: 1.2.3
5+
6+
channels:
7+
v2.issue267.test:
8+
subscribe:
9+
message:
10+
payload:
11+
$ref: '#/components/schemas/Test'
12+
13+
components:
14+
schemas:
15+
Test:
16+
type: object
17+
required:
18+
- EnumProp
19+
properties:
20+
EnumProp:
21+
type: string
22+
enum:
23+
- "nospaces"
24+
- "has a space"

‎test/v2/issues/267/suite_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:generate go run ../../../../cmd/asyncapi-codegen -p issue267 -i ./asyncapi.yaml -o ./asyncapi.gen.go
2+
3+
package issue267
4+
5+
import (
6+
"encoding/json"
7+
"testing"
8+
9+
"github.com/go-playground/validator/v10"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/suite"
12+
)
13+
14+
func TestSuite(t *testing.T) {
15+
suite.Run(t, NewSuite())
16+
}
17+
18+
type Suite struct {
19+
suite.Suite
20+
}
21+
22+
func NewSuite() *Suite {
23+
return &Suite{}
24+
}
25+
26+
func (suite *Suite) TestValidate_Valid() {
27+
var res TestSchema
28+
err := json.Unmarshal([]byte(`{
29+
"EnumProp": "has a space"
30+
}`), &res)
31+
assert.NoError(suite.T(), err)
32+
err = validator.New().Struct(res)
33+
assert.NoError(suite.T(), err)
34+
}
35+
36+
func (suite *Suite) TestValidate_Invalid() {
37+
var res TestSchema
38+
err := json.Unmarshal([]byte(`{
39+
"EnumProp": "nospace"
40+
}`), &res)
41+
assert.NoError(suite.T(), err)
42+
err = validator.New().Struct(res)
43+
assert.Error(suite.T(), err)
44+
}

‎test/v3/issues/131/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v3/issues/137/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v3/issues/137/suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func TestValidateOneOfExist(t *testing.T) {
2424
}
2525
}
2626

27-
assert.Equal(t, "oneof=API0 API1 API2 API3 API4", oneOfTag)
27+
assert.Equal(t, "oneof='API0' 'API1' 'API2' 'API3' 'API4'", oneOfTag)
2828
}

‎test/v3/issues/245/asyncapi.gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v3/issues/267/asyncapi.gen.go

+518
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/v3/issues/267/asyncapi.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
asyncapi: 3.0.0
2+
3+
channels:
4+
test:
5+
address: v3.issue267.test
6+
messages:
7+
test:
8+
payload:
9+
$ref: '#/components/schemas/Test'
10+
11+
operations:
12+
receiveTest:
13+
action: receive
14+
channel:
15+
$ref: '#/channels/test'
16+
17+
info:
18+
title: Test
19+
version: 1.0.0
20+
components:
21+
schemas:
22+
Test:
23+
type: object
24+
required:
25+
- EnumProp
26+
properties:
27+
EnumProp:
28+
type: string
29+
enum:
30+
- "nospaces"
31+
- "has a space"

‎test/v3/issues/267/suite_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:generate go run ../../../../cmd/asyncapi-codegen -p issue267 -i ./asyncapi.yaml -o ./asyncapi.gen.go
2+
3+
package issue267
4+
5+
import (
6+
"encoding/json"
7+
"testing"
8+
9+
"github.com/go-playground/validator/v10"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/suite"
12+
)
13+
14+
func TestSuite(t *testing.T) {
15+
suite.Run(t, NewSuite())
16+
}
17+
18+
type Suite struct {
19+
suite.Suite
20+
}
21+
22+
func NewSuite() *Suite {
23+
return &Suite{}
24+
}
25+
26+
func (suite *Suite) TestValidate_Valid() {
27+
var res TestSchema
28+
err := json.Unmarshal([]byte(`{
29+
"EnumProp": "has a space"
30+
}`), &res)
31+
assert.NoError(suite.T(), err)
32+
err = validator.New().Struct(res)
33+
assert.NoError(suite.T(), err)
34+
}
35+
36+
func (suite *Suite) TestValidate_Invalid() {
37+
var res TestSchema
38+
err := json.Unmarshal([]byte(`{
39+
"EnumProp": "nospace"
40+
}`), &res)
41+
assert.NoError(suite.T(), err)
42+
err = validator.New().Struct(res)
43+
assert.Error(suite.T(), err)
44+
}

0 commit comments

Comments
 (0)
Please sign in to comment.