Hone logo
Hone
Problems

Angular Template Validation with Custom Directives

Angular's built-in template validation is helpful, but often insufficient for enforcing specific coding standards or business rules within templates. This challenge asks you to implement a custom directive that performs more rigorous template validation, specifically focusing on ensuring the presence of required attributes on specific HTML elements. This is useful for maintaining consistency, preventing common errors, and ensuring data integrity within your Angular applications.

Problem Description

You need to create an Angular directive called requiredAttributeValidator that validates the presence of specified attributes on a given HTML element. The directive should accept an array of attribute names as input and throw an error if any of those attributes are missing from the element the directive is applied to. The error message should clearly indicate which attribute is missing and on which element.

Key Requirements:

  • Input: The directive must accept an array of strings representing the attribute names to validate. This array will be passed as an input property to the directive.
  • Validation: The directive should check if each attribute in the input array exists on the element it's applied to.
  • Error Handling: If any of the required attributes are missing, the directive should throw an error. The error message should be informative, including the missing attribute name and the element's tag name. Use throw new Error() to signal the validation failure.
  • Performance: The validation should be performed after the view has been initialized (i.e., in ngAfterViewInit).
  • Reusability: The directive should be reusable across different components and elements.

Expected Behavior:

  • When the directive is applied to an element, it should validate the presence of the specified attributes after the view is initialized.
  • If all required attributes are present, the directive should do nothing.
  • If any required attribute is missing, the directive should throw an error with a clear message.

Edge Cases to Consider:

  • Empty input array: The directive should handle an empty input array gracefully (no error should be thrown).
  • Attribute names with special characters: Ensure the directive correctly handles attribute names that might contain special characters.
  • Dynamic attribute names: Consider how the directive would behave if the attribute names are dynamically generated. (While not required to fully support this, acknowledge it as a consideration).
  • Elements with no attributes: The directive should handle elements that have no attributes at all.

Examples

Example 1:

Input: `<div requiredAttributeValidator [attributes]="['data-id']"></div>`
Output: (No output - validation passes if the div has a data-id attribute)
Explanation: The directive checks if the 'data-id' attribute exists on the div. If it does, no error is thrown.

**Example 2:**

Input: <div requiredAttributeValidator [attributes]="['data-id']"></div> (where the div does not have a data-id attribute) Output: Error: Missing required attribute 'data-id' on element 'DIV' Explanation: The directive detects that the 'data-id' attribute is missing and throws an error.

Example 3:

Input: `<button requiredAttributeValidator [attributes]="['disabled', 'type']"></button>` (where the button does not have a 'disabled' attribute)
Output: `Error: Missing required attribute 'disabled' on element 'BUTTON'`
Explanation: The directive checks for both 'disabled' and 'type' attributes. Since 'disabled' is missing, an error is thrown.

Constraints

  • The directive must be written in TypeScript.
  • The directive must be compatible with Angular version 14 or higher.
  • The validation must be performed after ngAfterViewInit.
  • The error message must be clear and informative.
  • The directive should not introduce any significant performance overhead. Avoid unnecessary DOM manipulations.

Notes

  • Consider using ElementRef to access the host element.
  • The ngAfterViewInit lifecycle hook is crucial for ensuring the element and its attributes are fully initialized before validation.
  • Think about how to make the directive flexible and reusable for different attribute validation scenarios.
  • While you don't need to implement a full-fledged error reporting system, the error message should be sufficient for developers to quickly identify and fix the issue.
  • Focus on the core validation logic; complex error handling or UI integration is not required for this challenge.
Loading editor...
typescript