Validation functions for ensuring version strings conform to semver standards, with both permissive and strict validation modes.
Permissive validation that accepts various version formats including wildcards and flexible patterns.
/**
* Validate semver version strings.
* @param version Version number to validate
* @returns `true` if the version number is a valid semver version number, `false` otherwise.
*/
function validate(version: string): boolean;Usage Examples:
import { validate } from "compare-versions";
// Standard semver versions
validate('1.0.0'); // true
validate('1.0.0-alpha.1'); // true
validate('1.0.0+build.123'); // true
validate('1.0.0-beta+exp.sha'); // true
// Flexible formats (accepted by permissive validate)
validate('1.0'); // true
validate('1'); // true
validate('v1.0.0'); // true
validate('1.01.1'); // true (leading zeros)
// Wildcards and special patterns
validate('1.0.x'); // true
validate('1.0.*'); // true
validate('1.x.x'); // true
// Chromium-style 4-part versions
validate('25.0.1364.126'); // true
// Invalid versions
validate('foo'); // false
validate('1.0.0.0.0'); // false
validate(''); // false
validate('1.0-rc.1'); // false (missing patch)Strict validation that only accepts properly formatted semver versions according to semver.org specification.
/**
* Validate semver version strings strictly. Will not accept wildcards and version ranges.
* @param version Version number to validate
* @returns `true` if the version number is a valid semver version number `false` otherwise
*/
function validateStrict(version: string): boolean;Usage Examples:
import { validateStrict } from "compare-versions";
// Valid strict semver versions
validateStrict('1.0.0'); // true
validateStrict('0.0.0'); // true
validateStrict('1.0.0-alpha'); // true
validateStrict('1.0.0-alpha.1'); // true
validateStrict('1.0.0-0.3.7'); // true
validateStrict('1.0.0+20130313144700'); // true
validateStrict('1.0.0-beta+exp.sha.5114f85'); // true
// Invalid for strict validation (but may be valid for permissive)
validateStrict('1.0'); // false (missing patch)
validateStrict('1'); // false (missing minor and patch)
validateStrict('v1.0.0'); // false (leading v)
validateStrict('1.01.0'); // false (leading zero)
validateStrict('1.0.x'); // false (wildcard)
validateStrict('1.0.*'); // false (wildcard)
// Always invalid
validateStrict('foo'); // false
validateStrict(''); // false
validateStrict('1.0.0.0'); // false (too many parts)| Version String | validate() | validateStrict() | Notes |
|---|---|---|---|
1.0.0 | ✅ true | ✅ true | Perfect semver |
1.0.0-alpha | ✅ true | ✅ true | Pre-release |
1.0.0+build | ✅ true | ✅ true | Build metadata |
1.0 | ✅ true | ❌ false | Missing patch |
1 | ✅ true | ❌ false | Missing minor & patch |
v1.0.0 | ✅ true | ❌ false | Leading v |
1.01.0 | ✅ true | ❌ false | Leading zero |
1.0.x | ✅ true | ❌ false | Wildcard |
25.0.1364.126 | ✅ true | ❌ false | 4-part Chromium |
foo | ❌ false | ❌ false | Invalid |
Use validate() when:
Use validateStrict() when:
Both validation functions handle type checking:
// Non-string inputs
validate(123); // false
validate(null); // false
validate(undefined); // false
validate({}); // false
validateStrict(123); // false
validateStrict(null); // false
validateStrict(undefined); // false
validateStrict([]); // falseimport { validate, validateStrict } from "compare-versions";
function validateVersionInput(version: string, strict: boolean = false) {
const isValid = strict ? validateStrict(version) : validate(version);
if (!isValid) {
const message = strict
? "Version must follow strict semver format (e.g., 1.0.0)"
: "Version format is invalid";
throw new Error(message);
}
return version;
}
// Usage
validateVersionInput("1.0.0", true); // OK
validateVersionInput("1.0", false); // OK
validateVersionInput("1.0", true); // Error: Version must follow strict semver formatimport { validate, validateStrict } from "compare-versions";
function processVersions(versions: string[]) {
// Filter to only valid versions
const validVersions = versions.filter(validate);
// Separate strict vs flexible versions
const strictVersions = validVersions.filter(validateStrict);
const flexibleVersions = validVersions.filter(v => !validateStrict(v));
return {
valid: validVersions,
strict: strictVersions,
flexible: flexibleVersions
};
}
// Usage
const result = processVersions(['1.0.0', '1.0', 'v2.0.0', 'invalid']);
// result.valid: ['1.0.0', '1.0', 'v2.0.0']
// result.strict: ['1.0.0']
// result.flexible: ['1.0', 'v2.0.0']The validation functions use different internal patterns:
validate(): Uses a permissive regex that accepts various formats and wildcardsvalidateStrict(): Uses the official semver.org regular expression patternBoth functions ensure the input is a string and starts with a digit or 'v' before applying regex validation.