Skip to content

Commit

Permalink
[WIP] 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 6fc0eaf
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 52 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 70dfb548b5b11d4e6d59020e5587bc1046a9b874
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc
1 change: 1 addition & 0 deletions instance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo"
8 changes: 8 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"oneOf": [
{ "type": "string" },
{ "minLength": 3 },
{ "type": "boolean" }
]
}
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))) {
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
8 changes: 4 additions & 4 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ 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"}))};
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast, output)};

std::ostringstream test_case_description;
if (test_case.defines("description")) {
Expand Down Expand Up @@ -278,7 +278,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));
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));
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
43 changes: 12 additions & 31 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,18 @@ 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 {
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
57 changes: 56 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 6fc0eaf

Please sign in to comment.