From e7b98dc7613131c87928ccf1bfe0886a64f0b802 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 12 Jun 2024 11:30:37 -0400 Subject: [PATCH] Fix `validate` not checking the metaschema without `-m` given no instances See: https://github.com/asyncapi/spec-json-schemas/pull/549 Signed-off-by: Juan Cruz Viotti --- src/command_validate.cc | 3 +- test/CMakeLists.txt | 1 + ...ate_fail_only_metaschema_without_option.sh | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 test/validate_fail_only_metaschema_without_option.sh diff --git a/src/command_validate.cc b/src/command_validate.cc index dde0fa04..634b2178 100644 --- a/src/command_validate.cc +++ b/src/command_validate.cc @@ -22,7 +22,8 @@ auto intelligence::jsonschema::cli::validate( const auto schema{sourcemeta::jsontoolkit::from_file(schema_path)}; - if (options.contains("m") || options.contains("metaschema")) { + if (options.contains("m") || options.contains("metaschema") || + options.at("").size() < 2) { const auto metaschema_template{sourcemeta::jsontoolkit::compile( sourcemeta::jsontoolkit::metaschema(schema, custom_resolver), sourcemeta::jsontoolkit::default_schema_walker, custom_resolver, diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9b2dd753..90bf2666 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,6 +34,7 @@ add_jsonschema_test_unix(validate_non_supported) add_jsonschema_test_unix(validate_pass_with_metaschema) add_jsonschema_test_unix(validate_pass_only_metaschema) add_jsonschema_test_unix(validate_fail_only_metaschema) +add_jsonschema_test_unix(validate_fail_only_metaschema_without_option) add_jsonschema_test_unix(bundle_non_remote) add_jsonschema_test_unix(bundle_remote_single_schema) add_jsonschema_test_unix(bundle_remote_no_http) diff --git a/test/validate_fail_only_metaschema_without_option.sh b/test/validate_fail_only_metaschema_without_option.sh new file mode 100755 index 00000000..279e829d --- /dev/null +++ b/test/validate_fail_only_metaschema_without_option.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +cat << 'EOF' > "$TMP/schema.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "properties": { + "foo": { + "type": 1 + } + } +} +EOF + +"$1" validate "$TMP/schema.json" && CODE="$?" || CODE="$?" + +if [ "$CODE" = "0" ] +then + echo "FAIL" 1>&2 + exit 1 +else + echo "PASS" 1>&2 +fi