File size: 1,907 Bytes
b40c49c | 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 | """
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import json
from cfnlint.jsonschema import ValidationError
from cfnlint.rules import CloudFormationLintRule
class EqualsIsUseful(CloudFormationLintRule):
"""
Validate that the Equals will return
true/false and not always be true or false
"""
id = "W8003"
shortdesc = "Fn::Equals will always return true or false"
description = (
"Validate Fn::Equals to see if its comparing two strings or two equal items."
" While this works it may not be intended."
)
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-equals"
tags = ["functions", "equals"]
def equals_is_useful(self, validator, s, instance, schema):
if not validator.is_type(instance, "array"):
return
if len(instance) != 2:
return
# testing on 2023/09/24
# True (boolean) != "True"
# True (boolean) == "true"
# 1 == "1"
if json.dumps(instance[0]) == json.dumps(instance[1]):
yield ValidationError(
f"{instance!r} will always return {True!r} or {False!r}",
rule=self,
)
return
try:
first = instance[0]
second = instance[1]
if validator.is_type(first, "boolean"):
first = "true" if first else "false"
if validator.is_type(second, "boolean"):
first = "true" if first else "false"
if str(first) == str(second):
yield ValidationError(
f"{instance!r} will always return {True!r} or {False!r}",
rule=self,
)
except: # noqa: E722
pass
|