Skip to content

Commit

Permalink
feat: support configuration via title (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Nov 27, 2019
1 parent 3a4f74a commit afddc10
Show file tree
Hide file tree
Showing 8 changed files with 852 additions and 550 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,26 @@ validate(

Allow to configure validator.

There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
For example:

```json
{
"title": "My Loader options",
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
```

The last word used for the `baseDataPath` option, other words used for the `name` option.
Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.

#### `name`

Type: `Object`
Expand Down
1,219 changes: 680 additions & 539 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -43,29 +43,29 @@
"ajv-keywords": "^3.4.1"
},
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/json-schema": "^7.0.3",
"@webpack-contrib/defaults": "^5.1.1",
"@webpack-contrib/defaults": "^6.2.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^24.9.0",
"commitlint-azure-pipelines-cli": "^1.0.2",
"cross-env": "^6.0.3",
"del": "^5.1.0",
"del-cli": "^3.0.0",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint": "^6.7.1",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.18.2",
"husky": "^3.0.9",
"husky": "^3.1.0",
"jest": "^24.9.0",
"jest-junit": "^9.0.0",
"lint-staged": "^9.4.2",
"lint-staged": "^9.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"standard-version": "^7.0.0",
"standard-version": "^7.0.1",
"typescript": "^3.7.2"
},
"keywords": [
Expand Down
24 changes: 22 additions & 2 deletions src/ValidationError.js
Expand Up @@ -408,10 +408,30 @@ class ValidationError extends Error {
this.errors = errors;
/** @type {Schema} */
this.schema = schema;

let headerNameFromSchema;
let baseDataPathFromSchema;

if (schema.title && (!configuration.name || !configuration.baseDataPath)) {
const splittedTitleFromSchema = schema.title.match(/^(.+) (.+)$/);

if (splittedTitleFromSchema) {
if (!configuration.name) {
[, headerNameFromSchema] = splittedTitleFromSchema;
}

if (!configuration.name) {
[, , baseDataPathFromSchema] = splittedTitleFromSchema;
}
}
}

/** @type {string} */
this.headerName = configuration.name || 'Object';
this.headerName = configuration.name || headerNameFromSchema || 'Object';
/** @type {string} */
this.baseDataPath = configuration.baseDataPath || 'configuration';
this.baseDataPath =
configuration.baseDataPath || baseDataPathFromSchema || 'configuration';

/** @type {PostFormatter | null} */
this.postFormatter = configuration.postFormatter || null;

Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/api.test.js.snap
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`api should get configuration from schema 1`] = `
"Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
- options has an unknown property 'foo'. These properties are valid:
object { name? }"
`;
exports[`api should prefer configuration over "title" #1 1`] = `
"Invalid configuration object. NAME has been initialised using a configuration object that does not match the API schema.
- configuration has an unknown property 'foo'. These properties are valid:
object { name? }"
`;
exports[`api should prefer configuration over "title" #2 1`] = `
"Invalid BaseDataPath object. CSS Loader has been initialised using a BaseDataPath object that does not match the API schema.
- BaseDataPath has an unknown property 'foo'. These properties are valid:
object { name? }"
`;
exports[`api should prefer configuration over "title" 1`] = `
"Invalid BaseDataPath object. NAME has been initialised using a BaseDataPath object that does not match the API schema.
- BaseDataPath has an unknown property 'foo'. These properties are valid:
object { name? }"
`;
exports[`api should use default values when "title" is broken 1`] = `
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
- configuration has an unknown property 'foo'. These properties are valid:
object { name? }"
`;
70 changes: 70 additions & 0 deletions test/api.test.js
@@ -1,6 +1,8 @@
import schemaUtils from '../src/index';

import schema from './fixtures/schema.json';
import schemaTitle from './fixtures/schema-title.json';
import schemaTitleBrone from './fixtures/schema-title-broken.json';

describe('api', () => {
it('should export validate and ValidateError', () => {
Expand Down Expand Up @@ -32,4 +34,72 @@ describe('api', () => {

schemaUtils(schema, options);
});

it('should get configuration from schema', () => {
try {
schemaUtils(schemaTitle, { foo: 'bar' });
} catch (error) {
if (error.name !== 'ValidationError') {
throw error;
}

expect(error.message).toMatchSnapshot();
}
});

it('should prefer configuration over "title"', () => {
try {
schemaUtils(
schemaTitle,
{ foo: 'bar' },
{ name: 'NAME', baseDataPath: 'BaseDataPath' }
);
} catch (error) {
if (error.name !== 'ValidationError') {
throw error;
}

expect(error.message).toMatchSnapshot();
}
});

it('should prefer configuration over "title" #1', () => {
try {
schemaUtils(schemaTitle, { foo: 'bar' }, { name: 'NAME' });
} catch (error) {
if (error.name !== 'ValidationError') {
throw error;
}

expect(error.message).toMatchSnapshot();
}
});

it('should prefer configuration over "title" #2', () => {
try {
schemaUtils(
schemaTitle,
{ foo: 'bar' },
{ baseDataPath: 'BaseDataPath' }
);
} catch (error) {
if (error.name !== 'ValidationError') {
throw error;
}

expect(error.message).toMatchSnapshot();
}
});

it('should use default values when "title" is broken', () => {
try {
schemaUtils(schemaTitleBrone, { foo: 'bar' });
} catch (error) {
if (error.name !== 'ValidationError') {
throw error;
}

expect(error.message).toMatchSnapshot();
}
});
});
10 changes: 10 additions & 0 deletions test/fixtures/schema-title-broken.json
@@ -0,0 +1,10 @@
{
"title": "CSSLoaderoptions",
"additionalProperties": false,
"properties": {
"name": {
"type": "boolean"
}
},
"type": "object"
}
10 changes: 10 additions & 0 deletions test/fixtures/schema-title.json
@@ -0,0 +1,10 @@
{
"title": "CSS Loader options",
"additionalProperties": false,
"properties": {
"name": {
"type": "boolean"
}
},
"type": "object"
}

0 comments on commit afddc10

Please sign in to comment.