-
Notifications
You must be signed in to change notification settings - Fork 1
Extract shared from_dict validation helpers for model classes (#70) #80
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 1 commit
Commits
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
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
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,96 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| def require_dict(raw: Any, *, model: str, field: str) -> dict[str, Any]: | ||
| """Raise SchemaError when raw is not a dict; return raw for chaining.""" | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| model, | ||
| field, | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| return raw | ||
|
|
||
|
|
||
| def require_key(raw: dict[str, Any], key: str, *, model: str) -> None: | ||
| """Raise SchemaError when a required key is absent.""" | ||
| if key not in raw: | ||
| raise SchemaError(model, key) | ||
|
|
||
|
|
||
| def require_non_empty_str(value: Any, *, model: str, field: str) -> str: | ||
| """Validate a caller-supplied id (workspace_id, composer_id, bubble_id).""" | ||
| if not isinstance(value, str) or not value: | ||
| raise SchemaError( | ||
| model, | ||
| field, | ||
| hint=f"expected non-empty str, got {type(value).__name__}", | ||
| ) | ||
| return value | ||
|
|
||
|
|
||
| def require_non_empty_str_field(raw: dict[str, Any], key: str, *, model: str) -> str: | ||
| """Validate a non-empty string field read from raw.""" | ||
| value = raw.get(key) | ||
| if not isinstance(value, str) or not value: | ||
| raise SchemaError( | ||
| model, | ||
| key, | ||
| hint=f"expected non-empty str, got {type(value).__name__}", | ||
| ) | ||
| return value | ||
|
|
||
|
|
||
| def require_non_empty_str_fields( | ||
| raw: dict[str, Any], | ||
| keys: tuple[str, ...], | ||
| *, | ||
| model: str, | ||
| ) -> None: | ||
| """Validate multiple non-empty string fields in raw (ExportEntry pattern).""" | ||
| for key in keys: | ||
| value = raw.get(key) | ||
| if not isinstance(value, str) or value == "": | ||
| raise SchemaError( | ||
| model, | ||
| key, | ||
| hint=f"expected non-empty str, got {type(value).__name__}", | ||
| ) | ||
|
|
||
|
|
||
| def require_truthy(value: Any, *, model: str, field: str) -> Any: | ||
| """Raise missing-field SchemaError when value is falsy (absent or empty).""" | ||
| if not value: | ||
| raise SchemaError(model, field) | ||
| return value | ||
|
|
||
|
|
||
| def require_type( | ||
| value: Any, | ||
| expected: type[Any] | tuple[type[Any], ...], | ||
| *, | ||
| model: str, | ||
| field: str, | ||
| hint: str | None = None, | ||
| ) -> Any: | ||
| if not isinstance(value, expected): | ||
| raise SchemaError( | ||
| model, | ||
| field, | ||
| hint=hint or f"expected {expected!r}, got {type(value).__name__}", | ||
| ) | ||
| return value | ||
|
|
||
|
|
||
| def require_optional_str(value: Any, *, model: str, field: str) -> str | None: | ||
| if value is not None and not isinstance(value, str): | ||
| raise SchemaError( | ||
| model, | ||
| field, | ||
| hint=f"expected str or None, got {type(value).__name__}", | ||
| ) | ||
| return value | ||
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
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.