Skip to content

Commit

Permalink
[savedObjects/mappings] limit valid type names (#17511)
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Apr 9, 2018
1 parent 5fc4ce1 commit 92a7423
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/server/mappings/__snapshots__/index_mappings.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`server/mapping/index_mapping constructor includes the pluginId from the extension in the _ error message if defined 1`] = `"Property names _foo registered by plugin abc123 are not allowed to start with an underscore (_)"`;

exports[`server/mapping/index_mapping constructor throws if any of the new properties start with _ 1`] = `"Property names _foo are not allowed to start with an underscore (_)"`;
12 changes: 11 additions & 1 deletion src/server/mappings/index_mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export class IndexMappings {
mappingExtensions.forEach(({ properties, pluginId }) => {
const rootProperties = getRootProperties(this._dsl);


const conflicts = Object.keys(properties)
.filter(key => rootProperties.hasOwnProperty(key));

const illegal = Object.keys(properties)
.filter(key => key.startsWith('_'));

if (conflicts.length) {
const props = formatListAsProse(conflicts);
const owner = pluginId ? `registered by plugin ${pluginId} ` : '';
Expand All @@ -37,6 +39,14 @@ export class IndexMappings {
);
}

if (illegal.length) {
const props = formatListAsProse(illegal);
const owner = pluginId ? `registered by plugin ${pluginId} ` : '';
throw new Error(
`Property name${props.length > 1 ? 's' : ''} ${props} ${owner}are not allowed to start with an underscore (_)`
);
}

this._setProperties({
...rootProperties,
...properties
Expand Down
35 changes: 35 additions & 0 deletions src/server/mappings/index_mappings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ describe('server/mapping/index_mapping', function () {
new IndexMappings(initialMapping, extensions);
}).toThrowError(/plugin abc123/);
});

it('throws if any of the new properties start with _', () => {
const initialMapping = {
root: { properties: { foo: 'bar' } }
};
const extensions = [
{
properties: {
_foo: 'bar'
}
}
];

expect(() => {
new IndexMappings(initialMapping, extensions);
}).toThrowErrorMatchingSnapshot();
});

it('includes the pluginId from the extension in the _ error message if defined', () => {
const initialMapping = {
root: { properties: { foo: 'bar' } }
};
const extensions = [
{
pluginId: 'abc123',
properties: {
_foo: 'bar'
}
}
];

expect(() => {
new IndexMappings(initialMapping, extensions);
}).toThrowErrorMatchingSnapshot();
});
});

describe('#getDsl()', () => {
Expand Down

0 comments on commit 92a7423

Please sign in to comment.