Skip to content
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
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vendorpull https://github.com/sourcemeta/vendorpull 1dcbac42809cf87cb5b045106b863e17ad84ba02
core https://github.com/sourcemeta/core 626949ede75fb77ac4a919a24af0ade6f776751d
core https://github.com/sourcemeta/core 81cf10ad7243e799991e5c692cd3d4a6a6e5cc9f
jsonbinpack https://github.com/sourcemeta/jsonbinpack abd40e41050d14d74af1fddb5c397de5cca3b13c
blaze https://github.com/sourcemeta/blaze 53d6ba77c26e613b2803f044c2852d4441bfb0a1
hydra https://github.com/sourcemeta/hydra af9f2c54709d620872ead0c3f8f683c15a0fa702
3 changes: 2 additions & 1 deletion src/command_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ auto sourcemeta::jsonschema::bundle(const sourcemeta::core::Options &options)

const std::filesystem::path schema_path{options.positional().front()};
const auto configuration_path{find_configuration(schema_path)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto &configuration{
read_configuration(options, configuration_path, schema_path)};
const auto dialect{default_dialect(options, configuration)};
auto schema{sourcemeta::core::read_yaml_or_json(schema_path)};

Expand Down
3 changes: 2 additions & 1 deletion src/command_compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)

const auto &schema_path{options.positional().at(0)};
const auto configuration_path{find_configuration(schema_path)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto &configuration{
read_configuration(options, configuration_path, schema_path)};
const auto dialect{default_dialect(options, configuration)};

const auto schema{sourcemeta::core::read_yaml_or_json(schema_path)};
Expand Down
2 changes: 1 addition & 1 deletion src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
try {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path)};
read_configuration(options, configuration_path, entry.first)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
Expand Down
3 changes: 2 additions & 1 deletion src/command_inspect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ auto sourcemeta::jsonschema::inspect(const sourcemeta::core::Options &options)
sourcemeta::core::read_yaml_or_json(schema_path, std::ref(positions))};

const auto configuration_path{find_configuration(schema_path)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto &configuration{
read_configuration(options, configuration_path, schema_path)};
const auto dialect{default_dialect(options, configuration)};

sourcemeta::core::SchemaFrame frame{
Expand Down
4 changes: 2 additions & 2 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
for (const auto &entry : for_each_json(options)) {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path)};
read_configuration(options, configuration_path, entry.first)};
const auto dialect{default_dialect(options, configuration)};

const auto &custom_resolver{
Expand Down Expand Up @@ -255,7 +255,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
for (const auto &entry : for_each_json(options)) {
const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{
read_configuration(options, configuration_path)};
read_configuration(options, configuration_path, entry.first)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
Expand Down
3 changes: 2 additions & 1 deletion src/command_metaschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ auto sourcemeta::jsonschema::metaschema(
}

const auto configuration_path{find_configuration(entry.first)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto &configuration{
read_configuration(options, configuration_path, entry.first)};
const auto default_dialect_option{default_dialect(options, configuration)};

const auto &custom_resolver{resolver(options, options.contains("http"),
Expand Down
3 changes: 2 additions & 1 deletion src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ auto sourcemeta::jsonschema::validate(const sourcemeta::core::Options &options)
}

const auto configuration_path{find_configuration(schema_path)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto &configuration{
read_configuration(options, configuration_path, schema_path)};
const auto dialect{default_dialect(options, configuration)};

const auto schema{sourcemeta::core::read_yaml_or_json(schema_path)};
Expand Down
14 changes: 13 additions & 1 deletion src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ inline auto find_configuration(const std::filesystem::path &path)

inline auto read_configuration(
const sourcemeta::core::Options &options,
const std::optional<std::filesystem::path> &configuration_path)
const std::optional<std::filesystem::path> &configuration_path,
const std::optional<std::filesystem::path> &schema_path = std::nullopt)
-> const std::optional<sourcemeta::core::SchemaConfig> & {
using CacheKey = std::optional<std::filesystem::path>;
static std::map<CacheKey, std::optional<sourcemeta::core::SchemaConfig>>
Expand All @@ -48,6 +49,17 @@ inline auto read_configuration(
throw FileError<sourcemeta::core::SchemaConfigParseError>(
configuration_path.value(), error);
}

assert(result.has_value());
if (schema_path.has_value() &&
!result.value().applies_to(schema_path.value())) {
LOG_VERBOSE(options)
<< "Ignoring configuration file given extensions mismatch: "
<< sourcemeta::core::weakly_canonical(configuration_path.value())
.string()
<< "\n";
result = std::nullopt;
}
}

auto [inserted_iterator, inserted] =
Expand Down
1 change: 1 addition & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ inline auto for_each_json(const std::vector<std::string_view> &arguments,
const auto configuration_path{find_configuration(current_path)};
const auto &configuration{read_configuration(options, configuration_path)};
const auto extensions{parse_extensions(options, configuration)};

handle_json_entry(configuration.has_value()
? configuration.value().absolute_path
: current_path,
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ add_jsonschema_test_unix(validate/fail_benchmark_directory)
add_jsonschema_test_unix(validate/pass_benchmark_loop)
add_jsonschema_test_unix(validate/pass_benchmark_loop_jsonl)
add_jsonschema_test_unix(validate/fail_benchmark_zero)
add_jsonschema_test_unix(validate/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(validate/pass_resolve_remap)
add_jsonschema_test_unix(validate/pass_resolve_remap_relative)

Expand All @@ -178,6 +179,7 @@ add_jsonschema_test_unix(metaschema/fail_single)
add_jsonschema_test_unix(metaschema/fail_yaml)
add_jsonschema_test_unix(metaschema/fail_non_schema)
add_jsonschema_test_unix(metaschema/fail_no_dialect)
add_jsonschema_test_unix(metaschema/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(metaschema/pass_cwd)
add_jsonschema_test_unix(metaschema/pass_single)
add_jsonschema_test_unix(metaschema/pass_2020_12)
Expand Down Expand Up @@ -267,6 +269,7 @@ add_jsonschema_test_unix(bundle/fail_resolve_duplicate)
add_jsonschema_test_unix(bundle/fail_resolve_invalid_json)
add_jsonschema_test_unix(bundle/fail_schema_invalid_json)
add_jsonschema_test_unix(bundle/fail_unknown_metaschema)
add_jsonschema_test_unix(bundle/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(bundle/pass_bigint)
add_jsonschema_test_unix(bundle/pass_resolve_default_dialect_config)
add_jsonschema_test_unix(bundle/pass_resolve_default_dialect_config_relative)
Expand All @@ -285,6 +288,7 @@ add_jsonschema_test_unix(inspect/pass_json_output)
add_jsonschema_test_unix(inspect/fail_no_schema)
add_jsonschema_test_unix(inspect/fail_schema_invalid_json)
add_jsonschema_test_unix(inspect/fail_unknown_metaschema)
add_jsonschema_test_unix(inspect/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(inspect/fail_relative_file_metaschema_ref)

# Compile
Expand All @@ -300,6 +304,7 @@ add_jsonschema_test_unix(compile/pass_yaml)
add_jsonschema_test_unix(compile/fail_no_schema)
add_jsonschema_test_unix(compile/fail_schema_invalid_json)
add_jsonschema_test_unix(compile/fail_unknown_metaschema)
add_jsonschema_test_unix(compile/fail_default_dialect_config_extension_mismatch)
add_jsonschema_test_unix(compile/pass_resolve_remap)
add_jsonschema_test_unix(compile/pass_resolve_remap_relative)

Expand Down
48 changes: 48 additions & 0 deletions test/bundle/fail_default_dialect_config_extension_mismatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/document.json"
{ "foo": 1 }
EOF

cat << 'EOF' > "$TMP/jsonschema.json"
{
"extension": [".schema.json"],
"defaultDialect": "https://json-schema.org/draft/2020-12/schema"
}
EOF

"$1" bundle "$TMP/document.json" --verbose 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Using configuration file: $(realpath "$TMP")/jsonschema.json
Ignoring configuration file given extensions mismatch: $(realpath "$TMP")/jsonschema.json
error: Could not determine the base dialect of the schema
at file path $(realpath "$TMP")/document.json

Are you sure the input is a valid JSON Schema and its base dialect is known?
If the input does not declare the \`\$schema\` keyword, you might want to
explicitly declare a default dialect using \`--default-dialect/-d\`
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" bundle "$TMP/document.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
{
"error": "Could not determine the base dialect of the schema",
"filePath": "$(realpath "$TMP")/document.json"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
48 changes: 48 additions & 0 deletions test/compile/fail_default_dialect_config_extension_mismatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/document.json"
{ "foo": 1 }
EOF

cat << 'EOF' > "$TMP/jsonschema.json"
{
"extension": [".schema.json"],
"defaultDialect": "https://json-schema.org/draft/2020-12/schema"
}
EOF

"$1" compile "$TMP/document.json" --verbose 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Using configuration file: $(realpath "$TMP")/jsonschema.json
Ignoring configuration file given extensions mismatch: $(realpath "$TMP")/jsonschema.json
error: Could not determine the base dialect of the schema
at file path $(realpath "$TMP")/document.json

Are you sure the input is a valid JSON Schema and its base dialect is known?
If the input does not declare the \`\$schema\` keyword, you might want to
explicitly declare a default dialect using \`--default-dialect/-d\`
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" compile "$TMP/document.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
{
"error": "Could not determine the base dialect of the schema",
"filePath": "$(realpath "$TMP")/document.json"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
48 changes: 48 additions & 0 deletions test/inspect/fail_default_dialect_config_extension_mismatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/document.json"
{ "foo": 1 }
EOF

cat << 'EOF' > "$TMP/jsonschema.json"
{
"extension": [".schema.json"],
"defaultDialect": "https://json-schema.org/draft/2020-12/schema"
}
EOF

"$1" inspect "$TMP/document.json" --verbose 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Using configuration file: $(realpath "$TMP")/jsonschema.json
Ignoring configuration file given extensions mismatch: $(realpath "$TMP")/jsonschema.json
error: Could not determine the base dialect of the schema
at file path $(realpath "$TMP")/document.json

Are you sure the input is a valid JSON Schema and its base dialect is known?
If the input does not declare the \`\$schema\` keyword, you might want to
explicitly declare a default dialect using \`--default-dialect/-d\`
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" inspect "$TMP/document.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
{
"error": "Could not determine the base dialect of the schema",
"filePath": "$(realpath "$TMP")/document.json"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
48 changes: 48 additions & 0 deletions test/metaschema/fail_default_dialect_config_extension_mismatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/document.json"
{ "foo": 1 }
EOF

cat << 'EOF' > "$TMP/jsonschema.json"
{
"extension": [".schema.json"],
"defaultDialect": "https://json-schema.org/draft/2020-12/schema"
}
EOF

"$1" metaschema "$TMP/document.json" --verbose 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Using configuration file: $(realpath "$TMP")/jsonschema.json
Ignoring configuration file given extensions mismatch: $(realpath "$TMP")/jsonschema.json
error: Could not determine the base dialect of the schema
at file path $(realpath "$TMP/document.json")

Are you sure the input is a valid JSON Schema and its base dialect is known?
If the input does not declare the \`\$schema\` keyword, you might want to
explicitly declare a default dialect using \`--default-dialect/-d\`
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

# JSON error
"$1" metaschema "$TMP/document.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
{
"error": "Could not determine the base dialect of the schema",
"filePath": "$(realpath "$TMP/document.json")"
}
EOF

diff "$TMP/stdout.txt" "$TMP/expected.txt"
Loading