forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCfnLintJsonSchema.py
More file actions
83 lines (69 loc) · 2.59 KB
/
CfnLintJsonSchema.py
File metadata and controls
83 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import annotations
from collections import namedtuple
from typing import Any, Sequence
from cfnlint.helpers import load_resource
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
from cfnlint.jsonschema.exceptions import best_match
from cfnlint.rules.jsonschema.Base import BaseJsonSchema
SchemaDetails = namedtuple("SchemaDetails", ["module", "filename"])
class CfnLintJsonSchema(BaseJsonSchema):
def __init__(
self,
keywords: Sequence[str] | None = None,
schema_details: SchemaDetails | None = None,
all_matches: bool = False,
) -> None:
super().__init__()
self.keywords = keywords or []
self.parent_rules = ["E1101"]
self.all_matches = all_matches
self._use_schema_arg = True
self._schema: dict[str, Any] = {}
if schema_details:
self._schema = load_resource(
schema_details.module,
filename=schema_details.filename,
)
self._use_schema_arg = False
@property
def schema(self):
return self._schema
def message(self, instance: Any, err: ValidationError) -> str:
return self.shortdesc
def _iter_errors(self, validator, instance):
errs = list(validator.iter_errors(instance))
if not self.all_matches:
err = best_match(errs)
if err is not None:
err.message = self.message(instance, err)
err.context = []
err.rule = self
yield err
return
for err in errs:
yield self._clean_error(err)
def validate(
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
) -> ValidationResult:
# if the schema has a description will only replace the message with that
# description and use the best error for the location information
if not self._use_schema_arg:
schema = self._schema
cfn_validator = self.extend_validator(
validator=validator.evolve(
function_filter=validator.function_filter.evolve(
validate_dynamic_references=False,
add_cfn_lint_keyword=False,
)
),
schema=schema,
context=validator.context.evolve(
functions=[],
strict_types=True,
),
)
yield from self._iter_errors(cfn_validator, instance)