mirror of
https://github.com/gosticks/web.git
synced 2026-10-07 07:07:07 +00:00
Add isValidIpv6OptionalPort
Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
isValidDomain,
|
||||
isValidIpv4,
|
||||
isValidIpv4OptionalPort,
|
||||
isValidIpv6OptionalPort,
|
||||
isValidRegex
|
||||
} from "../validate";
|
||||
|
||||
@@ -227,4 +228,36 @@ describe("Testing the validation functions", () => {
|
||||
expect(isValidIpv4OptionalPort("127.0.0.1#53")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isValidIpv6OptionalPort", () => {
|
||||
it("passes 1fff:0:a88:85a3::ac1f", () => {
|
||||
expect(isValidIpv6OptionalPort("1fff:0:a88:85a3::ac1f")).toBe(true);
|
||||
});
|
||||
|
||||
it("passes ::1", () => {
|
||||
expect(isValidIpv6OptionalPort("::1")).toBe(true);
|
||||
});
|
||||
|
||||
it("passes [::1]:5353", () => {
|
||||
expect(isValidIpv6OptionalPort("[::1]:5353")).toBe(true);
|
||||
});
|
||||
|
||||
it("passes [1fff:0:a88:85a3::ac1f]:8001", () => {
|
||||
expect(isValidIpv6OptionalPort("[1fff:0:a88:85a3::ac1f]:8001")).toBe(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it("fails 192.168.1.1", () => {
|
||||
expect(isValidIpv6OptionalPort("192.168.1.1")).toBe(false);
|
||||
});
|
||||
|
||||
it("fails 192.168.1.1:53", () => {
|
||||
expect(isValidIpv6OptionalPort("192.168.1.1:53")).toBe(false);
|
||||
});
|
||||
|
||||
it("fails [::1]", () => {
|
||||
expect(isValidIpv6OptionalPort("[::1]")).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -94,3 +94,17 @@ export function isValidIpv4OptionalPort(address: string): boolean {
|
||||
split.length === 1 || (split.length === 2 && isPositiveNumber(split[1]))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the string is a valid IPv6 address, and it can contain an optional
|
||||
* port. This function does not do a thorough check of the IPv6 address; the
|
||||
* backend will check it in detail.
|
||||
* Examples:
|
||||
* 1fff:0:a88:85a3::ac1f
|
||||
* [1fff:0:a88:85a3::ac1f]:8001
|
||||
*
|
||||
* @param address The IPv6 address
|
||||
*/
|
||||
export function isValidIpv6OptionalPort(address: string): boolean {
|
||||
return /^(\[[a-fA-F0-9:]+]:\d+|[a-fA-F0-9:]+)$/.test(address);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user