Skip to content

Commit

Permalink
Address feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Jan 5, 2022
1 parent d737fb8 commit 5ed7501
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsImportHook](./kibana-plugin-core-server.savedobjectsimporthook.md) | A hook associated with a specific saved object type, that will be invoked during the import process. The hook will have access to the objects of the registered type.<!-- -->Currently, the only supported feature for import hooks is to return warnings to be displayed in the UI when the import succeeds. The only interactions the hook can have with the import process is via the hook's response. Mutating the objects inside the hook's code will have no effect. |
| [SavedObjectsImportWarning](./kibana-plugin-core-server.savedobjectsimportwarning.md) | Composite type of all the possible types of import warnings.<!-- -->See [SavedObjectsImportSimpleWarning](./kibana-plugin-core-server.savedobjectsimportsimplewarning.md) and [SavedObjectsImportActionRequiredWarning](./kibana-plugin-core-server.savedobjectsimportactionrequiredwarning.md) for more details. |
| [SavedObjectsNamespaceType](./kibana-plugin-core-server.savedobjectsnamespacetype.md) | The namespace type dictates how a saved object can be interacted in relation to namespaces. Each type is mutually exclusive: \* single (default): This type of saved object is namespace-isolated, e.g., it exists in only one namespace. \* multiple: This type of saved object is shareable, e.g., it can exist in one or more namespaces. \* multiple-isolated: This type of saved object is namespace-isolated, e.g., it exists in only one namespace, but object IDs must be unique across all namespaces. This is intended to be an intermediate step when objects with a "single" namespace type are being converted to a "multiple" namespace type. In other words, objects with a "multiple-isolated" namespace type will be \*share-capable\*, but will not actually be shareable until the namespace type is changed to "multiple". \* agnostic: This type of saved object is global. |
| [SavedObjectsValidationSpec](./kibana-plugin-core-server.savedobjectsvalidationspec.md) | Allowed property validation options: either @<!-- -->kbn/config-schema validations or custom validation functions.<!-- -->See for custom validation. |
| [SavedObjectsValidationSpec](./kibana-plugin-core-server.savedobjectsvalidationspec.md) | Allows for validating properties using @<!-- -->kbn/config-schema validations. |
| [SavedObjectTypeExcludeFromUpgradeFilterHook](./kibana-plugin-core-server.savedobjecttypeexcludefromupgradefilterhook.md) | If defined, allows a type to run a search query and return a query filter that may match any documents which may be excluded from the next migration upgrade process. Useful for cleaning up large numbers of old documents which are no longer needed and may slow the migration process.<!-- -->If this hook fails, the migration will proceed without these documents having been filtered out, so this should not be used as a guarantee that these documents have been deleted.<!-- -->Experimental and subject to change |
| [SavedObjectUnsanitizedDoc](./kibana-plugin-core-server.savedobjectunsanitizeddoc.md) | Describes Saved Object documents from Kibana &lt; 7.0.0 which don't have a <code>references</code> root property defined. This type should only be used in migrations. |
| [ScopeableRequest](./kibana-plugin-core-server.scopeablerequest.md) | A user credentials container. It accommodates the necessary auth credentials to impersonate the current user.<!-- -->See [KibanaRequest](./kibana-plugin-core-server.kibanarequest.md)<!-- -->. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface SavedObjectsValidationMap


```typescript
const validationMap: SavedObjectValidationMap = {
const validationMap: SavedObjectsValidationMap = {
'1.0.0': schema.object({
foo: schema.string(),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

## SavedObjectsValidationSpec type

Allowed property validation options: either @<!-- -->kbn/config-schema validations or custom validation functions.

See for custom validation.
Allows for validating properties using @<!-- -->kbn/config-schema validations.

<b>Signature:</b>

Expand Down
22 changes: 10 additions & 12 deletions src/core/server/saved_objects/service/lib/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,18 +994,16 @@ describe('SavedObjectsRepository', () => {
{ ...obj3, id: 'three-again', attributes: { title: 123 } },
]);
expect(client.bulk).toHaveBeenCalledTimes(1); // only called once for the valid object
expect(result.saved_objects).toEqual(
expect.arrayContaining([
expect.objectContaining(obj3),
expect.objectContaining({
error: new Error(
'[attributes.title]: expected value of type [string] but got [number]: Bad Request'
),
id: 'three-again',
type: 'dashboard',
}),
])
);
expect(result.saved_objects).toEqual([
expect.objectContaining(obj3),
expect.objectContaining({
error: new Error(
'[attributes.title]: expected value of type [string] but got [number]: Bad Request'
),
id: 'three-again',
type: 'dashboard',
}),
]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,16 @@ describe('validates saved object types when a schema is provided', () => {
// @ts-expect-error - The invalidObj is intentionally malformed for testing
const results = await savedObjectsClient.bulkCreate([validObj, invalidObj]);

expect(results.saved_objects).toEqual(
expect.arrayContaining([
expect.objectContaining(validObj),
expect.objectContaining({
error: new Error(
'[attributes.a]: expected value of type [number] but got [string]: Bad Request'
),
id: 'bulk-invalid',
type: 'schema-using-kbn-config',
}),
])
);
expect(results.saved_objects).toEqual([
expect.objectContaining(validObj),
expect.objectContaining({
error: new Error(
'[attributes.a]: expected value of type [number] but got [string]: Bad Request'
),
id: 'bulk-invalid',
type: 'schema-using-kbn-config',
}),
]);
});

describe('when validating with a config schema', () => {
Expand Down
29 changes: 29 additions & 0 deletions src/core/server/saved_objects/validation/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ describe('Saved Objects type validation schema', () => {
);
});

it('should validate top-level properties', () => {
const objectSchema = createSavedObjectSanitizedDocSchema(validationMap['1.0.0']);
const data = createMockObject({ foo: 'heya' });

expect(() =>
objectSchema.validate({
...data,
id: 'abc-123',
type: 'dashboard',
references: [
{
name: 'ref_0',
type: 'visualization',
id: '123',
},
],
namespace: 'a',
namespaces: ['a', 'b'],
migrationVersion: {
dashboard: '1.0.0',
},
coreMigrationVersion: '1.0.0',
updated_at: '2022-01-05T03:17:07.183Z',
version: '2',
originId: 'def-456',
})
).not.toThrowError();
});

it('should fail if top-level properties are invalid', () => {
const objectSchema = createSavedObjectSanitizedDocSchema(validationMap['1.0.0']);
const data = createMockObject({ foo: 'heya' });
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type SavedObjectSanitizedDocSchema = {
*
* @internal
*/
export const createSavedObjectSanitizedDocSchema = (rule: SavedObjectsValidationSpec) =>
export const createSavedObjectSanitizedDocSchema = (attributesSchema: SavedObjectsValidationSpec) =>
schema.object<SavedObjectSanitizedDocSchema>({
attributes: rule,
attributes: attributesSchema,
id: schema.string(),
type: schema.string(),
references: schema.arrayOf(
Expand All @@ -38,7 +38,7 @@ export const createSavedObjectSanitizedDocSchema = (rule: SavedObjectsValidation
),
namespace: schema.maybe(schema.string()),
namespaces: schema.maybe(schema.arrayOf(schema.string())),
migrationVersion: schema.maybe(schema.object({}, { unknowns: 'allow' })),
migrationVersion: schema.maybe(schema.recordOf(schema.string(), schema.string())),
coreMigrationVersion: schema.maybe(schema.string()),
updated_at: schema.maybe(schema.string()),
version: schema.maybe(schema.string()),
Expand Down
6 changes: 2 additions & 4 deletions src/core/server/saved_objects/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import { ObjectType } from '@kbn/config-schema';

/**
* Allowed property validation options: either @kbn/config-schema validations or custom validation functions.
*
* See {@link SavedObjectsValidationFunction} for custom validation.
* Allows for validating properties using @kbn/config-schema validations.
*
* @public
*/
Expand All @@ -26,7 +24,7 @@ export type SavedObjectsValidationSpec = ObjectType;
*
* @example
* ```typescript
* const validationMap: SavedObjectValidationMap = {
* const validationMap: SavedObjectsValidationMap = {
* '1.0.0': schema.object({
* foo: schema.string(),
* }),
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2873,8 +2873,6 @@ export interface SavedObjectsValidationMap {
[version: string]: SavedObjectsValidationSpec;
}

// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "SavedObjectsValidationFunction"
//
// @public
export type SavedObjectsValidationSpec = ObjectType;

Expand Down

0 comments on commit 5ed7501

Please sign in to comment.