Skip to content

Commit e9bb190

Browse files
committed
refactor(schema): add docstrings and schema specification
1 parent b61cafa commit e9bb190

File tree

1 file changed

+80
-1
lines changed
  • aws_lambda_powertools/utilities/feature_flags

1 file changed

+80
-1
lines changed

aws_lambda_powertools/utilities/feature_flags/schema.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,82 @@ class RuleAction(str, Enum):
2323
CONTAINS = "CONTAINS"
2424

2525

26-
class SchemaValidator:
26+
class SchemaValidator(BaseValidator):
27+
"""Validates feature flag schema configuration
28+
29+
Schema
30+
------
31+
32+
**Feature object**
33+
34+
A dictionary containing default value and rules for matching.
35+
The value MUST be an object and MIGHT contain the following members:
36+
37+
* **default**: `bool`. Defines default feature value. This MUST be present
38+
* **rules**: `Dict[str, Dict]`. Rules object. This MIGHT be present
39+
40+
```json
41+
{
42+
"my_feature": {
43+
"default": True,
44+
"rules": {}
45+
}
46+
}
47+
```
48+
49+
**Rules object**
50+
51+
A dictionary with each rule and their conditions that a feature might have.
52+
The value MIGHT be present, and when defined it MUST contain the following members:
53+
54+
* **when_match**: `bool`. Defines value to return when context matches conditions
55+
* **conditions**: `List[Dict]`. Conditions object. This MUST be present
56+
57+
```json
58+
{
59+
"my_feature": {
60+
"default": True,
61+
"rules": {
62+
"tenant id equals 345345435": {
63+
"when_match": False,
64+
"conditions": []
65+
}
66+
}
67+
}
68+
}
69+
```
70+
71+
**Conditions object**
72+
73+
A list of dictionaries containing conditions for a given rule.
74+
The value MUST contain the following members:
75+
76+
* **action**: `str`. Operation to perform to match a key and value.
77+
The value MUST be either EQUALS, STARTSWITH, ENDSWITH, CONTAINS
78+
* **key**: `str`. Key in given context to perform operation
79+
* **value**: `Any`. Value in given context that should match action operation.
80+
81+
```json
82+
{
83+
"my_feature": {
84+
"default": True,
85+
"rules": {
86+
"tenant id equals 345345435": {
87+
"when_match": False,
88+
"conditions": [
89+
{
90+
"action": "EQUALS",
91+
"key": "tenant_id",
92+
"value": "345345435",
93+
}
94+
]
95+
}
96+
}
97+
}
98+
}
99+
```
100+
"""
101+
27102
def __init__(self, schema: Dict[str, Any]):
28103
self.schema = schema
29104

@@ -37,6 +112,8 @@ def validate(self) -> None:
37112

38113

39114
class FeaturesValidator(BaseValidator):
115+
"""Validates each feature and calls RulesValidator to validate its rules"""
116+
40117
def __init__(self, schema: Dict):
41118
self.schema = schema
42119

@@ -58,6 +135,8 @@ def validate_feature(name, feature):
58135

59136

60137
class RulesValidator(BaseValidator):
138+
"""Validates each rule and calls ConditionsValidator to validate each rule's conditions"""
139+
61140
def __init__(self, feature: Dict[str, Any]):
62141
self.feature = feature
63142
self.feature_name = next(iter(self.feature))

0 commit comments

Comments
 (0)