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

Product Catalog Validator

Build a runtime validator for product catalog data that can generate both a decoder for validation and a type guard for runtime checks from a single schema definition.

Requirements

Schema Definition

Create a reusable schema for products with the following structure:

  • Product ID (number)
  • Name (string)
  • Category (string, must be one of: "electronics", "clothing", or "furniture")
  • Price (number, must be positive)
  • In stock status (boolean)

Multiple Implementations

From the single schema definition, generate:

  1. A decoder that validates untrusted data and returns validation results
  2. A type guard that performs runtime type checking

Validation Behavior

The decoder should:

  • Accept valid product data and return success
  • Reject invalid data with appropriate errors
  • Validate that the category matches one of the allowed values
  • Validate that the price is a positive number

Test Cases

  • When given valid product data {id: 1, name: "Laptop", category: "electronics", price: 999.99, inStock: true}, the decoder returns success and the guard returns true @test
  • When given product data with invalid category {id: 2, name: "Book", category: "books", price: 19.99, inStock: true}, the decoder returns failure @test
  • When given product data with negative price {id: 3, name: "Chair", category: "furniture", price: -50, inStock: false}, the decoder returns failure @test
  • When given product data missing required field {id: 4, name: "Shirt", category: "clothing"}, the decoder returns failure @test

Implementation

@generates

API

import { Either } from 'fp-ts/Either';

/**
 * Product type representing catalog items
 */
export interface Product {
  id: number;
  name: string;
  category: 'electronics' | 'clothing' | 'furniture';
  price: number;
  inStock: boolean;
}

/**
 * Decoder for validating product data
 * Validates untrusted data against the product schema
 */
export const productDecoder: {
  decode(input: unknown): Either<unknown, Product>;
};

/**
 * Type guard for runtime product type checking
 * Returns true if the input matches the product schema
 */
export const isProduct: (input: unknown) => input is Product;

Dependencies { .dependencies }

io-ts { .dependency }

Provides schema-based type system with custom interpreters for generating decoders and guards from a single schema definition.

fp-ts { .dependency }

Provides functional programming utilities including Either type for representing validation results.

Version

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