Skip to content

Commit

Permalink
Improve workpad schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Robertson committed Apr 21, 2021
1 parent 4a8067b commit b7530c2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 15 deletions.
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';
}
},
}
);

0 comments on commit b7530c2

Please sign in to comment.