Skip to content
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

Merged
merged 12 commits into from
Sep 13, 2023

Conversation

thomasheartman
Copy link
Contributor

@thomasheartman thomasheartman commented Sep 12, 2023

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.

@vercel
Copy link

vercel bot commented Sep 12, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Sep 13, 2023 8:25am
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Visit Preview Sep 13, 2023 8:25am

Comment on lines 64 to 99
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;
};

Copy link
Contributor Author

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.

Comment on lines 90 to 95
if (featureNaming.pattern && !featureNaming.example) {
featureNaming.example = null;
}
if (featureNaming.pattern && !featureNaming.description) {
featureNaming.description = null;
}
Copy link
Contributor Author

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.

@kwasniew kwasniew self-requested a review September 12, 2023 12:21
Copy link
Contributor

@kwasniew kwasniew left a 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.

@thomasheartman
Copy link
Contributor Author

@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.

@thomasheartman thomasheartman merged commit 392beee into main Sep 13, 2023
9 checks passed
@thomasheartman thomasheartman deleted the 1-1362-update-handling-on-the-backend branch September 13, 2023 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants