-
Notifications
You must be signed in to change notification settings - Fork 414
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
Add and test JSON object matcher #819
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// IsJSONObjectWithTopLevelKey checks if the given bytes are a valid JSON object | ||
// with exactly one top-level key that is contained in the list of allowed keys. | ||
func IsJSONObjectWithTopLevelKey(jsonBytes []byte, allowedKeys []string) error { | ||
document := map[string]interface{}{} | ||
if err := json.Unmarshal(jsonBytes, &document); err != nil { | ||
return sdkerrors.Wrap(ErrNotAJSONObject, "failed to unmarshal JSON to map") | ||
} | ||
|
||
if len(document) == 0 { | ||
return sdkerrors.Wrap(ErrNoTopLevelKey, "JSON object has no top-level key") | ||
} | ||
|
||
if len(document) > 1 { | ||
return sdkerrors.Wrap(ErrMultipleTopLevelKeys, "JSON object has multiple top-level keys") | ||
} | ||
|
||
// Loop is executed exactly once | ||
for topLevelKey := range document { | ||
for _, allowedKey := range allowedKeys { | ||
if allowedKey == topLevelKey { | ||
return nil | ||
} | ||
} | ||
return sdkerrors.Wrapf(ErrTopKevelKeyNotAllowed, "JSON object has a top-level key which is not allowed: '%s'", topLevelKey) | ||
} | ||
|
||
panic("Reached unreachable code. This is a bug.") | ||
} |
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,115 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIsJSONObjectWithTopLevelKey(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wonderful test vectors here! |
||
specs := map[string]struct { | ||
src []byte | ||
allowedKeys []string | ||
exp error | ||
}{ | ||
"happy": { | ||
src: []byte(`{"msg": {"foo":"bar"}}`), | ||
allowedKeys: []string{"msg"}, | ||
exp: nil, | ||
}, | ||
"happy with many allowed keys 1": { | ||
src: []byte(`{"claim": {"foo":"bar"}}`), | ||
allowedKeys: []string{"claim", "swap", "burn", "mint"}, | ||
exp: nil, | ||
}, | ||
"happy with many allowed keys 2": { | ||
src: []byte(`{"burn": {"foo":"bar"}}`), | ||
allowedKeys: []string{"claim", "swap", "burn", "mint"}, | ||
exp: nil, | ||
}, | ||
"happy with many allowed keys 3": { | ||
src: []byte(`{"mint": {"foo":"bar"}}`), | ||
allowedKeys: []string{"claim", "swap", "burn", "mint"}, | ||
exp: nil, | ||
}, | ||
"happy with number": { | ||
src: []byte(`{"msg": 123}`), | ||
allowedKeys: []string{"msg"}, | ||
exp: nil, | ||
}, | ||
"happy with array": { | ||
src: []byte(`{"msg": [1, 2, 3, 4]}`), | ||
allowedKeys: []string{"msg"}, | ||
exp: nil, | ||
}, | ||
"happy with null": { | ||
src: []byte(`{"msg": null}`), | ||
allowedKeys: []string{"msg"}, | ||
exp: nil, | ||
}, | ||
"happy with whitespace": { | ||
src: []byte(`{ | ||
"msg": null }`), | ||
allowedKeys: []string{"msg"}, | ||
exp: nil, | ||
}, | ||
"happy with excaped key": { | ||
src: []byte(`{"event\u2468thing": {"foo":"bar"}}`), | ||
allowedKeys: []string{"event⑨thing"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 |
||
exp: nil, | ||
}, | ||
|
||
// Invalid JSON object | ||
"errors for bytes that are no JSON": { | ||
src: []byte(`nope`), | ||
allowedKeys: []string{"claim"}, | ||
exp: ErrNotAJSONObject, | ||
}, | ||
"errors for valid JSON (string)": { | ||
src: []byte(`"nope"`), | ||
allowedKeys: []string{"claim"}, | ||
exp: ErrNotAJSONObject, | ||
}, | ||
"errors for valid JSON (array)": { | ||
src: []byte(`[1, 2, 3]`), | ||
allowedKeys: []string{"claim"}, | ||
exp: ErrNotAJSONObject, | ||
}, | ||
|
||
// Not one top-level key | ||
"errors for no top-level key": { | ||
src: []byte(`{}`), | ||
allowedKeys: []string{"claim"}, | ||
exp: ErrNoTopLevelKey, | ||
}, | ||
"errors for multiple top-level keys": { | ||
src: []byte(`{"claim": {}, "and_swap": {}}`), | ||
allowedKeys: []string{"claim"}, | ||
exp: ErrMultipleTopLevelKeys, | ||
}, | ||
|
||
// Wrong top-level key | ||
"errors for wrong top-level key 1": { | ||
src: []byte(`{"claim": {}}`), | ||
allowedKeys: []string{""}, | ||
exp: ErrTopKevelKeyNotAllowed, | ||
}, | ||
"errors for wrong top-level key 2": { | ||
src: []byte(`{"claim": {}}`), | ||
allowedKeys: []string{"swap", "burn", "mint"}, | ||
exp: ErrTopKevelKeyNotAllowed, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
result := IsJSONObjectWithTopLevelKey(spec.src, spec.allowedKeys) | ||
if spec.exp == nil { | ||
require.NoError(t, result) | ||
} else { | ||
require.Error(t, result) | ||
require.Contains(t, result.Error(), spec.exp.Error()) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good stuff with the unique error codes