Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit f3d4dd2

Browse files
feat: improved validation error messages (#187)
1 parent 37c6acc commit f3d4dd2

7 files changed

+954
-606
lines changed

lint-staged.config.js

Lines changed: 2 additions & 5 deletions
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
};

package-lock.json

Lines changed: 909 additions & 567 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,36 @@
3737
"webpack": "^4.0.0"
3838
},
3939
"dependencies": {
40-
"loader-utils": "^1.1.0",
40+
"loader-utils": "^1.2.3",
4141
"mime": "^2.4.4",
42-
"schema-utils": "^1.0.0"
42+
"schema-utils": "^2.0.0"
4343
},
4444
"devDependencies": {
45-
"@babel/cli": "^7.4.4",
46-
"@babel/core": "^7.4.5",
47-
"@babel/preset-env": "^7.4.5",
48-
"@commitlint/cli": "^8.0.0",
49-
"@commitlint/config-conventional": "^8.0.0",
50-
"@webpack-contrib/defaults": "^5.0.1",
45+
"@babel/cli": "^7.5.5",
46+
"@babel/core": "^7.5.5",
47+
"@babel/preset-env": "^7.5.5",
48+
"@commitlint/cli": "^8.1.0",
49+
"@commitlint/config-conventional": "^8.1.0",
50+
"@webpack-contrib/defaults": "^5.0.2",
5151
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5252
"babel-jest": "^24.8.0",
5353
"commitlint-azure-pipelines-cli": "^1.0.2",
5454
"cross-env": "^5.2.0",
55-
"del": "^4.1.1",
55+
"del": "^5.0.0",
5656
"del-cli": "^2.0.0",
5757
"eslint": "^6.0.1",
58-
"eslint-config-prettier": "^5.1.0",
58+
"eslint-config-prettier": "^6.0.0",
5959
"eslint-plugin-import": "^2.18.0",
6060
"file-loader": "^4.0.0",
61-
"husky": "^2.5.0",
61+
"husky": "^3.0.0",
6262
"jest": "^24.8.0",
6363
"jest-junit": "^6.4.0",
64-
"lint-staged": "^8.2.1",
64+
"lint-staged": "^9.2.0",
6565
"memory-fs": "^0.4.1",
6666
"npm-run-all": "^4.1.5",
6767
"prettier": "^1.18.2",
6868
"standard-version": "^6.0.1",
69-
"webpack": "^4.35.0"
69+
"webpack": "^4.36.1"
7070
},
7171
"keywords": [
7272
"webpack"

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export default function loader(src) {
2727
// Loader Options
2828
const options = getOptions(this) || {};
2929

30-
validateOptions(schema, options, 'URL Loader');
30+
validateOptions(schema, options, {
31+
name: 'URL Loader',
32+
baseDataPath: 'options',
33+
});
3134

3235
// No limit or within the specified limit
3336
if (shouldTransform(options.limit, src.length)) {

src/options.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
"type": "object",
33
"properties": {
44
"limit": {
5+
"description": "Enables/Disables transformation target file into base64 URIs (https://github.com/webpack-contrib/url-loader#limit).",
56
"type": ["boolean", "number", "string"]
67
},
78
"mimetype": {
9+
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
810
"type": "string"
911
},
1012
"fallback": {
13+
"description": "An alternative loader to use when a target file's size exceeds the limit set in the limit option (https://github.com/webpack-contrib/url-loader#fallback).",
1114
"anyOf": [
1215
{
1316
"type": "string"
@@ -16,11 +19,11 @@
1619
"additionalProperties": false,
1720
"properties": {
1821
"loader": {
19-
"description": "Fallback loader name",
22+
"description": "Fallback loader name.",
2023
"type": "string"
2124
},
2225
"options": {
23-
"description": "Fallback loader options",
26+
"description": "Fallback loader options.",
2427
"anyOf": [
2528
{
2629
"type": "object"
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`validation 1`] = `
4-
"URL Loader Invalid Options
5-
6-
options.limit should be boolean,number,string
7-
"
3+
exports[`validate options 1`] = `
4+
"Invalid options object. URL Loader has been initialised using an options object that does not match the API schema.
5+
- options.limit should be:
6+
boolean | number | string
7+
-> Enables/Disables transformation target file into base64 URIs (https://github.com/webpack-contrib/url-loader#limit)."
88
`;
99

10-
exports[`validation 2`] = `
11-
"URL Loader Invalid Options
12-
13-
options.mimetype should be string
14-
"
10+
exports[`validate options 2`] = `
11+
"Invalid options object. URL Loader has been initialised using an options object that does not match the API schema.
12+
- options.mimetype should be a string.
13+
-> The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype)."
1514
`;
1615

17-
exports[`validation 3`] = `
18-
"URL Loader Invalid Options
19-
20-
options.fallback should be string
21-
options.fallback should be object
22-
options.fallback should match some schema in anyOf
23-
"
16+
exports[`validate options 3`] = `
17+
"Invalid options object. URL Loader has been initialised using an options object that does not match the API schema.
18+
- options.fallback should be one of these:
19+
string | object { loader?, options? }
20+
-> An alternative loader to use when a target file's size exceeds the limit set in the limit option (https://github.com/webpack-contrib/url-loader#fallback).
21+
Details:
22+
* options.fallback should be a string.
23+
* options.fallback should be an object:
24+
object { loader?, options? }"
2425
`;

test/validate-options.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import loader from '../src';
22

3-
it('validation', async () => {
3+
it('validate options', async () => {
44
const validate = (options) =>
55
loader.call(
66
Object.assign(
@@ -14,6 +14,8 @@ it('validation', async () => {
1414
'context'
1515
);
1616

17+
expect(() => validate()).not.toThrow();
18+
1719
// The `fallback` loader can have any optsions so we use `additionalProperties: false` to avoid problems.
1820
expect(() => validate({ unknown: 'unknown' })).not.toThrow();
1921

0 commit comments

Comments
 (0)