-
Notifications
You must be signed in to change notification settings - Fork 7
Feat/validate api responses #1129
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
isabeleliassen
merged 6 commits into
csg-org:development
from
InspiringApps:feat/validate-api-responses
Oct 7, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6acfaa7
Add validation to staff get me, purchase options
jusdino fb4625a
Add more response schema validation
jusdino 1659669
Update api docs
jusdino 774ed5b
Use sanitized privilege options
jusdino cf7d34d
Simplify import
jusdino dd72a41
Update test snapshot
jusdino 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
432 changes: 216 additions & 216 deletions
432
backend/compact-connect/docs/api-specification/latest-oas30.json
Large diffs are not rendered by default.
Oops, something went wrong.
6,227 changes: 3,113 additions & 3,114 deletions
6,227
backend/compact-connect/docs/internal/api-specification/latest-oas30.json
Large diffs are not rendered by default.
Oops, something went wrong.
282 changes: 141 additions & 141 deletions
282
backend/compact-connect/docs/internal/postman/postman-collection.json
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 19 additions & 19 deletions
38
backend/compact-connect/docs/postman/postman-collection.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
...compact-connect/lambdas/python/common/cc_common/data_model/schema/attestation/__init__.py
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,3 @@ | ||
| # ruff: noqa: F401 | ||
| from .api import AttestationResponseSchema | ||
| from .record import AttestationRecordSchema |
30 changes: 30 additions & 0 deletions
30
backend/compact-connect/lambdas/python/common/cc_common/data_model/schema/attestation/api.py
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,30 @@ | ||
| # ruff: noqa: N801, N815 invalid-name | ||
| from marshmallow.fields import Boolean, Raw, String | ||
| from marshmallow.validate import OneOf | ||
|
|
||
| from cc_common.data_model.schema.base_record import ForgivingSchema | ||
| from cc_common.data_model.schema.fields import Compact | ||
|
|
||
|
|
||
| class AttestationResponseSchema(ForgivingSchema): | ||
| """ | ||
| Schema for attestation API responses. | ||
|
|
||
| This schema validates the response from the attestation endpoint, | ||
| matching the actual API behavior as tested. | ||
|
|
||
| Serialization direction: | ||
| Python -> load() -> API | ||
| """ | ||
|
|
||
| type = String(required=True, allow_none=False, validate=OneOf(['attestation'])) | ||
| compact = Compact(required=True, allow_none=False) | ||
| attestationId = String(required=True, allow_none=False) | ||
| version = String(required=True, allow_none=False) | ||
| dateCreated = Raw(required=True, allow_none=False) | ||
| dateOfUpdate = Raw(required=True, allow_none=False) | ||
| text = String(required=True, allow_none=False) | ||
| required = Boolean(required=True, allow_none=False) | ||
| displayName = String(required=True, allow_none=False) | ||
| description = String(required=True, allow_none=False) | ||
| locale = String(required=True, allow_none=False, validate=OneOf(['en'])) |
File renamed without changes.
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
83 changes: 83 additions & 0 deletions
83
backend/compact-connect/lambdas/python/common/cc_common/data_model/schema/purchase/api.py
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,83 @@ | ||
| # ruff: noqa: N801, N815 invalid-name | ||
| from cc_common.data_model.schema.base_record import ForgivingSchema | ||
| from cc_common.data_model.schema.compact.api import CompactOptionsResponseSchema | ||
| from cc_common.data_model.schema.compact.common import COMPACT_TYPE | ||
| from cc_common.data_model.schema.jurisdiction.api import JurisdictionOptionsResponseSchema | ||
| from cc_common.data_model.schema.jurisdiction.common import JURISDICTION_TYPE | ||
| from marshmallow import ValidationError, validates_schema | ||
| from marshmallow.fields import Dict, List, String | ||
|
|
||
|
|
||
| class PurchasePrivilegeOptionsResponseSchema(ForgivingSchema): | ||
| """ | ||
| Schema for purchase privilege options response. | ||
|
|
||
| This schema validates the overall response structure containing available privilege | ||
| purchase options for a provider. It validates each item individually based on its | ||
| type field, ensuring only supported types are allowed through. | ||
|
|
||
| Serialization direction: | ||
| Python -> load() -> API | ||
| """ | ||
|
|
||
| items = List( | ||
| Dict(required=True, allow_none=False), # Allow any dict through since items are validated individually | ||
| required=True, | ||
| allow_none=False, | ||
| ) | ||
|
|
||
| @validates_schema | ||
| def validate_items(self, data, **kwargs): # noqa: ARG002 unused-argument | ||
| """Validate each item in the items list based on its type field.""" | ||
| if 'items' not in data: | ||
| return # Let the field validation handle missing items | ||
|
|
||
| items = data['items'] | ||
| if not isinstance(items, list): | ||
| raise ValidationError({'items': ['Expected a list of items']}) | ||
|
|
||
| sanitized_items = [] | ||
| for i, item in enumerate(items): | ||
| # Ensure item is a dictionary | ||
| if not isinstance(item, dict): | ||
| raise ValidationError({'items': {i: [f'Invalid item type: expected dict, got {type(item).__name__}']}}) | ||
|
|
||
| # Ensure item has a type field | ||
| if 'type' not in item: | ||
| raise ValidationError({'items': {i: ['Item missing required "type" field']}}) | ||
|
|
||
| # Validate based on type | ||
| item_type = item['type'] | ||
| if item_type == COMPACT_TYPE: | ||
| # Validate as compact option | ||
| compact_schema = CompactOptionsResponseSchema() | ||
| try: | ||
| sanitized_items.append(compact_schema.load(item)) | ||
| except ValidationError as e: | ||
| raise ValidationError({'items': {i: {'compact': e.messages}}}) from e | ||
| elif item_type == JURISDICTION_TYPE: | ||
| # Validate as jurisdiction option | ||
| jurisdiction_schema = JurisdictionOptionsResponseSchema() | ||
| try: | ||
| sanitized_items.append(jurisdiction_schema.load(item)) | ||
| except ValidationError as e: | ||
| raise ValidationError({'items': {i: {'jurisdiction': e.messages}}}) from e | ||
| else: | ||
| # Reject unsupported types | ||
| raise ValidationError({'items': {i: [f'Unsupported item type: {item_type}']}}) | ||
| data['items'] = sanitized_items # Replace original items with sanitized ones | ||
|
|
||
|
|
||
| class TransactionResponseSchema(ForgivingSchema): | ||
| """ | ||
| Schema for transaction processing response. | ||
|
|
||
| This schema validates the response from payment processor transaction operations. | ||
|
|
||
| Serialization direction: | ||
| Python -> load() -> API | ||
| """ | ||
|
|
||
| transactionId = String(required=True, allow_none=False) | ||
| message = String(required=False, allow_none=False) | ||
| lineItems = List(Dict(required=True, allow_none=False), required=False, allow_none=False) |
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.
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.