[core/api/app] split name validation into function

This commit is contained in:
Bilawal Hameed
2020-03-27 11:35:38 +01:00
parent 4e38f03221
commit 24f987b29b
2 changed files with 36 additions and 15 deletions
@@ -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
*
+28 -15
View File
@@ -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) {