Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix domain validation #247

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/components/NameServerGroupAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "../store/nameservers/types";
import { useGetGroupTagHelpers } from "../utils/groups";
import { useGetTokenSilently } from "../utils/token";
import { domainValidator } from "../utils/common";

const { Paragraph, Text } = Typography;

Expand Down Expand Up @@ -272,21 +273,6 @@ const NameServerGroupAdd = () => {
setEditDescription(status);
};

const domainRegex =
/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/;

const domainValidator = (_: RuleObject, domain: string) => {
if (domainRegex.test(domain)) {
return Promise.resolve();
}
setIsPrimary(false);
return Promise.reject(
new Error(
"Please enter a valid domain, e.g. example.com or intra.example.com"
)
);
};

const nameValidator = (_: RuleObject, value: string) => {
const found = nsGroupData.find(
(u) => u.name == value && u.id !== formNSGroup.id
Expand Down
59 changes: 22 additions & 37 deletions src/components/NameServerGroupUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { useGetGroupTagHelpers } from "../utils/groups";
import { useGetTokenSilently } from "../utils/token";
import { Container } from "./Container";

import { domainValidator } from "../utils/common";
const { Paragraph, Text } = Typography;

interface formNSGroup extends NameServerGroup {}
Expand Down Expand Up @@ -253,21 +253,6 @@ const NameServerGroupUpdate = () => {
setEditDescription(status);
};

const domainRegex =
/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/;

const domainValidator = (_: RuleObject, domain: string) => {
if (domainRegex.test(domain)) {
return Promise.resolve();
}
setIsPrimary(false);
return Promise.reject(
new Error(
"Please enter a valid domain, e.g. example.com or intra.example.com"
)
);
};

const nameValidator = (_: RuleObject, value: string) => {
const found = nsGroupData.find(
(u) => u.name == value && u.id !== formNSGroup.id
Expand Down Expand Up @@ -316,38 +301,38 @@ const NameServerGroupUpdate = () => {
<Col span={24} style={{ marginBottom: "15px" }}>
<Form.Item name="enabled" label="">
<div
style={{
display: "flex",
gap: "15px",
}}
style={{
display: "flex",
gap: "15px",
}}
>
<Switch
onChange={handleChangeDisabled}
defaultChecked={formNSGroup.enabled}
size="small"
checked={formNSGroup.enabled}
onChange={handleChangeDisabled}
defaultChecked={formNSGroup.enabled}
size="small"
checked={formNSGroup.enabled}
/>
<div>
<label
style={{
color: "rgba(0, 0, 0, 0.88)",
fontSize: "14px",
fontWeight: "500",
}}
style={{
color: "rgba(0, 0, 0, 0.88)",
fontSize: "14px",
fontWeight: "500",
}}
>
Enabled
</label>
<Paragraph
type={"secondary"}
style={{
marginTop: "-2",
fontWeight: "400",
marginBottom: "0",
}}
type={"secondary"}
style={{
marginTop: "-2",
fontWeight: "400",
marginBottom: "0",
}}
>
{formNSGroup.enabled
? "Disable this server if you don't want the configuration to apply immediately"
: " Enable this server if you want the configuration to apply immediately"}
? "Disable this server if you don't want the configuration to apply immediately"
: " Enable this server if you want the configuration to apply immediately"}
</Paragraph>
</div>
</div>
Expand Down
29 changes: 22 additions & 7 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ function getFormattedDate(date, preformattedDate = false, hideYear = false) {

if (minutes < 10) {
// Adding leading zero to minutes
minutes = `0${ minutes }`;
minutes = `0${minutes}`;
}

if (preformattedDate) {
// Today
// Yesterday
return `${ preformattedDate }`;
return `${preformattedDate}`;
}

if (hideYear) {
// 10. January
return `${ day }. ${ month }`;
return `${day}. ${month}`;
}

// 10. January 2017.
return `${ day }. ${ month } ${ year }`;
return `${day}. ${month} ${year}`;
}

export const fullDate = (dateParam) => {
Expand Down Expand Up @@ -107,11 +107,11 @@ export const timeAgo = (dateParam) => {
} else if (seconds < 5) {
return 'just now';
} else if (seconds < 60) {
return `${ seconds } seconds ago`;
return `${seconds} seconds ago`;
} else if (seconds < 90) {
return 'about a minute ago';
} else if (minutes < 60) {
return `${ minutes } minutes ago`;
return `${minutes} minutes ago`;
} else if (isToday) {
return getFormattedDate(date, 'today'); // Today at 10:20
} else if (isYesterday) {
Expand All @@ -133,4 +133,19 @@ export const isNetBirdHosted = () => {

export const isLocalDev = () => {
return window.location.hostname.includes("localhost")
}
}

const domainRegex =
/^(?!.*\s)[a-zA-Z0-9](?!.*\s$)(?!.*\.$)(?:(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.){1,126}(?!-)[a-zA-Z0-9-]{1,63}(?<!-)$/;

export const domainValidator = (_, domain) => {
if (domainRegex.test(domain)) {
return Promise.resolve();
}
// setIsPrimary(false);
return Promise.reject(
new Error(
"Please enter a valid domain, e.g. example.com or intra.example.com"
)
);
};