# Common Validator - Shared validation functions
# Version: 1.0.0
#
# Provides reusable validation predicates and helpers

{
  # Validate string length is within range
  validate_string_length = fun min max =>
    fun s =>
      let len = std.string.length s in
      len >= min && len <= max,

  # Validate string matches pattern
  validate_pattern = fun pattern =>
    fun s =>
      std.string.is_match pattern s,

  # Validate number is within range
  validate_number_range = fun min max =>
    fun n =>
      n >= min && n <= max,

  # Validate port number
  validate_port = fun p =>
    p >= 1 && p <= 65535,

  # Validate host port (non-root range)
  validate_host_port = fun p =>
    p >= 1024 && p <= 65535,

  # Validate project name (lowercase alphanumeric with hyphens)
  validate_project_name = fun s =>
    std.string.is_match "^[a-z0-9-]+$" s &&
    std.string.length s >= 1 && std.string.length s <= 64,

  # Validate semantic version
  validate_semver = fun s =>
    std.string.is_match "^[0-9]+\\.[0-9]+\\.[0-9]+$" s,

  # Validate IP address (IPv4)
  validate_ipv4 = fun s =>
    std.string.is_match "^([0-9]{1,3}\\.){3}[0-9]{1,3}$" s,

  # Validate CIDR notation
  validate_cidr = fun s =>
    std.string.is_match "^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$" s,

  # Validate CPU limit format (e.g., "1000m", "1.5")
  validate_cpu_limit = fun s =>
    std.string.is_match "^[0-9]+(\\.[0-9]+)?m?$" s,

  # Validate memory limit format (e.g., "512Mi", "1Gi")
  validate_memory_limit = fun s =>
    std.string.is_match "^[0-9]+(Mi|Gi|M|G|Ki|K|Ti|T)$" s,

  # Validate storage size format
  validate_storage_size = fun s =>
    std.string.is_match "^[0-9]+(Mi|Gi|Ti|M|G|T)$" s,

  # Validate duration format (e.g., "30s", "5m", "2h", "7d")
  validate_duration = fun s =>
    std.string.is_match "^[0-9]+(s|m|h|d)$" s,

  # Validate percentage format (e.g., "25%", "50%")
  validate_percentage = fun s =>
    std.string.is_match "^[0-9]+(\\.[0-9]+)?%?$" s,

  # Validate Kubernetes name (RFC 1123 subdomain)
  validate_k8s_name = fun s =>
    std.string.is_match "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" s &&
    std.string.length s >= 1 && std.string.length s <= 63,

  # Validate Docker image name
  validate_image_name = fun s =>
    std.string.length s >= 1 && std.string.length s <= 256,

  # Validate image tag
  validate_image_tag = fun s =>
    std.string.is_match "^[a-zA-Z0-9._-]+$" s &&
    std.string.length s <= 128,

  # Validate URL
  validate_url = fun s =>
    std.string.is_match "^https?://.*" s ||
    std.string.is_match "^wss?://.*" s,

  # Validate email address (basic validation)
  validate_email = fun s =>
    std.string.is_match "^[^@]+@[^@]+\\.[^@]+$" s,

  # Validate password strength (min 8 chars, max 128 chars)
  validate_password = fun s =>
    std.string.length s >= 8 && std.string.length s <= 128,

  # Validate array is not empty
  validate_not_empty_array = fun arr =>
    std.array.length arr > 0,

  # Validate record is not empty
  validate_not_empty_record = fun rec =>
    std.record.length rec > 0,

  # Validate all array elements match predicate
  validate_all = fun predicate =>
    fun arr =>
      std.array.all predicate arr,

  # Validate at least one array element matches predicate
  validate_any = fun predicate =>
    fun arr =>
      std.array.any predicate arr,

  # Validate value is one of allowed values
  validate_one_of = fun allowed =>
    fun value =>
      std.array.any (fun x => x == value) allowed,

  # Compose multiple validators (all must pass)
  validate_and = fun validators =>
    fun value =>
      std.array.all (fun validator => validator value) validators,

  # Compose multiple validators (at least one must pass)
  validate_or = fun validators =>
    fun value =>
      std.array.any (fun validator => validator value) validators,

  # Negate validator result
  validate_not = fun validator =>
    fun value =>
      !(validator value),

  # Conditional validator (if condition is true, apply validator)
  validate_if = fun condition validator =>
    fun value =>
      if condition value then validator value else true,
}
