From 7064e7a66a8547a78ec80ffdf4eae15a7a3ee579 Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Fri, 14 Aug 2020 13:01:48 -0700 Subject: [PATCH] fix schema test --- .../reporting/server/config/schema.test.ts | 35 ++++++++++++ .../plugins/reporting/server/config/schema.ts | 57 ++++++++----------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/x-pack/plugins/reporting/server/config/schema.test.ts b/x-pack/plugins/reporting/server/config/schema.test.ts index 69e4d443cf04020..2eef5353e22974e 100644 --- a/x-pack/plugins/reporting/server/config/schema.test.ts +++ b/x-pack/plugins/reporting/server/config/schema.test.ts @@ -105,6 +105,41 @@ describe('Reporting Config Schema', () => { }); }); + it('allows Duration values for certain keys', () => { + expect(ConfigSchema.validate({ queue: { timeout: '2m' } }).queue.timeout).toMatchInlineSnapshot( + `"PT2M"` + ); + + expect( + ConfigSchema.validate({ capture: { loadDelay: '3s' } }).capture.loadDelay + ).toMatchInlineSnapshot(`"PT3S"`); + + expect( + ConfigSchema.validate({ + capture: { timeouts: { openUrl: '1m', waitForElements: '30s', renderComplete: '10s' } }, + }).capture.timeouts + ).toMatchInlineSnapshot(` + Object { + "openUrl": "PT1M", + "renderComplete": "PT10S", + "waitForElements": "PT30S", + } + `); + + expect( + ConfigSchema.validate({ csv: { scroll: { duration: '1m' } } }).csv.scroll.duration + ).toMatchInlineSnapshot(`"1m"`); + }); + + it('allows ByteSizeValue values for certain keys', () => { + expect(ConfigSchema.validate({ csv: { maxSizeBytes: '12mb' } }).csv.maxSizeBytes) + .toMatchInlineSnapshot(` + ByteSizeValue { + "valueInBytes": 12582912, + } + `); + }); + it(`allows optional settings`, () => { // encryption key expect( diff --git a/x-pack/plugins/reporting/server/config/schema.ts b/x-pack/plugins/reporting/server/config/schema.ts index d3bac308f99208c..96819cd755e62b4 100644 --- a/x-pack/plugins/reporting/server/config/schema.ts +++ b/x-pack/plugins/reporting/server/config/schema.ts @@ -5,7 +5,6 @@ */ import { schema, TypeOf } from '@kbn/config-schema'; -import moment from 'moment'; const KibanaServerSchema = schema.object({ hostname: schema.maybe( @@ -35,10 +34,7 @@ const QueueSchema = schema.object({ pollEnabled: schema.boolean({ defaultValue: true }), pollInterval: schema.number({ defaultValue: 3000 }), pollIntervalErrorMultiplier: schema.number({ defaultValue: 10 }), - timeout: schema.oneOf([ - schema.number({ defaultValue: 120000 }), - schema.duration({ defaultValue: moment.duration(2, 'm') }), - ]), + timeout: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 120000 }), }); const RulesSchema = schema.object({ @@ -49,18 +45,9 @@ const RulesSchema = schema.object({ const CaptureSchema = schema.object({ timeouts: schema.object({ - openUrl: schema.oneOf([ - schema.number({ defaultValue: 30000 }), - schema.duration({ defaultValue: moment.duration(1, 'm') }), - ]), - waitForElements: schema.oneOf([ - schema.number({ defaultValue: 30000 }), - schema.duration({ defaultValue: moment.duration(30, 's') }), - ]), - renderComplete: schema.oneOf([ - schema.number({ defaultValue: 30000 }), - schema.duration({ defaultValue: moment.duration(30, 's') }), - ]), + openUrl: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 30000 }), + waitForElements: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 30000 }), + renderComplete: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 30000 }), }), networkPolicy: schema.object({ enabled: schema.boolean({ defaultValue: true }), @@ -80,10 +67,7 @@ const CaptureSchema = schema.object({ width: schema.number({ defaultValue: 1950 }), height: schema.number({ defaultValue: 1200 }), }), - loadDelay: schema.oneOf([ - schema.number({ defaultValue: 30000 }), - schema.duration({ defaultValue: moment.duration(3, 's') }), - ]), + loadDelay: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 3000 }), browser: schema.object({ autoDownload: schema.conditional( schema.contextRef('dist'), @@ -129,13 +113,24 @@ const CsvSchema = schema.object({ checkForFormulas: schema.boolean({ defaultValue: true }), escapeFormulaValues: schema.boolean({ defaultValue: false }), enablePanelActionDownload: schema.boolean({ defaultValue: true }), - maxSizeBytes: schema.oneOf([ - schema.number({ defaultValue: 1024 * 1024 * 10 }), - schema.byteSize({ defaultValue: '10mb' }), - ]), + maxSizeBytes: schema.oneOf([schema.number(), schema.byteSize()], { + defaultValue: 1024 * 1024 * 10, + }), useByteOrderMarkEncoding: schema.boolean({ defaultValue: false }), scroll: schema.object({ - duration: schema.duration({ defaultValue: moment.duration(30, 's') }), + duration: schema.oneOf( + [ + schema.string({ + validate(value) { + if (!/^[0-9]+(d|h|m|s|ms|micros|nanos)$/.test(value)) { + return 'must be a duration string'; + } + }, + }), + schema.duration(), + ], + { defaultValue: '30s' } + ), size: schema.number({ defaultValue: 500 }), }), }); @@ -155,17 +150,11 @@ const IndexSchema = schema.string({ defaultValue: '.reporting' }); const PollSchema = schema.object({ jobCompletionNotifier: schema.object({ - interval: schema.oneOf([ - schema.number({ defaultValue: 10000 }), - schema.duration({ defaultValue: moment.duration(10, 's') }), - ]), + interval: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 10000 }), intervalErrorMultiplier: schema.number({ defaultValue: 5 }), }), jobsRefresh: schema.object({ - interval: schema.oneOf([ - schema.number({ defaultValue: 5000 }), - schema.duration({ defaultValue: moment.duration(5, 's') }), - ]), + interval: schema.oneOf([schema.number(), schema.duration()], { defaultValue: 5000 }), intervalErrorMultiplier: schema.number({ defaultValue: 5 }), }), });