diff --git a/lib/routes/client-api/metrics-schema.js b/lib/routes/client-api/metrics-schema.js index c446676841..425b9b754a 100644 --- a/lib/routes/client-api/metrics-schema.js +++ b/lib/routes/client-api/metrics-schema.js @@ -9,10 +9,12 @@ const countSchema = joi yes: joi .number() .min(0) + .empty('') .default(0), no: joi .number() .min(0) + .empty('') .default(0), variants: joi.object().pattern(joi.string(), joi.number().min(0)), }); diff --git a/lib/routes/client-api/metrics.test.js b/lib/routes/client-api/metrics.test.js index 72bb80a7a0..1e59792242 100644 --- a/lib/routes/client-api/metrics.test.js +++ b/lib/routes/client-api/metrics.test.js @@ -1,10 +1,12 @@ 'use strict'; const test = require('ava'); +const supertest = require('supertest'); +const joi = require('joi'); const store = require('./../../../test/fixtures/store'); const getLogger = require('../../../test/fixtures/no-logger'); -const supertest = require('supertest'); const getApp = require('../../app'); +const { clientMetricsSchema } = require('./metrics-schema'); const { EventEmitter } = require('events'); const eventBus = new EventEmitter(); @@ -119,3 +121,35 @@ test('should accept client metrics without yes/no', t => { }) .expect(202); }); + +test('shema allow empty strings', t => { + const data = { + appName: 'java-test', + instanceId: 'instance y', + bucket: { + toggles: { Demo2: { yes: '', no: '', variants: {} } }, + start: '2019-05-06T08:30:40.514Z', + stop: '2019-05-06T09:30:50.515Z', + }, + }; + const { error, value } = joi.validate(data, clientMetricsSchema); + t.falsy(error); + t.is(value.bucket.toggles.Demo2.yes, 0); + t.is(value.bucket.toggles.Demo2.no, 0); +}); + +test('shema allow yes=', t => { + const data = { + appName: 'java-test', + instanceId: 'instance y', + bucket: { + toggles: { Demo2: { yes: '12', no: 256, variants: {} } }, + start: '2019-05-06T08:30:40.514Z', + stop: '2019-05-06T09:30:50.515Z', + }, + }; + const { error, value } = joi.validate(data, clientMetricsSchema); + t.falsy(error); + t.is(value.bucket.toggles.Demo2.yes, 12); + t.is(value.bucket.toggles.Demo2.no, 256); +});