1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

refactor: remove check for feature naming data object (#4745)

We already check for its presence before doing anything. We don't need
to
check it twice (we're not Santa Claus, after all).
This commit is contained in:
Thomas Heartman 2023-09-15 12:51:47 +02:00 committed by GitHub
parent d838b5f2c3
commit 94b65542e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,52 +2,43 @@ import { IFeatureNaming } from '../../types/model';
const compileRegex = (pattern: string) => new RegExp(`^${pattern}$`); const compileRegex = (pattern: string) => new RegExp(`^${pattern}$`);
const disallowedStrings = [' ', '\\t', '\\s', '\\n', '\\r', '\\f', '\\v'];
// Helper functions for error messages
const whitespaceError = (pattern: string) =>
`Feature flag names cannot contain whitespace. You've provided a feature flag naming pattern that contains a whitespace character: "${pattern}". Remove any whitespace characters from your pattern.`;
const exampleMismatchError = (example: string, pattern: string) =>
`You've provided a feature flag naming example ("${example}") that doesn't match your feature flag naming pattern ("${pattern}").`;
const invalidValueError = (valueName: string) =>
`You've provided a feature flag naming ${valueName}, but no feature flag naming pattern. You must specify a pattern to use a ${valueName}.`;
export const checkFeatureNamingData = ( export const checkFeatureNamingData = (
featureNaming?: IFeatureNaming, featureNaming: IFeatureNaming,
): ):
| { state: 'valid' } | { state: 'valid' }
| { state: 'invalid'; reasons: [string, ...string[]] } => { | { state: 'invalid'; reasons: [string, ...string[]] } => {
if (featureNaming) { const { pattern, example, description } = featureNaming;
const { pattern, example, description } = featureNaming; const errors: string[] = [];
const errors: string[] = [];
const disallowedStrings = [
' ',
'\\t',
'\\s',
'\\n',
'\\r',
'\\f',
'\\v',
];
if (pattern) { if (disallowedStrings.some((str) => pattern?.includes(str))) {
if (disallowedStrings.some((str) => pattern.includes(str))) { errors.push(whitespaceError(pattern as string));
errors.push( } else if (pattern && example && !compileRegex(pattern).test(example)) {
`Feature flag names can not contain whitespace. You've provided a feature flag naming pattern that contains a whitespace character: "${pattern}". Remove any whitespace characters from your pattern.`, errors.push(exampleMismatchError(example, pattern));
); }
} else if (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 "$".`,
);
}
} else {
if (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 (description) { if (!pattern && example) {
errors.push( errors.push(invalidValueError('example'));
"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 (!pattern && description) {
if (first) { errors.push(invalidValueError('description'));
return { state: 'invalid', reasons: [first, ...rest] }; }
}
const [first, ...rest] = errors;
if (first) {
return { state: 'invalid', reasons: [first, ...rest] };
} }
return { state: 'valid' }; return { state: 'valid' };