[savedObjects/mappings] limit valid type names (#17511)

This commit is contained in:
Spencer 2018-04-09 16:11:12 -07:00 committed by GitHub
parent 5fc4ce170d
commit 92a742361a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View file

@ -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 (_)"`;

View file

@ -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} ` : '';
@ -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

View file

@ -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()', () => {