Skip to content

Commit 492b12a

Browse files
authored
Switch event rule requiredXor to requiredOr (#3688)
1 parent 7b0ad97 commit 492b12a

File tree

12 files changed

+62
-20
lines changed

12 files changed

+62
-20
lines changed

docs/cfn-schema-specification.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ _properties_ provides the key names and a value that represents the schema to va
8383

8484
_required_ defines a list of required properties. [JSON Schema docs](https://json-schema.org/understanding-json-schema/reference/object#required)
8585

86-
##### requiredXor
86+
##### requiredOr
8787

88-
_requiredXor_ is used to define when only one property from a set properties is required.
88+
_requiredOr_ is used to define when at least one property from a set properties is required.
8989

9090
On the following defined object
9191

@@ -104,15 +104,15 @@ The cfn-lint schema
104104

105105
```json
106106
{
107-
"requiredXor": ["a", "b", "c"]
107+
"requiredOr": ["a", "b", "c"]
108108
}
109109
```
110110

111111
is equivalent to the JSON schema
112112

113113
```json
114114
{
115-
"oneOf": [
115+
"anyOf": [
116116
{
117117
"required": ["a"]
118118
},
@@ -126,9 +126,9 @@ is equivalent to the JSON schema
126126
}
127127
```
128128

129-
##### propertiesNand
129+
##### requiredXor
130130

131-
_propertiesNand_ is used to define when none or only one property from a set properties can be defined.
131+
_requiredXor_ is used to define when only one property from a set properties is required.
132132

133133
On the following defined object
134134

@@ -147,7 +147,7 @@ The cfn-lint schema
147147

148148
```json
149149
{
150-
"propertiesNand": ["a", "b", "c"]
150+
"requiredXor": ["a", "b", "c"]
151151
}
152152
```
153153

@@ -164,13 +164,6 @@ is equivalent to the JSON schema
164164
},
165165
{
166166
"required": ["c"]
167-
},
168-
{
169-
"properties": {
170-
"a": false,
171-
"b": false,
172-
"c": false
173-
}
174167
}
175168
]
176169
}

scripts/update_schemas_manually.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@
965965
resource_type="AWS::Events::Rule",
966966
patches=[
967967
Patch(
968-
values={"requiredXor": ["EventPattern", "ScheduleExpression"]},
968+
values={"requiredOr": ["EventPattern", "ScheduleExpression"]},
969969
path="/",
970970
),
971971
Patch(

src/cfnlint/data/schemas/patches/extensions/all/aws_events_rule/manual.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"op": "add",
4-
"path": "/requiredXor",
4+
"path": "/requiredOr",
55
"value": [
66
"EventPattern",
77
"ScheduleExpression"

src/cfnlint/data/schemas/providers/us_east_1/aws-events-rule.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@
535535
"readOnlyProperties": [
536536
"/properties/Arn"
537537
],
538-
"requiredXor": [
538+
"requiredOr": [
539539
"EventPattern",
540540
"ScheduleExpression"
541541
],

src/cfnlint/data/schemas/providers/us_gov_east_1/aws-events-rule.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
"/properties/Id",
497497
"/properties/Arn"
498498
],
499-
"requiredXor": [
499+
"requiredOr": [
500500
"EventPattern",
501501
"ScheduleExpression"
502502
],

src/cfnlint/data/schemas/providers/us_gov_west_1/aws-events-rule.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
"/properties/Id",
497497
"/properties/Arn"
498498
],
499-
"requiredXor": [
499+
"requiredOr": [
500500
"EventPattern",
501501
"ScheduleExpression"
502502
],

src/cfnlint/jsonschema/_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class FunctionFilter:
4545
"minItems",
4646
"minProperties",
4747
"required",
48-
"requiredAtLeastOne",
48+
"requiredOr",
4949
"requiredXor",
5050
"then",
5151
"uniqueItems",

src/cfnlint/jsonschema/_keywords.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,17 @@ def required(
541541
yield ValidationError(f"{property!r} is a required property")
542542

543543

544+
def requiredOr(
545+
validator: Validator, required: Any, instance: Any, schema: dict[str, Any]
546+
) -> ValidationResult:
547+
if not validator.is_type(instance, "object"):
548+
return
549+
matches = set(required).intersection(instance.keys())
550+
if not matches:
551+
yield ValidationError(f"One of {required!r} is a required property")
552+
return
553+
554+
544555
def requiredXor(
545556
validator: Validator, required: Any, instance: Any, schema: dict[str, Any]
546557
) -> ValidationResult:

src/cfnlint/jsonschema/validators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def extend(
396396
"properties": _keywords.properties,
397397
"propertyNames": _keywords.propertyNames,
398398
"required": _keywords.required,
399+
"requiredOr": _keywords.requiredOr,
399400
"requiredXor": _keywords.requiredXor,
400401
"type": _keywords.type,
401402
"uniqueItems": _keywords.uniqueItems,

src/cfnlint/rules/resources/properties/Properties.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self):
3737
"dependentExcluded": "E3020",
3838
"dependentRequired": "E3021",
3939
"required": "E3003",
40+
"requiredOr": "E3058",
4041
"requiredXor": "E3014",
4142
"enum": "E3030",
4243
"type": "E3012",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from cfnlint.rules import CloudFormationLintRule
7+
8+
9+
class RequiredOr(CloudFormationLintRule):
10+
id = "E3058"
11+
shortdesc = "Validate at least one of the properties are required"
12+
description = "Make sure at least one of the resource properties are included"
13+
source_url = "https://github.com/aws-cloudformation/cfn-lint/blob/main/docs/cfn-schema-specification.md#requiredor"
14+
tags = ["resources"]

test/unit/module/jsonschema/test_validator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,28 @@ def test_validator(name, schema, instance, expected, validator):
947947
)
948948
],
949949
),
950+
(
951+
"valid requiredOr",
952+
{"requiredOr": ["foo", "bar"]},
953+
{"foo": {}},
954+
[],
955+
),
956+
(
957+
"valid requiredOr with wrong type",
958+
{"requiredOr": ["foo", "bar"]},
959+
[],
960+
[],
961+
),
962+
(
963+
"invalid requiredOr with empty object",
964+
{"requiredOr": ["foo", "bar"]},
965+
{},
966+
[
967+
ValidationError(
968+
"One of ['foo', 'bar'] is a required property",
969+
)
970+
],
971+
),
950972
(
951973
"valid requiredXor",
952974
{"requiredXor": ["foo", "bar"]},

0 commit comments

Comments
 (0)