Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validating only against a metaschema #27

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading