-
-
Notifications
You must be signed in to change notification settings - Fork 658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add implicit surrounding ^
and $
to patterns
#4664
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
src/lib/services/project-service.ts
Outdated
const validateFlagNaming = (naming?: IFeatureNaming) => { | ||
if (naming) { | ||
const { pattern, example } = naming; | ||
if ( | ||
pattern != null && | ||
example && | ||
!example.match(new RegExp(`^${pattern}$`)) | ||
) { | ||
throw new BadDataError( | ||
`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) { | ||
throw new BadDataError( | ||
"You've provided a feature flag naming example, but no feature flag naming pattern. You must specify a pattern to use an example.", | ||
); | ||
} | ||
} | ||
}; | ||
|
||
export const validateAndProcessFeatureNamingPattern = ( | ||
featureNaming: IFeatureNaming, | ||
): IFeatureNaming => { | ||
validateFlagNaming(featureNaming); | ||
|
||
if (featureNaming.pattern && !featureNaming.example) { | ||
featureNaming.example = null; | ||
} | ||
if (featureNaming.pattern && !featureNaming.description) { | ||
featureNaming.description = null; | ||
} | ||
|
||
return featureNaming; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been moved out of the project service class to make it easier to unit test.
src/lib/services/project-service.ts
Outdated
if (featureNaming.pattern && !featureNaming.example) { | ||
featureNaming.example = null; | ||
} | ||
if (featureNaming.pattern && !featureNaming.description) { | ||
featureNaming.description = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null fields out if people update a pattern without touching the description or example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd extract name validation code into a separate file based on the following principle: "things that change together should be kept together". If you feel like you keep changing the same name validation functions over and over again independently from the other code in the feature-toggle-service and project-service then you have a candidate for extraction.
@kwasniew More lines, but it's been extracted into a separate feature directory (feature-naming-pattern) and most tests have been moved with it. It feels like a better way to do it to me (even if it meant changing some more files), so I appreciate the push. Now all the actual validation of input data and of feature names live in the same file. |
This PR updates the back-end handling of feature naming patterns to add implicit leading
^
s and trailing$
s to the regexes when comparing them.It also adds tests for the new behavior, both for new flag names and for examples.
Discussion points
Regarding stripping incoming ^ and $: We don't actually need to strip incoming
^
s and$
s: it appears that^^^^^x$$$$$
is just as valid as^x$
. As such, we can leave that in. However, if we think it's better to strip, we can do that too.Second, I'm considering moving the flag naming validation into a dedicated module to encapsulate everything a little better. Not sure if this is the time or where it would live, but open to hearing suggestions.