ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

HCL Configuration Validation

HCL

Goal -- WPM

Ready
Exercise Algorithm Area
1variable "config" {
2type = object({
3name = string
4version = number
5components = list(object({
6id = string
7enabled = bool
8settings = object({
9timeout = number
10retries = optional(number, 0)
11})
12}))
13dependencies = optional(list(string), [])
14})
15description = "The configuration object to validate."
16}
17
18locals {
19validation_errors = try(merge(var.config.validation_errors, {
20name_empty = length(var.config.name) == 0
21version_negative = var.config.version < 0
22component_ids = {
23for comp in var.config.components : comp.id => true
24}
25duplicate_ids = length(keys(local.validation_errors.component_ids)) != length(var.config.components)
26dependency_format = anytrue([for dep in var.config.dependencies : !can(regex("^\w+:\d+", dep))])
27component_timeouts = {
28for comp in var.config.components : comp.id => comp.settings.timeout
29}
30invalid_timeout = anytrue([for comp in var.config.components : comp.settings.timeout <= 0])
31}), {
32name_empty = false
33version_negative = false
34duplicate_ids = false
35dependency_format= false
36invalid_timeout = false
37})
38}
39
40output "configuration_valid" {
41value = !local.validation_errors.name_empty && !local.validation_errors.version_negative && !local.validation_errors.duplicate_ids && !local.validation_errors.dependency_format && !local.validation_errors.invalid_timeout
42}
43
44output "validation_messages" {
45value = {
46name_empty = local.validation_errors.name_empty ? "Configuration name cannot be empty."
47version_negative = local.validation_errors.version_negative ? "Configuration version cannot be negative."
48duplicate_ids = local.validation_errors.duplicate_ids ? "Component IDs must be unique."
49dependency_format = local.validation_errors.dependency_format ? "Dependencies must be in 'name:version' format."
50invalid_timeout = local.validation_errors.invalid_timeout ? "Component timeouts must be positive."
51}
52}
53
54# Helper function to check for duplicate component IDs
55# This is implicitly handled by the map creation in locals, but conceptually useful.
56# A more explicit helper could iterate and count occurrences.
57
58# Helper function to check dependency format
59# This is also handled by anytrue in locals, but demonstrates a pattern.
60# A dedicated function could be written if the format was more complex.
Algorithm description viewbox

HCL Configuration Validation

Algorithm description:

This HCL code defines a configuration structure and implements validation logic to ensure its integrity. It checks for various conditions such as empty names, negative versions, duplicate component identifiers, incorrect dependency formats, and non-positive timeouts. This is commonly used in infrastructure-as-code tools to prevent deployment of misconfigured resources.

Algorithm explanation:

The algorithm uses HCL's `variable` and `locals` blocks to define and validate a complex configuration object. It employs boolean logic and string manipulation functions like `length`, `regex`, and `anytrue` to check for invalid states. The `try` function is used to gracefully handle potential errors during validation, especially when dealing with optional attributes. The `merge` function combines validation results from different checks. The core logic involves iterating over lists of components and dependencies to identify specific violations. Time complexity is roughly proportional to the number of components and dependencies, as each is checked. Space complexity is minimal, primarily for storing validation results. Edge cases include empty lists, missing optional attributes, and malformed string inputs.

Pseudocode:

Define configuration schema with required and optional attributes.
Initialize a map to store validation error flags.
Check if configuration name is empty; set flag if true.
Check if configuration version is negative; set flag if true.
Create a map of component IDs to detect duplicates.
Compare the count of unique IDs with the total component count; set flag if different.
Iterate through dependencies, checking if each matches the 'name:version' format; set flag if any mismatch.
Iterate through components, checking if timeout settings are positive; set flag if any are not.
Combine all error flags into a single validation result.
Output a boolean indicating overall configuration validity.
Output detailed messages for each detected validation error.