log an invalid type for SO (#115175)

This commit is contained in:
Mikhail Shustov 2021-10-15 23:56:17 +03:00 committed by GitHub
parent e18afaa45d
commit 852c5dd205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View file

@ -491,6 +491,52 @@ describe('#rawToSavedObject', () => {
expect(actual).toHaveProperty('namespaces', ['baz']);
});
});
describe('throws if provided invalid type', () => {
expect(() =>
singleNamespaceSerializer.rawToSavedObject({
_id: 'foo:bar',
_source: {
// @ts-expect-error expects a string
// eslint-disable-next-line
type: new String('foo'),
},
})
).toThrowErrorMatchingInlineSnapshot(
`"Expected saved object type to be a string but given [String] with [foo] value."`
);
expect(() =>
singleNamespaceSerializer.rawToSavedObject({
_id: 'foo:bar',
_source: {
// @ts-expect-error expects astring
type: {
toString() {
return 'foo';
},
},
},
})
).toThrowErrorMatchingInlineSnapshot(
`"Expected saved object type to be a string but given [Object] with [foo] value."`
);
});
describe('throws if provided invalid id', () => {
expect(() =>
singleNamespaceSerializer.rawToSavedObject({
// @ts-expect-error expects a string
// eslint-disable-next-line
_id: new String('foo:bar'),
_source: {
type: 'foo',
},
})
).toThrowErrorMatchingInlineSnapshot(
`"Expected document id to be a string but given [String] with [foo:bar] value."`
);
});
});
describe('#savedObjectToRaw', () => {

View file

@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import typeDetect from 'type-detect';
import { LEGACY_URL_ALIAS_TYPE } from '../object_types';
import { decodeVersion, encodeVersion } from '../version';
import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry';
@ -236,6 +236,8 @@ function checkIdMatchesPrefix(id: string, prefix: string) {
function assertNonEmptyString(value: string, name: string) {
if (!value || typeof value !== 'string') {
throw new TypeError(`Expected "${value}" to be a ${name}`);
throw new TypeError(
`Expected ${name} to be a string but given [${typeDetect(value)}] with [${value}] value.`
);
}
}