Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Types Laravel Package

atournayre/types

Lightweight PHP library providing reusable types/value objects. Installable via Composer, intended to standardize and validate common domain data. Open-source on GitHub with issue tracker and MIT license.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Email Validation Enhancement: The new EmailAddress type introduces specialized email validation (e.g., RFC compliance, disposable domain checks), addressing a critical gap in Laravel/PHP ecosystems where email validation is often handled ad-hoc (e.g., Str::of($email)->contains('@')). This aligns with:
    • Laravel Validation: Complements Validator::make()->email() with stricter rules (e.g., rejecting user@example or user@disposable.com).
    • API Contracts: Enables type-safe email handling in DTOs (e.g., UserType::email).
    • Domain Modeling: Supports value objects for emails (e.g., EmailAddress::fromString($raw)).
  • Limitation: Still requires PHP 8.0+ for basic type support. The package’s niche focus (email-specific) may limit broader adoption unless extended to other domains (e.g., phone numbers, URLs).

Integration Feasibility

  • Low-Coupling: Email-specific types can be adopted incrementally without affecting dynamic Laravel code.
    • Use Case 1: Replace Validator::email() with Type::email()->validate($request->email).
    • Use Case 2: Enforce email types in Eloquent models (e.g., protected $emailType = EmailAddress::class).
  • Tooling Synergy:
    • PHPStan: Can now statically analyze email fields with EmailAddress constraints.
    • Laravel Fortify: Type-constrain auth emails (e.g., EmailAddress::fromRequest()).
  • Potential Conflicts:
    • Overhead: Disposable email checks add runtime cost (~5–10ms per validation). Benchmark against laravel-shift/email-verification.
    • Legacy Systems: Dynamic email handling (e.g., user@{$dynamicDomain}.com) may conflict with strict typing.

Technical Risk

Risk Area Mitigation Strategy
False Positives Combine with Laravel’s Validator (e.g., Type::email()->validate() + Rule::unique()).
Performance Cache EmailAddress validation results for bulk operations (e.g., user imports).
Adoption Resistance Pilot in registration/login flows where email validation is critical.
Maintenance Monitor for updates to RFC standards or disposable email lists.

Key Questions

  1. Does EmailAddress support custom disposable domain lists (e.g., temp-mail.org) or rely on a third-party API?
  2. Can it integrate with Laravel’s HasEmailVerification trait for seamless email validation + verification?
  3. Are there performance benchmarks vs. existing solutions (e.g., egulias/email-validator)?
  4. Does it handle internationalized email addresses (e.g., Unicode domains)?
  5. Can EmailAddress be serialized/deserialized (e.g., for API responses or database storage)?

Integration Approach

Stack Fit

  • PHP 8.0+: Required for type system features used by EmailAddress.
  • Laravel 9+: Ideal for:
    • Form Requests: Extend FormRequest with typed email validation.
    • Eloquent: Add EmailAddress to model casts or accessors.
    • API Resources: Type email fields in responses (e.g., UserResource::email()).
  • Alternatives: If using Symfony Mailer, assess overlap with Symfony\Component\Validator\Constraints\Email.

Migration Path

  1. Phase 1: Email Validation
    • Replace Validator::email() with Type::email()->validate() in controllers.
    • Example:
      use Type\EmailAddress;
      
      public function store(Request $request) {
          $email = EmailAddress::validate($request->email); // Throws on failure
          // Proceed with typed $email
      }
      
  2. Phase 2: Eloquent Integration
    • Add EmailAddress to model casts or accessors:
      protected $casts = [
          'email' => EmailAddress::class,
      ];
      
  3. Phase 3: Domain Layer
    • Use EmailAddress in services/repositories (e.g., UserService::create(EmailAddress $email)).

Compatibility

  • Backward Compatibility: Minimal risk. Existing dynamic email handling remains unchanged.
  • Laravel-Specific:
    • Notifications: Type email recipients in Mailable classes.
    • Password Reset: Integrate with Illuminate\Auth\Passwords\PasswordBroker.
  • Third-Party Packages:
    • Laravel Nova: Custom field validation for email fields.
    • Spatie Mailables: Type email attachments or recipients.

Sequencing

Priority Component Integration Strategy
High Registration/Login Replace Validator::email() with EmailAddress::validate().
Medium Eloquent Models Add EmailAddress casts/accessors.
Low Legacy Dynamic Code Isolate in non-critical paths (e.g., admin panels).
Future Real-time Features Livewire/Inertia form submissions with typed email validation.

Operational Impact

Maintenance

  • Pros:
    • Reduced Bugs: Catches malformed emails early (e.g., missing @, disposable domains).
    • Consistency: Enforces email format across APIs, notifications, and databases.
  • Cons:
    • Boilerplate: May require wrapping existing email fields in EmailAddress.
    • Dependency: Relies on package updates for disposable email lists/RFC changes.
  • Mitigation:
    • Use macros to extend EmailAddress with custom rules (e.g., EmailAddress::companyDomain('acme.com')).

Support

  • Developer Onboarding:
    • Pros: Clearer error messages (e.g., "Invalid email: user@example").
    • Cons: Teams unfamiliar with static typing may resist adoption.
  • Runtime Errors:
    • Logging: Integrate with Laravel’s logging to track email validation failures.
    • Fallback: Provide dynamic fallback (e.g., EmailAddress::tryValidate($email)).
  • Community:
    • Limited Adoption: Package’s niche focus may limit community support. Document fallback to egulias/email-validator if needed.

Scaling

  • Performance:
    • Runtime Cost: Disposable email checks add ~5–10ms per validation. Cache results for bulk operations.
    • Caching: Store validated EmailAddress instances in memory (e.g., Redis).
  • Team Scaling:
    • Consistency: Enforces email standards across microservices/APIs.
    • Handoffs: Types serve as documentation for frontend/backend teams.
  • Infrastructure:
    • No Impact: Pure PHP library; no external dependencies beyond PHP’s core.

Failure Modes

Scenario Impact Mitigation
Strict Validation Fails Rejected valid emails (e.g., user+tag@example.com). Whitelist edge cases or use EmailAddress::looseValidate().
Disposable List Stale False positives (e.g., user@temp-mail.org accepted). Update disposable domain lists manually or integrate with a paid API.
Over-Typing Excessive boilerplate for simple emails. Reserve for critical paths (e.g., user auth); use dynamic for internal.
Tooling Conflicts PHPStan false positives. Configure tooling to ignore EmailAddress or extend its rules.

Ramp-Up

  • Training:
    • Workshop: 30-minute session on EmailAddress vs. Validator::email().
    • Cheat Sheet: Examples for controllers, Eloquent, and API resources.
  • Pilot Project:
    • Scope: User registration/login flows.
    • Metrics: Track reduction in invalid email submissions.
  • Gradual Rollout:
    • Phase 1: Typed validation in auth endpoints.
    • Phase 2: Eloquent models and notifications.
  • Fallback Plan:
    • Dynamic Mode: Provide EmailAddress::tryValidate($email) with fallback to filter_var($email, FILTER_VALIDATE_EMAIL).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky