Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[content management / maps] Content management / Saved object schema abstraction #155342

Merged
merged 37 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d601dbd
abstract types
mattkime Apr 15, 2023
9e06b50
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 15, 2023
83b1ff5
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Apr 15, 2023
86e3a85
fix types
mattkime Apr 15, 2023
8daec60
Merge branch 'content_management_api_so_type_abstraction' of github.c…
mattkime Apr 15, 2023
aea2173
remove comment
mattkime Apr 15, 2023
28548f7
fix partial types
mattkime Apr 15, 2023
44ca8ee
add readme content
mattkime Apr 15, 2023
5b8eb84
Merge branch 'main' into content_management_api_so_type_abstraction
mattkime Apr 15, 2023
3ab8198
simplify type export
mattkime Apr 17, 2023
e10b297
simplify type export
mattkime Apr 17, 2023
055679a
simplify type export
mattkime Apr 17, 2023
ac841b5
Merge branch 'main' into content_management_api_so_type_abstraction
mattkime Apr 17, 2023
df3fd85
Merge branch 'main' into content_management_api_so_type_abstraction
mattkime Apr 18, 2023
eee1744
move Option types external to main interface
mattkime Apr 19, 2023
530c23d
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Apr 19, 2023
8f9c939
schema abstraction
mattkime Apr 20, 2023
5303a6b
schema abstraction
mattkime Apr 20, 2023
10048c5
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 20, 2023
c473549
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Apr 20, 2023
b3da60a
Update CODEOWNERS
mattkime Apr 20, 2023
f32ecb4
Update kibana.jsonc
mattkime Apr 20, 2023
64c4733
more complete types for SO Options
mattkime Apr 20, 2023
907ce41
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 20, 2023
c3d7211
fix SO types for config
mattkime Apr 20, 2023
48756d5
Merge branch 'content_management_api_so_type_abstraction' of github.c…
mattkime Apr 21, 2023
dd9d980
Merge branch 'main' into content_management_api_so_type_abstraction
mattkime Apr 24, 2023
a357f39
Merge branch 'content_management_api_so_type_abstraction' into conten…
mattkime Apr 24, 2023
ef07c0a
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Apr 24, 2023
45642ea
cleanup
mattkime Apr 24, 2023
bd1ca70
Merge branch 'content_management_code_abstraction' of github.com:matt…
mattkime Apr 24, 2023
744c083
more complete schema
mattkime Apr 24, 2023
63ffe47
Merge branch 'main' into content_management_code_abstraction
mattkime Apr 25, 2023
2a85688
add notes
mattkime Apr 25, 2023
0d9d35d
remove mistaken duplicate types
mattkime Apr 25, 2023
dda5f23
better importing of partial schema
mattkime Apr 27, 2023
f96cf21
Merge branch 'main' into content_management_code_abstraction
mattkime Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/kbn-content-management-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* Side Public License, v 1.
*/

export * from './types';
export * from './src/types';
export * from './src/schema';
123 changes: 123 additions & 0 deletions packages/kbn-content-management-utils/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { schema, ObjectType } from '@kbn/config-schema';

export const apiError = schema.object({
error: schema.string(),
message: schema.string(),
statusCode: schema.number(),
metadata: schema.object({}, { unknowns: 'allow' }),
});

export const referenceSchema = schema.object(
{
name: schema.maybe(schema.string()),
type: schema.string(),
id: schema.string(),
},
{ unknowns: 'forbid' }
);

export const referencesSchema = schema.arrayOf(referenceSchema);

export const savedObjectSchema = (attributesSchema: ObjectType<any>) =>
schema.object(
{
id: schema.string(),
type: schema.string(),
version: schema.maybe(schema.string()),
createdAt: schema.maybe(schema.string()),
updatedAt: schema.maybe(schema.string()),
error: schema.maybe(apiError),
attributes: attributesSchema,
references: referencesSchema,
namespaces: schema.maybe(schema.arrayOf(schema.string())),
originId: schema.maybe(schema.string()),
},
{ unknowns: 'allow' }
);

export const objectTypeToGetResultSchema = (soSchema: ObjectType<any>) =>
schema.object(
{
item: soSchema,
meta: schema.object(
{
outcome: schema.oneOf([
schema.literal('exactMatch'),
schema.literal('aliasMatch'),
schema.literal('conflict'),
]),
aliasTargetId: schema.maybe(schema.string()),
aliasPurpose: schema.maybe(
schema.oneOf([
schema.literal('savedObjectConversion'),
schema.literal('savedObjectImport'),
])
),
},
{ unknowns: 'forbid' }
),
},
{ unknowns: 'forbid' }
);

// its recommended to create a subset of this schema for stricter validation
export const createOptionsSchema = schema.object({
id: schema.maybe(schema.string()),
references: schema.maybe(referencesSchema),
overwrite: schema.maybe(schema.boolean()),
version: schema.maybe(schema.string()),
refresh: schema.maybe(schema.boolean()),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string())),
});

export const schemaAndOr = schema.oneOf([schema.literal('AND'), schema.literal('OR')]);

// its recommended to create a subset of this schema for stricter validation
export const searchOptionsSchema = schema.object({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better then to expose the properties separately for composing the schema then?

export const searchOptionsSchemaProperties = {
  page: schema.maybe(schema.number()),
  perPage: schema.maybe(schema.number()),
  sortField: schema.maybe(schema.string()),
  ...
}

// consumer side
const { sortField } = searchOptionsSchemaProperties;

const mySearchOptionsSchema = schema.object({
  sortField
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good idea, will do.

page: schema.maybe(schema.number()),
perPage: schema.maybe(schema.number()),
sortField: schema.maybe(schema.string()),
sortOrder: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
fields: schema.maybe(schema.arrayOf(schema.string())),
search: schema.maybe(schema.string()),
searchFields: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
rootSearchFields: schema.maybe(schema.arrayOf(schema.string())),

hasReference: schema.maybe(schema.oneOf([referenceSchema, schema.arrayOf(referenceSchema)])),
hasReferenceOperator: schema.maybe(schemaAndOr),
hasNoReference: schema.maybe(schema.oneOf([referenceSchema, schema.arrayOf(referenceSchema)])),
hasNoReferenceOperator: schema.maybe(schemaAndOr),
defaultSearchOperator: schema.maybe(schemaAndOr),
namespaces: schema.maybe(schema.arrayOf(schema.string())),
type: schema.maybe(schema.string()),

filter: schema.maybe(schema.string()),
pit: schema.maybe(
schema.object({ id: schema.string(), keepAlive: schema.maybe(schema.string()) })
),
});

// its recommended to create a subset of this schema for stricter validation
export const updateOptionsSchema = (attributesSchema: ObjectType<any>) =>
schema.object({
references: schema.maybe(referencesSchema),
version: schema.maybe(schema.string()),
refresh: schema.maybe(schema.oneOf([schema.boolean(), schema.literal('wait_for')])),
upsert: schema.maybe(savedObjectSchema(attributesSchema)),
retryOnConflict: schema.maybe(schema.number()),
});

export const createResultSchema = (soSchema: ObjectType<any>) =>
schema.object(
{
item: soSchema,
},
{ unknowns: 'forbid' }
);
1 change: 1 addition & 0 deletions packages/kbn-content-management-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"kbn_references": [
"@kbn/content-management-plugin",
"@kbn/config-schema",
"@kbn/core-saved-objects-api-server",
]
}
91 changes: 17 additions & 74 deletions x-pack/plugins/maps/common/content_management/v1/cm_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@
*/
import { schema } from '@kbn/config-schema';
import type { ContentManagementServicesDefinition as ServicesDefinition } from '@kbn/object-versioning';

const apiError = schema.object({
error: schema.string(),
message: schema.string(),
statusCode: schema.number(),
metadata: schema.object({}, { unknowns: 'allow' }),
});

const referenceSchema = schema.object(
{
name: schema.maybe(schema.string()),
type: schema.string(),
id: schema.string(),
},
{ unknowns: 'forbid' }
);

const referencesSchema = schema.arrayOf(referenceSchema);
import {
savedObjectSchema,
objectTypeToGetResultSchema,
createOptionsSchema,
createResultSchema,
} from '@kbn/content-management-utils';

const mapAttributesSchema = schema.object(
{
Expand All @@ -36,57 +24,24 @@ const mapAttributesSchema = schema.object(
{ unknowns: 'forbid' }
);

const mapSavedObjectSchema = schema.object(
{
id: schema.string(),
type: schema.string(),
version: schema.maybe(schema.string()),
createdAt: schema.maybe(schema.string()),
updatedAt: schema.maybe(schema.string()),
error: schema.maybe(apiError),
attributes: mapAttributesSchema,
references: referencesSchema,
namespaces: schema.maybe(schema.arrayOf(schema.string())),
originId: schema.maybe(schema.string()),
},
{ unknowns: 'allow' }
);
const mapSavedObjectSchema = savedObjectSchema(mapAttributesSchema);

const getResultSchema = schema.object(
{
item: mapSavedObjectSchema,
meta: schema.object(
{
outcome: schema.oneOf([
schema.literal('exactMatch'),
schema.literal('aliasMatch'),
schema.literal('conflict'),
]),
aliasTargetId: schema.maybe(schema.string()),
aliasPurpose: schema.maybe(
schema.oneOf([
schema.literal('savedObjectConversion'),
schema.literal('savedObjectImport'),
])
),
},
{ unknowns: 'forbid' }
),
},
{ unknowns: 'forbid' }
const searchOptionsSchema = schema.maybe(
schema.object(
{
onlyTitle: schema.maybe(schema.boolean()),
},
{ unknowns: 'forbid' }
)
);

const createOptionsSchema = schema.object({
references: schema.maybe(referencesSchema),
});

// Content management service definition.
// We need it for BWC support between different versions of the content
export const serviceDefinition: ServicesDefinition = {
get: {
out: {
result: {
schema: getResultSchema,
schema: objectTypeToGetResultSchema(mapSavedObjectSchema),
},
},
},
Expand All @@ -101,12 +56,7 @@ export const serviceDefinition: ServicesDefinition = {
},
out: {
result: {
schema: schema.object(
{
item: mapSavedObjectSchema,
},
{ unknowns: 'forbid' }
),
schema: createResultSchema(mapSavedObjectSchema),
},
},
},
Expand All @@ -123,14 +73,7 @@ export const serviceDefinition: ServicesDefinition = {
search: {
in: {
options: {
schema: schema.maybe(
schema.object(
{
onlyTitle: schema.maybe(schema.boolean()),
},
{ unknowns: 'forbid' }
)
),
schema: searchOptionsSchema,
},
},
},
Expand Down