CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-aggregate-error

Create an error from multiple errors

94

1.28x
Overview
Eval results
Files

task.mdevals/scenario-7/

Type-Safe Error Validator

Build a validation system that collects and categorizes multiple validation errors in a type-safe manner.

Requirements

Create a validation system with the following features:

Error Types

Define custom error types for different validation scenarios:

  • A ValidationError class that extends Error with a field property (string) and a code property (string)
  • A RangeError class that extends Error with min and max properties (numbers)

Validator Function

Implement a validateUser function that:

  • Accepts a user object with properties: name (string), email (string), age (number)
  • Returns an array of validation errors if validation fails, or an empty array if valid
  • Validates that:
    • name is not empty (error code: 'NAME_REQUIRED')
    • email contains an '@' symbol (error code: 'EMAIL_INVALID')
    • age is between 0 and 120 (use RangeError with min: 0, max: 120)

Error Handler

Implement a handleValidationErrors function that:

  • Accepts an array of errors (ValidationError or RangeError instances)
  • If the array is empty, returns null
  • If the array contains errors, creates and returns a single error that aggregates all validation errors while preserving their types
  • The returned error should allow type-safe access to the individual errors

Test Cases

  • When a user object has an empty name, missing '@' in email, and age of -5, the validator should collect all three errors (name validation error, email validation error, and range error) and the error handler should aggregate them into a single error that maintains type information for each individual error. @test

  • When a user object is valid (name: "John", email: "john@example.com", age: 25), the validator should return an empty array and the error handler should return null. @test

@generates

API

export class ValidationError extends Error {
  field: string;
  code: string;
  constructor(message: string, field: string, code: string);
}

export class RangeError extends Error {
  min: number;
  max: number;
  constructor(message: string, min: number, max: number);
}

export interface User {
  name: string;
  email: string;
  age: number;
}

export function validateUser(user: User): Array<ValidationError | RangeError>;

export function handleValidationErrors(errors: Array<ValidationError | RangeError>): Error | null;

Dependencies { .dependencies }

aggregate-error { .dependency }

Provides error aggregation support for collecting multiple errors into a single error instance.

Install with Tessl CLI

npx tessl i tessl/npm-aggregate-error

tile.json