-
Notifications
You must be signed in to change notification settings - Fork 54
[feature] Added basic validation for parameters #456
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package validators | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| "realm.pub/tavern/tomes" | ||
| ) | ||
|
|
||
| // NewJSONStringString returns a validator that errors if the string field has a value that cannot be JSON unmarshalled to a map[string]string. | ||
| func NewJSONStringString() func(string) error { | ||
| return func(data string) error { | ||
| if data == "" { | ||
| return nil | ||
| } | ||
| var dataMap map[string]string | ||
| return json.Unmarshal([]byte(data), &dataMap) | ||
| } | ||
| } | ||
|
|
||
| // NewTomeParameterDefinitions returns a validator that errors if the string field has a value that cannot be JSON unmarshalled to a []tomes.TomeParamDefinition. | ||
| func NewTomeParameterDefinitions() func(string) error { | ||
| return func(data string) error { | ||
| if data == "" { | ||
| return nil | ||
| } | ||
| var paramDefs []tomes.ParamDefinition | ||
| if err := json.Unmarshal([]byte(data), ¶mDefs); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Validate parameters | ||
| for _, paramDef := range paramDefs { | ||
| if err := paramDef.Validate(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package validators_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "realm.pub/tavern/internal/ent/schema/validators" | ||
| ) | ||
|
|
||
| func TestNewJSONStringString(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| data string | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "Empty", | ||
| data: ``, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Valid", | ||
| data: `{"data":"stuff"}`, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Invalid", | ||
| data: `blah`, | ||
| wantErr: &json.SyntaxError{}, | ||
| }, | ||
| { | ||
| name: "Partial", | ||
| data: `{"blah":"stuff"`, | ||
| wantErr: &json.SyntaxError{}, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := validators.NewJSONStringString()(tc.data) | ||
| if tc.wantErr == nil { | ||
| assert.NoError(t, err) | ||
| return | ||
| } | ||
| assert.ErrorAs(t, err, &tc.wantErr) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNewTomeParameterDefinitions(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| data string | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "Empty", | ||
| data: ``, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Int32", | ||
| data: `[{"name":"an-int","type": "int32"}]`, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Multiple", | ||
| data: `[{"name":"an-int","type":"int32"},{"name":"a-str","type": "string"}]`, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Valid", | ||
| data: `[{"name":"stuff","type":"string"}]`, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "Invalid", | ||
| data: `blah`, | ||
| wantErr: &json.SyntaxError{}, | ||
| }, | ||
| { | ||
| name: "Partial", | ||
| data: `{"blah":"stuff"`, | ||
| wantErr: &json.SyntaxError{}, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := validators.NewTomeParameterDefinitions()(tc.data) | ||
| if tc.wantErr == nil { | ||
| assert.NoError(t, err) | ||
| return | ||
| } | ||
| assert.ErrorAs(t, err, &tc.wantErr) | ||
| }) | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually support int type in params?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No not yet