Skip to content

Commit

Permalink
Make validate report exit code 2 on schema validation failure (#215)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 22, 2025
1 parent 8dce649 commit 7842cee
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/validate.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ information on unsuccessful validation.
**If you want to validate that a schema adheres to its metaschema, use the
[`metaschema`](./metaschema.markdown) command instead.**

To help scripts distinguish validation errors, these are reported using exit
code 2.

Examples
--------

Expand Down
5 changes: 4 additions & 1 deletion src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,8 @@ auto sourcemeta::jsonschema::cli::validate(
}
}

return result ? EXIT_SUCCESS : EXIT_FAILURE;
return result ? EXIT_SUCCESS
// Report a different exit code for validation failures, to
// distinguish them from other errors
: 2;
}
2 changes: 1 addition & 1 deletion test/validate/fail_2019_09.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_2020_12.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_draft4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_draft6.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_draft7.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_jsonl_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.jsonl" 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.jsonl (entry #1)
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_jsonl_all_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.jsonl" --verbose 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
Interpreting input as JSONL: $(realpath "$TMP")/instance.jsonl
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_jsonl_one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.jsonl" 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.jsonl (entry #2)
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_jsonl_one_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.jsonl" --verbose 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
Interpreting input as JSONL: $(realpath "$TMP")/instance.jsonl
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_many.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EOF
"$TMP/instance_3.json" \
2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance_2.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_many_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EOF
"$TMP/instance_3.json" \
--verbose 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
ok: $(realpath "$TMP")/instance_1.json
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_trace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ EOF

"$1" validate "$TMP/schema.json" "$TMP/instance.json" --trace > "$TMP/output.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
-> (push) "/properties/foo/type"
Expand Down
2 changes: 1 addition & 1 deletion test/validate/fail_yaml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ EOF

"$1" validate "$TMP/schema.yaml" "$TMP/instance.yaml" 2> "$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/instance.yaml
Expand Down

0 comments on commit 7842cee

Please sign in to comment.