Skip to content

Enhancement: Add "dns_label" or "hostname_label" validator for single-label DNS validation #1543

@sayem314

Description

@sayem314

Currently, the hostname_rfc1123 tag validates a full hostname which can include multiple labels separated by dots (e.g., sub.domain.com). However, many modern use cases, such as validating a subdomain part in a multi-tenant application (e.g., [xxx].example.com) or Kubernetes resource names, require validating a single DNS label that strictly forbids dots.

Proposed Change:
Add a new baked-in validation tag, such as dns_label or hostname_rfc1123_label, that enforces the following RFC 1123 rules for a single part:

  • Characters: Only alphanumeric (a-z, A-Z, 0-9) and hyphens (-).
  • Start/End: Must start and end with an alphanumeric character.
  • Length: 1 to 63 characters.
  • No Dots: Strictly forbids the . character within the string.
regexp=^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$

Use Case:
Validating a single subdomain prefix before appending it to a base domain in code:

type Tenant struct {
    // Current hostname_rfc1123 would allow "illegal.sub.domain"
    // We need something that only allows "legal-sub"
    Subdomain string `validate:"required,dns_label"` 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions