Always validate function parameters and configuration options at the beginning of functions, providing specific and actionable error messages that guide users toward correct usage. This prevents downstream errors and improves developer experience.
Always validate function parameters and configuration options at the beginning of functions, providing specific and actionable error messages that guide users toward correct usage. This prevents downstream errors and improves developer experience.
Key practices:
Example:
static validateNumber(value: string, options: { min?: number, max?: number }): string {
if (/[^0-9]/.test(value))
throw new InvalidArgumentError('Not a number.');
const parsed = parseInt(value, 10);
if (isNaN(parsed))
throw new InvalidArgumentError('Not a number.');
if (options.min !== undefined && parsed < options.min)
throw new InvalidArgumentError(`Expected a number greater than ${options.min}.`);
if (options.max !== undefined && parsed > options.max)
throw new InvalidArgumentError(`Expected a number less than ${options.max}.`);
return value;
}
// Validate enum values
if (!['begin', 'end'].includes(options.debug))
throw new Error(`Unsupported debug mode "${options.debug}", must be one of "begin" or "end"`);
// Defensive checks for edge cases
if (!options.expectedText)
throw new Error('expectedText is required for class validation');
This approach catches errors early, provides clear guidance to developers, and prevents cascading failures deeper in the system.
Enter the URL of a public GitHub repository