Skip to content

Instantly share code, notes, and snippets.

@annibal
Last active November 14, 2025 03:47
Show Gist options
  • Select an option

  • Save annibal/74fbf17b290877332ea5f7753bcfc5a0 to your computer and use it in GitHub Desktop.

Select an option

Save annibal/74fbf17b290877332ea5f7753bcfc5a0 to your computer and use it in GitHub Desktop.
Validates "v1" or "v0.1" or "v0.0.1" but not "v01" nor "v1.01" nor "v0.0.01" nor "vRegExp"
const regExVersion = new RegExp([
// starts with "v" ➔ v1, v2, v56, v100 works. vv1, 12345, 1.2.3 fails.
"^(?:v)",
// major version: either "0" or a number that not starts with 0. ➔ v01 fails
"(?<major>(?:[1-9][0-9]*)|(?:0))",
// minor version (optional) - same as major ➔ v1.1, v0.1, v2.15, v9, v9.0, v9.9 works. v9.09 fails
"(?:\.(?<minor>(?:[1-9][0-9]*)|(?:0)))?",
// revision (optional too) - same as major ➔ v1.2.3, v0.0.1, v9.0.9 works, v0.0.0, v6.5.04 fails.
"(?:\.(?<revision>(?:[1-9][0-9]*)|(?:0)))?",
"$"
].join(""),
"i"); // Global + case Insensitive
const DEFAULT_API_VERSION = "v0.0.1";
function getValidatedVersionParam(version) {
let strVersion = version;
if (typeof strVersion === "string" && regExVersion.test(strVersion)) {
return strVersion;
}
if (!strVersion) {
return DEFAULT_API_VERSION;
}
const isVersionValidNumber = typeof version === "number" && !isNaN(version);
if (isVersionValidNumber) {
strVersion = `v${version}`;
if (regExVersion.test(strVersion)) {
return strVersion;
}
}
const isVersionAllNumbers = Array.isArray(version) && version.every(vp => Number.isFinite(+vp));
if (isVersionAllNumbers) {
strVersion = `v${version.join(".")}`;
if (regExVersion.test(strVersion)) {
return strVersion;
}
}
return DEFAULT_API_VERSION;
}
const regExVersion = new RegExp([
// starts with "v" ➔ v1, v2, v56, v100 works. vv1, 12345, 1.2.3 fails.
"^(?:v)",
// major version: either "0" or a number that not starts with 0. ➔ v01 fails
"(?<major>(?:[1-9][0-9]*)|(?:0))",
// minor version (optional) - same as major ➔ v1.1, v0.1, v2.15, v9, v9.0, v9.9 works. v9.09 fails
"(?:\.(?<minor>(?:[1-9][0-9]*)|(?:0)))?",
// revision (optional too) - same as major ➔ v1.2.3, v0.0.1, v9.0.9 works, v0.0.0, v6.5.04 fails.
"(?:\.(?<revision>(?:[1-9][0-9]*)|(?:0)))?",
"$"
].join(""),
"i"); // Global + case Insensitive
type TVersionParam = string | number | number[] | null | undefined;
const DEFAULT_API_VERSION: string = "v0.0.1";
function getValidatedVersionParam(version: TVersionParam): string {
let strVersion = version;
if (typeof strVersion === "string" && regExVersion.test(strVersion)) {
return strVersion;
}
if (!strVersion) {
return DEFAULT_API_VERSION;
}
const isVersionValidNumber = typeof version === "number" && !isNaN(version);
if (isVersionValidNumber) {
strVersion = `v${version}`;
if (regExVersion.test(strVersion)) {
return strVersion;
}
}
const isVersionAllNumbers = Array.isArray(version) && version.every(vp => Number.isFinite(+vp));
if (isVersionAllNumbers) {
strVersion = `v${version.join(".")}`;
if (regExVersion.test(strVersion)) {
return strVersion;
}
}
return DEFAULT_API_VERSION;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment