Skip to content

Commit 46b9b6a

Browse files
authored
Add new valid-values-license rule (#21)
Closes #19 New rule: valid-values-license
1 parent bf458d1 commit 46b9b6a

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Removed
1313

14+
## [1.4.0] - 2016-10-15
15+
### Added
16+
- New rule: [valid-values-license](https://github.com/tclindner/npm-package-json-lint/wiki/valid-values-license)
17+
1418
## [1.3.0] - 2016-07-26
1519
### Added
1620
- New rule: [prefer-no-engineStrict](https://github.com/tclindner/npm-package-json-lint/wiki/prefer-no-engineStrict)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",

src/rules/valid-values-license.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const LintIssue = require('./../LintIssue');
4+
const isValidValue = require('./../validators/valid-values').isValidValue;
5+
const lintId = 'valid-values-license';
6+
const nodeName = 'license';
7+
const message = 'Invalid value for license';
8+
const ruleType = 'valid-values';
9+
10+
/**
11+
* Lints package.json file to check for valid values in the license field
12+
*
13+
* @param {Object} packageJsonData Valid package.json object
14+
* @param {String} lintType 'error' or 'warning'
15+
* @param {Array} validValues An array of valid values
16+
* @return {Object|Boolean} LintIssue object if invalid. True if valid
17+
*/
18+
const lint = function(packageJsonData, lintType, validValues) {
19+
if (!isValidValue(packageJsonData, nodeName, validValues)) {
20+
return new LintIssue(lintId, lintType, nodeName, message);
21+
}
22+
23+
return true;
24+
};
25+
26+
module.exports.lint = lint;
27+
module.exports.ruleType = ruleType;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const should = require('should');
4+
const requireHelper = require('../../require_helper');
5+
const lint = requireHelper('rules/valid-values-license').lint;
6+
7+
describe('valid-values-license Unit Tests', function() {
8+
context('when package.json has node with incorrect value', function() {
9+
it('LintIssue object should be returned', function() {
10+
const packageJsonData = {
11+
license: 'MIT'
12+
};
13+
const validValues = [
14+
'private',
15+
'unlicensed'
16+
];
17+
const response = lint(packageJsonData, 'error', validValues);
18+
19+
response.lintId.should.equal('valid-values-license');
20+
response.lintType.should.equal('error');
21+
response.node.should.equal('license');
22+
response.lintMessage.should.equal('Invalid value for license');
23+
});
24+
});
25+
26+
context('when package.json has node with correct value', function() {
27+
it('LintIssue object should be returned', function() {
28+
const packageJsonData = {
29+
license: 'unlicensed'
30+
};
31+
const validValues = [
32+
'private',
33+
'unlicensed'
34+
];
35+
const response = lint(packageJsonData, 'error', validValues);
36+
37+
response.should.be.true();
38+
});
39+
});
40+
41+
context('when package.json does not have node', function() {
42+
it('true should be returned', function() {
43+
const packageJsonData = {};
44+
const response = lint(packageJsonData, 'error');
45+
46+
response.should.be.true();
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)