Skip to content

Commit

Permalink
log an invalid type for SO (#115175)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov authored Oct 15, 2021
1 parent e18afaa commit 852c5dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/core/server/saved_objects/serialization/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/core/server/saved_objects/serialization/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.`
);
}
}

0 comments on commit 852c5dd

Please sign in to comment.