Skip to content

Commit

Permalink
fix(network) fix types for network validation functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Jan 19, 2024
1 parent c8287a2 commit 6b97adf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/util/networks.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("areNetworksEqual", () => {
it("accepts matches", () => {
const a: Partial<LxdNetwork> & Required<Pick<LxdNetwork, "config">> = {
config: {
"bridge.mode": "standard",
"bridge.driver": "native",
"ipv4.address": "auto",
"ipv6.address": "fd42:2c50:bf9f:52b1::1/64",
"ipv6.nat": "true",
Expand All @@ -17,7 +17,7 @@ describe("areNetworksEqual", () => {
};
const b: Partial<LxdNetwork> & Required<Pick<LxdNetwork, "config">> = {
config: {
"bridge.mode": "standard",
"bridge.driver": "native",
"ipv4.address": "10.191.170.1/24",
"ipv6.address": "fd42:2c50:bf9f:52b1::1/64",
"ipv6.nat": "true",
Expand Down Expand Up @@ -47,12 +47,12 @@ describe("areNetworksEqual", () => {
it("rejects config diff", () => {
const a: Partial<LxdNetwork> & Required<Pick<LxdNetwork, "config">> = {
config: {
"bridge.mode": "standard",
"bridge.driver": "native",
},
};
const b: Partial<LxdNetwork> & Required<Pick<LxdNetwork, "config">> = {
config: {
"bridge.mode": "fan",
"bridge.driver": "openvswitch",
},
};

Expand Down
10 changes: 2 additions & 8 deletions src/util/networks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LxdInstance } from "types/instance";
import { LxdNetwork, LxdNetworkConfig } from "types/network";
import { ConfigRowMetadata } from "util/configInheritance";
import { AnyObject, TestFunction } from "yup";

export const getIpAddresses = (
instance: LxdInstance,
Expand Down Expand Up @@ -79,9 +78,7 @@ export const areNetworksEqual = (
return !hasMainKeyDiff;
};

export const testValidIp: TestFunction<string | null | undefined, AnyObject> = (
ip,
) => {
export const testValidIp = (ip: string | null | undefined) => {
const expression =
/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/;

Expand All @@ -91,10 +88,7 @@ export const testValidIp: TestFunction<string | null | undefined, AnyObject> = (
return expression.test(ip);
};

export const testValidPort: TestFunction<
string | null | undefined,
AnyObject
> = (port) => {
export const testValidPort = (port: string | null | undefined) => {
const expression =
/^(([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])[-,]){0,9}([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;

Expand Down

0 comments on commit 6b97adf

Please sign in to comment.