How to use the valid function from semver
Find comprehensive JavaScript semver.valid code examples handpicked from public code repositorys.
semver.valid is a function that checks if a given string represents a valid semantic version number.
GitHub: qooxdoo/qooxdoo
77 78 79 80 81 82 83 84 85 86
nargs: 1, describe: "Install a library/the given library from a local path" }, "qx-version": { check: argv => semver.valid(argv.qxVersion), describe: "A semver string. If given, the maximum qooxdoo version for which to install a package" } }
+ 2 other calls in file
GitHub: benzino77/tasmocompiler
40 41 42 43 44 45 46 47 48 49
if (tcVersion.includes('dev')) { return [edgeBranch]; } const allTags = await git(tasmotaRepo).tags(); let tags = allTags.all.filter((t) => t.startsWith('v')); tags = tags.filter((t) => semver.valid(t) && semver.gte(t, minVersion) && semver.lte(t, maxVersion)); return [...tags]; } catch (e) { debug(e.message); debug(message);
How does semver.valid work?
semver.valid is a function provided by the semver library that checks if a given string represents a valid semantic version number. To determine if a string is a valid semantic version number, the function first checks if it matches the basic format of a semantic version, which is "major.minor.patch". If the string matches this format, the function then checks if each component is a non-negative integer. If all three components are non-negative integers, then the string is considered a valid semantic version number and the function returns it. If the string does not match the format or any component is not a non-negative integer, then the function returns null to indicate that the string is not a valid semantic version number. The semver.valid function is useful for validating user input or ensuring that a version number provided by a package or module follows the semantic versioning standard.
GitHub: cnpm/npminstall
284 285 286 287 288 289 290 291 292 293 294
exports.findMaxSatisfyingVersion = (spec, distTags, allVersions) => { // try tag first let realPkgVersion = distTags[spec]; if (!realPkgVersion) { const version = semver.valid(spec); const range = semver.validRange(spec, true); if (semver.satisfies(distTags.latest, spec)) { realPkgVersion = distTags.latest; } else if (version) {
20 21 22 23 24 25 26 27 28 29
const urlsToCheck = []; function isValidFullRelease(tag) { // Is valid semver and does not contain any pre-release labels return semver.valid(tag) && !semver.prerelease(tag); } async function writeBoltVersionUrlsToJson(versionData) { const config = await getConfig();
+ 3 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9
const semver = require("semver"); const version1 = "1.2.3"; const version2 = "2.0"; const version3 = "1.x"; console.log(semver.valid(version1)); // '1.2.3' console.log(semver.valid(version2)); // null console.log(semver.valid(version3)); // null
In this example, we use the semver library to check if three different version strings are valid semantic versions using the semver.valid function. The version1 string matches the basic format of a semantic version ("major.minor.patch") and has three non-negative integer components, so the function returns the string '1.2.3'. The version2 and version3 strings do not match the basic format of a semantic version or have non-integer components, so the function returns null for both of them. This example demonstrates how you can use semver.valid to validate version numbers in your code or to parse user input to ensure that it follows the semantic versioning standard.
GitHub: SAP/fundamental-ngx
91 92 93 94 95 96 97 98 99 100 101
const run = async () => { const release = await bumpedVersionType(); core.info(`${release.reason}, therefore release type should be ${release.releaseType}`); const newVersion = semver.valid(release.releaseType, undefined) || semver.inc(currentVersion, release.releaseType, prereleaseRequested, 'rc'); core.info(`new version is ${newVersion}`); if (writeFile) { packageJson.version = newVersion;
+ 3 other calls in file
GitHub: qiqiboy/tiger-new
102 103 104 105 106 107 108 109 110 111
name: 'version', type: 'input', message: '请输入项目版本号(version):', default: answers.type === 'application' ? '1.0.0' : '0.0.1', validate: function (input) { return semver.valid(input) ? true : chalk.cyan(input) + ' 不是一个有效的版本号'; } }, { name: 'name',
67 68 69 70 71 72 73 74 75 76
if (versions[semverRange] != null) return versions[semverRange] } // get all the valid version numbers, sort by semver in descending order const versionNames = Object.keys(versions) .filter(versionName => semver.valid(versionName)) // might be crap in there .sort(semver.rcompare) // find the first version that satisfies for (let versionName of versionNames) {
105 106 107 108 109 110 111 112 113 114
const semverExtension = { base: Joi.string(), type: 'semver', validate: function (value, { error }) { return semver.valid(value) ? { value } : { errors: error(`${extensionName}.valid`) } }, messages: { [`${extensionName}.valid`]: '{{#label}} needs to be a valid semver expression', [`${extensionName}.gt`]: '{{#label}} needs to be greater than {{#exp}}',
GitHub: athombv/node-homey
899 900 901 902 903 904 905 906 907 908
manifest = App.getManifest({ appPath: this.path }); manifestFolder = this.path; } switch (true) { case semver.valid(version): manifest.version = version; break; case ['minor', 'major', 'patch'].includes(version):
+ 3 other calls in file
114 115 116 117 118 119 120 121 122 123
} }; var sortBySemver = function(versions) { const cmp = function(a, b) { const a_valid = semver.valid(a); const b_valid = semver.valid(b); switch (false) { case !a_valid || !b_valid: return semver.rcompare(a, b); case !a_valid: return -1;
+ 3 other calls in file
128 129 130 131 132 133 134 135 136 137
if (invalidVersions.length > 0) { log.warn('pickVersion', 'The package %s has invalid semver-version(s): %s. This usually only happens for unofficial private registries. ' + 'You should delete or re-publish the invalid versions.', pkg.name, invalidVersions.join(', ')) } versions = versions.filter(function (v) { return semver.valid(v) }) if (dep.type === 'tag') { var tagVersion = pkg['dist-tags'][dep.spec] if (pkg.versions[tagVersion]) return thenAddMetadata(pkg.versions[tagVersion])
+ 5 other calls in file
GitHub: Jokerfive7/-Duelyst-
26 27 28 29 30 31 32 33 34 35 36 37
return umask.validate(data, k, val) } function validateSemver (data, k, val) { if (!semver.valid(val)) return false data[k] = semver.valid(val) } function validateStream (data, k, val) { if (!(val instanceof Stream)) return false
+ 5 other calls in file
223 224 225 226 227 228 229 230 231 232
pluginMap[plugin.id].version = plugin.version; pluginMap[plugin.id].settingsRoute = plugin.settingsRoute; pluginMap[plugin.id].license = plugin.license; // If package.json defines a version to use, stick to that if (dependencies.hasOwnProperty(plugin.id) && semver.valid(dependencies[plugin.id])) { pluginMap[plugin.id].latest = dependencies[plugin.id]; } else { pluginMap[plugin.id].latest = pluginMap[plugin.id].latest || plugin.version; }
571 572 573 574 575 576 577 578 579 580 581
}); } function getInstallPackage(version, originalDirectory) { let packageToInstall = 'react-scripts'; const validSemver = semver.valid(version); if (validSemver) { packageToInstall += `@${validSemver}`; } else if (version) { if (version[0] === '@' && !version.includes('/')) {
+ 2 other calls in file
GitHub: wuguokai/gpt_bot
124 125 126 127 128 129 130 131 132 133
return _.orderBy( glob.sync('migrations/*.js', { cwd: rootPath }).map(filepath => { const [rawVersion, timestamp, title] = path.basename(filepath).split('-') return { filename: path.basename(filepath), version: semver.valid(rawVersion.replace(/_/g, '.')), title: (title || '').replace(/\.js$/i, ''), date: Number(timestamp), location: path.join(rootPath, filepath) }
+ 4 other calls in file
95 96 97 98 99 100 101 102 103 104
if (!session.version) { logger.error('Missing version information in session'); return true; } var sessionVersion = semver.valid(session.version); if (!sessionVersion) { logger.error('Invalid version information in this session:', session.version); return true;
+ 3 other calls in file
56 57 58 59 60 61 62 63 64 65
node: { extensions: ['.mjs', '.js', '.json', '.ts', '.tsx'], }, }, react: { version: semver.valid(semver.coerce(PKG.dependencies.react)), }, }, env: {
412 413 414 415 416 417 418 419 420 421
const spec = res.rawSpec.trim() // no save spec for registry components as we save based on the fetched // version, not on the argument so this can't compute that. res.saveSpec = null res.fetchSpec = spec const version = semver.valid(spec, true) const range = semver.validRange(spec, true) if (version) { res.type = 'version' } else if (range) {
43 44 45 46 47 48 49 50 51 52
} if (args[0] === 'from-git') { retrieveTagVersion(silent, data, cb_) } else { var newVersion = semver.valid(args[0]) if (!newVersion) newVersion = semver.inc(data.version, args[0], npm.config.get('preid')) if (!newVersion) return cb_(version.usage) persistVersion(newVersion, silent, data, cb_) }
625 626 627 628 629 630 631
} else { return null; } } return semver.valid(version); }
semver.gte is the most popular function in semver (528 examples)