Skip to content

Commit 2f884cd

Browse files
committed
include warnings in packageJson validator
1 parent 81603ff commit 2f884cd

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ var tutorials_1 = require('./tutorials');
1111
exports.tutorials = tutorials_1.default;
1212
var update_1 = require('./update');
1313
exports.update = update_1.default;
14-
var name_1 = require('./validate/name');
15-
exports.validateName = name_1.default;
14+
var packageJson_1 = require('./validate/packageJson');
15+
exports.validatePackageJson = packageJson_1.default;

lib/validate/packageJson.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,49 @@ var pJKeys = [{
5656
validate: function (runner) { return typeof runner === 'string' && runner.length; },
5757
msg: 'must specify a test runner',
5858
example: 'mocha-coderoad',
59+
}, {
60+
name: 'repository',
61+
optional: true,
62+
validate: function (repo) {
63+
return typeof repo === 'string' && repo.length ||
64+
typeof repo === 'object' && repo.hasOwnProperty('type')
65+
&& typeof repo.type === 'string' &&
66+
repo.hasOwnProperty('url') && typeof repo.url === 'string';
67+
},
68+
msg: 'should have a valid repository',
69+
example: 'https://github.com/shmck/coderoad-tutorial-name',
70+
}, {
71+
name: 'bugs',
72+
optional: true,
73+
validate: function (bugs) { return typeof bugs === 'object' &&
74+
bugs.hasOwnProperty('url') && typeof bugs.url === 'string'; },
75+
msg: 'should have a link to where to post bugs',
76+
example: '"bugs": { "url": "https://github.com/shmck/coderoad-tutorial-name" }'
77+
}, {
78+
name: 'license',
79+
optional: true,
80+
validate: function (license) { return typeof license === 'string' && license.length; },
81+
msg: 'should have a valid license (ex: MIT, ISC, etc.)',
82+
example: 'MIT',
5983
}];
6084
function validatePackageJson(pj) {
6185
var errors = [];
86+
var warnings = [];
6287
pJKeys.forEach(function (key) {
6388
var target = pj.config ? pj.config : pj;
6489
if (!target.hasOwnProperty(key.name) || key.validate(target[key.name])) {
65-
errors.push({ msg: key.msg, example: key.example });
90+
if (!key.optional) {
91+
errors.push({ msg: key.msg, example: key.example });
92+
}
93+
else {
94+
warnings.push({ msg: key.msg, example: key.example });
95+
}
6696
}
6797
});
68-
return errors;
98+
return {
99+
errors: errors,
100+
warnings: warnings,
101+
};
69102
}
70103
Object.defineProperty(exports, "__esModule", { value: true });
71104
exports.default = validatePackageJson;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export {default as search} from './search';
55
export {default as tutorials} from './tutorials';
66
export {default as update} from './update';
77

8-
export {default as validateName} from './validate/name';
8+
export {default as validatePackageJson} from './validate/packageJson';

src/validate/packageJson.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ const pJKeys: PJKeys[] = [{
5555
validate: runner => typeof runner === 'string' && runner.length,
5656
msg: 'must specify a test runner',
5757
example: 'mocha-coderoad',
58+
}, {
59+
name: 'repository',
60+
optional: true,
61+
validate: (repo: string | { type: string, url: string }) => {
62+
return typeof repo === 'string' && repo.length ||
63+
typeof repo === 'object' && repo.hasOwnProperty('type')
64+
&& typeof repo.type === 'string' &&
65+
repo.hasOwnProperty('url') && typeof repo.url === 'string';
66+
},
67+
msg: 'should have a valid repository',
68+
example: 'https://github.com/shmck/coderoad-tutorial-name',
69+
}, {
70+
name: 'bugs',
71+
optional: true,
72+
validate: (bugs: { url: string }) => typeof bugs === 'object' &&
73+
bugs.hasOwnProperty('url') && typeof bugs.url === 'string',
74+
msg: 'should have a link to where to post bugs',
75+
example: '"bugs": { "url": "https://github.com/shmck/coderoad-tutorial-name" }'
76+
}, {
77+
name: 'license',
78+
optional: true,
79+
validate: license => typeof license === 'string' && license.length,
80+
msg: 'should have a valid license (ex: MIT, ISC, etc.)',
81+
example: 'MIT',
5882
}];
5983

6084
interface PJErrors {
@@ -66,17 +90,31 @@ interface PJKeys extends PJErrors {
6690
name: string;
6791
validate: (content: string) => boolean;
6892
config?: boolean;
93+
optional?: boolean;
94+
}
95+
96+
interface ValidatePjOutput {
97+
errors: PJErrors[];
98+
warnings: PJErrors[];
6999
}
70100

71-
export default function validatePackageJson(pj: PackageJson): PJErrors[] {
101+
export default function validatePackageJson(pj: PackageJson): ValidatePjOutput {
72102
const errors = [];
103+
const warnings = [];
73104
pJKeys.forEach(key => {
74105
// key on pj or pj.config
75106
const target = pj.config ? pj.config : pj;
76107
// key doesn't exist or key is invalid
77108
if (!target.hasOwnProperty(key.name) || key.validate(target[key.name])) {
78-
errors.push({ msg: key.msg, example: key.example });
109+
if (!key.optional) {
110+
errors.push({ msg: key.msg, example: key.example });
111+
} else {
112+
warnings.push({ msg: key.msg, example: key.example });
113+
}
79114
}
80115
});
81-
return errors;
116+
return {
117+
errors,
118+
warnings,
119+
};
82120
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy