Raxol.Core.Utils.Validation (Raxol Core v2.4.0)

Copy Markdown View Source

Common validation utilities to reduce code duplication across the codebase. Provides standardized validation functions for dimensions, configs, and common patterns.

Summary

Functions

Validates that a value is within specified bounds.

Validates a configuration map against required keys.

Validates that coordinates are valid non-negative integers.

Validates that a dimension is a positive integer, returning default if invalid.

Validates that a value is one of the allowed options.

Validates that a list contains only specific types.

Validates that a string is not empty and optionally matches a pattern.

Functions

validate_bounds(value, min, max)

@spec validate_bounds(number(), number(), number()) ::
  {:ok, number()} | {:error, :out_of_bounds}

Validates that a value is within specified bounds.

validate_config(config, required_keys)

@spec validate_config(map(), [atom()]) ::
  {:ok, map()} | {:error, {:missing_keys, [atom()]}}

Validates a configuration map against required keys.

validate_coordinates(x, y)

@spec validate_coordinates(integer(), integer()) ::
  {:ok, {non_neg_integer(), non_neg_integer()}} | {:error, :invalid_coordinates}

Validates that coordinates are valid non-negative integers.

validate_dimension(dimension, default)

@spec validate_dimension(integer(), non_neg_integer()) :: non_neg_integer()

Validates that a dimension is a positive integer, returning default if invalid.

validate_enum(value, allowed)

@spec validate_enum(any(), list()) :: {:ok, any()} | {:error, :invalid_option}

Validates that a value is one of the allowed options.

validate_list_types(list, type)

@spec validate_list_types(list(), atom()) :: {:ok, list()} | {:error, :invalid_types}

Validates that a list contains only specific types.

validate_string(str, pattern \\ nil)

@spec validate_string(binary(), Regex.t() | nil) ::
  {:ok, binary()} | {:error, :invalid_string}

Validates that a string is not empty and optionally matches a pattern.