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

Batch Validation Error Reporter

Build a validation error reporter that collects multiple validation errors from different data fields and produces a well-formatted consolidated error report.

Requirements

Your task is to implement a validation error reporter for a data processing system. When validating records with multiple fields, validation can fail on several fields simultaneously. Instead of stopping at the first error, the system should collect all validation errors and report them together in a clean, readable format.

Implement a function validateRecord(record) that:

  1. Accepts a record object with fields: email, age, and username
  2. Validates each field according to these rules:
    • email: Must contain an '@' symbol
    • age: Must be a number between 0 and 120
    • username: Must be at least 3 characters long
  3. If any validations fail, throws a single error that consolidates all validation failures with properly formatted output
  4. If all validations pass, returns true

The consolidated error should display all validation failures in a clean, indented format that makes it easy to identify which validations failed.

Implementation

@generates

API

/**
 * Validates a record object with email, age, and username fields.
 *
 * @param {Object} record - The record to validate
 * @param {string} record.email - Email address
 * @param {number} record.age - Age value
 * @param {string} record.username - Username
 * @returns {boolean} Returns true if all validations pass
 * @throws {Error} Throws a consolidated error with all validation failures
 */
function validateRecord(record) {
  // IMPLEMENTATION HERE
}

module.exports = { validateRecord };

Test Cases

  • Given a valid record with email "test@example.com", age 25, username "john_doe", the function returns true @test
  • Given a record with invalid email "invalid-email", valid age 30, valid username "alice", the function throws an error mentioning the email validation failure @test
  • Given a record with valid email "bob@test.com", invalid age 150, valid username "bobby", the function throws an error mentioning the age validation failure @test
  • Given a record with invalid email "no-at-sign", invalid age -5, invalid username "ab", the function throws a single error that consolidates all three validation failures in a formatted way @test

Dependencies { .dependencies }

aggregate-error { .dependency }

Provides error aggregation and formatting capabilities.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-aggregate-error

tile.json