-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: panic on oneof validation (#86)
* test for illustration * add error on empty oneoff * add required check for validation * use custom validator only for nilable case
- Loading branch information
Showing
15 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...rs/models/testdata/cases/oneof_mapped_discriminator/expected/container_validation_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package generatortest | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContainerValidation(t *testing.T) { | ||
t.Run("validation succeeds for missing property", func(t *testing.T) { | ||
rawBytes := `{}` | ||
var container Container | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("validation succeeds for valid property", func(t *testing.T) { | ||
rawBytes := `{"error":{"kind": "external", "service": "", "traceId": "12345"}}` | ||
var container Container | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("validation fails for invalid property", func(t *testing.T) { | ||
rawBytes := `{"error":{"kind": "external", "service": "", "traceId": ""}}` | ||
var container Container | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.EqualError(t, err, "error: (traceId: cannot be blank.).") | ||
}) | ||
} | ||
|
||
func TestNonEmptyContainerValidation(t *testing.T) { | ||
t.Run("validation fails for empty payload", func(t *testing.T) { | ||
rawBytes := `{}` | ||
var container NonEmptyContainer | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.Equal(t, err, validation.Errors{ | ||
"error": EmptyErrorError, | ||
}) | ||
}) | ||
|
||
t.Run("validation succeeds for valid property", func(t *testing.T) { | ||
rawBytes := `{"error":{"kind": "external", "service": "", "traceId": "12345"}}` | ||
var container NonEmptyContainer | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("validation succeeds for valid property", func(t *testing.T) { | ||
rawBytes := `{"error":{"kind": "external", "service": "", "traceId": ""}}` | ||
var container NonEmptyContainer | ||
|
||
err := json.Unmarshal([]byte(rawBytes), &container) | ||
require.NoError(t, err) | ||
|
||
err = container.Validate() | ||
require.EqualError(t, err, "error: (traceId: cannot be blank.).") | ||
}) | ||
} |
38 changes: 38 additions & 0 deletions
38
pkg/generators/models/testdata/cases/oneof_mapped_discriminator/expected/model_container.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// This file is auto-generated, DO NOT EDIT. | ||
// | ||
// Source: | ||
// | ||
// Title: Test oneOf discriminator support | ||
// Version: 0.1.0 | ||
package generatortest | ||
|
||
import ( | ||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
) | ||
|
||
// Container is an object. | ||
type Container struct { | ||
// Error: | ||
Error Error `json:"error,omitempty"` | ||
} | ||
|
||
// Validate implements basic validation for this model | ||
func (m Container) Validate() error { | ||
return validation.Errors{ | ||
"error": validation.Validate( | ||
m.Error, | ||
ErrorNilableRule{}, | ||
validation.Skip, | ||
), | ||
}.Filter() | ||
} | ||
|
||
// GetError returns the Error property | ||
func (m Container) GetError() Error { | ||
return m.Error | ||
} | ||
|
||
// SetError sets the Error property | ||
func (m *Container) SetError(val Error) { | ||
m.Error = val | ||
} |
Oops, something went wrong.