Create an error from multiple errors
94
Build a validation error collector that aggregates multiple validation errors from different fields and provides access to them for error reporting.
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.
The validator receives a user registration object with the following structure:
{
username: string,
email: string,
age: number,
password: string
}Implement the following validation rules:
trueAfter catching the aggregated error:
@generates
/**
* 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
};Provides error aggregation functionality to combine multiple errors into a single error object.
Install with Tessl CLI
npx tessl i tessl/npm-aggregate-errordocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9