Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contrib/compile.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonpointer.h>
#include <sourcemeta/core/jsonschema.h>
#include <sourcemeta/core/options.h>

#include <chrono> // std::chrono
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions test/evaluator/evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading