Skip to content

Commit

Permalink
Use new metaschema helper from JSON Toolkit (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jun 1, 2024
1 parent e5435ed commit 63f5b7c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 36 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 5ff4024902642afc9cc2f9a9e02ae9dff9d15d4f
jsontoolkit https://github.com/sourcemeta/jsontoolkit 72dde22434cfa827201dbde10af976c82bd99a43
jsontoolkit https://github.com/sourcemeta/jsontoolkit 958d686c4bbcb1f232ed8d313956929e38af84d2
hydra https://github.com/sourcemeta/hydra d5e0c314dae88b0bf2ac4eeff2c7395910e2c7e9
11 changes: 8 additions & 3 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ auto intelligence::jsonschema::cli::test(
}

if (options.contains("m") || options.contains("metaschema")) {
const auto metaschema_result{
validate_against_metaschema(schema.value(), test_resolver)};
if (metaschema_result) {
const auto metaschema_template{sourcemeta::jsontoolkit::compile(
sourcemeta::jsontoolkit::metaschema(schema.value(), test_resolver),
sourcemeta::jsontoolkit::default_schema_walker, test_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
if (sourcemeta::jsontoolkit::evaluate(
metaschema_template, schema.value(),
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback)) {
log_verbose(options)
<< "The schema is valid with respect to its metaschema\n";
} else {
Expand Down
11 changes: 8 additions & 3 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ auto intelligence::jsonschema::cli::validate(
// TODO: If not instance is passed, just validate the schema against its
// metaschema?
if (options.contains("m") || options.contains("metaschema")) {
const auto metaschema_result{
validate_against_metaschema(schema, custom_resolver)};
if (metaschema_result) {
const auto metaschema_template{sourcemeta::jsontoolkit::compile(
sourcemeta::jsontoolkit::metaschema(schema, custom_resolver),
sourcemeta::jsontoolkit::default_schema_walker, custom_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
if (sourcemeta::jsontoolkit::evaluate(
metaschema_template, schema,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback)) {
log_verbose(options)
<< "The schema is valid with respect to its metaschema\n";
} else {
Expand Down
25 changes: 0 additions & 25 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,4 @@ auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
return null_stream;
}

auto validate_against_metaschema(
const sourcemeta::jsontoolkit::JSON &schema,
const sourcemeta::jsontoolkit::SchemaResolver &resolver) -> bool {
const std::optional<std::string> dialect{
sourcemeta::jsontoolkit::dialect(schema)};
if (!dialect.has_value()) {
throw std::runtime_error(
"Cannot determine the dialect of the input schema");
}

const auto metaschema{resolver(dialect.value()).get()};
if (!metaschema.has_value()) {
throw sourcemeta::jsontoolkit::SchemaResolutionError(
dialect.value(), "Could not resolve metaschema");
}

const auto metaschema_template{sourcemeta::jsontoolkit::compile(
metaschema.value(), sourcemeta::jsontoolkit::default_schema_walker,
resolver, sourcemeta::jsontoolkit::default_schema_compiler)};
return sourcemeta::jsontoolkit::evaluate(
metaschema_template, schema,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback);
}

} // namespace intelligence::jsonschema::cli
4 changes: 0 additions & 4 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ auto resolver(const std::map<std::string, std::vector<std::string>> &options,
auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
-> std::ostream &;

auto validate_against_metaschema(
const sourcemeta::jsontoolkit::JSON &schema,
const sourcemeta::jsontoolkit::SchemaResolver &resolver) -> bool;

} // namespace intelligence::jsonschema::cli

#endif

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

21 changes: 21 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/jsonschema.cc

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

0 comments on commit 63f5b7c

Please sign in to comment.