Skip to content

Commit 509fc7b

Browse files
committed
Set format_checker when checking schemas
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.
1 parent 2032725 commit 509fc7b

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

src/check_jsonschema/schema_loader/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _get_validator(
167167
if self.validator_class is None:
168168
# get the correct validator class and check the schema under its metaschema
169169
validator_cls = jsonschema.validators.validator_for(schema)
170-
validator_cls.check_schema(schema)
170+
validator_cls.check_schema(schema, format_checker=format_checker)
171171
else:
172172
# for a user-provided validator class, don't check_schema
173173
# on the grounds that it might *not* be valid but the user wants to use

tests/acceptance/test_example_files.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_hook_positive_examples(case_name, run_line):
6363

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

6868

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

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

7777

7878
@pytest.mark.parametrize("case_name", _get_explicit_cases("positive"))
@@ -102,7 +102,37 @@ def test_explicit_positive_examples(case_name, run_line):
102102
str(instance),
103103
]
104104
)
105-
assert ret.exit_code == 0
105+
assert ret.exit_code == 0, _format_cli_result(ret)
106+
107+
108+
@pytest.mark.parametrize("case_name", _get_explicit_cases("negative"))
109+
def test_explicit_negative_examples(case_name, run_line):
110+
_check_file_format_skip(case_name)
111+
casedir = EXAMPLE_EXPLICIT_FILES / "negative" / case_name
112+
113+
instance = casedir / "instance.json"
114+
if not instance.exists():
115+
instance = casedir / "instance.yaml"
116+
if not instance.exists():
117+
instance = casedir / "instance.toml"
118+
if not instance.exists():
119+
raise Exception("could not find an instance file for test case")
120+
121+
schema = casedir / "schema.json"
122+
if not schema.exists():
123+
schema = casedir / "schema.yaml"
124+
if not schema.exists():
125+
raise Exception("could not find a schema file for test case")
126+
127+
ret = run_line(
128+
[
129+
"check-jsonschema",
130+
"--schemafile",
131+
str(schema),
132+
str(instance),
133+
]
134+
)
135+
assert ret.exit_code == 1, _format_cli_result(ret)
106136

107137

108138
def _check_file_format_skip(case_name):
@@ -166,10 +196,12 @@ def _package_is_installed(pkg: str) -> bool:
166196
return True
167197

168198

169-
def _format_cli_result(rcase: ResolvedCase, result) -> str:
199+
def _format_cli_result(result, rcase: ResolvedCase | None = None) -> str:
200+
prefix = ""
201+
if rcase is not None:
202+
prefix = f"config.add_args={rcase.add_args}\n"
170203
return (
171-
"\n"
172-
f"config.add_args={rcase.add_args}\n"
204+
f"\n{prefix}"
173205
f"{result.exit_code=}\n"
174206
f"result.stdout={result.output}\n"
175207
f"{result.stderr=}"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"key": "foo 1",
3+
"value": "bar 2"
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"additionalProperties": false,
3+
"properties": {
4+
"key": {
5+
"description": "some key",
6+
"maxLength": 128,
7+
"minLength": 1,
8+
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
9+
"type": "string"
10+
},
11+
"value": {
12+
"description": "some value",
13+
"maxLength": 256,
14+
"minLength": 0,
15+
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
16+
"type": "string"
17+
}
18+
},
19+
"type": "object"
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"key": "a 1",
3+
"value": "b 2"
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"additionalProperties": false,
3+
"properties": {
4+
"key": {
5+
"description": "some key",
6+
"maxLength": 128,
7+
"minLength": 1,
8+
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
9+
"type": "string"
10+
},
11+
"value": {
12+
"description": "some value",
13+
"maxLength": 256,
14+
"minLength": 0,
15+
"pattern": "^\\p{L}\\p{Z}\\p{N}$",
16+
"type": "string"
17+
}
18+
},
19+
"type": "object"
20+
}

0 commit comments

Comments
 (0)