CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydantic

Data validation using Python type hints

90

1.30x
Overview
Eval results
Files

task.mdevals/scenario-9/

API Data Transformer

Summary

Build a data transformation system that converts between internal application data models and external API formats. The system should handle field name conversions automatically during serialization while maintaining internal consistency.

Problem

You're building an internal application that integrates with an external API. The external API uses camelCase naming for its JSON fields, while your application uses snake_case naming internally. You need to create data models that:

  1. Accept and validate data using internal snake_case field names
  2. Serialize data to the external API format using camelCase field names
  3. Support nested data structures with consistent naming conventions
  4. Allow some fields to have custom serialization names that don't follow the standard camelCase pattern

Requirements

Create a module api_transformer.py that defines the following:

  1. A User model with these internal fields:

    • user_id (integer)
    • full_name (string)
    • email_address (string)
    • account_status (string)
  2. An Address model with these internal fields:

    • street_name (string)
    • city_name (string)
    • zip_code (string)
  3. A UserProfile model that:

    • Contains a user field (User model)
    • Contains a primary_address field (Address model)
    • Contains a registration_date field (string)
    • The registration_date field should serialize as "regDate" (not "registrationDate")

When serializing any of these models to dictionaries, the output should automatically use camelCase field names (except for the custom "regDate" field).

Example Usage

from api_transformer import User, Address, UserProfile

# Create models using internal snake_case names
user = User(
    user_id=101,
    full_name="Alice Smith",
    email_address="alice@example.com",
    account_status="active"
)

address = Address(
    street_name="123 Main St",
    city_name="Springfield",
    zip_code="12345"
)

profile = UserProfile(
    user=user,
    primary_address=address,
    registration_date="2024-01-15"
)

# Serialize to external API format
api_data = profile.to_api_format()
print(api_data)

Expected output structure:

{
  "user": {
    "userId": 101,
    "fullName": "Alice Smith",
    "emailAddress": "alice@example.com",
    "accountStatus": "active"
  },
  "primaryAddress": {
    "streetName": "123 Main St",
    "cityName": "Springfield",
    "zipCode": "12345"
  },
  "regDate": "2024-01-15"
}

Test Cases { .test }

Create test file test_api_transformer.py:

Test 1: User serialization with camelCase { .test @test }

from api_transformer import User

def test_user_serialization():
    user = User(
        user_id=42,
        full_name="Bob Jones",
        email_address="bob@test.com",
        account_status="pending"
    )
    result = user.to_api_format()
    assert result == {
        "userId": 42,
        "fullName": "Bob Jones",
        "emailAddress": "bob@test.com",
        "accountStatus": "pending"
    }

Test 2: Address serialization with camelCase { .test @test }

from api_transformer import Address

def test_address_serialization():
    address = Address(
        street_name="456 Oak Ave",
        city_name="Portland",
        zip_code="97201"
    )
    result = address.to_api_format()
    assert result == {
        "streetName": "456 Oak Ave",
        "cityName": "Portland",
        "zipCode": "97201"
    }

Test 3: UserProfile with custom alias { .test @test }

from api_transformer import UserProfile, User, Address

def test_user_profile_custom_alias():
    user = User(user_id=1, full_name="Test User",
                email_address="test@example.com", account_status="active")
    address = Address(street_name="Main St", city_name="Boston", zip_code="02101")
    profile = UserProfile(user=user, primary_address=address, registration_date="2024-06-01")

    result = profile.to_api_format()
    assert "regDate" in result
    assert result["regDate"] == "2024-06-01"
    assert "registrationDate" not in result

Test 4: Nested structure serialization { .test @test }

from api_transformer import UserProfile, User, Address

def test_nested_serialization():
    user = User(user_id=99, full_name="Jane Doe",
                email_address="jane@example.com", account_status="active")
    address = Address(street_name="Elm St", city_name="Seattle", zip_code="98101")
    profile = UserProfile(user=user, primary_address=address, registration_date="2023-12-01")

    result = profile.to_api_format()
    assert result["user"]["userId"] == 99
    assert result["primaryAddress"]["cityName"] == "Seattle"
    assert result["regDate"] == "2023-12-01"

Dependencies { .dependencies }

pydantic { .dependency }

Provides data validation and serialization support.

Install with Tessl CLI

npx tessl i tessl/pypi-pydantic

tile.json