From 24f987b29b1340389bc322e50c72e9e95f4203c6 Mon Sep 17 00:00:00 2001 From: Bilawal Hameed Date: Fri, 27 Mar 2020 11:35:38 +0100 Subject: [PATCH] [core/api/app] split name validation into function --- .../src/api/apis/definitions/featureFlags.ts | 8 ++++ packages/core/src/api/app/FeatureFlags.tsx | 43 ++++++++++++------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/core/src/api/apis/definitions/featureFlags.ts b/packages/core/src/api/apis/definitions/featureFlags.ts index 952e2b0121..15c9ecb084 100644 --- a/packages/core/src/api/apis/definitions/featureFlags.ts +++ b/packages/core/src/api/apis/definitions/featureFlags.ts @@ -35,6 +35,14 @@ export enum FeatureFlagState { } export type FeatureFlagsApi = { + /** + * Check the feature flag name convention. Used in the + * `registerFeatureFlag` method as well as in the `set` method. + * + * @returns string[] errors List of errors as string. Empty array if no errors. + */ + checkFeatureFlagNameErrors(name: FeatureFlagName): string[]; + /** * Get the current user's status of a Feature Flag * diff --git a/packages/core/src/api/app/FeatureFlags.tsx b/packages/core/src/api/app/FeatureFlags.tsx index 2b543db216..8d3d366906 100644 --- a/packages/core/src/api/app/FeatureFlags.tsx +++ b/packages/core/src/api/app/FeatureFlags.tsx @@ -45,6 +45,31 @@ class FeatureFlagsImpl implements FeatureFlagsApi { } } + // We don't make this private as we need this to validate + // in the `registerFeatureFlag` method in the Plugin API. + checkFeatureFlagNameErrors(name: FeatureFlagName): string[] { + const errors = []; + + if (name.length < 3) { + errors.push( + 'The `name` argument must have a minimum length of three characters.', + ); + } + + if (name.length > 150) { + errors.push('The `name` argument must not exceed 150 characters.'); + } + + 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.' + + 'Examples: feature-flag-one, alpha, release-2020', + ); + } + + return errors; + } + get(name: FeatureFlagName): FeatureFlagState { if (this.getUserEnabledFeatureFlags().has(name)) { return FeatureFlagState.Enabled; @@ -54,23 +79,11 @@ class FeatureFlagsImpl implements FeatureFlagsApi { } set(name: FeatureFlagName, state: FeatureFlagState): void { + const errors = this.checkFeatureFlagNameErrors(name); const flags = this.getUserEnabledFeatureFlags(); - if (name.length < 3) { - throw new Error( - 'The `name` argument must have a minimum length of three characters.', - ); - } - - if (name.length > 150) { - throw new Error('The `name` argument must not exceed 150 characters.'); - } - - if (!name.match(/^[a-z]+[a-z0-9-]+$/)) { - throw new Error( - 'The `name` argument must start with a lowercase letter and only contain lowercase letters, numbers and hyphens.' + - 'Examples: feature-flag-one, alpha, release-2020', - ); + if (errors.length > 0) { + throw new Error(errors[0]); } if (state === FeatureFlagState.Enabled) {