Skip to content

Commit 8725cbb

Browse files
authored
Updates array schema validation to work with prefer-property-order (#144)
* Updates array schema validation to work with prefer-property-order Fixes #142 * Update prefer-property-order.test.js
1 parent 8971257 commit 8725cbb

29 files changed

+163
-54
lines changed

CHANGELOG.md

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

55
## [Unreleased]
66

7+
## [4.0.2] - 2019-10-19
8+
### Fixed
9+
- Resolved an issue with config schema validation that caused `prefer-property-order` to fail with valid config. Addresses [#142](https://github.com/tclindner/npm-package-json-lint/issues/142) from @sakthivel-tw.
10+
711
## [4.0.1] - 2019-10-19
812
### Fixed
913
- Resolved an issue with applying override config. Please see [#123](https://github.com/tclindner/npm-package-json-lint/issues/123) for more details. Thank you @simison for reporting this issue.

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": "4.0.1",
3+
"version": "4.0.2",
44
"description": "Configurable linter for package.json files.",
55
"keywords": [
66
"lint",

src/config/ConfigSchema.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,33 @@ const standardRuleSchema = {
2424
}
2525
};
2626

27-
const arrayRuleSchema = {
28-
type: 'array',
29-
items: [
30-
standardRuleSchema,
31-
{
32-
type: 'array',
33-
minItems: 1,
34-
uniqueItems: true,
35-
errorMessage: {
36-
type: 'the second item in an array rule config must be an array.',
37-
minItems: 'the second item in an array rule config must have at least 1 item.',
38-
uniqueItems: 'the second item in an array rule config must have unique items.'
27+
const arrayRuleSchema = minItems => {
28+
return {
29+
type: 'array',
30+
items: [
31+
standardRuleSchema,
32+
{
33+
type: 'array',
34+
minItems,
35+
uniqueItems: true,
36+
errorMessage: {
37+
type: 'the second item in an array rule config must be an array.',
38+
minItems: 'the second item in an array rule config must have at least 1 item.',
39+
uniqueItems: 'the second item in an array rule config must have unique items.'
40+
}
3941
}
42+
],
43+
minItems: 2,
44+
maxItems: 2,
45+
additionalItems: false,
46+
errorMessage: {
47+
type: 'rule config must be an array, e.g. ["error", ["value1", "value2"]].',
48+
minItems: 'array rules must have two items, severity and options array. e.g. ["error", ["value1", "value2"]].',
49+
maxItems: 'array rules must have two items, severity and options array. e.g. ["error", ["value1", "value2"]].',
50+
additionalItems:
51+
'array rules are only allowed two items, severity and the list is values. e.g. ["error", ["value1", "value2"]].'
4052
}
41-
],
42-
minItems: 2,
43-
maxItems: 2,
44-
additionalItems: false,
45-
errorMessage: {
46-
type: 'rule config must be an array, e.g. ["error", ["value1", "value2"]].',
47-
minItems: 'array rules must have two items, severity and options array. e.g. ["error", ["value1", "value2"]].',
48-
maxItems: 'array rules must have two items, severity and options array. e.g. ["error", ["value1", "value2"]].',
49-
additionalItems:
50-
'array rules are only allowed two items, severity and the list is values. e.g. ["error", ["value1", "value2"]].'
51-
}
53+
};
5254
};
5355

5456
const objectRuleSchema = {
@@ -150,10 +152,11 @@ const isStandardRuleSchemaValid = ruleConfig => {
150152
* Validates array rules config.
151153
*
152154
* @param {Object} ruleConfig The ruleConfig object to validate.
155+
* @param {number} minItems Min number of items in the array
153156
* @returns {boolean} True if valid. Error if not.
154157
*/
155-
const isArrayRuleSchemaValid = ruleConfig => {
156-
const validate = ajv.compile(arrayRuleSchema);
158+
const isArrayRuleSchemaValid = (ruleConfig, minItems) => {
159+
const validate = ajv.compile(arrayRuleSchema(minItems));
157160
const isValid = validate(ruleConfig);
158161

159162
if (!isValid) {

src/config/ConfigValidator.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ const isOptionalObjRuleConfigValid = ruleConfig => {
4646
* Validates array rule config
4747
*
4848
* @param {Array} ruleConfig Array rule
49+
* @param {number} minItems Min number of items in the array
4950
* @return {Boolean} True if config is valid, false if not
5051
* @static
5152
*/
52-
const isArrayRuleConfigValid = ruleConfig => {
53+
const isArrayRuleConfigValid = (ruleConfig, minItems) => {
5354
if (typeof ruleConfig === 'string' && ruleConfig === 'off') {
5455
return true;
5556
}
@@ -58,7 +59,7 @@ const isArrayRuleConfigValid = ruleConfig => {
5859
throw new Error('\t- is an array type rule. It must be set to "off" if an array is not supplied.');
5960
}
6061

61-
return ConfigSchema.isArrayRuleSchemaValid(ruleConfig);
62+
return ConfigSchema.isArrayRuleSchemaValid(ruleConfig, minItems);
6263
};
6364

6465
/**
@@ -85,7 +86,7 @@ const validateRule = (ruleModule, ruleName, userConfig, source) => {
8586
if (ruleModule) {
8687
try {
8788
if (ruleModule.ruleType === 'array') {
88-
isArrayRuleConfigValid(userConfig);
89+
isArrayRuleConfigValid(userConfig, ruleModule.minItems);
8990
} else if (ruleModule.ruleType === 'object') {
9091
isObjectRuleConfigValid(userConfig);
9192
} else if (ruleModule.ruleType === 'optionalObject') {

src/rules/no-restricted-dependencies.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lintId = 'no-restricted-dependencies';
55
const nodeName = 'dependencies';
66
const message = 'You are using a restricted dependency. Please remove it.';
77
const ruleType = 'array';
8+
const minItems = 1;
89

910
const lint = (packageJsonData, severity, invalidDependencies) => {
1011
if (hasDependency(packageJsonData, nodeName, invalidDependencies)) {
@@ -16,5 +17,6 @@ const lint = (packageJsonData, severity, invalidDependencies) => {
1617

1718
module.exports = {
1819
lint,
19-
ruleType
20+
ruleType,
21+
minItems
2022
};

src/rules/no-restricted-devDependencies.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lintId = 'no-restricted-devDependencies';
55
const nodeName = 'devDependencies';
66
const message = 'You are using a restricted dependency. Please remove it.';
77
const ruleType = 'array';
8+
const minItems = 1;
89

910
const lint = (packageJsonData, severity, invalidDependencies) => {
1011
if (hasDependency(packageJsonData, nodeName, invalidDependencies)) {
@@ -16,5 +17,6 @@ const lint = (packageJsonData, severity, invalidDependencies) => {
1617

1718
module.exports = {
1819
lint,
19-
ruleType
20+
ruleType,
21+
minItems
2022
};

src/rules/no-restricted-pre-release-dependencies.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lintId = 'no-restricted-pre-release-dependencies';
55
const nodeName = 'dependencies';
66
const message = 'You are using a restricted pre-release dependency. Please remove it.';
77
const ruleType = 'array';
8+
const minItems = 1;
89

910
const lint = (packageJsonData, severity, invalidPreRelDeps) => {
1011
if (hasDepPrereleaseVers(packageJsonData, nodeName, invalidPreRelDeps)) {
@@ -16,5 +17,6 @@ const lint = (packageJsonData, severity, invalidPreRelDeps) => {
1617

1718
module.exports = {
1819
lint,
19-
ruleType
20+
ruleType,
21+
minItems
2022
};

src/rules/no-restricted-pre-release-devDependencies.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lintId = 'no-restricted-pre-release-devDependencies';
55
const nodeName = 'devDependencies';
66
const message = 'You are using a restricted pre-release dependency. Please remove it.';
77
const ruleType = 'array';
8+
const minItems = 1;
89

910
const lint = (packageJsonData, severity, invalidPreRelDeps) => {
1011
if (hasDepPrereleaseVers(packageJsonData, nodeName, invalidPreRelDeps)) {
@@ -16,5 +17,6 @@ const lint = (packageJsonData, severity, invalidPreRelDeps) => {
1617

1718
module.exports = {
1819
lint,
19-
ruleType
20+
ruleType,
21+
minItems
2022
};

src/rules/prefer-property-order.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lintId = 'prefer-property-order';
55
const nodeName = '';
66
const message = 'Your package.json properties are not in the desired order.';
77
const ruleType = 'array';
8+
const minItems = 0;
89

910
const lint = (packageJsonData, severity, preferredOrder) => {
1011
const result = isInPreferredOrder(packageJsonData, preferredOrder);
@@ -18,5 +19,6 @@ const lint = (packageJsonData, severity, preferredOrder) => {
1819

1920
module.exports = {
2021
lint,
21-
ruleType
22+
ruleType,
23+
minItems
2224
};

src/rules/valid-values-author.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const lintId = 'valid-values-author';
77
const nodeName = 'author';
88
const message = 'Invalid value for author';
99
const ruleType = 'array';
10+
const minItems = 1;
1011

1112
const lint = (packageJsonData, severity, validValues) => {
1213
let value;
@@ -32,5 +33,6 @@ const lint = (packageJsonData, severity, validValues) => {
3233

3334
module.exports = {
3435
lint,
35-
ruleType
36+
ruleType,
37+
minItems
3638
};

0 commit comments

Comments
 (0)