Skip to content

Commit

Permalink
feat: disallow description when no pattern exists (#4679)
Browse files Browse the repository at this point in the history
This PR makes it so that adding a feature naming description when there
is no pattern is disallowed. It also changes the validation for feature
naming slightly so that it can return multiple errors at once.
  • Loading branch information
thomasheartman committed Sep 14, 2023
1 parent 8c90b45 commit 6dbea08
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib/error/bad-data-error.ts
Expand Up @@ -4,7 +4,7 @@ import getProp from 'lodash.get';
import { ApiErrorSchema, UnleashError } from './unleash-error';

type ValidationErrorDescription = {
description: string;
description?: string;
message: string;
path?: string;
};
Expand Down
Expand Up @@ -60,6 +60,24 @@ describe('validate incoming feature naming data', () => {
).toMatchObject({ state: 'invalid' });
}
});

test('feature naming data with a non-empty example but an empty pattern is invalid', () => {
expect(
checkFeatureNamingData({
pattern: '',
example: 'example',
}),
).toMatchObject({ state: 'invalid' });
});

test('feature naming data with a non-empty description but an empty pattern is invalid', () => {
expect(
checkFeatureNamingData({
pattern: '',
description: 'description',
}),
).toMatchObject({ state: 'invalid' });
});
});

describe('validate feature flag names against a pattern', () => {
Expand Down
Expand Up @@ -4,25 +4,33 @@ const compileRegex = (pattern: string) => new RegExp(`^${pattern}$`);

export const checkFeatureNamingData = (
featureNaming?: IFeatureNaming,
): { state: 'valid' } | { state: 'invalid'; reason: string } => {
):
| { state: 'valid' }
| { state: 'invalid'; reasons: [string, ...string[]] } => {
if (featureNaming) {
const { pattern, example } = featureNaming;
if (
pattern != null &&
example &&
!example.match(compileRegex(pattern))
) {
return {
state: 'invalid',
reason: `You've provided a feature flag naming example ("${example}") that doesn't match your feature flag naming pattern ("${pattern}"). Please provide an example that matches your supplied pattern. Bear in mind that the pattern must match the whole example, as if it were surrounded by '^' and "$".`,
};
const { pattern, example, description } = featureNaming;
const errors: string[] = [];
if (pattern && example && !example.match(compileRegex(pattern))) {
errors.push(
`You've provided a feature flag naming example ("${example}") that doesn't match your feature flag naming pattern ("${pattern}"). Please provide an example that matches your supplied pattern. Bear in mind that the pattern must match the whole example, as if it were surrounded by '^' and "$".`,
);
}

if (!pattern && example) {
return {
state: 'invalid',
reason: "You've provided a feature flag naming example, but no feature flag naming pattern. You must specify a pattern to use an example.",
};
errors.push(
"You've provided a feature flag naming example, but no feature flag naming pattern. You must specify a pattern to use an example.",
);
}

if (!pattern && description) {
errors.push(
"You've provided a feature flag naming pattern description, but no feature flag naming pattern. You must have a pattern to use a description.",
);
}

const [first, ...rest] = errors;
if (first) {
return { state: 'invalid', reasons: [first, ...rest] };
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/lib/services/project-service.ts
Expand Up @@ -176,7 +176,14 @@ export default class ProjectService {
const validationResult = checkFeatureNamingData(featureNaming);

if (validationResult.state === 'invalid') {
throw new BadDataError(validationResult.reason);
const [firstReason, ...remainingReasons] =
validationResult.reasons.map((message) => ({
message,
}));
throw new BadDataError(
'The feature naming pattern data you provided was invalid.',
[firstReason, ...remainingReasons],
);
}

if (featureNaming.pattern && !featureNaming.example) {
Expand Down

1 comment on commit 6dbea08

@vercel
Copy link

@vercel vercel bot commented on 6dbea08 Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.