[savedObjects/mappings] limit valid type names (#17511)
This commit is contained in:
parent
5fc4ce170d
commit
92a742361a
|
@ -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 (_)"`;
|
|
@ -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
|
||||
|
|
|
@ -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()', () => {
|
||||
|
|
Loading…
Reference in a new issue