Skip to content

isValid

Whether version is a valid version in the given scheme. Never throws: any input that is not valid, a non-string included, gives false.

'semver' (default) — SemVer 2.0.0, strictly: MAJOR.MINOR.PATCH with no leading zeros, an optional -prerelease (numeric identifiers without leading zeros) and an optional +build. Unlike parse, which reads anything, and unlike compare, a leading v is refused by default (v1.2.3 is a git tag, not a version; see IsValidOptions.prefix to accept or require it), and so are 1.2 and 1.2.3.4.

'gentoo', 'debian', 'rpm', 'pacman', 'pep440' — whether parse accepts it (these schemes throw on invalid input, so their grammar is the one parse documents).

'maven' — any non-empty string: Maven reads every text as a version.

Available since v3.1.9

import { isValid } from '@helpers4/version';
// or, from the all-in-one package (same code, one install):
import { isValid } from 'helpers4/version';
isValid(version: unknown, scheme: VersionScheme, options: IsValidOptions): boolean
ParameterTypeDescription
versionunknownThe value to check
schemeVersionSchemeWhich version scheme to validate against. Defaults to `‘semver’`.
optionsIsValidOptionsThe treatment of a leading `v` — see IsValidOptions

boolean — true if version is a valid version

Unlike parse, which reads anything, isValid refuses a v prefix, missing parts and leading zeros.

isValid('1.2.3')    // => true
isValid('v1.2.3')   // => false
isValid('garbage')  // => false

prefix: ‘require’ demands the v of a tag; ‘allow’ accepts both spellings.

isValid('v1.2.3', 'semver', { prefix: 'require' }) // => true
isValid('1.2.3', 'semver', { prefix: 'require' })  // => false

Debian versions are checked against dpkg’s grammar.

isValid('1:2.30~rc1-3', 'debian') // => true
isValid('1.0_1', 'debian')        // => false

Options for isValid.

interface IsValidOptions {
  prefix?: "require" | "forbid" | "allow";
}

Identifies which version scheme parse/compare should use to interpret a version string. Defaults to 'semver' everywhere it’s accepted.

type VersionScheme = 'semver' | 'gentoo' | 'debian' | 'rpm' | 'pacman' | 'pep440' | 'maven'

View source on GitHub