Skip to content

Commit c833538

Browse files
committed
feat: support ndjson extension using json parser
Signed-off-by: Andreas Grub <grub@posteo.de>
1 parent 43169be commit c833538

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

acceptance.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ EOF"
461461
[ "$status" -eq 0 ]
462462
}
463463

464+
@test "Can validate newline-delimited JSON log files" {
465+
run ./conftest test -p examples/ndjson/policy/ examples/ndjson/sample.ndjson
466+
[ "$status" -eq 1 ]
467+
[[ "$output" =~ "2 tests, 1 passed, 0 warnings, 1 failure, 0 exceptions" ]]
468+
}
469+
464470
@test "Should fail if strict is set and there are unused variables in the policy" {
465471
run ./conftest test -p examples/strict-rules/policy/ examples/kubernetes/deployment.yaml --strict
466472
[ "$status" -eq 1 ]

docs/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ You can find examples using various other tools in the `examples` directory, inc
2323
* [Jsonnet](https://github.com/open-policy-agent/conftest/tree/master/examples/jsonnet)
2424
* [Kubernetes](https://github.com/open-policy-agent/conftest/tree/master/examples/kubernetes)
2525
* [Kustomize](https://github.com/open-policy-agent/conftest/tree/master/examples/kustomize)
26+
* [Newline-delimited JSON](https://github.com/open-policy-agent/conftest/tree/master/examples/ndjson)
2627
* [Properties](https://github.com/open-policy-agent/conftest/tree/master/examples/properties)
2728
* [Report](https://github.com/open-policy-agent/conftest/tree/master/examples/report)
2829
* [Serverless Framework](https://github.com/open-policy-agent/conftest/tree/master/examples/serverless)

examples/ndjson/policy/logs.rego

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
import rego.v1
3+
4+
deny contains msg if {
5+
input.level == "ERROR"
6+
msg = sprintf("error log found, message '%s'", [input.message])
7+
}

examples/ndjson/sample.ndjson

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"level": "INFO", "message": "some message"}
2+
{"level": "ERROR", "message": "some failure"}

parser/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ func NewFromPath(path string) (Parser, error) {
180180
return New(Dockerfile)
181181
}
182182

183+
if fileExtension == "ndjson" {
184+
return New(JSON)
185+
}
186+
183187
if fileExtension == "yml" || fileExtension == "yaml" {
184188
return New(YAML)
185189
}

parser/parser_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func TestNewFromPath(t *testing.T) {
9090
&jsonc.Parser{},
9191
false,
9292
},
93+
{
94+
"jsonlines.ndjson",
95+
&json.Parser{},
96+
false,
97+
},
9398
{
9499
"noextension",
95100
&yaml.Parser{},

0 commit comments

Comments
 (0)