Skip to content

Commit

Permalink
Add schema validation (#822)
Browse files Browse the repository at this point in the history
* Add `schema-utils`

* Add schema validation
  • Loading branch information
ogonkov committed Feb 21, 2020
1 parent 8b47312 commit 5f55638
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -13,7 +13,8 @@
"find-cache-dir": "^2.0.0",
"loader-utils": "^1.0.2",
"mkdirp": "^0.5.1",
"pify": "^4.0.1"
"pify": "^4.0.1",
"schema-utils": "^2.6.4"
},
"peerDependencies": {
"@babel/core": "^7.0.0",
Expand Down Expand Up @@ -45,7 +46,7 @@
},
"scripts": {
"clean": "rimraf lib/",
"build": "babel src/ --out-dir lib/",
"build": "babel src/ --out-dir lib/ --copy-files",
"format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
"lint": "eslint src test",
"precommit": "lint-staged",
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Expand Up @@ -23,9 +23,11 @@ const { version } = require("../package.json");
const cache = require("./cache");
const transform = require("./transform");
const injectCaller = require("./injectCaller");
const schema = require("./schema");

const { isAbsolute } = require("path");
const loaderUtils = require("loader-utils");
const validateOptions = require("schema-utils");

function subscribe(subscriber, metadata, context) {
if (context[subscriber]) {
Expand Down Expand Up @@ -54,6 +56,10 @@ async function loader(source, inputSourceMap, overrides) {

let loaderOptions = loaderUtils.getOptions(this) || {};

validateOptions(schema, loaderOptions, {
name: "Babel loader",
});

if (loaderOptions.customize != null) {
if (typeof loaderOptions.customize !== "string") {
throw new Error(
Expand Down
28 changes: 28 additions & 0 deletions src/schema.json
@@ -0,0 +1,28 @@
{
"type": "object",
"properties": {
"cacheDirectory": {
"oneOf": [
{
"type": "boolean"
},
{
"type": "string"
}
],
"default": false
},
"cacheIdentifier": {
"type": "string"
},
"cacheCompression": {
"type": "boolean",
"default": true
},
"customize": {
"type": "string",
"default": null
}
},
"additionalProperties": true
}
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -5713,6 +5713,14 @@ schema-utils@^1.0.0:
ajv-errors "^1.0.0"
ajv-keywords "^3.1.0"

schema-utils@^2.6.4:
version "2.6.4"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-2.6.4.tgz#a27efbf6e4e78689d91872ee3ccfa57d7bdd0f53"
integrity sha512-VNjcaUxVnEeun6B2fiiUDjXXBtD4ZSH7pdbfIu1pOFwgptDPLMo/z9jr4sUfsjFVPqDCEin/F7IYlq7/E6yDbQ==
dependencies:
ajv "^6.10.2"
ajv-keywords "^3.4.1"

semver-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
Expand Down

0 comments on commit 5f55638

Please sign in to comment.