-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,11 +1,37 @@ | ||
from marshmallow import fields, Schema | ||
# transaction_schema.py | ||
|
||
from marshmallow import Schema, fields, validate | ||
|
||
# Transaction Schema | ||
class TransactionSchema(Schema): | ||
""" | ||
A Transaction schema for the FineX project. | ||
""" | ||
id = fields.Int(dump_only=True) | ||
amount = fields.Float(required=True) | ||
type = fields.Str(required=True) | ||
timestamp = fields.DateTime(dump_only=True) | ||
user_id = fields.Int(required=True) | ||
id = fields.Int(required=True, description="Unique identifier for the transaction") | ||
sender = fields.Str(required=True, validate=validate.Length(min=1), description="The sender's identifier") | ||
recipient = fields.Str(required=True, validate=validate.Length(min=1), description="The recipient's identifier") | ||
amount = fields.Float(required=True, validate=validate.Range(min=0), description="The amount of the transaction") | ||
timestamp = fields.DateTime(required=True, description="The timestamp of the transaction") | ||
status = fields.Str(required=True, validate=validate.OneOf(["pending", "completed", "failed"]), description="The status of the transaction") | ||
currency = fields.Str(required=True, validate=validate.Length(equal=3), description="The currency code (ISO 4217)") | ||
|
||
# Example of how to use the schema | ||
if __name__ == "__main__": | ||
# Example transaction data | ||
transaction_data = { | ||
"id": 1, | ||
"sender": "user123", | ||
"recipient": "user456", | ||
"amount": 100.50, | ||
"timestamp": "2024-10-18T12:00:00", | ||
"status": "completed", | ||
"currency": "USD" | ||
} | ||
|
||
# Create an instance of the schema | ||
schema = TransactionSchema() | ||
|
||
# Validate and serialize the transaction data | ||
result = schema.load(transaction_data) | ||
print("Validated Transaction Data:", result) | ||
|
||
# Serialize the transaction data | ||
serialized_data = schema.dump(result) | ||
print("Serialized Transaction Data:", serialized_data) |