Skip to content

Commit 7842cee

Browse files
authored
Make validate report exit code 2 on schema validation failure (#215)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 8dce649 commit 7842cee

15 files changed

+20
-14
lines changed

docs/validate.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ information on unsuccessful validation.
1818
**If you want to validate that a schema adheres to its metaschema, use the
1919
[`metaschema`](./metaschema.markdown) command instead.**
2020

21+
To help scripts distinguish validation errors, these are reported using exit
22+
code 2.
23+
2124
Examples
2225
--------
2326

src/command_validate.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,8 @@ auto sourcemeta::jsonschema::cli::validate(
178178
}
179179
}
180180

181-
return result ? EXIT_SUCCESS : EXIT_FAILURE;
181+
return result ? EXIT_SUCCESS
182+
// Report a different exit code for validation failures, to
183+
// distinguish them from other errors
184+
: 2;
182185
}

test/validate/fail_2019_09.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EOF
2525

2626
"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
2727
&& CODE="$?" || CODE="$?"
28-
test "$CODE" = "1" || exit 1
28+
test "$CODE" = "2" || exit 1
2929

3030
cat << EOF > "$TMP/expected.txt"
3131
fail: $(realpath "$TMP")/instance.json

test/validate/fail_2020_12.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EOF
2525

2626
"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
2727
&& CODE="$?" || CODE="$?"
28-
test "$CODE" = "1" || exit 1
28+
test "$CODE" = "2" || exit 1
2929

3030
cat << EOF > "$TMP/expected.txt"
3131
fail: $(realpath "$TMP")/instance.json

test/validate/fail_draft4.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EOF
2525

2626
"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
2727
&& CODE="$?" || CODE="$?"
28-
test "$CODE" = "1" || exit 1
28+
test "$CODE" = "2" || exit 1
2929

3030
cat << EOF > "$TMP/expected.txt"
3131
fail: $(realpath "$TMP")/instance.json

test/validate/fail_draft6.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EOF
2525

2626
"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
2727
&& CODE="$?" || CODE="$?"
28-
test "$CODE" = "1" || exit 1
28+
test "$CODE" = "2" || exit 1
2929

3030
cat << EOF > "$TMP/expected.txt"
3131
fail: $(realpath "$TMP")/instance.json

test/validate/fail_draft7.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ EOF
2525

2626
"$1" validate "$TMP/schema.json" "$TMP/instance.json" 2> "$TMP/stderr.txt" \
2727
&& CODE="$?" || CODE="$?"
28-
test "$CODE" = "1" || exit 1
28+
test "$CODE" = "2" || exit 1
2929

3030
cat << EOF > "$TMP/expected.txt"
3131
fail: $(realpath "$TMP")/instance.json

test/validate/fail_jsonl_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ EOF
2222

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

2727
cat << EOF > "$TMP/expected.txt"
2828
fail: $(realpath "$TMP")/instance.jsonl (entry #1)

test/validate/fail_jsonl_all_verbose.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ EOF
2222

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

2727
cat << EOF > "$TMP/expected.txt"
2828
Interpreting input as JSONL: $(realpath "$TMP")/instance.jsonl

test/validate/fail_jsonl_one.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ EOF
2222

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

2727
cat << EOF > "$TMP/expected.txt"
2828
fail: $(realpath "$TMP")/instance.jsonl (entry #2)

0 commit comments

Comments
 (0)