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

[Canvas] Improve workpad schema validation #97838

Merged
merged 4 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
110 changes: 110 additions & 0 deletions x-pack/plugins/canvas/server/routes/workpad/workpad_schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { WorkpadSchema } from './workpad_schema';

const pageOneId = 'page-1';
const pageTwoId = 'page-2';
const elementOneId = 'element-1';
const elementTwoId = 'element-2';
const elementThreeId = 'element-3';

const position = {
angle: 0,
height: 0,
left: 0,
parent: null,
top: 0,
width: 0,
};
const baseWorkpad = {
colors: [],
css: '',
variables: [],
height: 0,
id: 'workpad-id',
name: 'workpad',
page: 1,
pages: [
{
elements: [
{
expression: 'expression',
id: elementOneId,
position,
},
{
expression: 'expression',
id: elementTwoId,
position,
},
],
id: pageOneId,
style: {},
},
{
elements: [
{
expression: 'expression',
id: elementThreeId,
position,
},
],
id: pageTwoId,
style: {},
},
],
width: 0,
};

it('validates there are no duplicate page ids', () => {
const dupePage = {
...baseWorkpad,
pages: [{ ...baseWorkpad.pages[0] }, { ...baseWorkpad.pages[1], id: pageOneId }],
};

expect(() => WorkpadSchema.validate(dupePage)).toThrowError('Page Ids are not unique');
});

it('validates there are no duplicate element ids on the same page', () => {
const dupeElement = {
...baseWorkpad,
pages: [
{
...baseWorkpad.pages[0],
elements: [
{ ...baseWorkpad.pages[0].elements[0] },
{ ...baseWorkpad.pages[0].elements[1], id: elementOneId },
],
},
{ ...baseWorkpad.pages[1] },
],
};

expect(() => WorkpadSchema.validate(dupeElement)).toThrowError('Element Ids are not unique');
});

it('validates there are no duplicate element ids in the workpad', () => {
const dupeElement = {
...baseWorkpad,
pages: [
{
...baseWorkpad.pages[0],
elements: [
{ ...baseWorkpad.pages[0].elements[0] },
{ ...baseWorkpad.pages[0].elements[1], id: elementOneId },
],
},
{
...baseWorkpad.pages[1],
elements: [{ ...baseWorkpad.pages[1].elements[0], id: elementOneId }],
},
],
};

expect(() => WorkpadSchema.validate(dupeElement)).toThrowError('Element Ids are not unique');
});
53 changes: 38 additions & 15 deletions x-pack/plugins/canvas/server/routes/workpad/workpad_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,41 @@ export const WorkpadVariable = schema.object({
type: schema.string(),
});

export const WorkpadSchema = schema.object({
'@created': schema.maybe(schema.string()),
'@timestamp': schema.maybe(schema.string()),
assets: schema.maybe(schema.recordOf(schema.string(), WorkpadAssetSchema)),
colors: schema.arrayOf(schema.string()),
css: schema.string(),
variables: schema.arrayOf(WorkpadVariable),
height: schema.number(),
id: schema.string(),
isWriteable: schema.maybe(schema.boolean()),
name: schema.string(),
page: schema.number(),
pages: schema.arrayOf(WorkpadPageSchema),
width: schema.number(),
});
export const WorkpadSchema = schema.object(
{
'@created': schema.maybe(schema.string()),
'@timestamp': schema.maybe(schema.string()),
assets: schema.maybe(schema.recordOf(schema.string(), WorkpadAssetSchema)),
colors: schema.arrayOf(schema.string()),
css: schema.string(),
variables: schema.arrayOf(WorkpadVariable),
height: schema.number(),
id: schema.string(),
isWriteable: schema.maybe(schema.boolean()),
name: schema.string(),
page: schema.number(),
pages: schema.arrayOf(WorkpadPageSchema),
width: schema.number(),
},
{
validate: (workpad) => {
// Validate unique page ids
const pageIdsArray = workpad.pages.map((page) => page.id);
const pageIdsSet = new Set(pageIdsArray);

if (pageIdsArray.length !== pageIdsSet.size) {
return 'Page Ids are not unique';
}

// Validate unique element ids
const elementIdsArray = workpad.pages
.map((page) => page.elements.map((element) => element.id))
.flat();
const elementIdsSet = new Set(elementIdsArray);

if (elementIdsArray.length !== elementIdsSet.size) {
return 'Element Ids are not unique';
}
},
}
);