CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-io-ts

tessl install tessl/npm-io-ts@2.2.0

TypeScript runtime type system for IO decoding/encoding

Agent Success

Agent success rate when using this tile

72%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.14x

Baseline

Agent success rate without this tile

63%

task.mdevals/scenario-7/

Email Validator

Build a TypeScript module that validates email addresses with detailed, user-friendly error messages. The validator should check multiple constraints and provide specific feedback about what's wrong with invalid emails.

Requirements

Email Validation Rules

Implement a function that validates email addresses according to these rules:

  1. Format: Must contain exactly one @ symbol
  2. Local part (before @): Must be at least 1 character and no more than 64 characters
  3. Domain part (after @): Must be at least 3 characters and contain at least one .
  4. Characters: Only allow alphanumeric characters, dots (.), hyphens (-), and underscores (_) in the local part
  5. Top-level domain: The part after the last . must be at least 2 characters

Error Messages

When validation fails, provide clear, specific error messages that explain exactly what rule was violated:

  • "Email must contain exactly one @ symbol" - when @ count is wrong
  • "Local part must be between 1 and 64 characters" - when local part length is invalid
  • "Domain must be at least 3 characters and contain a dot" - when domain format is wrong
  • "Local part contains invalid characters" - when disallowed characters are present
  • "Top-level domain must be at least 2 characters" - when TLD is too short

API

Implement a validation function that:

  • Takes a string input
  • Returns a structured result indicating success or failure
  • On failure, includes a list of human-readable error messages
  • On success, returns the validated email string

Test Cases

  • Validates a proper email address like "user@example.com" @test
  • Rejects an email without @ symbol like "userexample.com" with message "Email must contain exactly one @ symbol" @test
  • Rejects an email with multiple @ symbols like "user@@example.com" with message "Email must contain exactly one @ symbol" @test
  • Rejects an email with empty local part like "@example.com" with message "Local part must be between 1 and 64 characters" @test
  • Rejects an email with too long local part (65+ chars) with appropriate error message @test
  • Rejects an email with invalid domain like "user@ex" with message "Domain must be at least 3 characters and contain a dot" @test
  • Rejects an email with short TLD like "user@example.c" with message "Top-level domain must be at least 2 characters" @test
  • Rejects an email with invalid characters like "user!name@example.com" with message "Local part contains invalid characters" @test

Implementation

@generates

API

/**
 * Validates an email address string.
 *
 * @param input - The email string to validate
 * @returns A result containing either the valid email or a list of error messages
 */
export function validateEmail(input: string): ValidationResult;

/**
 * Result type for email validation
 */
export type ValidationResult =
  | { success: true; value: string }
  | { success: false; errors: string[] };

Dependencies { .dependencies }

io-ts { .dependency }

Provides runtime type validation with custom error messages.

fp-ts { .dependency }

Provides functional data types like Either for handling validation results.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/io-ts@2.2.x
tile.json