Skip to content

Commit 69c0755

Browse files
committed
working packageJson validator
1 parent 36f1bd3 commit 69c0755

File tree

6 files changed

+123
-270
lines changed

6 files changed

+123
-270
lines changed

lib/validate/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ function validate() {
99
return false;
1010
}
1111
var validation = packageJson_1.default(pj);
12-
getValidationMessages(validation.warnings, chalk_1.yellow);
13-
return getValidationMessages(validation.errors, chalk_1.red);
12+
getValidationMessages('Warning', validation.warnings, chalk_1.yellow);
13+
return getValidationMessages('Error', validation.errors, chalk_1.red);
1414
}
1515
Object.defineProperty(exports, "__esModule", { value: true });
1616
exports.default = validate;
17-
function getValidationMessages(validation, color) {
17+
function getValidationMessages(title, validation, color) {
1818
if (validation && validation.length) {
19-
validation.forEach(function (e) {
20-
console.log(color("\n Error: " + e.name + " " + e.msg + ".\n Example: " + e.example + "\n "));
19+
validation.forEach(function (e, index) {
20+
console.log(color((index + 1) + ". " + title + ": \"" + e.name + "\" " + e.msg + ".\n Example: " + e.example + "\n"));
2121
});
2222
return false;
2323
}

lib/validate/packageJson.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use strict";
22
var pJKeys = [{
33
name: 'name',
4-
validate: function (name) { return name.match(/^coderoad-[A-Za-z0-9\-]+$/); },
4+
validate: function (name) { return !!name.match(/^coderoad-[A-Za-z0-9\-]+$/); },
55
msg: 'must be kebabcased and start with "coderoad"',
66
example: 'coderoad-tutorial-name',
77
}, {
88
name: 'version',
9-
validate: function (version) { return version.match(/^[0-9]+\.[0-9]+\.[0-9]+$/); },
9+
validate: function (version) { return !!version.match(/^[0-9]+\.[0-9]+\.[0-9]+$/); },
1010
msg: 'must be 3 numbers separated by dots',
1111
example: '0.1.0',
1212
}, {
@@ -21,7 +21,7 @@ var pJKeys = [{
2121
example: 'CodeRoad tutorial on ES2015 new features.'
2222
}, {
2323
name: 'keywords',
24-
validate: function (keywords) { return Array.isArray(keywords) && keywords.length && keywords.includes('coderoad'); },
24+
validate: function (keywords) { return Array.isArray(keywords) && !!keywords.length && keywords.includes('coderoad'); },
2525
msg: 'must be an array containing "coderoad"',
2626
example: '["coderoad", "tutorial", "js"]',
2727
}, {
@@ -41,26 +41,26 @@ var pJKeys = [{
4141
example: '["coderoad.json", "tutorial"]',
4242
}, {
4343
name: 'engines',
44-
validate: function (engines) { return typeof engines === 'object' && engines.node && engines.node.match(/^[>=]?[0-9]+/); },
44+
validate: function (engines) { return typeof engines === 'object' && !!engines.node && !!engines.node.match(/^(>=)?[0-9]+/); },
4545
msg: 'must specify a valid node version',
4646
example: '"engines": { "node": ">=0.10.3"}',
4747
}, {
4848
name: 'language',
4949
config: true,
50-
validate: function (lang) { return typeof lang === 'string' && lang.length; },
50+
validate: function (lang) { return typeof lang === 'string' && !!lang.length; },
5151
msg: 'must specify a programming language',
5252
example: 'JS',
5353
}, {
5454
name: 'runner',
5555
config: true,
56-
validate: function (runner) { return typeof runner === 'string' && runner.length; },
56+
validate: function (runner) { return typeof runner === 'string' && !!runner.length; },
5757
msg: 'must specify a test runner',
5858
example: 'mocha-coderoad',
5959
}, {
6060
name: 'repository',
6161
optional: true,
6262
validate: function (repo) {
63-
return typeof repo === 'string' && repo.length ||
63+
return typeof repo === 'string' && !!repo.length ||
6464
typeof repo === 'object' && repo.hasOwnProperty('type')
6565
&& typeof repo.type === 'string' &&
6666
repo.hasOwnProperty('url') && typeof repo.url === 'string';
@@ -77,21 +77,21 @@ var pJKeys = [{
7777
}, {
7878
name: 'license',
7979
optional: true,
80-
validate: function (license) { return typeof license === 'string' && license.length; },
80+
validate: function (license) { return typeof license === 'string' && !!license.length; },
8181
msg: 'should have a valid license (ex: MIT, ISC, etc.)',
8282
example: 'MIT',
8383
}];
8484
function validatePackageJson(pj) {
8585
var errors = [];
8686
var warnings = [];
8787
pJKeys.forEach(function (key) {
88-
var target = pj.config ? pj.config : pj;
89-
if (!target.hasOwnProperty(key.name) || key.validate(target[key.name])) {
90-
if (!key.optional) {
91-
errors.push({ name: key.name, msg: key.msg, example: key.example });
88+
var target = key.config ? pj.config : pj;
89+
if (!target.hasOwnProperty(key.name) || !key.validate(target[key.name])) {
90+
if (key.optional) {
91+
warnings.push({ name: key.name, msg: key.msg, example: key.example });
9292
}
9393
else {
94-
warnings.push({ name: key.name, msg: key.msg, example: key.example });
94+
errors.push({ name: key.name, msg: key.msg, example: key.example });
9595
}
9696
}
9797
});

src/validate/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ export default function validate(): boolean {
1010
}
1111
const validation = validatePackageJson(pj);
1212
// log warnings
13-
getValidationMessages(validation.warnings, yellow);
13+
getValidationMessages('Warning', validation.warnings, yellow);
1414
// log and return errors
15-
return getValidationMessages(validation.errors, red);
15+
return getValidationMessages('Error', validation.errors, red);
1616
}
1717

18-
function getValidationMessages(validation: PJErrors[], color: any) {
18+
function getValidationMessages(title: string, validation: PJErrors[], color: any) {
1919
if (validation && validation.length) {
20-
validation.forEach((e) => {
21-
console.log(color(`
22-
Error: ${e.name} ${e.msg}.
20+
validation.forEach((e, index) => {
21+
console.log(
22+
color(`${index + 1}. ${title}: "${e.name}" ${e.msg}.
2323
Example: ${e.example}
24-
`));
24+
`));
2525
});
2626
return false;
2727
}

src/validate/packageJson.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const pJKeys: PJKeys[] = [{
22
name: 'name',
3-
validate: name => name.match(/^coderoad-[A-Za-z0-9\-]+$/),
3+
validate: name => !!name.match(/^coderoad-[A-Za-z0-9\-]+$/),
44
msg: 'must be kebabcased and start with "coderoad"',
55
example: 'coderoad-tutorial-name',
66
}, {
77
name: 'version',
8-
validate: version => version.match(/^[0-9]+\.[0-9]+\.[0-9]+$/),
8+
validate: version => !!version.match(/^[0-9]+\.[0-9]+\.[0-9]+$/),
99
msg: 'must be 3 numbers separated by dots',
1010
example: '0.1.0',
1111
}, {
@@ -20,7 +20,7 @@ const pJKeys: PJKeys[] = [{
2020
example: 'CodeRoad tutorial on ES2015 new features.'
2121
}, {
2222
name: 'keywords',
23-
validate: keywords => Array.isArray(keywords) && keywords.length && keywords.includes('coderoad'),
23+
validate: keywords => Array.isArray(keywords) && !!keywords.length && keywords.includes('coderoad'),
2424
msg: 'must be an array containing "coderoad"',
2525
example: '["coderoad", "tutorial", "js"]',
2626
}, {
@@ -40,26 +40,26 @@ const pJKeys: PJKeys[] = [{
4040
example: '["coderoad.json", "tutorial"]',
4141
}, {
4242
name: 'engines',
43-
validate: engines => typeof engines === 'object' && engines.node && engines.node.match(/^[>=]?[0-9]+/),
43+
validate: engines => typeof engines === 'object' && !!engines.node && !!engines.node.match(/^(>=)?[0-9]+/),
4444
msg: 'must specify a valid node version',
4545
example: '"engines": { "node": ">=0.10.3"}',
4646
}, {
4747
name: 'language',
4848
config: true,
49-
validate: lang => typeof lang === 'string' && lang.length,
49+
validate: lang => typeof lang === 'string' && !!lang.length,
5050
msg: 'must specify a programming language',
5151
example: 'JS',
5252
}, {
5353
name: 'runner',
5454
config: true,
55-
validate: runner => typeof runner === 'string' && runner.length,
55+
validate: runner => typeof runner === 'string' && !!runner.length,
5656
msg: 'must specify a test runner',
5757
example: 'mocha-coderoad',
5858
}, {
5959
name: 'repository',
6060
optional: true,
6161
validate: (repo: string | { type: string, url: string }) => {
62-
return typeof repo === 'string' && repo.length ||
62+
return typeof repo === 'string' && !!repo.length ||
6363
typeof repo === 'object' && repo.hasOwnProperty('type')
6464
&& typeof repo.type === 'string' &&
6565
repo.hasOwnProperty('url') && typeof repo.url === 'string';
@@ -76,25 +76,23 @@ const pJKeys: PJKeys[] = [{
7676
}, {
7777
name: 'license',
7878
optional: true,
79-
validate: license => typeof license === 'string' && license.length,
79+
validate: license => typeof license === 'string' && !!license.length,
8080
msg: 'should have a valid license (ex: MIT, ISC, etc.)',
8181
example: 'MIT',
8282
}];
8383

84-
85-
8684
export default function validatePackageJson(pj: PackageJson): ValidatePjOutput {
8785
const errors = [];
8886
const warnings = [];
8987
pJKeys.forEach(key => {
9088
// key on pj or pj.config
91-
const target = pj.config ? pj.config : pj;
89+
const target = key.config ? pj.config : pj;
9290
// key doesn't exist or key is invalid
93-
if (!target.hasOwnProperty(key.name) || key.validate(target[key.name])) {
94-
if (!key.optional) {
95-
errors.push({ name: key.name, msg: key.msg, example: key.example });
96-
} else {
91+
if (!target.hasOwnProperty(key.name) || !key.validate(target[key.name])) {
92+
if (key.optional) {
9793
warnings.push({ name: key.name, msg: key.msg, example: key.example });
94+
} else {
95+
errors.push({ name: key.name, msg: key.msg, example: key.example });
9896
}
9997
}
10098
});

test/build-chapter.spec.js

Lines changed: 0 additions & 145 deletions
This file was deleted.

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