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 46c04a0
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 35 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" }
]
}
17 changes: 13 additions & 4 deletions src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ 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();
std::ostringstream error;
for (const auto &trace : output) {
error << " " << trace.message << "\n";
error << " at instance location \"";
sourcemeta::jsontoolkit::stringify(trace.instance_location, error);
error << "\"\n";
error << " at evaluate path \"";
sourcemeta::jsontoolkit::stringify(trace.evaluate_path, error);
error << "\"\n";
}

std::cerr << entry.first.string()
<< ": The schema is NOT valid with respect to its metaschema\n";
result = false;
Expand Down
91 changes: 65 additions & 26 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,76 @@ auto parse_options(const std::span<const std::string> &arguments,
return options;
}

class StacktraceEvaluator {
public:
std::ostringstream &output;
const sourcemeta::jsontoolkit::JSON &instance;
const sourcemeta::jsontoolkit::Pointer &base;
std::set<sourcemeta::jsontoolkit::Pointer> mask;

auto operator()(
const sourcemeta::jsontoolkit::SchemaCompilerEvaluationType type,
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 (type == sourcemeta::jsontoolkit::SchemaCompilerEvaluationType::Pre) {
assert(!evaluate_path.empty());
assert(evaluate_path.back().is_property());
const auto &keyword{evaluate_path.back().to_property()};
if (keyword == "oneOf") {
this->mask.insert(evaluate_path);
std::cout << "Masking: ";
sourcemeta::jsontoolkit::stringify(evaluate_path, std::cout);
std::cout << "\n";
}

return;
}

if (type == sourcemeta::jsontoolkit::SchemaCompilerEvaluationType::Post) {
if (this->mask.contains(evaluate_path)) {
std::cout << "Unmasking: ";
sourcemeta::jsontoolkit::stringify(evaluate_path, std::cout);
std::cout << "\n";
this->mask.erase(evaluate_path);
}
}

if (result) {
return;
}

for (const auto &masked_base : this->mask) {
if (evaluate_path.starts_with(masked_base)) {
return;
}
}

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

this->output << " at evaluate path \"";
sourcemeta::jsontoolkit::stringify(evaluate_path.resolve_from(this->base),
this->output);
this->output << "\"\n";
}
};

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";
};
return StacktraceEvaluator{output, instance, base, {}};
}

static auto fallback_resolver(
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 46c04a0

Please sign in to comment.