Skip to content

Commit

Permalink
Support validating only against a metaschema (#27)
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 3, 2024
1 parent 63f5b7c commit defa5c3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ auto intelligence::jsonschema::cli::validate(
const std::span<const std::string> &arguments) -> int {
const auto options{
parse_options(arguments, {"h", "http", "m", "metaschema"})};
CLI_ENSURE(options.at("").size() >= 2,
"You must pass a schema followed by an instance")
CLI_ENSURE(options.at("").size() >= 1, "You must pass a schema")
const auto &schema_path{options.at("").at(0)};
const auto &instance_path{options.at("").at(1)};
const auto custom_resolver{
resolver(options, options.contains("h") || options.contains("http"))};

const auto schema{sourcemeta::jsontoolkit::from_file(schema_path)};

// TODO: If not instance is passed, just validate the schema against its
// metaschema?
if (options.contains("m") || options.contains("metaschema")) {
const auto metaschema_template{sourcemeta::jsontoolkit::compile(
sourcemeta::jsontoolkit::metaschema(schema, custom_resolver),
Expand All @@ -43,19 +39,23 @@ auto intelligence::jsonschema::cli::validate(
}
}

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker, custom_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
bool result{true};
if (options.at("").size() >= 2) {
const auto &instance_path{options.at("").at(1)};
const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker, custom_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};

const auto instance{sourcemeta::jsontoolkit::from_file(instance_path)};
const auto instance{sourcemeta::jsontoolkit::from_file(instance_path)};

const auto result{sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback)};
result = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback);

if (result) {
log_verbose(options) << "Valid\n";
if (result) {
log_verbose(options) << "Valid\n";
}
}

return result ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down
15 changes: 8 additions & 7 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ Global Options:
Commands:
validate <schema.json> <instance.json> [--http/-h] [--metaschema/-m]
Validate an instance against a schema, printing error information, if
any, in a human-readable manner. The `--http/-h` option enables resolving
remote schemas over the HTTP protocol. The `--metaschema/-m` option
checks that the given schema is valid with respects to its dialect
metaschema.
validate <schema.json> [instance.json] [--http/-h] [--metaschema/-m]
If an instance is passed, validate it against the given schema.
Otherwise, validate the schema against its dialect metaschema. The
`--http/-h` option enables resolving remote schemas over the HTTP
protocol. The `--metaschema/-m` option checks that the given schema
is valid with respects to its dialect metaschema even if an instance
was passed.
test [schema.json...] [--http/-h] [--metaschema/-m]
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ add_jsonschema_test_unix(validate_fail)
add_jsonschema_test_unix(validate_fail_remote_no_http)
add_jsonschema_test_unix(validate_non_supported)
add_jsonschema_test_unix(validate_pass_with_metaschema)
add_jsonschema_test_unix(validate_pass_only_metaschema)
add_jsonschema_test_unix(validate_fail_only_metaschema)
add_jsonschema_test_unix(bundle_non_remote)
add_jsonschema_test_unix(bundle_remote_single_schema)
add_jsonschema_test_unix(bundle_remote_no_http)
Expand Down
29 changes: 29 additions & 0 deletions test/validate_fail_only_metaschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"foo": {
"type": 1
}
}
}
EOF

"$1" validate "$TMP/schema.json" --metaschema && CODE="$?" || CODE="$?"

if [ "$CODE" = "0" ]
then
echo "FAIL" 1>&2
exit 1
else
echo "PASS" 1>&2
fi
21 changes: 21 additions & 0 deletions test/validate_pass_only_metaschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"foo": {
"type": "string"
}
}
}
EOF

"$1" validate "$TMP/schema.json"

0 comments on commit defa5c3

Please sign in to comment.