-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Annotate DynamoDB schemas with validation code #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
25476b7
feat: Annotate DynamoDB schemas with validation code
khvn26 a6f7bb9
fix tests, annotated types
khvn26 6940b2b
Improve coverage
khvn26 959bc8c
add max string length
khvn26 dd72418
fix typo
khvn26 b08502f
O(n) `validate_identity_feature_states`
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from importlib.util import find_spec | ||
|
|
||
| PYDANTIC_INSTALLED = find_spec("pydantic") is not None | ||
| MAX_STRING_FEATURE_STATE_VALUE_LENGTH = 20_000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from datetime import datetime | ||
| from decimal import Decimal | ||
| from uuid import UUID | ||
|
|
||
| from pydantic import AfterValidator, BeforeValidator, ValidateAs | ||
|
|
||
| from flagsmith_schemas.validators import ( | ||
| validate_dynamo_feature_state_value, | ||
| validate_identity_feature_states, | ||
| validate_multivariate_feature_state_values, | ||
| ) | ||
|
|
||
| ValidateDecimalAsFloat = ValidateAs(float, lambda v: Decimal(str(v))) | ||
| ValidateDecimalAsInt = ValidateAs(int, lambda v: Decimal(v)) | ||
| ValidateStrAsISODateTime = ValidateAs(datetime, lambda dt: dt.isoformat()) | ||
| ValidateStrAsUUID = ValidateAs(UUID, str) | ||
|
|
||
| ValidateDynamoFeatureStateValue = BeforeValidator(validate_dynamo_feature_state_value) | ||
| ValidateIdentityFeatureStatesList = AfterValidator(validate_identity_feature_states) | ||
| ValidateMultivariateFeatureValuesList = AfterValidator( | ||
| validate_multivariate_feature_state_values | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import typing | ||
| from decimal import Decimal | ||
|
|
||
| from flagsmith_schemas.constants import MAX_STRING_FEATURE_STATE_VALUE_LENGTH | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from flagsmith_schemas.dynamodb import FeatureState, MultivariateFeatureStateValue | ||
| from flagsmith_schemas.types import DynamoFeatureValue | ||
|
|
||
|
|
||
| def validate_dynamo_feature_state_value( | ||
| value: typing.Any, | ||
| ) -> "DynamoFeatureValue": | ||
| if isinstance(value, bool | None): | ||
| return value | ||
| if isinstance(value, str): | ||
| if len(value) > MAX_STRING_FEATURE_STATE_VALUE_LENGTH: | ||
| raise ValueError( | ||
| "Dynamo feature state value string length cannot exceed " | ||
| f"{MAX_STRING_FEATURE_STATE_VALUE_LENGTH} characters " | ||
| f"(got {len(value)} characters)." | ||
| ) | ||
| return value | ||
| if isinstance(value, int): | ||
| return Decimal(value) | ||
| return str(value) | ||
|
|
||
|
|
||
| def validate_multivariate_feature_state_values( | ||
| values: "list[MultivariateFeatureStateValue]", | ||
| ) -> "list[MultivariateFeatureStateValue]": | ||
| total_percentage = sum(value["percentage_allocation"] for value in values) | ||
| if total_percentage > 100: | ||
| raise ValueError( | ||
| "Total `percentage_allocation` of multivariate feature state values " | ||
| "cannot exceed 100." | ||
| ) | ||
| return values | ||
|
|
||
|
|
||
| def validate_identity_feature_states( | ||
| values: "list[FeatureState]", | ||
| ) -> "list[FeatureState]": | ||
| seen: set[Decimal] = set() | ||
|
|
||
| for feature_state in values: | ||
| feature_id = feature_state["feature"]["id"] | ||
| if feature_id in seen: | ||
| raise ValueError( | ||
| f"Feature id={feature_id} cannot have multiple " | ||
| "feature states for a single identity." | ||
| ) | ||
| seen.add(feature_id) | ||
|
|
||
| return values |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.