Skip to content

Commit

Permalink
Validate json content before parsing (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Mar 20, 2023
1 parent 7c7d70b commit 0a65ebd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]


### Changed

### Added

## [0.8.3] - 2023-03-20

### Added

- Validates json content before parsing.

## [0.8.2] - 2023-03-01

### Added
Expand Down
3 changes: 3 additions & 0 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func NewJsonParseNode(content []byte) (*JsonParseNode, error) {
if len(content) == 0 {
return nil, errors.New("content is empty")
}
if !json.Valid(content) {
return nil, errors.New("invalid json type")
}
decoder := json.NewDecoder(bytes.NewReader(content))
value, err := loadJsonTree(decoder)
return value, err
Expand Down
9 changes: 9 additions & 0 deletions json_parse_node_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ func TestJsonParseNodeFactoryHonoursInterface(t *testing.T) {
instance := NewJsonParseNodeFactory()
assert.Implements(t, (*absser.ParseNodeFactory)(nil), instance)
}

func TestInvalidContentShouldFail(t *testing.T) {
source := "3 [ }"
sourceArray := []byte(source)

parseNode, err := NewJsonParseNode(sourceArray)
assert.Error(t, err)
assert.Nil(t, parseNode)
}

0 comments on commit 0a65ebd

Please sign in to comment.