Skip to content

Commit

Permalink
Revamp the currently messy pretty validation formatted
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Aug 22, 2024
1 parent 7dce049 commit 3561995
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 54 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 7e26abce7a4e31e86a16ef2851702a56773ca527
jsontoolkit https://github.com/sourcemeta/jsontoolkit c9b844557d3b116b272be5198ec547a7eee18347
jsontoolkit https://github.com/sourcemeta/jsontoolkit 760aecee95213152bfea907c19cdaa43f616dc9b
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc
7 changes: 3 additions & 4 deletions src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ auto intelligence::jsonschema::cli::metaschema(
cache.insert({dialect.value(), metaschema_template});
}

std::ostringstream error;
sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput output{metaschema};
if (sourcemeta::jsontoolkit::evaluate(
cache.at(dialect.value()), entry.second,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error, metaschema,
sourcemeta::jsontoolkit::empty_pointer))) {
std::ref(output))) {
log_verbose(options)
<< entry.first.string()
<< ": The schema is valid with respect to its metaschema\n";
} else {
std::cerr << error.str();
print(output, std::cerr);
std::cerr << entry.first.string()
<< ": The schema is NOT valid with respect to its metaschema\n";
result = false;
Expand Down
7 changes: 4 additions & 3 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ auto intelligence::jsonschema::cli::test(
return EXIT_FAILURE;
}

std::ostringstream error;
sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput output{
schema.value(), {"$ref"}};
const auto case_result{sourcemeta::jsontoolkit::evaluate(
schema_template,
get_data(test_case, entry.first.parent_path(), verbose),
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error, schema.value(), {"$ref"}))};
std::ref(output))};

std::ostringstream test_case_description;
if (test_case.defines("description")) {
Expand Down Expand Up @@ -278,7 +279,7 @@ auto intelligence::jsonschema::cli::test(

std::cout << " " << index << "/" << total << " FAIL "
<< test_case_description.str() << "\n\n";
std::cout << error.str();
print(output, std::cout);

if (index != total && verbose) {
std::cout << "\n";
Expand Down
11 changes: 7 additions & 4 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ auto intelligence::jsonschema::cli::validate(
for (const auto &instance : sourcemeta::jsontoolkit::JSONL{stream}) {
index += 1;
std::ostringstream error;
sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput output{
instance};
bool subresult = true;
if (benchmark) {
const auto timestamp_start{
Expand All @@ -88,8 +90,7 @@ auto intelligence::jsonschema::cli::validate(
subresult = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(
error, instance, sourcemeta::jsontoolkit::empty_pointer));
std::ref(output));
}

if (subresult) {
Expand All @@ -108,6 +109,7 @@ auto intelligence::jsonschema::cli::validate(
sourcemeta::jsontoolkit::prettify(instance, std::cerr);
std::cerr << "\n\n";
std::cerr << error.str();
print(output, std::cerr);
result = false;
break;
}
Expand All @@ -123,6 +125,7 @@ auto intelligence::jsonschema::cli::validate(
} else {
const auto instance{sourcemeta::jsontoolkit::from_file(instance_path)};
std::ostringstream error;
sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput output{instance};
bool subresult{true};
if (benchmark) {
const auto timestamp_start{std::chrono::high_resolution_clock::now()};
Expand All @@ -142,8 +145,7 @@ auto intelligence::jsonschema::cli::validate(
subresult = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error, instance,
sourcemeta::jsontoolkit::empty_pointer));
std::ref(output));
}

if (subresult) {
Expand All @@ -157,6 +159,7 @@ auto intelligence::jsonschema::cli::validate(
<< std::filesystem::weakly_canonical(instance_path).string()
<< "\n";
std::cerr << error.str();
print(output, std::cerr);
result = false;
}
}
Expand Down
44 changes: 13 additions & 31 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,19 @@ auto parse_options(const std::span<const std::string> &arguments,
return options;
}

auto pretty_evaluate_callback(std::ostringstream &output,
const sourcemeta::jsontoolkit::JSON &instance,
const sourcemeta::jsontoolkit::Pointer &base)
-> sourcemeta::jsontoolkit::SchemaCompilerEvaluationCallback {
output << "error: Schema validation failure\n";
return [&output, &instance, &base](
const sourcemeta::jsontoolkit::SchemaCompilerEvaluationType,
const bool result,
const sourcemeta::jsontoolkit::SchemaCompilerTemplate::value_type
&step,
const sourcemeta::jsontoolkit::Pointer &evaluate_path,
const sourcemeta::jsontoolkit::Pointer &instance_location,
const sourcemeta::jsontoolkit::JSON &annotation) -> void {
if (result) {
return;
}

output << " "
<< sourcemeta::jsontoolkit::describe(result, step, evaluate_path,
instance_location, instance,
annotation)
<< "\n";
output << " at instance location \"";
sourcemeta::jsontoolkit::stringify(instance_location, output);
output << "\"\n";

output << " at evaluate path \"";
sourcemeta::jsontoolkit::stringify(evaluate_path.resolve_from(base),
output);
output << "\"\n";
};
auto print(
const sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput &output,
std::ostream &stream) -> void {
stream << "error: Schema validation failure\n";
for (const auto &entry : output) {
stream << " " << entry.message << "\n";
stream << " at instance location \"";
sourcemeta::jsontoolkit::stringify(entry.instance_location, stream);
stream << "\"\n";
stream << " at evaluate path \"";
sourcemeta::jsontoolkit::stringify(entry.evaluate_path, stream);
stream << "\"\n";
}
}

static auto fallback_resolver(
Expand Down
7 changes: 3 additions & 4 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ auto for_each_json(const std::vector<std::string> &arguments,
-> std::vector<
std::pair<std::filesystem::path, sourcemeta::jsontoolkit::JSON>>;

auto pretty_evaluate_callback(std::ostringstream &,
const sourcemeta::jsontoolkit::JSON &,
const sourcemeta::jsontoolkit::Pointer &)
-> sourcemeta::jsontoolkit::SchemaCompilerEvaluationCallback;
auto print(
const sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput &output,
std::ostream &stream) -> void;

auto resolver(const std::map<std::string, std::vector<std::string>> &options,
const bool remote = false)
Expand Down
3 changes: 0 additions & 3 deletions test/test/fail_true_resolve_fragment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ error: Schema validation failure
The value was expected to be of type string but it was of type object
at instance location ""
at evaluate path "/type"
The object value was expected to validate against the statically referenced schema
at instance location ""
at evaluate path ""
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
63 changes: 62 additions & 1 deletion vendor/jsontoolkit/src/jsonschema/compile.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3561995

Please sign in to comment.