| 1 | """Module containing cached JSON schemas.""" |
| 2 | |
| 3 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import annotationsscip-python python python-stdlib 3.11 __future__/annotations.annotations. |
| 4 | |
| 5 | import jsonscip-python python python-stdlib 3.11 json/__init__: |
| 6 | import loggingscip-python python python-stdlib 3.11 logging/__init__: |
| 7 | import rescip-python python python-stdlib 3.11 re/__init__: |
| 8 | from typingscip-python python python-stdlib 3.11 typing/__init__: import TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING., Anyscip-python python python-stdlib 3.11 typing/Any. |
| 9 | |
| 10 | import yaml |
| 11 | from jsonschemascip-python python jsonschema 4.26.0 jsonschema/__init__: import Draft202012Validator |
| 12 | from jsonschema.exceptionsscip-python python jsonschema 4.26.0 `jsonschema.exceptions`/__init__: import ValidationError |
| 13 | |
| 14 | from ansiblelint.loadersscip-python python ansible-lint 26 `ansiblelint.loaders`/__init__: import yaml_load_safescip-python python ansible-lint 26 `src.ansiblelint.loaders`/yaml_load_safe. |
| 15 | from ansiblelint.schemas.__main__scip-python python ansible-lint 26 `ansiblelint.schemas.__main__`/__init__: import JSON_SCHEMASscip-python python ansible-lint 26 `src.ansiblelint.schemas.__main__`/JSON_SCHEMAS., _schema_cachescip-python python ansible-lint 26 `src.ansiblelint.schemas.__main__`/_schema_cache. |
| 16 | |
| 17 | _loggerscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_logger. = loggingscip-python python python-stdlib 3.11 logging/__init__:.getLoggerscip-python python python-stdlib 3.11 logging/getLogger().(__package__scip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/) |
| 18 | |
| 19 | if TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING.: |
| 20 | from collections.abcscip-python python python-stdlib 3.11 `collections.abc`/__init__: import Iteratorscip-python python python-stdlib 3.11 typing/Iterator# |
| 21 | |
| 22 | from ansiblelint.file_utilsscip-python python ansible-lint 26 `ansiblelint.file_utils`/__init__: import Lintablescip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable# |
| 23 | |
| 24 | |
| 25 | RE_MD_URLSscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/RE_MD_URLS. = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().( |
| 26 | r"\[.{0,256}?\]\((?P<url>https?://[^\s]+)\)|(?P<url2>https?://[^\s]+)" |
| 27 | ) |
| 28 | RE_URLS_AND_MD_URLSscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/RE_URLS_AND_MD_URLS. = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().(r"\[.{0,256}?\]\((https?://[^\s]+)\)|https?://[^\s]+") |
| 29 | |
| 30 | |
| 31 | def find_best_deep_matchscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().( |
| 32 | errorsscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().(errors): ValidationError, |
| 33 | ) -> ValidationError: |
| 34 | """Return the deepest schema validation error.""" |
| 35 | |
| 36 | def iter_validation_errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().( |
| 37 | errscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().(err): ValidationError, |
| 38 | ) -> Iteratorscip-python python python-stdlib 3.11 typing/Iterator#[ValidationError]: |
| 39 | if errscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().(err).context: |
| 40 | for e in errscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().(err).context: |
| 41 | yield e |
| 42 | yield from iter_validation_errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().(e) |
| 43 | |
| 44 | return maxscip-python python python-stdlib 3.11 builtins/max().(iter_validation_errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().iter_validation_error().(errorsscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().(errors)), keyscip-python python python-stdlib 3.11 builtins/max().(key)=_deep_match_relevancescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().) |
| 45 | |
| 46 | |
| 47 | def validate_file_schemascip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file): Lintablescip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#) -> listscip-python python python-stdlib 3.11 builtins/list#[strscip-python python python-stdlib 3.11 builtins/str#]: |
| 48 | """Return list of JSON validation errors found.""" |
| 49 | schema: dictscip-python python python-stdlib 3.11 builtins/dict#[Anyscip-python python python-stdlib 3.11 typing/Any., Anyscip-python python python-stdlib 3.11 typing/Any.] = {} |
| 50 | if filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).kindscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#kind. not in JSON_SCHEMASscip-python python ansible-lint 26 `src.ansiblelint.schemas.__main__`/JSON_SCHEMAS.: |
| 51 | return [f"Unable to find JSON Schema '{filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).kindscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#kind.}' for '{filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).pathscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#path.}' file."] |
| 52 | try: |
| 53 | # convert yaml to json (keys are converted to strings) |
| 54 | yaml_data = yaml_load_safescip-python python ansible-lint 26 `src.ansiblelint.loaders`/yaml_load_safe.(filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).contentscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#content().) |
| 55 | json_data = jsonscip-python python python-stdlib 3.11 json/__init__:.loadsscip-python python python-stdlib 3.11 json/loads().(jsonscip-python python python-stdlib 3.11 json/__init__:.dumpsscip-python python python-stdlib 3.11 json/dumps().(yaml_data)) |
| 56 | schema = _schema_cachescip-python python ansible-lint 26 `src.ansiblelint.schemas.__main__`/_schema_cache.[filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).kindscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#kind.] |
| 57 | |
| 58 | validator = Draft202012Validator(schema) |
| 59 | try: |
| 60 | error = nextscip-python python python-stdlib 3.11 builtins/next().(validator.iter_errors(json_data)) |
| 61 | except StopIterationscip-python python python-stdlib 3.11 builtins/StopIteration#: |
| 62 | return [] |
| 63 | if error.context: |
| 64 | error = find_best_deep_matchscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/find_best_deep_match().(error) |
| 65 | # determine if we want to use our own messages embedded into schemas inside title/markdownDescription fields |
| 66 | if not hasattr(error, "schema") or not isinstance( |
| 67 | error.schema, dictscip-python python python-stdlib 3.11 builtins/dict# |
| 68 | ): # pragma: no cover |
| 69 | msg = "error object does not have schema attribute" |
| 70 | raise TypeErrorscip-python python python-stdlib 3.11 builtins/TypeError#(msg) |
| 71 | if "not" in error.schema and len(error.schema["not"]) == 0: |
| 72 | message = error.schema["title"] |
| 73 | schema = error.schema |
| 74 | else: |
| 75 | message = f"{error.json_path} {error.message}" |
| 76 | |
| 77 | documentation_url = "" |
| 78 | for json_schema in (error.schema, schema): |
| 79 | for k in ("description", "markdownDescription"): |
| 80 | if k in json_schema: |
| 81 | # Find standalone URLs and also markdown urls. |
| 82 | match = RE_MD_URLSscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/RE_MD_URLS..searchscip-python python python-stdlib 3.11 re/Pattern#search().( |
| 83 | json_schema[k], |
| 84 | ) |
| 85 | if match: |
| 86 | documentation_url = nextscip-python python python-stdlib 3.11 builtins/next().( |
| 87 | x for x in match.groupsscip-python python python-stdlib 3.11 re/Match#groups().() if x is not None |
| 88 | ) |
| 89 | break |
| 90 | if documentation_url: |
| 91 | break |
| 92 | if documentation_url: |
| 93 | if not message.endswithscip-python python python-stdlib 3.11 builtins/str#endswith().("."): |
| 94 | message += "." |
| 95 | message += f" See {documentation_url}" |
| 96 | except yaml.constructor.ConstructorError as exc: |
| 97 | return [f"Failed to load YAML file '{filescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/validate_file_schema().(file).pathscip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#path.}': {exc.problem}"] |
| 98 | except ValidationError as exc: # pragma: no cover |
| 99 | message = exc.message |
| 100 | documentation_url = "" |
| 101 | for k in ("description", "markdownDescription"): |
| 102 | if k in schema: |
| 103 | # Find standalone URLs and also markdown urls. |
| 104 | |
| 105 | match = RE_URLS_AND_MD_URLSscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/RE_URLS_AND_MD_URLS..searchscip-python python python-stdlib 3.11 re/Pattern#search().( |
| 106 | schema[k], |
| 107 | ) |
| 108 | if match: |
| 109 | documentation_url = match.groupsscip-python python python-stdlib 3.11 re/Match#groups().()[0] |
| 110 | break |
| 111 | if documentation_url: |
| 112 | if not message.endswithscip-python python python-stdlib 3.11 builtins/str#endswith().("."): |
| 113 | message += "." |
| 114 | message += f" See {documentation_url}" |
| 115 | return [message] |
| 116 | return [message] |
| 117 | |
| 118 | |
| 119 | def _deep_match_relevancescip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().(errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().(error): ValidationError) -> tuplescip-python python python-stdlib 3.11 builtins/tuple#[boolscip-python python python-stdlib 3.11 builtins/bool# | intscip-python python python-stdlib 3.11 builtins/int#, ...]: |
| 120 | validator = errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().(error).validator |
| 121 | return ( |
| 122 | validator not in ("anyOf", "oneOf"), |
| 123 | len(errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().(error).absolute_path), |
| 124 | -len(errorscip-python python ansible-lint 26 `src.ansiblelint.schemas.main`/_deep_match_relevance().(error).path), |
| 125 | ) |