Skip to content

Commit

Permalink
catch common resource schema issues in cfn validate (aws-cloudformati…
Browse files Browse the repository at this point in the history
…on#729)

expanding type_specific_keywords similar to aws-cloudformation#414
  • Loading branch information
PatMyron authored and kddejong committed Oct 24, 2022
1 parent 44b2450 commit ebd7eb2
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/rpdk/core/data_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901
LOG.debug("Resource spec validation failed", exc_info=True)
raise SpecValidationError(str(e)) from e

min_max_keywords = {
"minimum",
"maximum",
"minLength",
"maxLength",
"minProperties",
"maxProperties",
"minItems",
"maxItems",
"exclusiveMinimum",
"exclusiveMaximum",
}
try: # pylint: disable=R
for _key, schema in JsonSchemaFlattener(resource_spec).flatten_schema().items():
for property_name, property_details in schema.get("properties", {}).items():
Expand All @@ -168,44 +156,57 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901
try:
property_type = property_details["type"]
property_keywords = property_details.keys()
for types, allowed_keywords in [
keyword_mappings = [
(
{"integer", "number"},
{
"minimum",
"maximum",
"exclusiveMinimum",
"exclusiveMaximum",
"multipleOf",
},
),
(
{"string"},
{
"minLength",
"maxLength",
"pattern",
},
),
(
{"object"},
{
"minProperties",
"maxProperties",
"additionalProperties",
"patternProperties",
},
),
(
{"array"},
{
"minItems",
"maxItems",
"additionalItems",
"uniqueItems",
},
),
]:
]
type_specific_keywords = set().union(
*(mapping[1] for mapping in keyword_mappings)
)
for types, allowed_keywords in keyword_mappings:
if (
property_type in types
and min_max_keywords - allowed_keywords & property_keywords
and type_specific_keywords - allowed_keywords
& property_keywords
):
LOG.warning(
"Incorrect min/max JSON schema keywords for type: %s for property: %s",
"Incorrect JSON schema keyword(s) %s for type: %s for property: %s",
type_specific_keywords - allowed_keywords
& property_keywords,
property_type,
property_name,
)
Expand Down

0 comments on commit ebd7eb2

Please sign in to comment.