From e42337e5231e5b67e49df2778360b2cb662540a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Mon, 3 Aug 2020 13:24:51 +0200 Subject: [PATCH] feat: add weightType as legal property on variant schema (#614) --- lib/routes/admin-api/feature-schema.js | 4 +++ lib/routes/admin-api/feature-schema.test.js | 40 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/routes/admin-api/feature-schema.js b/lib/routes/admin-api/feature-schema.js index d71088fc74..b70dcfe1e2 100644 --- a/lib/routes/admin-api/feature-schema.js +++ b/lib/routes/admin-api/feature-schema.js @@ -27,6 +27,10 @@ const variantsSchema = joi.object().keys({ .min(0) .max(1000) .required(), + weightType: joi + .string() + .valid('variable', 'fix') + .default('variable'), payload: joi .object() .keys({ diff --git a/lib/routes/admin-api/feature-schema.test.js b/lib/routes/admin-api/feature-schema.test.js index df587f9f38..84545e8660 100644 --- a/lib/routes/admin-api/feature-schema.test.js +++ b/lib/routes/admin-api/feature-schema.test.js @@ -44,6 +44,45 @@ test('should strip extra variant fields', t => { t.falsy(value.variants[0].unkown); }); +test('should allow weightType=fix', t => { + const toggle = { + name: 'app.name', + enabled: false, + strategies: [{ name: 'default' }], + variants: [ + { + name: 'variant-a', + weight: 1, + weightType: 'fix', + }, + ], + }; + + const { value } = featureShema.validate(toggle); + t.deepEqual(value, toggle); +}); + +test('should disallow weightType=unknown', t => { + const toggle = { + name: 'app.name', + enabled: false, + strategies: [{ name: 'default' }], + variants: [ + { + name: 'variant-a', + weight: 1, + weightType: 'unknown', + }, + ], + }; + + const { error } = featureShema.validate(toggle); + t.deepEqual( + error.details[0].message, + '"variants[0].weightType" must be one of [variable, fix]', + ); +}); + test('should be possible to define variant overrides', t => { const toggle = { name: 'app.name', @@ -53,6 +92,7 @@ test('should be possible to define variant overrides', t => { { name: 'variant-a', weight: 1, + weightType: 'variable', overrides: [ { contextName: 'userId',