updated and improved ip address validation #8

Merged
TobaOjo merged 1 commits from bugfix/ipaddress into develop 2025-11-12 09:49:04 +00:00
3 changed files with 29 additions and 15 deletions
Showing only changes of commit ed271964d8 - Show all commits

View File

@@ -5,6 +5,8 @@ import { timezones } from "./timezones";
import SystemFileUpload from "./SystemFileUpload"; import SystemFileUpload from "./SystemFileUpload";
import type { SystemValues, SystemValuesErrors } from "../../../types/types"; import type { SystemValues, SystemValuesErrors } from "../../../types/types";
import { useDNSSettings, useSystemConfig } from "../../../hooks/useSystemConfig"; import { useDNSSettings, useSystemConfig } from "../../../hooks/useSystemConfig";
import { ValidateIPaddress } from "../../../utils/utils";
import { toast } from "sonner";
const SystemConfigFields = () => { const SystemConfigFields = () => {
const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } = useSystemConfig(); const { saveSystemSettings, systemSettingsData, saveSystemSettingsLoading } = useSystemConfig();
@@ -35,6 +37,14 @@ const SystemConfigFields = () => {
if (!values.timeZone) errors.timeZone = "Required"; if (!values.timeZone) errors.timeZone = "Required";
if (isNaN(interval) || interval <= 0) errors.sntpInterval = "Cannot be less than 0"; if (isNaN(interval) || interval <= 0) errors.sntpInterval = "Cannot be less than 0";
if (!values.sntpServer) errors.sntpServer = "Required"; if (!values.sntpServer) errors.sntpServer = "Required";
const invalidPrimary = ValidateIPaddress(values.serverPrimary);
const invalidSecondary = ValidateIPaddress(values.serverSecondary);
if (invalidPrimary || invalidSecondary) {
toast.error(invalidPrimary || invalidSecondary, {
id: "invalid-ip",
});
}
return errors; return errors;
}; };
@@ -52,7 +62,7 @@ const SystemConfigFields = () => {
onSubmit={handleSubmit} onSubmit={handleSubmit}
validate={validateValues} validate={validateValues}
enableReinitialize enableReinitialize
validateOnChange validateOnChange={false}
validateOnBlur validateOnBlur
> >
{({ values, errors, touched, isSubmitting }) => ( {({ values, errors, touched, isSubmitting }) => (

View File

@@ -35,6 +35,14 @@ const ModemSettings = () => {
}; };
const handleSubmit = async (values: ModemSettingsType) => { const handleSubmit = async (values: ModemSettingsType) => {
const invalidPrimary = ValidateIPaddress(values.serverPrimary);
const invalidSecondary = ValidateIPaddress(values.serverSecondary);
if (invalidPrimary || invalidSecondary) {
toast.error(invalidPrimary || invalidSecondary, {
id: "invalid-ip",
});
return;
}
const modemConfig = { const modemConfig = {
id: "ModemAndWifiManager-modem", id: "ModemAndWifiManager-modem",
fields: [ fields: [
@@ -68,7 +76,7 @@ const ModemSettings = () => {
const response = await modemMutation.mutateAsync(modemConfig); const response = await modemMutation.mutateAsync(modemConfig);
if (response?.id) { if (!response?.id) {
toast.success("Modem settings updated successfully", { toast.success("Modem settings updated successfully", {
id: "modemSettings", id: "modemSettings",
}); });
@@ -138,7 +146,6 @@ const ModemSettings = () => {
id="serverPrimary" id="serverPrimary"
type="text" type="text"
className="p-1.5 border border-gray-400 rounded-lg" className="p-1.5 border border-gray-400 rounded-lg"
validate={ValidateIPaddress}
/> />
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>
@@ -151,7 +158,6 @@ const ModemSettings = () => {
id="serverSecondary" id="serverSecondary"
type="text" type="text"
className="p-1.5 border border-gray-400 rounded-lg" className="p-1.5 border border-gray-400 rounded-lg"
validate={ValidateIPaddress}
/> />
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>

View File

@@ -185,15 +185,13 @@ export const reverseZoomMapping = (magnification: string) => {
} }
}; };
export function ValidateIPaddress(ipaddress: string) { export const ValidateIPaddress = (value: string | undefined) => {
if (!ipaddress) { if (!value) return;
return undefined;
} else if ( const regex =
!/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test( /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
ipaddress
) if (!regex.test(value)) {
) { return "Invalid IP address format";
return "Invalid IP";
}
return undefined;
} }
};