Skip to content

Commit

Permalink
Set format_checker when checking schemas
Browse files Browse the repository at this point in the history
This allows the `check_schema` call to succeed when it requires that
the regex implementation is regress. Patterns which can compile as
regress patterns are thereby supported.

Add initial acceptance tests for regress 'pattern' support, which
revealed this issue.
  • Loading branch information
sirosen committed Nov 2, 2024
1 parent d5249de commit 8a97d0a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/check_jsonschema/schema_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _get_validator(
if self.validator_class is None:
# get the correct validator class and check the schema under its metaschema
validator_cls = jsonschema.validators.validator_for(schema)
validator_cls.check_schema(schema)
validator_cls.check_schema(schema, format_checker=format_checker)
else:
# for a user-provided validator class, don't check_schema
# on the grounds that it might *not* be valid but the user wants to use
Expand Down
44 changes: 38 additions & 6 deletions tests/acceptance/test_example_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_hook_positive_examples(case_name, run_line):

hook_id = POSITIVE_HOOK_CASES[case_name]
ret = run_line(HOOK_CONFIG[hook_id] + [rcase.path] + rcase.add_args)
assert ret.exit_code == 0, _format_cli_result(rcase, ret)
assert ret.exit_code == 0, _format_cli_result(ret, rcase)


@pytest.mark.parametrize("case_name", NEGATIVE_HOOK_CASES.keys())
Expand All @@ -72,7 +72,7 @@ def test_hook_negative_examples(case_name, run_line):

hook_id = NEGATIVE_HOOK_CASES[case_name]
ret = run_line(HOOK_CONFIG[hook_id] + [rcase.path] + rcase.add_args)
assert ret.exit_code == 1, _format_cli_result(rcase, ret)
assert ret.exit_code == 1, _format_cli_result(ret, rcase)


@pytest.mark.parametrize("case_name", _get_explicit_cases("positive"))
Expand Down Expand Up @@ -102,7 +102,37 @@ def test_explicit_positive_examples(case_name, run_line):
str(instance),
]
)
assert ret.exit_code == 0
assert ret.exit_code == 0, _format_cli_result(ret)


@pytest.mark.parametrize("case_name", _get_explicit_cases("negative"))
def test_explicit_negative_examples(case_name, run_line):
_check_file_format_skip(case_name)
casedir = EXAMPLE_EXPLICIT_FILES / "negative" / case_name

instance = casedir / "instance.json"
if not instance.exists():
instance = casedir / "instance.yaml"
if not instance.exists():
instance = casedir / "instance.toml"
if not instance.exists():
raise Exception("could not find an instance file for test case")

schema = casedir / "schema.json"
if not schema.exists():
schema = casedir / "schema.yaml"
if not schema.exists():
raise Exception("could not find a schema file for test case")

ret = run_line(
[
"check-jsonschema",
"--schemafile",
str(schema),
str(instance),
]
)
assert ret.exit_code == 1, _format_cli_result(ret)


def _check_file_format_skip(case_name):
Expand Down Expand Up @@ -166,10 +196,12 @@ def _package_is_installed(pkg: str) -> bool:
return True


def _format_cli_result(rcase: ResolvedCase, result) -> str:
def _format_cli_result(result, rcase: ResolvedCase | None = None) -> str:
prefix = ""
if rcase is not None:
prefix = f"config.add_args={rcase.add_args}\n"
return (
"\n"
f"config.add_args={rcase.add_args}\n"
f"\n{prefix}"
f"{result.exit_code=}\n"
f"result.stdout={result.output}\n"
f"{result.stderr=}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": "foo 1",
"value": "bar 2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"additionalProperties": false,
"properties": {
"key": {
"description": "some key",
"maxLength": 128,
"minLength": 1,
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
"type": "string"
},
"value": {
"description": "some value",
"maxLength": 256,
"minLength": 0,
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
"type": "string"
}
},
"type": "object"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": "a 1",
"value": "b 2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"additionalProperties": false,
"properties": {
"key": {
"description": "some key",
"maxLength": 128,
"minLength": 1,
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
"type": "string"
},
"value": {
"description": "some value",
"maxLength": 256,
"minLength": 0,
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
"type": "string"
}
},
"type": "object"
}

0 comments on commit 8a97d0a

Please sign in to comment.