Skip to content

Commit 4efda6a

Browse files
feat: support configuration via title
1 parent 3a4f74a commit 4efda6a

File tree

5 files changed

+143
-2
lines changed

5 files changed

+143
-2
lines changed

src/ValidationError.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,30 @@ class ValidationError extends Error {
408408
this.errors = errors;
409409
/** @type {Schema} */
410410
this.schema = schema;
411+
412+
let headerNameFromSchema;
413+
let baseDataPathFromSchema;
414+
415+
if (schema.title && (!configuration.name || !configuration.baseDataPath)) {
416+
const splittedTitleFromSchema = schema.title.match(/^(.+) (.+)$/);
417+
418+
if (splittedTitleFromSchema) {
419+
if (!configuration.name) {
420+
[, headerNameFromSchema] = splittedTitleFromSchema;
421+
}
422+
423+
if (!configuration.name) {
424+
[, , baseDataPathFromSchema] = splittedTitleFromSchema;
425+
}
426+
}
427+
}
428+
411429
/** @type {string} */
412-
this.headerName = configuration.name || 'Object';
430+
this.headerName = configuration.name || headerNameFromSchema || 'Object';
413431
/** @type {string} */
414-
this.baseDataPath = configuration.baseDataPath || 'configuration';
432+
this.baseDataPath =
433+
configuration.baseDataPath || baseDataPathFromSchema || 'configuration';
434+
415435
/** @type {PostFormatter | null} */
416436
this.postFormatter = configuration.postFormatter || null;
417437

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`api should get configuration from schema 1`] = `
4+
"Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
5+
- options has an unknown property 'foo'. These properties are valid:
6+
object { name? }"
7+
`;
8+
9+
exports[`api should prefer configuration over "title" #1 1`] = `
10+
"Invalid configuration object. NAME has been initialised using a configuration object that does not match the API schema.
11+
- configuration has an unknown property 'foo'. These properties are valid:
12+
object { name? }"
13+
`;
14+
15+
exports[`api should prefer configuration over "title" #2 1`] = `
16+
"Invalid BaseDataPath object. CSS Loader has been initialised using a BaseDataPath object that does not match the API schema.
17+
- BaseDataPath has an unknown property 'foo'. These properties are valid:
18+
object { name? }"
19+
`;
20+
21+
exports[`api should prefer configuration over "title" 1`] = `
22+
"Invalid BaseDataPath object. NAME has been initialised using a BaseDataPath object that does not match the API schema.
23+
- BaseDataPath has an unknown property 'foo'. These properties are valid:
24+
object { name? }"
25+
`;
26+
27+
exports[`api should use default values when "title" is broken 1`] = `
28+
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
29+
- configuration has an unknown property 'foo'. These properties are valid:
30+
object { name? }"
31+
`;

test/api.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import schemaUtils from '../src/index';
22

33
import schema from './fixtures/schema.json';
4+
import schemaTitle from './fixtures/schema-title.json';
5+
import schemaTitleBrone from './fixtures/schema-title-broken.json';
46

57
describe('api', () => {
68
it('should export validate and ValidateError', () => {
@@ -32,4 +34,72 @@ describe('api', () => {
3234

3335
schemaUtils(schema, options);
3436
});
37+
38+
it('should get configuration from schema', () => {
39+
try {
40+
schemaUtils(schemaTitle, { foo: 'bar' });
41+
} catch (error) {
42+
if (error.name !== 'ValidationError') {
43+
throw error;
44+
}
45+
46+
expect(error.message).toMatchSnapshot();
47+
}
48+
});
49+
50+
it('should prefer configuration over "title"', () => {
51+
try {
52+
schemaUtils(
53+
schemaTitle,
54+
{ foo: 'bar' },
55+
{ name: 'NAME', baseDataPath: 'BaseDataPath' }
56+
);
57+
} catch (error) {
58+
if (error.name !== 'ValidationError') {
59+
throw error;
60+
}
61+
62+
expect(error.message).toMatchSnapshot();
63+
}
64+
});
65+
66+
it('should prefer configuration over "title" #1', () => {
67+
try {
68+
schemaUtils(schemaTitle, { foo: 'bar' }, { name: 'NAME' });
69+
} catch (error) {
70+
if (error.name !== 'ValidationError') {
71+
throw error;
72+
}
73+
74+
expect(error.message).toMatchSnapshot();
75+
}
76+
});
77+
78+
it('should prefer configuration over "title" #2', () => {
79+
try {
80+
schemaUtils(
81+
schemaTitle,
82+
{ foo: 'bar' },
83+
{ baseDataPath: 'BaseDataPath' }
84+
);
85+
} catch (error) {
86+
if (error.name !== 'ValidationError') {
87+
throw error;
88+
}
89+
90+
expect(error.message).toMatchSnapshot();
91+
}
92+
});
93+
94+
it('should use default values when "title" is broken', () => {
95+
try {
96+
schemaUtils(schemaTitleBrone, { foo: 'bar' });
97+
} catch (error) {
98+
if (error.name !== 'ValidationError') {
99+
throw error;
100+
}
101+
102+
expect(error.message).toMatchSnapshot();
103+
}
104+
});
35105
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"title": "CSSLoaderoptions",
3+
"additionalProperties": false,
4+
"properties": {
5+
"name": {
6+
"type": "boolean"
7+
}
8+
},
9+
"type": "object"
10+
}

test/fixtures/schema-title.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"title": "CSS Loader options",
3+
"additionalProperties": false,
4+
"properties": {
5+
"name": {
6+
"type": "boolean"
7+
}
8+
},
9+
"type": "object"
10+
}

0 commit comments

Comments
 (0)