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-2/

User Settings Validator

Build a user settings validator that enforces strict validation rules and handles updates safely.

Requirements

Create a UserSettings model that:

  1. Uses strict validation to prevent type coercion (e.g., string "123" should not be accepted as an integer)
  2. Validates data when attributes are reassigned after initialization
  3. Rejects any extra fields not defined in the model
  4. Strips leading and trailing whitespace from all string inputs automatically

The model should have these fields:

  • user_id: an integer representing the user's ID
  • email: a string for the user's email address
  • max_connections: an integer for maximum allowed connections (must be positive)
  • theme: a string for UI theme preference

Test Cases

  • Creating a UserSettings with user_id="123" (string instead of int) raises a validation error @test
  • Creating a UserSettings with an undefined field extra_field raises a validation error @test
  • After creating a valid UserSettings, reassigning email to an integer raises a validation error @test
  • Creating a UserSettings with theme=" dark " (with spaces) stores the value as "dark" (spaces stripped) @test

Implementation

@generates

API

from pydantic import BaseModel, ConfigDict, Field

class UserSettings(BaseModel):
    """User settings with strict validation and safe updates."""

    user_id: int
    email: str
    max_connections: int = Field(gt=0)
    theme: str

# Example usage:
# settings = UserSettings(user_id=1, email="user@example.com", max_connections=5, theme="dark")
# settings.email = "new@example.com"  # This should revalidate

Dependencies { .dependencies }

pydantic { .dependency }

Provides data validation support with model configuration options.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/pypi-pydantic

tile.json