From 0ee54bb5b48266b2a29680d447ea79a796003b0c Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Sat, 16 Nov 2019 14:59:46 -0700 Subject: [PATCH] starts converting Joi validation into kbn-config-schema terminology --- .../home/server/lib/tutorial_schema.ts | 149 +++++++++++------- 1 file changed, 93 insertions(+), 56 deletions(-) diff --git a/src/plugins/home/server/lib/tutorial_schema.ts b/src/plugins/home/server/lib/tutorial_schema.ts index eb001ac96cceb..34c676ab1b364 100644 --- a/src/plugins/home/server/lib/tutorial_schema.ts +++ b/src/plugins/home/server/lib/tutorial_schema.ts @@ -18,6 +18,7 @@ */ import Joi from 'joi'; +import { schema } from '@kbn/config-schema'; const PARAM_TYPES = { NUMBER: 'number', @@ -31,65 +32,101 @@ const TUTORIAL_CATEGORY = { OTHER: 'other', }; -const dashboardSchema = Joi.object({ - id: Joi.string().required(), // Dashboard saved object id - linkLabel: Joi.string().when('isOverview', { - is: true, - then: Joi.required(), - }), - // Is this an Overview / Entry Point dashboard? - isOverview: Joi.boolean().required(), -}); - -const artifactsSchema = Joi.object({ +// const dashboardSchema = schema.object({ +// id: schema.string(), // Dashboard saved object id +// linkLabel: Joi.string().when('isOverview', { +// is: true, +// then: Joi.required(), +// }), +// // Is this an Overview / Entry Point dashboard? +// isOverview: schema.boolean(), +// }); + +const artifactsSchema = schema.object({ // Fields present in Elasticsearch documents created by this product. - exportedFields: Joi.object({ - documentationUrl: Joi.string().required(), + exportedFields: schema.object({ + documentationUrl: schema.string(), }), // Kibana dashboards created by this product. - dashboards: Joi.array() - .items(dashboardSchema) - .required(), - application: Joi.object({ - path: Joi.string().required(), - label: Joi.string().required(), + dashboards: schema.arrayOf( + schema.object({ + id: schema.string(), // Dashboard saved object id + linkLabel: schema.conditional( + // linkLabel is required and must be a string when 'isOverview' has a value of true + schema.siblingRef('isOverview'), + true, + schema.string(), + schema.boolean() + ), + isOverview: schema.boolean(), + }) + ), + application: schema.object({ + path: schema.string(), + label: schema.string(), }), }); -const statusCheckSchema = Joi.object({ - title: Joi.string(), - text: Joi.string(), - btnLabel: Joi.string(), - success: Joi.string(), - error: Joi.string(), - esHitsCheck: Joi.object({ - index: Joi.alternatives() - .try(Joi.string(), Joi.array().items(Joi.string())) - .required(), - query: Joi.object().required(), - }).required(), +const statusCheckSchema = schema.object({ + title: schema.string(), + text: schema.string(), + btnLabel: schema.string(), + success: schema.string(), + error: schema.string(), + esHitsCheck: schema.object({ + // index: either a string or an array of strings + index: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]), + // .try(Joi.string(), Joi.array().items(Joi.string())) + // .required(), + query: joi.object(), + }), }); - -const instructionSchema = Joi.object({ - title: Joi.string(), - textPre: Joi.string(), - commands: Joi.array().items(Joi.string().allow('')), - textPost: Joi.string(), +/* +One of: + +query: { + match_all: {}, +}, + +query: { + bool: { + filter: { + term: { + 'agent.type': 'auditbeat', + }, + }, + }, +}, +query: { + bool: { + filter: [ + { term: { 'processor.event': 'onboarding' } }, + { range: { 'observer.version_major': { gte: 7 } } }, + ], + }, +}, +*/ + +const instructionSchema = schema.object({ + title: schema.string(), + textPre: schema.string(), + commands: schema.arrayOf(schema.maybe(schema.oneOf([schema.maybe(schema.string())]))), + textPost: schema.string(), }); -const instructionVariantSchema = Joi.object({ - id: Joi.string().required(), +const instructionVariantSchema = schema.object({ + id: schema.string(), instructions: Joi.array() .items(instructionSchema) .required(), }); -const instructionSetSchema = Joi.object({ - title: Joi.string(), - callOut: Joi.object({ - title: Joi.string().required(), - message: Joi.string(), - iconType: Joi.string(), +const instructionSetSchema = schema.object({ + title: schema.string(), + callOut: schema.object({ + title: schema.string(), + message: schema.string(), + iconType: schema.string(), }), // Variants (OSes, languages, etc.) for which tutorial instructions are specified. instructionVariants: Joi.array() @@ -98,18 +135,18 @@ const instructionSetSchema = Joi.object({ statusCheck: statusCheckSchema, }); -const paramSchema = Joi.object({ +const paramSchema = schema.object({ defaultValue: Joi.required(), id: Joi.string() .regex(/^[a-zA-Z_]+$/) .required(), - label: Joi.string().required(), + label: schema.string(), type: Joi.string() .valid(Object.values(PARAM_TYPES)) .required(), }); -const instructionsSchema = Joi.object({ +const instructionsSchema = schema.object({ instructionSets: Joi.array() .items(instructionSetSchema) .required(), @@ -123,13 +160,13 @@ export const tutorialSchema = { category: Joi.string() .valid(Object.values(TUTORIAL_CATEGORY)) .required(), - name: Joi.string().required(), - isBeta: Joi.boolean().default(false), - shortDescription: Joi.string().required(), - euiIconType: Joi.string(), // EUI icon type string, one of https://elastic.github.io/eui/#/icons - longDescription: Joi.string().required(), - completionTimeMinutes: Joi.number().integer(), - previewImagePath: Joi.string(), + name: schema.string(), + isBeta: schema.boolean({ defaultValue: false }), + shortDescription: schema.string(), + euiIconType: schema.string(), // EUI icon type string, one of https://elastic.github.io/eui/#/icons + longDescription: schema.string(), + completionTimeMinutes: schema.number(), + previewImagePath: schema.string(), // kibana and elastic cluster running on prem onPrem: instructionsSchema.required(), @@ -145,5 +182,5 @@ export const tutorialSchema = { // saved objects used by data module. savedObjects: Joi.array().items(), - savedObjectsInstallMsg: Joi.string(), + savedObjectsInstallMsg: schema.string(), };