CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-io-ts

TypeScript runtime type system for IO decoding/encoding

72

1.14x
Overview
Eval results
Files

task.mdevals/scenario-6/

User Profile Validator

Build a runtime validation system for a complex user profile data structure that includes nested objects and collections.

Capabilities

Validate User Profile Structure

The system must validate user profile data that contains multiple levels of nested objects and arrays. A valid user profile has the following structure:

  • User object with id (number), username (string), and email (string)
  • User settings object containing:
    • Preferences object with theme (string: "light" or "dark"), language (string), and notifications (boolean)
    • Privacy object with profileVisible (boolean) and allowMessages (boolean)
  • An array of addresses where each address has:
    • Street (string), city (string), zipCode (string)
    • Coordinates object with latitude (number) and longitude (number)
  • An array of social connections where each connection has:
    • Platform (string: "twitter", "linkedin", or "github")
    • Profile object with handle (string) and verified (boolean)

Test Cases

  • Validates a complete valid user profile with all nested structures populated @test
  • Rejects a profile when user.id is a string instead of a number @test
  • Rejects a profile when settings.preferences.theme is not "light" or "dark" @test
  • Validates a profile with an empty addresses array @test
  • Rejects a profile when an address is missing the coordinates object @test
  • Rejects a profile when a social connection has an invalid platform value @test

Implementation

@generates

API

/**
 * Validates user profile data against the expected schema.
 * Returns a validation result that indicates success or failure with error details.
 */
export function validateUserProfile(data: unknown): ValidationResult;

/**
 * Type representing the validated user profile structure.
 */
export type UserProfile = {
  user: {
    id: number;
    username: string;
    email: string;
  };
  settings: {
    preferences: {
      theme: "light" | "dark";
      language: string;
      notifications: boolean;
    };
    privacy: {
      profileVisible: boolean;
      allowMessages: boolean;
    };
  };
  addresses: Array<{
    street: string;
    city: string;
    zipCode: string;
    coordinates: {
      latitude: number;
      longitude: number;
    };
  }>;
  socialConnections: Array<{
    platform: "twitter" | "linkedin" | "github";
    profile: {
      handle: string;
      verified: boolean;
    };
  }>;
};

/**
 * Validation result containing either the validated data or error information.
 */
export type ValidationResult =
  | { success: true; data: UserProfile }
  | { success: false; errors: string[] };

Dependencies { .dependencies }

io-ts { .dependency }

Provides runtime type validation for TypeScript.

fp-ts { .dependency }

Provides functional programming utilities for handling validation results.

Install with Tessl CLI

npx tessl i tessl/npm-io-ts

tile.json