66import logging
77import re
88import typing
9- from typing import TYPE_CHECKING
9+ from typing import TYPE_CHECKING , Any
1010
11- import jsonschema
1211import yaml
1312from jsonschema .exceptions import ValidationError
13+ from jsonschema .validators import validator_for
1414
1515from ansiblelint .loaders import yaml_load_safe
1616from ansiblelint .schemas .__main__ import JSON_SCHEMAS , _schema_cache
2222
2323
2424def find_best_deep_match (
25- errors : jsonschema . ValidationError ,
26- ) -> jsonschema . ValidationError :
25+ errors : ValidationError ,
26+ ) -> ValidationError :
2727 """Return the deepest schema validation error."""
2828
2929 def iter_validation_error (
30- err : jsonschema . ValidationError ,
31- ) -> typing .Iterator [jsonschema . ValidationError ]:
30+ err : ValidationError ,
31+ ) -> typing .Iterator [ValidationError ]:
3232 if err .context :
3333 for e in err .context :
3434 yield e
@@ -39,7 +39,7 @@ def iter_validation_error(
3939
4040def validate_file_schema (file : Lintable ) -> list [str ]:
4141 """Return list of JSON validation errors found."""
42- schema = {}
42+ schema : dict [ Any , Any ] = {}
4343 if file .kind not in JSON_SCHEMAS :
4444 return [f"Unable to find JSON Schema '{ file .kind } ' for '{ file .path } ' file." ]
4545 try :
@@ -48,7 +48,7 @@ def validate_file_schema(file: Lintable) -> list[str]:
4848 json_data = json .loads (json .dumps (yaml_data ))
4949 schema = _schema_cache [file .kind ]
5050
51- validator = jsonschema . validators . validator_for (schema )
51+ validator = validator_for (schema )
5252 v = validator (schema )
5353 try :
5454 error = next (v .iter_errors (json_data ))
@@ -57,6 +57,9 @@ def validate_file_schema(file: Lintable) -> list[str]:
5757 if error .context :
5858 error = find_best_deep_match (error )
5959 # determine if we want to use our own messages embedded into schemas inside title/markdownDescription fields
60+ if not hasattr (error , "schema" ) or not isinstance (error .schema , dict ):
61+ msg = "error object does not have schema attribute"
62+ raise TypeError (msg )
6063 if "not" in error .schema and len (error .schema ["not" ]) == 0 :
6164 message = error .schema ["title" ]
6265 schema = error .schema
@@ -106,7 +109,7 @@ def validate_file_schema(file: Lintable) -> list[str]:
106109 return [message ]
107110
108111
109- def _deep_match_relevance (error : jsonschema . ValidationError ) -> tuple [bool | int , ...]:
112+ def _deep_match_relevance (error : ValidationError ) -> tuple [bool | int , ...]:
110113 validator = error .validator
111114 return (
112115 validator not in ("anyOf" , "oneOf" ), # type: ignore[comparison-overlap]
0 commit comments