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

User Configuration Validator

Build a user configuration validator that validates and parses configuration objects from external sources using runtime type checking.

Requirements

Your system should validate user configuration objects that contain user profiles. The configuration must include:

  • A username field (string, required)
  • An age field (number, required)
  • An email field (string, required)
  • An isActive field (boolean, required)

Your implementation should:

  1. Define a validation schema for the user configuration structure
  2. Implement a validateConfig function that accepts an unknown input and returns a validation result
  3. When validation succeeds, return an object with success: true and the validated data
  4. When validation fails, return an object with success: false and a user-friendly errors array containing error messages

Test Cases

  • When given a valid configuration object { username: 'alice', age: 30, email: 'alice@example.com', isActive: true }, it returns success with the validated data @test
  • When given an invalid configuration with a string age { username: 'bob', age: '25', email: 'bob@example.com', isActive: true }, it returns failure with error messages @test
  • When given a configuration missing the email field { username: 'charlie', age: 35, isActive: false }, it returns failure with error messages @test

Implementation

@generates

API

export interface ValidationSuccess<T> {
  success: true;
  data: T;
}

export interface ValidationFailure {
  success: false;
  errors: string[];
}

export type ValidationResult<T> = ValidationSuccess<T> | ValidationFailure;

export function validateConfig(input: unknown): ValidationResult<{
  username: string;
  age: number;
  email: string;
  isActive: boolean;
}>;

Dependencies { .dependencies }

io-ts { .dependency }

Provides runtime type validation and decoding capabilities.

fp-ts { .dependency }

Provides functional programming utilities including the Either type used by io-ts.

Install with Tessl CLI

npx tessl i tessl/npm-io-ts

tile.json