mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-09 21:44:51 +02:00
create target validator and add url validator
This commit is contained in:
parent
a418195b28
commit
d5a220a004
4 changed files with 46 additions and 96 deletions
|
@ -11,7 +11,7 @@ export function isValidIP(ip: string): boolean {
|
|||
export function isValidUrlGlobPattern(pattern: string): boolean {
|
||||
// Remove leading slash if present
|
||||
pattern = pattern.startsWith("/") ? pattern.slice(1) : pattern;
|
||||
|
||||
|
||||
// Empty string is not valid
|
||||
if (!pattern) {
|
||||
return false;
|
||||
|
@ -19,11 +19,11 @@ export function isValidUrlGlobPattern(pattern: string): boolean {
|
|||
|
||||
// Split path into segments
|
||||
const segments = pattern.split("/");
|
||||
|
||||
|
||||
// Check each segment
|
||||
for (let i = 0; i < segments.length; i++) {
|
||||
const segment = segments[i];
|
||||
|
||||
|
||||
// Empty segments are not allowed (double slashes), except at the end
|
||||
if (!segment && i !== segments.length - 1) {
|
||||
return false;
|
||||
|
@ -37,12 +37,15 @@ export function isValidUrlGlobPattern(pattern: string): boolean {
|
|||
// Check each character in the segment
|
||||
for (let j = 0; j < segment.length; j++) {
|
||||
const char = segment[j];
|
||||
|
||||
|
||||
// Check for percent-encoded sequences
|
||||
if (char === "%" && j + 2 < segment.length) {
|
||||
const hex1 = segment[j + 1];
|
||||
const hex2 = segment[j + 2];
|
||||
if (!/^[0-9A-Fa-f]$/.test(hex1) || !/^[0-9A-Fa-f]$/.test(hex2)) {
|
||||
if (
|
||||
!/^[0-9A-Fa-f]$/.test(hex1) ||
|
||||
!/^[0-9A-Fa-f]$/.test(hex2)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
j += 2; // Skip the next two characters
|
||||
|
@ -58,6 +61,36 @@ export function isValidUrlGlobPattern(pattern: string): boolean {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function isUrlValid(url: string | undefined) {
|
||||
if (!url) return true; // the link is optional in the schema so if it's empty it's valid
|
||||
var pattern = new RegExp(
|
||||
"^(https?:\\/\\/)?" + // protocol
|
||||
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
|
||||
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
|
||||
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
|
||||
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
|
||||
"(\\#[-a-z\\d_]*)?$",
|
||||
"i"
|
||||
);
|
||||
return !!pattern.test(url);
|
||||
}
|
||||
|
||||
export function isTargetValid(value: string | undefined) {
|
||||
if (!value) return true;
|
||||
|
||||
const DOMAIN_REGEX =
|
||||
/^[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?(?:\.[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9_])?)*$/;
|
||||
const IPV4_REGEX =
|
||||
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||
const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
||||
|
||||
if (IPV4_REGEX.test(value) || IPV6_REGEX.test(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return DOMAIN_REGEX.test(value);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue