diff --git a/DEPENDENCIES b/DEPENDENCIES index 2917f639..6a01fad6 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -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 diff --git a/instance.json b/instance.json new file mode 100644 index 00000000..810c96ee --- /dev/null +++ b/instance.json @@ -0,0 +1 @@ +"foo" diff --git a/schema.json b/schema.json new file mode 100644 index 00000000..98ab7452 --- /dev/null +++ b/schema.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "oneOf": [ + { "type": "string" }, + { "minLength": 3 }, + { "type": "boolean" } + ] +} diff --git a/src/command_metaschema.cc b/src/command_metaschema.cc index ee80e187..76213704 100644 --- a/src/command_metaschema.cc +++ b/src/command_metaschema.cc @@ -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; diff --git a/src/command_test.cc b/src/command_test.cc index 8d3dd204..158e3294 100644 --- a/src/command_test.cc +++ b/src/command_test.cc @@ -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")) { @@ -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"; diff --git a/src/command_validate.cc b/src/command_validate.cc index 3405c153..9a4eabd4 100644 --- a/src/command_validate.cc +++ b/src/command_validate.cc @@ -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{ @@ -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) { @@ -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; } @@ -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()}; @@ -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) { @@ -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; } } diff --git a/src/utils.cc b/src/utils.cc index 60a25058..04d32789 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -175,37 +175,18 @@ auto parse_options(const std::span &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( diff --git a/src/utils.h b/src/utils.h index f7d172e6..40da9d4a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,10 +27,9 @@ auto for_each_json(const std::vector &arguments, -> std::vector< std::pair>; -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> &options, const bool remote = false) diff --git a/vendor/jsontoolkit/src/jsonschema/compile.cc b/vendor/jsontoolkit/src/jsonschema/compile.cc index 40bfe8d4..02b1e536 100644 --- a/vendor/jsontoolkit/src/jsonschema/compile.cc +++ b/vendor/jsontoolkit/src/jsonschema/compile.cc @@ -1,7 +1,7 @@ #include #include -#include // std::move +#include // std::move, std::any_of #include // assert #include // std::back_inserter #include // std::move @@ -212,4 +212,59 @@ auto compile(const SchemaCompilerContext &context, entry.dialect); } +SchemaCompilerErrorTraceOutput::SchemaCompilerErrorTraceOutput( + const JSON &instance, const Pointer &base) + : instance_{instance}, base_{base} {} + +auto SchemaCompilerErrorTraceOutput::begin() const -> const_iterator { + return this->output.begin(); +} + +auto SchemaCompilerErrorTraceOutput::end() const -> const_iterator { + return this->output.end(); +} + +auto SchemaCompilerErrorTraceOutput::cbegin() const -> const_iterator { + return this->output.cbegin(); +} + +auto SchemaCompilerErrorTraceOutput::cend() const -> const_iterator { + return this->output.cend(); +} + +auto SchemaCompilerErrorTraceOutput::operator()( + const SchemaCompilerEvaluationType type, const bool result, + const SchemaCompilerTemplate::value_type &step, + const Pointer &evaluate_path, const Pointer &instance_location, + const JSON &annotation) -> void { + assert(!evaluate_path.empty()); + assert(evaluate_path.back().is_property()); + + if (type == sourcemeta::jsontoolkit::SchemaCompilerEvaluationType::Pre) { + const auto &keyword{evaluate_path.back().to_property()}; + // To ease the output + if (keyword == "oneOf" || keyword == "not") { + this->mask.insert(evaluate_path); + } + } else if (type == + sourcemeta::jsontoolkit::SchemaCompilerEvaluationType::Post && + this->mask.contains(evaluate_path)) { + this->mask.erase(evaluate_path); + } + + // Ignore successful or masked steps + if (result || std::any_of(this->mask.cbegin(), this->mask.cend(), + [&evaluate_path](const auto &entry) { + return evaluate_path.starts_with(entry); + })) { + return; + } + + this->output.push_back( + {sourcemeta::jsontoolkit::describe(result, step, evaluate_path, + instance_location, this->instance_, + annotation), + instance_location, evaluate_path.resolve_from(this->base_)}); +} + } // namespace sourcemeta::jsontoolkit diff --git a/vendor/jsontoolkit/src/jsonschema/include/sourcemeta/jsontoolkit/jsonschema_compile.h b/vendor/jsontoolkit/src/jsonschema/include/sourcemeta/jsontoolkit/jsonschema_compile.h index dae34a3f..b71af3f9 100644 --- a/vendor/jsontoolkit/src/jsonschema/include/sourcemeta/jsontoolkit/jsonschema_compile.h +++ b/vendor/jsontoolkit/src/jsonschema/include/sourcemeta/jsontoolkit/jsonschema_compile.h @@ -524,6 +524,90 @@ using SchemaCompilerEvaluationCallback = const SchemaCompilerTemplate::value_type &, const Pointer &, const Pointer &, const JSON &)>; +// TODO: Support standard output formats too + +/// @ingroup jsonschema +/// +/// A simple evaluation callback that reports a stack trace in the case of +/// validation error that you can report as you with. For example: +/// +/// ```cpp +/// #include +/// #include +/// #include +/// +/// const sourcemeta::jsontoolkit::JSON schema = +/// sourcemeta::jsontoolkit::parse(R"JSON({ +/// "$schema": "https://json-schema.org/draft/2020-12/schema", +/// "type": "string" +/// })JSON"); +/// +/// const auto schema_template{sourcemeta::jsontoolkit::compile( +/// schema, sourcemeta::jsontoolkit::default_schema_walker, +/// sourcemeta::jsontoolkit::official_resolver, +/// sourcemeta::jsontoolkit::default_schema_compiler)}; +/// +/// const sourcemeta::jsontoolkit::JSON instance{5}; +/// +/// sourcemeta::jsontoolkit::SchemaCompilerErrorTraceOutput output; +/// const auto result{sourcemeta::jsontoolkit::evaluate( +/// schema_template, instance, +/// sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast, +/// output)}; +/// +/// if (!result) { +/// // If validation failed, you will get at least one error +/// assert(!output.empty()); +/// +/// for (const auto &trace : output) { +/// std::cerr << trace.message << "\n"; +/// sourcemeta::jsontoolkit::stringify(trace.instance_location, std::cerr); +/// std::cerr << "\n"; +/// sourcemeta::jsontoolkit::stringify(trace.evaluate_path, std::cerr); +/// std::cerr << "\n"; +/// } +/// } +/// ``` +class SOURCEMETA_JSONTOOLKIT_JSONSCHEMA_EXPORT SchemaCompilerErrorTraceOutput { +public: + SchemaCompilerErrorTraceOutput(const JSON &instance, + const Pointer &base = empty_pointer); + + struct Entry { + const std::string message; + const Pointer instance_location; + const Pointer evaluate_path; + }; + + auto operator()(const SchemaCompilerEvaluationType type, const bool result, + const SchemaCompilerTemplate::value_type &step, + const Pointer &evaluate_path, + const Pointer &instance_location, + const JSON &annotation) -> void; + + using container_type = typename std::vector; + using const_iterator = typename container_type::const_iterator; + auto begin() const -> const_iterator; + auto end() const -> const_iterator; + auto cbegin() const -> const_iterator; + auto cend() const -> const_iterator; + +private: + const JSON &instance_; + const Pointer base_; +// Exporting symbols that depends on the standard C++ library is considered +// safe. +// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN +#if defined(_MSC_VER) +#pragma warning(disable : 4251) +#endif + container_type output; + std::set mask; +#if defined(_MSC_VER) +#pragma warning(default : 4251) +#endif +}; + /// @ingroup jsonschema /// /// This function translates a step execution into a human-readable string. @@ -533,9 +617,6 @@ describe(const bool valid, const SchemaCompilerTemplate::value_type &step, const Pointer &evaluate_path, const Pointer &instance_location, const JSON &instance, const JSON &annotation) -> std::string; -// TODO: Support standard output formats. Maybe through pre-made evaluation -// callbacks? - /// @ingroup jsonschema /// /// This function evaluates a schema compiler template in validation mode,