From 9aaad5d6608c8cb9f588d2ad85f8ae057de7a167 Mon Sep 17 00:00:00 2001 From: Bilawal Hameed Date: Fri, 27 Mar 2020 11:52:14 +0100 Subject: [PATCH] [core/api] add name validation in registerFeatureFlag --- packages/core/src/api/app/FeatureFlags.tsx | 2 +- packages/core/src/api/plugin/Plugin.tsx | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/app/FeatureFlags.tsx b/packages/core/src/api/app/FeatureFlags.tsx index 8d3d366906..b8799e5a71 100644 --- a/packages/core/src/api/app/FeatureFlags.tsx +++ b/packages/core/src/api/app/FeatureFlags.tsx @@ -62,7 +62,7 @@ class FeatureFlagsImpl implements FeatureFlagsApi { if (!name.match(/^[a-z]+[a-z0-9-]+$/)) { errors.push( - 'The `name` argument must start with a lowercase letter and only contain lowercase letters, numbers and hyphens.' + + 'The `name` argument must start with a lowercase letter and only contain lowercase letters, numbers and hyphens. ' + 'Examples: feature-flag-one, alpha, release-2020', ); } diff --git a/packages/core/src/api/plugin/Plugin.tsx b/packages/core/src/api/plugin/Plugin.tsx index ddb95bfff8..d342cb2429 100644 --- a/packages/core/src/api/plugin/Plugin.tsx +++ b/packages/core/src/api/plugin/Plugin.tsx @@ -21,6 +21,7 @@ import { RouteOptions, FeatureFlagName, } from './types'; +import { FeatureFlags } from '../app/FeatureFlags'; import { Widget } from '../widgetView/types'; export type PluginConfig = { @@ -76,6 +77,7 @@ export default class Plugin { return []; } + const pluginId = this.getId(); const outputs = new Array(); this.config.register({ @@ -94,6 +96,14 @@ export default class Plugin { }, featureFlags: { registerFeatureFlag(name) { + const errors = FeatureFlags.checkFeatureFlagNameErrors(name); + + if (errors.length > 0) { + throw new Error( + `Failed to register '${name}' feature flag in '${pluginId}' plugin: ${errors[0]}`, + ); + } + outputs.push({ type: 'feature-flag', name }); }, },