unify subdomain validation schema to handle edge cases

This commit is contained in:
Pallavi
2025-08-30 01:14:03 +05:30
parent e8a6efd079
commit 54764dfacd
4 changed files with 174 additions and 148 deletions

View File

@@ -1,24 +1,19 @@
import { z } from "zod";
export const subdomainSchema = z
.string()
.regex(
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/,
/^(?!:\/\/)([a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/,
"Invalid subdomain format"
)
.min(1, "Subdomain must be at least 1 character long")
.max(63, "Subdomain must not exceed 63 characters")
.transform((val) => val.toLowerCase());
export const tlsNameSchema = z
.string()
.regex(
/^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?)(\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*$/,
/^(?!:\/\/)([a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$|^$/,
"Invalid subdomain format"
).max(253, "Domain must not exceed 253 characters")
.refine((val) => {
const labels = val.split('.');
return labels.every((label) => label.length <= 63);
}, "Each part of the domain must not exceed 63 characters")
.transform((val) => val.toLowerCase());
)
.transform((val) => val.toLowerCase());