CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--helper-module-imports

Babel helper functions for inserting module loads

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-10/

Conditional Value Validator

Build a utility function that validates and transforms configuration objects using conditional expressions. The function should throw descriptive errors when validation fails, using compact inline error handling.

Requirements

Your task is to implement a validateConfig function that:

  1. Accepts a configuration object with optional fields: port, host, timeout, and maxConnections
  2. Returns a validated and transformed configuration object with defaults applied
  3. Uses inline conditional expressions to validate each field and throw errors when invalid
  4. Provides clear error messages for each validation failure

Validation Rules

  • port: Must be a number between 1 and 65535 (default: 3000)
  • host: Must be a non-empty string (default: "localhost")
  • timeout: Must be a positive number (default: 5000)
  • maxConnections: Must be a positive integer (default: 100)

Expected Behavior

  • When a field is undefined, apply the default value
  • When a field is present but invalid, throw an error with a descriptive message
  • Return an object with all four fields populated with either provided valid values or defaults

Test Cases

  • Validates a complete valid configuration object and returns it unchanged @test
  • Applies default values when configuration object is empty @test
  • Throws an error when port is out of range (e.g., 70000) @test
  • Throws an error when host is an empty string @test
  • Throws an error when timeout is negative @test
  • Throws an error when maxConnections is not an integer @test

Implementation

@generates

API

/**
 * Validates and transforms a configuration object with defaults.
 *
 * @param {Object} config - The configuration object to validate
 * @param {number} [config.port] - Port number (1-65535)
 * @param {string} [config.host] - Host address
 * @param {number} [config.timeout] - Timeout in milliseconds
 * @param {number} [config.maxConnections] - Maximum number of connections
 * @returns {Object} Validated configuration with defaults applied
 * @throws {Error} When validation fails
 */
function validateConfig(config) {
  // IMPLEMENTATION HERE
}

module.exports = { validateConfig };

Dependencies { .dependencies }

@babel/plugin-proposal-throw-expressions { .dependency }

Enables throw expressions syntax for inline error handling in conditional expressions.

Install with Tessl CLI

npx tessl i tessl/npm-babel--helper-module-imports

tile.json