1 var scopedPackagePattern = new RegExp("^(?:@([^/]+?)[/])?([^/]+?)$");
2 var builtins = require("builtins")
8 var validate = module.exports = function(name) {
14 errors.push("name cannot be null")
15 return done(warnings, errors)
18 if (name === undefined) {
19 errors.push("name cannot be undefined")
20 return done(warnings, errors)
23 if (typeof name !== "string") {
24 errors.push("name must be a string")
25 return done(warnings, errors)
29 errors.push("name length must be greater than zero")
32 if (name.match(/^\./)) {
33 errors.push("name cannot start with a period")
36 if (name.match(/^_/)) {
37 errors.push("name cannot start with an underscore")
40 if (name.trim() !== name) {
41 errors.push("name cannot contain leading or trailing spaces")
45 blacklist.forEach(function(blacklistedName){
46 if (name.toLowerCase() === blacklistedName) {
47 errors.push(blacklistedName + " is a blacklisted name")
51 // Generate warnings for stuff that used to be allowed
53 // core module names like http, events, util, etc
54 builtins.forEach(function(builtin){
55 if (name.toLowerCase() === builtin) {
56 warnings.push(builtin + " is a core module name")
60 // really-long-package-names-------------------------------such--length-----many---wow
61 // the thisisareallyreallylongpackagenameitshouldpublishdowenowhavealimittothelengthofpackagenames-poch.
62 if (name.length > 214) {
63 warnings.push("name can no longer contain more than 214 characters")
67 if (name.toLowerCase() !== name) {
68 warnings.push("name can no longer contain capital letters")
71 if (encodeURIComponent(name) !== name) {
73 // Maybe it's a scoped package name, like @user/package
74 var nameMatch = name.match(scopedPackagePattern)
76 var user = nameMatch[1]
77 var pkg = nameMatch[2]
78 if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
79 return done(warnings, errors)
83 errors.push("name can only contain URL-friendly characters")
86 return done(warnings, errors)
90 validate.scopedPackagePattern = scopedPackagePattern
92 var done = function (warnings, errors) {
94 validForNewPackages: errors.length === 0 && warnings.length === 0,
95 validForOldPackages: errors.length === 0,
99 if (!result.warnings.length) delete result.warnings
100 if (!result.errors.length) delete result.errors