-
Notifications
You must be signed in to change notification settings - Fork 4
validate property types of feature flags #17
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ebecb4
resovle conflicts
Eskibear 8b816ca
update validation
linglingye001 c30d32e
update
linglingye001 3f8b7e7
update
linglingye001 0e6abdf
update
linglingye001 920c12f
update
linglingye001 c61b4a5
update
linglingye001 a8c548e
update
linglingye001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| /** | ||
| * Validates a feature flag object, checking if it conforms to the schema. | ||
| * @param featureFlag The feature flag object to validate. | ||
| */ | ||
| export function validateFeatureFlag(featureFlag: any): void { | ||
| if (featureFlag === undefined) { | ||
| return; // no-op if feature flag is undefined, indicating that the feature flag is not found | ||
| } | ||
| if (featureFlag === null || typeof featureFlag !== "object") { // Note: typeof null = "object" | ||
| throw new TypeError("Feature flag must be an object."); | ||
| } | ||
| if (typeof featureFlag.id !== "string") { | ||
| throw new TypeError("Feature flag 'id' must be a string."); | ||
| } | ||
| if (featureFlag.enabled !== undefined && typeof featureFlag.enabled !== "boolean") { | ||
| throw new TypeError("Feature flag 'enabled' must be a boolean."); | ||
| } | ||
| if (featureFlag.conditions !== undefined) { | ||
| validateFeatureEnablementConditions(featureFlag.conditions); | ||
| } | ||
| if (featureFlag.variants !== undefined) { | ||
| validateVariants(featureFlag.variants); | ||
| } | ||
| if (featureFlag.allocation !== undefined) { | ||
| validateVariantAllocation(featureFlag.allocation); | ||
| } | ||
| if (featureFlag.telemetry !== undefined) { | ||
| validateTelemetryOptions(featureFlag.telemetry); | ||
| } | ||
| } | ||
|
|
||
| function validateFeatureEnablementConditions(conditions: any) { | ||
| if (typeof conditions !== "object") { | ||
| throw new TypeError("Feature flag 'conditions' must be an object."); | ||
| } | ||
| if (conditions.requirement_type !== undefined && conditions.requirement_type !== "Any" && conditions.requirement_type !== "All") { | ||
| throw new TypeError("'requirement_type' must be 'Any' or 'All'."); | ||
| } | ||
| if (conditions.client_filters !== undefined) { | ||
| validateClientFilters(conditions.client_filters); | ||
| } | ||
| } | ||
|
|
||
| function validateClientFilters(client_filters: any) { | ||
| if (!Array.isArray(client_filters)) { | ||
| throw new TypeError("Feature flag conditions 'client_filters' must be an array."); | ||
| } | ||
|
|
||
| for (const filter of client_filters) { | ||
| if (typeof filter.name !== "string") { | ||
zhiyuanliang-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new TypeError("Client filter 'name' must be a string."); | ||
| } | ||
| if (filter.parameters !== undefined && typeof filter.parameters !== "object") { | ||
| throw new TypeError("Client filter 'parameters' must be an object."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function validateVariants(variants: any) { | ||
| if (!Array.isArray(variants)) { | ||
| throw new TypeError("Feature flag 'variants' must be an array."); | ||
| } | ||
|
|
||
| for (const variant of variants) { | ||
| if (typeof variant.name !== "string") { | ||
| throw new TypeError("Variant 'name' must be a string."); | ||
| } | ||
| // skip configuration_value validation as it accepts any type | ||
| if (variant.status_override !== undefined && typeof variant.status_override !== "string") { | ||
| throw new TypeError("Variant 'status_override' must be a string."); | ||
| } | ||
| if (variant.status_override !== undefined && variant.status_override !== "None" && variant.status_override !== "Enabled" && variant.status_override !== "Disabled") { | ||
| throw new TypeError("Variant 'status_override' must be 'None', 'Enabled', or 'Disabled'."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function validateVariantAllocation(allocation: any) { | ||
| if (typeof allocation !== "object") { | ||
| throw new TypeError("Variant 'allocation' must be an object."); | ||
| } | ||
|
|
||
| if (allocation.default_when_disabled !== undefined && typeof allocation.default_when_disabled !== "string") { | ||
| throw new TypeError("Variant allocation 'default_when_disabled' must be a string."); | ||
| } | ||
| if (allocation.default_when_enabled !== undefined && typeof allocation.default_when_enabled !== "string") { | ||
| throw new TypeError("Variant allocation 'default_when_enabled' must be a string."); | ||
| } | ||
| if (allocation.user !== undefined) { | ||
| validateUserVariantAllocation(allocation.user); | ||
| } | ||
| if (allocation.group !== undefined) { | ||
| validateGroupVariantAllocation(allocation.group); | ||
| } | ||
| if (allocation.percentile !== undefined) { | ||
| validatePercentileVariantAllocation(allocation.percentile); | ||
| } | ||
| if (allocation.seed !== undefined && typeof allocation.seed !== "string") { | ||
| throw new TypeError("Variant allocation 'seed' must be a string."); | ||
| } | ||
| } | ||
|
|
||
| function validateUserVariantAllocation(UserAllocations: any) { | ||
| if (!Array.isArray(UserAllocations)) { | ||
| throw new TypeError("Variant 'user' allocation must be an array."); | ||
| } | ||
|
|
||
| for (const allocation of UserAllocations) { | ||
| if (typeof allocation !== "object") { | ||
| throw new TypeError("Elements in variant 'user' allocation must be an object."); | ||
| } | ||
| if (typeof allocation.variant !== "string") { | ||
linglingye001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new TypeError("User allocation 'variant' must be a string."); | ||
| } | ||
| if (!Array.isArray(allocation.users)) { | ||
| throw new TypeError("User allocation 'users' must be an array."); | ||
| } | ||
| for (const user of allocation.users) { | ||
| if (typeof user !== "string") { | ||
| throw new TypeError("Elements in user allocation 'users' must be strings."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function validateGroupVariantAllocation(groupAllocations: any) { | ||
| if (!Array.isArray(groupAllocations)) { | ||
| throw new TypeError("Variant 'group' allocation must be an array."); | ||
| } | ||
|
|
||
| for (const allocation of groupAllocations) { | ||
| if (typeof allocation !== "object") { | ||
| throw new TypeError("Elements in variant 'group' allocation must be an object."); | ||
| } | ||
| if (typeof allocation.variant !== "string") { | ||
| throw new TypeError("Group allocation 'variant' must be a string."); | ||
| } | ||
| if (!Array.isArray(allocation.groups)) { | ||
| throw new TypeError("Group allocation 'groups' must be an array."); | ||
| } | ||
| for (const group of allocation.groups) { | ||
| if (typeof group !== "string") { | ||
| throw new TypeError("Elements in group allocation 'groups' must be strings."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function validatePercentileVariantAllocation(percentileAllocations: any) { | ||
| if (!Array.isArray(percentileAllocations)) { | ||
| throw new TypeError("Variant 'percentile' allocation must be an array."); | ||
| } | ||
|
|
||
| for (const allocation of percentileAllocations) { | ||
| if (typeof allocation !== "object") { | ||
| throw new TypeError("Elements in variant 'percentile' allocation must be an object."); | ||
| } | ||
| if (typeof allocation.variant !== "string") { | ||
| throw new TypeError("Percentile allocation 'variant' must be a string."); | ||
| } | ||
| if (typeof allocation.from !== "number" || allocation.from < 0 || allocation.from > 100) { | ||
| throw new TypeError("Percentile allocation 'from' must be a number between 0 and 100."); | ||
| } | ||
| if (typeof allocation.to !== "number" || allocation.to < 0 || allocation.to > 100) { | ||
| throw new TypeError("Percentile allocation 'to' must be a number between 0 and 100."); | ||
| } | ||
| } | ||
| } | ||
| // #endregion | ||
|
|
||
| // #region Telemetry | ||
| function validateTelemetryOptions(telemetry: any) { | ||
| if (typeof telemetry !== "object") { | ||
| throw new TypeError("Feature flag 'telemetry' must be an object."); | ||
| } | ||
| if (telemetry.enabled !== undefined && typeof telemetry.enabled !== "boolean") { | ||
| throw new TypeError("Telemetry 'enabled' must be a boolean."); | ||
| } | ||
| if (telemetry.metadata !== undefined && typeof telemetry.metadata !== "object") { | ||
| throw new TypeError("Telemetry 'metadata' must be an object."); | ||
| } | ||
| } | ||
| // #endregion | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.