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

Validation Error Collector

Build a validation error collector that aggregates multiple validation errors from different fields and provides access to them for error reporting.

Requirements

Your task is to create a module that validates user registration data and collects all validation errors into a single error object. The validator should check multiple fields and return all errors at once rather than stopping at the first error.

Input Data Structure

The validator receives a user registration object with the following structure:

{
  username: string,
  email: string,
  age: number,
  password: string
}

Validation Rules

Implement the following validation rules:

  1. Username: Must be at least 3 characters long
  2. Email: Must contain an '@' symbol
  3. Age: Must be 18 or greater
  4. Password: Must be at least 8 characters long

Error Handling

  • If validation fails for one or more fields, create and throw a single aggregated error containing all individual validation errors
  • Each validation error should include a descriptive message indicating which field failed and why
  • If all validations pass, return true

Error Access

After catching the aggregated error:

  • Provide a function that extracts and returns an array of error messages from the aggregated error
  • The function should iterate over all individual errors and collect their messages

Implementation

@generates

API

/**
 * Validates user registration data against defined rules.
 *
 * @param {Object} userData - The user registration data to validate
 * @param {string} userData.username - The username
 * @param {string} userData.email - The email address
 * @param {number} userData.age - The user's age
 * @param {string} userData.password - The password
 * @returns {boolean} Returns true if all validations pass
 * @throws {Error} Throws an aggregated error containing all validation failures
 */
function validateUser(userData) {
  // IMPLEMENTATION HERE
}

/**
 * Extracts error messages from an aggregated error.
 *
 * @param {Error} error - The aggregated error containing validation errors
 * @returns {string[]} Array of error messages
 */
function getErrorMessages(error) {
  // IMPLEMENTATION HERE
}

module.exports = {
  validateUser,
  getErrorMessages
};

Test Cases

  • Given valid user data with username "john_doe", email "john@example.com", age 25, and password "secure123", the validator returns true @test
  • Given user data with username "ab", the validator throws an error and getErrorMessages returns an array containing "Username must be at least 3 characters long" @test
  • Given user data with multiple validation failures (short username "ab", invalid email "invalidemail", age 16, short password "pass"), the validator throws an error and getErrorMessages returns an array with 4 error messages @test
  • Given an aggregated error, calling getErrorMessages returns an array with all individual error messages @test

Dependencies { .dependencies }

aggregate-error { .dependency }

Provides error aggregation functionality to combine multiple errors into a single error object.

Install with Tessl CLI

npx tessl i tessl/npm-aggregate-error

tile.json