diff --git a/contrib/compile.cc b/contrib/compile.cc index a98941e7..dba72328 100644 --- a/contrib/compile.cc +++ b/contrib/compile.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include // std::chrono @@ -145,6 +146,20 @@ auto main(int argc, char **argv) noexcept -> int { document = std::move(extracted); } + // The compiler assumes valid schema input (object or boolean) and + // asserts otherwise. Guard here at the CLI boundary to ensure + // graceful failure instead of aborting on malformed inputs. + if (!sourcemeta::core::is_schema(document)) { + if (options.contains("path")) { + std::cerr << "error: the value at the given path is not a valid " + "JSON Schema\n"; + } else { + std::cerr << "error: the input is not a valid JSON Schema\n"; + } + + return EXIT_FAILURE; + } + const auto compile_start{std::chrono::high_resolution_clock::now()}; const auto schema_template{sourcemeta::blaze::compile( document, sourcemeta::core::schema_walker, resolver, diff --git a/test/evaluator/evaluator_test.cc b/test/evaluator/evaluator_test.cc index 3f5c866e..b83ae76b 100644 --- a/test/evaluator/evaluator_test.cc +++ b/test/evaluator/evaluator_test.cc @@ -314,3 +314,30 @@ TEST(Evaluator, format_assertion_vocabulary_unsupported) { FAIL() << "The compile function was expected to throw a vocabulary error"; } } + +TEST(Evaluator, invalid_additional_properties_type) { + const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "additionalProperties": 42 + })JSON")}; + + EXPECT_THROW( + sourcemeta::blaze::compile(schema, sourcemeta::core::schema_walker, + sourcemeta::core::schema_resolver, + sourcemeta::blaze::default_schema_compiler), + sourcemeta::core::SchemaError); +} + +TEST(Evaluator, invalid_items_type) { + const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "items": "invalid" + })JSON")}; + + EXPECT_THROW( + sourcemeta::blaze::compile(schema, sourcemeta::core::schema_walker, + sourcemeta::core::schema_resolver, + sourcemeta::blaze::default_schema_compiler), + sourcemeta::core::SchemaError); +}