fix: Fixes issue with adding hyphenated subdomains.

This commit is contained in:
towfiqi
2023-11-02 22:15:26 +06:00
parent 1d6b2be95a
commit c0470cfa9d
2 changed files with 37 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import Modal from '../common/Modal';
import { useAddDomain } from '../../services/domains';
import { isValidDomain } from '../../utils/validators';
type AddDomainProps = {
closeModal: Function
@@ -13,7 +14,7 @@ const AddDomain = ({ closeModal }: AddDomainProps) => {
const addDomain = () => {
// console.log('ADD NEW DOMAIN', newDomain);
if (/^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(newDomain.trim())) {
if (isValidDomain(newDomain.trim())) {
setNewDomainError(false);
// TODO: Domain Action
addMutate(newDomain.trim());

35
utils/validators.ts Normal file
View File

@@ -0,0 +1,35 @@
/* eslint-disable import/prefer-default-export */
export const isValidDomain = (domain:string): boolean => {
if (typeof domain !== 'string') return false;
if (!domain.includes('.')) return false;
let value = domain;
const validHostnameChars = /^[a-zA-Z0-9-.]{1,253}\.?$/g;
if (!validHostnameChars.test(value)) {
return false;
}
if (value.endsWith('.')) {
value = value.slice(0, value.length - 1);
}
if (value.length > 253) {
return false;
}
const labels = value.split('.');
const isValid = labels.every((label) => {
const validLabelChars = /^([a-zA-Z0-9-]+)$/g;
const validLabel = (
validLabelChars.test(label)
&& label.length < 64
&& !label.startsWith('-')
&& !label.endsWith('-')
);
return validLabel;
});
return isValid;
};