Skip to content

Commit 3903f2f

Browse files
committed
Improve invalid pattern regex error messages
Fixes: #678 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 7e63ba4 commit 3903f2f

65 files changed

Lines changed: 1836 additions & 1869 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
- name: Install dependencies (Alpine)
7373
if: matrix.platform.type == 'container'
7474
run: |
75-
apk add --no-cache cmake make g++ zsh bash jq shellcheck pipx git perl-utils ca-certificates
75+
apk add --no-cache cmake make g++ linux-headers zsh bash jq shellcheck pipx git perl-utils ca-certificates
7676
apk add --no-cache --repository https://dl-cdn.alpinelinux.org/alpine/edge/main --repository https://dl-cdn.alpinelinux.org/alpine/edge/community --repository https://dl-cdn.alpinelinux.org/alpine/edge/testing fuse3 bindfs
7777
7878
- name: Install pre-commit

DEPENDENCIES

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
vendorpull https://github.com/sourcemeta/vendorpull 1dcbac42809cf87cb5b045106b863e17ad84ba02
2-
core https://github.com/sourcemeta/core 6cdc6b362fef03e3efb5b674095287d983bafffa
2+
core https://github.com/sourcemeta/core ac96b5fba550a5097d174c1681ba3155cb3e3914
33
jsonbinpack https://github.com/sourcemeta/jsonbinpack 3898774a945ebf7392bad8a9015ead97a0518a19
4-
blaze https://github.com/sourcemeta/blaze a7d43ba8a4de39918f44b97602950e260809cd0d
4+
blaze https://github.com/sourcemeta/blaze 6ea546a2698c636693e2cdc8a30fcc933b602429
55
hydra https://github.com/sourcemeta/hydra 6e0ad118846ce57275bc7d6434f8440baae2c27e
66
codegen https://github.com/sourcemeta/codegen 7733b3071cb140bc0d09406ebb9c3f306d71f7bf
77
ctrf https://github.com/ctrf-io/ctrf 93ea827d951390190171d37443bff169cf47c808

src/command_compile.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
7979
} catch (const sourcemeta::blaze::CompilerInvalidEntryPoint &error) {
8080
throw FileError<sourcemeta::blaze::CompilerInvalidEntryPoint>(schema_path,
8181
error);
82+
} catch (const sourcemeta::blaze::CompilerInvalidRegexError &error) {
83+
throw FileError<sourcemeta::blaze::CompilerInvalidRegexError>(schema_path,
84+
error);
8285
} catch (
8386
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
8487
throw FileError<sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(

src/command_lint.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ static auto load_rule(sourcemeta::core::SchemaTransformer &bundle,
166166
} catch (const sourcemeta::blaze::LinterInvalidNameError &error) {
167167
throw sourcemeta::jsonschema::FileError<
168168
sourcemeta::blaze::LinterInvalidNameError>(rule_path, error);
169+
} catch (const sourcemeta::blaze::LinterInvalidNamePatternError &error) {
170+
throw sourcemeta::jsonschema::FileError<
171+
sourcemeta::blaze::LinterInvalidNamePatternError>(rule_path, error);
169172
} catch (
170173
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
171174
throw sourcemeta::jsonschema::FileError<
@@ -379,6 +382,14 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
379382
"Could not autofix the schema without breaking its internal "
380383
"references",
381384
entry.resolution_base, error.location()};
385+
} catch (
386+
const sourcemeta::blaze::CompilerInvalidRegexError &error) {
387+
if (printed_progress) {
388+
std::cerr << "\n";
389+
}
390+
391+
throw FileError<sourcemeta::blaze::CompilerInvalidRegexError>(
392+
entry.resolution_base, error);
382393
} catch (
383394
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError
384395
&error) {
@@ -494,6 +505,10 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
494505
// Return 2 for logical lint failures
495506
return EXIT_EXPECTED_FAILURE;
496507
}
508+
} catch (
509+
const sourcemeta::blaze::CompilerInvalidRegexError &error) {
510+
throw FileError<sourcemeta::blaze::CompilerInvalidRegexError>(
511+
entry.resolution_base, error);
497512
} catch (
498513
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError
499514
&error) {

src/command_metaschema.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ auto sourcemeta::jsonschema::metaschema(
105105
result = false;
106106
}
107107
}
108+
} catch (const sourcemeta::blaze::CompilerInvalidRegexError &error) {
109+
throw FileError<sourcemeta::blaze::CompilerInvalidRegexError>(
110+
entry.resolution_base, error);
108111
} catch (
109112
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
110113
throw FileError<sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(

src/command_validate.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ auto sourcemeta::jsonschema::validate(const sourcemeta::core::Options &options)
258258
} catch (const sourcemeta::blaze::CompilerInvalidEntryPoint &error) {
259259
throw FileError<sourcemeta::blaze::CompilerInvalidEntryPoint>(schema_path,
260260
error);
261+
} catch (const sourcemeta::blaze::CompilerInvalidRegexError &error) {
262+
throw FileError<sourcemeta::blaze::CompilerInvalidRegexError>(schema_path,
263+
error);
261264
} catch (
262265
const sourcemeta::blaze::CompilerReferenceTargetNotSchemaError &error) {
263266
throw FileError<sourcemeta::blaze::CompilerReferenceTargetNotSchemaError>(

src/error.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ inline auto print_exception(const bool is_json, const Exception &exception)
340340
}
341341
}
342342

343+
if constexpr (requires(const Exception &current) { current.regex(); }) {
344+
if (is_json) {
345+
error_json.assign("regex", sourcemeta::core::JSON{exception.regex()});
346+
} else {
347+
std::cerr << " at regex " << exception.regex() << "\n";
348+
}
349+
}
350+
343351
if constexpr (requires(const Exception &current) {
344352
{
345353
current.path()
@@ -496,6 +504,11 @@ inline auto try_catch(const sourcemeta::core::Options &options,
496504
const auto is_json{options.contains("json")};
497505
print_exception(is_json, error);
498506
return EXIT_OTHER_INPUT_ERROR;
507+
} catch (const FileError<sourcemeta::blaze::LinterInvalidNamePatternError>
508+
&error) {
509+
const auto is_json{options.contains("json")};
510+
print_exception(is_json, error);
511+
return EXIT_OTHER_INPUT_ERROR;
499512
} catch (const FileError<sourcemeta::blaze::LinterMissingNameError> &error) {
500513
const auto is_json{options.contains("json")};
501514
print_exception(is_json, error);
@@ -562,6 +575,16 @@ inline auto try_catch(const sourcemeta::core::Options &options,
562575
<< "\nUse the `inspect` command to find valid schema locations\n";
563576
}
564577

578+
return EXIT_SCHEMA_INPUT_ERROR;
579+
} catch (
580+
const FileError<sourcemeta::blaze::CompilerInvalidRegexError> &error) {
581+
const auto is_json{options.contains("json")};
582+
print_exception(is_json, error);
583+
if (!is_json) {
584+
std::cerr << "\nDetailed regex error messages are not yet supported\n"
585+
"Try tools like https://regex101.com to debug further\n";
586+
}
587+
565588
return EXIT_SCHEMA_INPUT_ERROR;
566589
} catch (const FileError<sourcemeta::core::SchemaReferenceError> &error) {
567590
const auto is_json{options.contains("json")};

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ add_jsonschema_test_unix(format/pass_config_ignore)
7777
add_jsonschema_test_unix(validate/fail_instance_enoent)
7878
add_jsonschema_test_unix(validate/fail_instance_invalid_json)
7979
add_jsonschema_test_unix(validate/fail_invalid_ref)
80+
add_jsonschema_test_unix(validate/fail_invalid_regex)
8081
add_jsonschema_test_unix(validate/fail_no_schema)
8182
add_jsonschema_test_unix(validate/fail_relative_external_ref_missing)
8283
add_jsonschema_test_unix(validate/fail_resolve_enoent)

test/lint/fail_draft7_defs_ref_target_fix.sh

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,32 @@ cat << 'EOF' > "$TMP/schema.json"
2525
EOF
2626

2727
"$1" lint "$TMP/schema.json" --fix 2>"$TMP/stderr.txt" && EXIT_CODE="$?" || EXIT_CODE="$?"
28-
# Unexpected error
29-
test "$EXIT_CODE" = "1" || exit 1
28+
# Schema input error
29+
test "$EXIT_CODE" = "4" || exit 1
3030

3131
cat << EOF > "$TMP/expected.txt"
3232
.
33-
error: Could not autofix the schema without breaking its internal references
33+
error: The referenced schema is not considered to be a valid subschema given the dialect and vocabularies in use
34+
at identifier file://$(realpath "$TMP")/schema.json#/x-\$defs/not-a-schema
3435
at file path $(realpath "$TMP")/schema.json
35-
at location "/allOf/0/\$ref"
36+
at location "/x-\$defs"
3637
37-
This is an unexpected error, as making the auto-fix functionality work in all
38-
cases is tricky. We are working hard to improve the auto-fixing functionality
39-
to handle all possible edge cases, but for now, try again without \`--fix/-f\`
40-
and apply the suggestions by hand.
41-
42-
Also consider consider reporting this problematic case to the issue tracker,
43-
so we can add it to the test suite and fix it:
44-
45-
https://github.com/sourcemeta/jsonschema/issues
38+
Are you sure the reported location is a valid JSON Schema keyword in this dialect?
4639
EOF
4740

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

5043
# JSON error
5144
"$1" lint "$TMP/schema.json" --fix --json >"$TMP/stdout.txt" && EXIT_CODE="$?" || EXIT_CODE="$?"
52-
# Unexpected error
53-
test "$EXIT_CODE" = "1" || exit 1
45+
# Schema input error
46+
test "$EXIT_CODE" = "4" || exit 1
5447

5548
cat << EOF > "$TMP/expected.txt"
5649
{
57-
"error": "Could not autofix the schema without breaking its internal references",
50+
"error": "The referenced schema is not considered to be a valid subschema given the dialect and vocabularies in use",
51+
"identifier": "file://$(realpath "$TMP")/schema.json#/x-\$defs/not-a-schema",
5852
"filePath": "$(realpath "$TMP")/schema.json",
59-
"location": "/allOf/0/\$ref"
53+
"location": "/x-\$defs"
6054
}
6155
EOF
6256

test/lint/fail_lint_config_rule_invalid_title.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ cd "$TMP"
4040
test "$EXIT_CODE" = "6" || exit 1
4141

4242
cat << EOF > "$TMP/expected.txt"
43-
error: The schema rule name must match ^[a-z0-9_/]+\$
43+
error: The schema rule name does not match the required pattern
4444
at identifier Invalid Title!
45+
at regex ^[a-z0-9_/]+\$
4546
at file path $(realpath "$TMP")/rule.json
4647
EOF
4748

@@ -55,8 +56,9 @@ test "$EXIT_CODE" = "6" || exit 1
5556

5657
cat << EOF > "$TMP/expected_json.txt"
5758
{
58-
"error": "The schema rule name must match ^[a-z0-9_/]+\$",
59+
"error": "The schema rule name does not match the required pattern",
5960
"identifier": "Invalid Title!",
61+
"regex": "^[a-z0-9_/]+\$",
6062
"filePath": "$(realpath "$TMP")/rule.json"
6163
}
6264
EOF

0 commit comments

Comments
 (0)