Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate options with ajv #9

Merged
merged 2 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/html/.umirc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

export default {
plugins: [
['../../index', {
['../../dist/index', {
html: {},
}],
],
Expand Down
2 changes: 1 addition & 1 deletion examples/normal/.umirc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

export default {
plugins: [
['../../index', {
['../../dist/index', {
splitChunks: true,
}],
],
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
"test": "umi-test"
},
"dependencies": {
"ajv": "^6.9.1",
"deasync-promise": "^1.0.1",
"html-webpack-plugin": "^3.2.0",
"inquirer": "^6.2.1",
"is-plain-object": "^2.0.4",
"lodash": "^4.17.11",
"semver": "^5.6.0"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@types/jest": "^24.0.3",
"@types/lodash": "^4.14.120",
"@types/node": "^10.12.18",
"coveralls": "^3.0.2",
"execa": "^1.0.0",
Expand Down
57 changes: 18 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IApi } from 'umi-plugin-types';
import { existsSync, readdirSync } from 'fs';
import { join, extname, basename, dirname } from 'path';
import { cloneDeep, isPlainObject } from 'lodash';
import AJV from 'ajv';
import schema from './schema';

const isPlainObject = require('is-plain-object');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const assert = require('assert');
const { cloneDeep } = require('lodash');
const deasyncPromise = require('deasync-promise');
const inquirer = require('inquirer');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const semver = require('semver');

interface IOption {
Expand All @@ -28,49 +29,27 @@ interface IEntryConfig {
context?: object,
}

module.exports = function(api: IApi, options = {} as IOption) {
export default function(api: IApi, options = {} as IOption) {
const { log, paths } = api;

const umiVersion = process.env.UMI_VERSION;
assert(
semver.gte(umiVersion, '2.4.3') && semver.lt(umiVersion, '3.0.0'),
`Your umi version is ${umiVersion}, >=2.4.3 and <3 is required.`,
);
assert(
!options.entry || isPlainObject(options.entry),
`options.entry should be object, but got ${JSON.stringify(options.entry)}`,
);
assert(
!options.htmlName || typeof options.htmlName === 'string',
`options.htmlName should be string, but got ${JSON.stringify(
options.htmlName,
)}`,
);
assert(
!options.splitChunks ||
typeof options.splitChunks === 'boolean' ||
isPlainObject(options.splitChunks),
`options.splitChunks should be Boolean or Object, but got ${JSON.stringify(
options.splitChunks,
)}`,
);
assert(
!options.html || isPlainObject(options.html),
`options.html should be Object, but got ${JSON.stringify(options.html)}`,
);
assert(
!options.selectEntry ||
typeof options.selectEntry === 'boolean' ||
isPlainObject(options.selectEntry),
`options.selectEntry should be Boolean or Object, but got ${JSON.stringify(
options.selectEntry,
)}`,
);
if (options.html && options.html.template) {
assert(
extname(options.html.template) === '.ejs',
`options.html.template should use ejs file, but got options.html.template`,
);

// validate options with ajv
const ajv = new AJV({ allErrors: true });
const isValid = ajv.validate(schema, options);
if (!isValid) {
const errors = ajv.errors.map(({ dataPath, message }, index) => {
return `${index + 1}. ${dataPath}${dataPath ? ' ' : ''}${message}`;
});
throw new Error(`
Invalid options applied to umi-plugin-mpa

${errors.join('\n')}
`.trim());
}

log.warn(
Expand Down
70 changes: 70 additions & 0 deletions src/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import schema from './schema';
import AJV from 'ajv';

describe('schema', () => {
const ajv = new AJV();

it('entry', () => {
expect(ajv.validate(schema, {
entry: {},
})).toEqual(true);
});

it('htmlName', () => {
expect(ajv.validate(schema, {
htmlName: 'foo',
})).toEqual(true);
});

it('splitChunks', () => {
// boolean
expect(ajv.validate(schema, {
splitChunks: true,
})).toEqual(true);
// object
expect(ajv.validate(schema, {
splitChunks: {},
})).toEqual(true);
// additional properties
expect(ajv.validate(schema, {
splitChunks: {
foo: 'bar',
},
})).toEqual(true);
});

it('html', () => {
// object
expect(ajv.validate(schema, {
html: {},
})).toEqual(true);
// template only support .ejs extname
expect(ajv.validate(schema, {
html: {
template: 'document.ejs',
},
})).toEqual(true);
expect(ajv.validate(schema, {
html: {
template: 'document.html',
},
})).toEqual(false);
// additional properties
expect(ajv.validate(schema, {
html: {
foo: 'bar',
},
})).toEqual(true);
});

it('selectEntry', () => {
// boolean
expect(ajv.validate(schema, {
selectEntry: true,
})).toEqual(true);
// object
expect(ajv.validate(schema, {
selectEntry: {},
})).toEqual(true);
});
});
36 changes: 36 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

export default {
type: 'object',
additionalProperties: false,
properties: {
entry: {
type: 'object'
},
htmlName: {
type: 'string',
minLength: 1,
},
splitChunks: {
anyOf: [
{ type: 'boolean' },
{ type: 'object' },
],
},
html: {
type: 'object',
properties: {
template: {
type: 'string',
minLength: 1,
pattern: '.ejs$',
},
},
},
selectEntry: {
anyOf: [
{ type: 'boolean' },
{ type: 'object' },
],
},
},
};
4 changes: 2 additions & 2 deletions test/build.test.js → test/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import execa from 'execa';
process.env.COMPRESS = 'none';
process.env.PROGRESS = 'none';
process.env.BABEL_POLYFILL = 'none';
process.env.__FROM_UMI_TEST = true;
process.env.UMI_TEST = true;
process.env.__FROM_UMI_TEST = 'true';
process.env.UMI_TEST = 'true';

function build({ cwd }) {
process.env.APP_ROOT = cwd;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"noEmit": true
"noEmit": true,
"esModuleInterop": true
},
"include": ["src"]
}
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,16 @@
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.2.tgz#0e670ea254d559241b6eeb3894f8754991e73220"
integrity sha512-ui3WwXmjTaY73fOQ3/m3nnajU/Orhi6cEu5rzX+BrAAJxa3eITXZ5ch9suPqtM03OWhAHhPSyBGCN4UKoxO20Q==

"@types/jest@^24.0.3":
version "24.0.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.3.tgz#df8dc56565bd2d27fcbd099434f04f9891e16ab7"
integrity sha512-keRxrIwZZ/Ml83cGl3G0OjGlBO4UFQCCa5QlBPz2pY+ZvSnoeUvMJw0vmL4JU6g4vbpIW5H4qSq1xv9h8naAfg==

"@types/lodash@^4.14.120":
version "4.14.120"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.120.tgz#cf265d06f6c7a710db087ed07523ab8c1a24047b"
integrity sha512-jQ21kQ120mo+IrDs1nFNVm/AsdFxIx2+vZ347DbogHJPd/JzKNMOqU6HCYin1W6v8l5R9XSO2/e9cxmn7HAnVw==

"@types/node@*", "@types/node@^10.12.18":
version "10.12.18"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.18.tgz#1d3ca764718915584fcd9f6344621b7672665c67"
Expand Down Expand Up @@ -1356,6 +1366,16 @@ ajv@^6.1.0, ajv@^6.5.3, ajv@^6.5.5, ajv@^6.6.1:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^6.9.1:
version "6.9.1"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.9.1.tgz#a4d3683d74abc5670e75f0b16520f70a20ea8dc1"
integrity sha512-XDN92U311aINL77ieWHmqCcNlwjoP5cHXDxIxbf2MaPYuCXOHS7gHH8jktxeK5omgd52XbSTX6a4Piwd1pQmzA==
dependencies:
fast-deep-equal "^2.0.1"
fast-json-stable-stringify "^2.0.0"
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

alphanum-sort@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/alphanum-sort/-/alphanum-sort-1.0.2.tgz#97a1119649b211ad33691d9f9f486a8ec9fbe0a3"
Expand Down