Enhancement: Add "dns_label" or "hostname_label" validator for single-label DNS validation
Author: sayem314Created Mar 14, 2026Updated Mar 14, 2026
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"`
}Source: go-playground/validator