Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Commit 074bcdc

Browse files
feat: improved validation error messages (#89)
1 parent fe5f341 commit 074bcdc

10 files changed

+1371
-952
lines changed

Diff for: .github/CONTRIBUTING.md

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ module. Thankfully, Github provides a means to do this. Add a dependency to the
147147

148148
Where `{id}` is the # ID of your Pull Request.
149149

150+
## Contributor License Agreement
151+
152+
When submitting your contribution, a CLA (Contributor License Agreement) bot will come by to verify that you signed the [CLA](https://cla.js.foundation/webpack-contrib/cache-loader).
153+
If it is your first time, it will link you to the right place to sign it.
154+
However, if you have committed your contributions using an email that is not the same as your email used on GitHub, the CLA bot can't accept your contribution.
155+
156+
Run `git config user.email` to see your Git email, and verify it with [your GitHub email](https://github.com/settings/emails).
157+
150158
## Thanks
151159

152160
For your interest, time, understanding, and for following this simple guide.

Diff for: lint-staged.config.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
module.exports = {
2-
ignore: ['package-lock.json', 'CHANGELOG.md'],
3-
linters: {
4-
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
5-
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
6-
},
2+
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
3+
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
74
};

Diff for: package-lock.json

+1,266-891
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+17-18
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,38 @@
4242
"loader-utils": "^1.2.3",
4343
"mkdirp": "^0.5.1",
4444
"neo-async": "^2.6.1",
45-
"schema-utils": "^1.0.0"
45+
"schema-utils": "^2.0.0"
4646
},
4747
"devDependencies": {
48-
"@babel/cli": "^7.4.4",
49-
"@babel/core": "^7.4.5",
50-
"@babel/preset-env": "^7.4.5",
51-
"@commitlint/cli": "^8.0.0",
52-
"@commitlint/config-conventional": "^8.0.0",
53-
"@webpack-contrib/defaults": "^5.0.0",
48+
"@babel/cli": "^7.5.5",
49+
"@babel/core": "^7.5.5",
50+
"@babel/preset-env": "^7.5.5",
51+
"@commitlint/cli": "^8.1.0",
52+
"@commitlint/config-conventional": "^8.1.0",
53+
"@webpack-contrib/defaults": "^5.0.2",
5454
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5555
"babel-jest": "^24.8.0",
5656
"babel-loader": "^8.0.6",
5757
"commitlint-azure-pipelines-cli": "^1.0.2",
5858
"cross-env": "^5.2.0",
59-
"del": "^4.1.1",
59+
"del": "^5.0.0",
6060
"del-cli": "^2.0.0",
61-
"eslint": "^5.16.0",
62-
"eslint-config-prettier": "^4.3.0",
63-
"eslint-plugin-import": "^2.17.3",
64-
"eslint-plugin-prettier": "^3.1.0",
65-
"file-loader": "^4.0.0",
66-
"husky": "^2.4.0",
61+
"eslint": "^6.0.1",
62+
"eslint-config-prettier": "^6.0.0",
63+
"eslint-plugin-import": "^2.18.0",
64+
"file-loader": "^4.1.0",
65+
"husky": "^3.0.0",
6766
"jest": "^24.8.0",
6867
"jest-junit": "^6.4.0",
69-
"lint-staged": "^8.1.7",
68+
"lint-staged": "^9.2.0",
7069
"memory-fs": "^0.4.1",
7170
"normalize-path": "^3.0.0",
7271
"npm-run-all": "^4.1.5",
73-
"prettier": "^1.17.1",
72+
"prettier": "^1.18.2",
7473
"standard-version": "^6.0.1",
7574
"uuid": "^3.3.2",
76-
"webpack": "^4.33.0",
77-
"webpack-cli": "^3.3.2"
75+
"webpack": "^4.36.1",
76+
"webpack-cli": "^3.3.6"
7877
},
7978
"keywords": [
8079
"webpack"

Diff for: src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ function roundMs(mtime, precision) {
5959
// to break watchpack -> chokidar watch logic over webpack@4 --watch
6060
function loader(...args) {
6161
const options = Object.assign({}, defaults, getOptions(this));
62-
validateOptions(schema, options, 'Cache Loader');
62+
63+
validateOptions(schema, options, {
64+
name: 'Cache Loader',
65+
baseDataPath: 'options',
66+
});
6367

6468
const { readOnly, write: writeFn } = options;
6569

@@ -148,7 +152,10 @@ function loader(...args) {
148152
function pitch(remainingRequest, prevRequest, dataInput) {
149153
const options = Object.assign({}, defaults, getOptions(this));
150154

151-
validateOptions(schema, options, 'Cache Loader (Pitch)');
155+
validateOptions(schema, options, {
156+
name: 'Cache Loader (Pitch)',
157+
baseDataPath: 'options',
158+
});
152159

153160
const {
154161
cacheContext,

Diff for: src/options.json

+9
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@
22
"type": "object",
33
"properties": {
44
"cacheContext": {
5+
"description": "The default cache context in order to generate the cache relatively to a path. By default it will use absolute paths.",
56
"type": "string"
67
},
78
"cacheKey": {
9+
"description": "Allows you to override default cache key generator.",
810
"instanceof": "Function"
911
},
1012
"cacheIdentifier": {
13+
"description": "Provide a cache directory where cache items should be stored (used for default read/write implementation).",
1114
"type": "string"
1215
},
1316
"cacheDirectory": {
17+
"description": "Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation).",
1418
"type": "string"
1519
},
1620
"compare": {
21+
"description": "Allows you to override default comparison function between the cached dependency and the one is being read. Return true to use the cached resource.",
1722
"instanceof": "Function"
1823
},
1924
"precision": {
25+
"description": "Round mtime by this number of milliseconds both for stats and deps before passing those params to the comparing function.",
2026
"type": "number"
2127
},
2228
"read": {
29+
"description": "Allows you to override default read cache data from file.",
2330
"instanceof": "Function"
2431
},
2532
"readOnly": {
33+
"description": "Allows you to override default value and make the cache read only (useful for some environments where you don't want the cache to be updated, only read from it).",
2634
"type": "boolean"
2735
},
2836
"write": {
37+
"description": "Allows you to override default write cache data to file (e.g. Redis, memcached).",
2938
"instanceof": "Function"
3039
}
3140
},

Diff for: test/__snapshots__/errors.test.js.snap

-15
This file was deleted.

Diff for: test/__snapshots__/validate-options.test.js.snap

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`validate options error (pitch) 1`] = `
4+
"Invalid options object. Cache Loader (Pitch) has been initialised using an options object that does not match the API schema.
5+
- options.cacheIdentifier should be a string.
6+
-> Provide a cache directory where cache items should be stored (used for default read/write implementation)."
7+
`;
8+
9+
exports[`validate options error 1`] = `
10+
"Invalid options object. Cache Loader has been initialised using an options object that does not match the API schema.
11+
- options.cacheIdentifier should be a string.
12+
-> Provide a cache directory where cache items should be stored (used for default read/write implementation)."
13+
`;
14+
15+
exports[`validate options unknown (pitch) 1`] = `
16+
"Invalid options object. Cache Loader (Pitch) has been initialised using an options object that does not match the API schema.
17+
- options has an unknown property 'unknown'. These properties are valid:
18+
object { cacheContext?, cacheKey?, cacheIdentifier?, cacheDirectory?, compare?, precision?, read?, readOnly?, write? }"
19+
`;
20+
21+
exports[`validate options unknown 1`] = `
22+
"Invalid options object. Cache Loader has been initialised using an options object that does not match the API schema.
23+
- options has an unknown property 'unknown'. These properties are valid:
24+
object { cacheContext?, cacheKey?, cacheIdentifier?, cacheDirectory?, compare?, precision?, read?, readOnly?, write? }"
25+
`;

Diff for: test/errors.test.js

-21
This file was deleted.

Diff for: test/validate-options.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import loader, { pitch } from '../src';
2+
3+
// Needed for `schema-utils` to not call
4+
// process.exit(1) when running tests
5+
process.env.JEST = true;
6+
7+
describe('validate options', () => {
8+
test('error', () => {
9+
const err = () => loader.call({ query: { cacheIdentifier: 1 } });
10+
11+
expect(err).toThrow();
12+
expect(err).toThrowErrorMatchingSnapshot();
13+
});
14+
15+
test('unknown', () => {
16+
const err = () => loader.call({ query: { unknown: 'unknown' } });
17+
18+
expect(err).toThrow();
19+
expect(err).toThrowErrorMatchingSnapshot();
20+
});
21+
22+
test('error (pitch)', () => {
23+
const err = () => pitch.call({ query: { cacheIdentifier: 1 } });
24+
25+
expect(err).toThrow();
26+
expect(err).toThrowErrorMatchingSnapshot();
27+
});
28+
29+
test('unknown (pitch)', () => {
30+
const err = () => pitch.call({ query: { unknown: 'unknown' } });
31+
32+
expect(err).toThrow();
33+
expect(err).toThrowErrorMatchingSnapshot();
34+
});
35+
});

0 commit comments

Comments
 (0)