diff --git a/src/command_validate.cc b/src/command_validate.cc index f3a4a884..dde0fa04 100644 --- a/src/command_validate.cc +++ b/src/command_validate.cc @@ -15,17 +15,13 @@ auto intelligence::jsonschema::cli::validate( const std::span &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), @@ -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; diff --git a/src/main.cc b/src/main.cc index fecb16f1..4ffe2712 100644 --- a/src/main.cc +++ b/src/main.cc @@ -20,13 +20,14 @@ Global Options: Commands: - validate [--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 [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] diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4445675b..b3f36683 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/validate_fail_only_metaschema.sh b/test/validate_fail_only_metaschema.sh new file mode 100755 index 00000000..131d8862 --- /dev/null +++ b/test/validate_fail_only_metaschema.sh @@ -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 diff --git a/test/validate_pass_only_metaschema.sh b/test/validate_pass_only_metaschema.sh new file mode 100755 index 00000000..6488e30a --- /dev/null +++ b/test/validate_pass_only_metaschema.sh @@ -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"