Skip to content

Commit

Permalink
Make metaschema report exit code 2 on schema validation failure (#217)
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 7842cee commit e68d245
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/metaschema.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jsonschema metaschema [schemas-or-directories...]
Ensure that a schema or a set of schemas are considered valid with regards to
their metaschemas.

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

Examples
--------

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

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/metaschema/fail_directory.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cat << 'EOF' > "$TMP/schemas/schema_2.json"
EOF

"$1" metaschema "$TMP/schemas" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1
test "$CODE" = "2" || exit 1

cat << EOF > "$TMP/expected.txt"
fail: $(realpath "$TMP")/schemas/schema_2.json
Expand Down
2 changes: 1 addition & 1 deletion test/metaschema/fail_single.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
EOF

"$1" metaschema "$TMP/schema.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")/schema.json
Expand Down
2 changes: 1 addition & 1 deletion test/metaschema/fail_trace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ EOF

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

# Order of execution can vary

Expand Down
2 changes: 1 addition & 1 deletion test/metaschema/fail_yaml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type: 1
EOF

"$1" metaschema "$TMP/schema.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")/schema.yaml
Expand Down

0 comments on commit e68d245

Please sign in to comment.