|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +/** |
| 5 | + * Validates a feature flag object, checking if it conforms to the schema. |
| 6 | + * @param featureFlag The feature flag object to validate. |
| 7 | + */ |
| 8 | +export function validateFeatureFlag(featureFlag: any): void { |
| 9 | + if (featureFlag === undefined) { |
| 10 | + return; // no-op if feature flag is undefined, indicating that the feature flag is not found |
| 11 | + } |
| 12 | + if (featureFlag === null || typeof featureFlag !== "object") { // Note: typeof null = "object" |
| 13 | + throw new TypeError("Feature flag must be an object."); |
| 14 | + } |
| 15 | + if (typeof featureFlag.id !== "string") { |
| 16 | + throw new TypeError("Feature flag 'id' must be a string."); |
| 17 | + } |
| 18 | + if (featureFlag.enabled !== undefined && typeof featureFlag.enabled !== "boolean") { |
| 19 | + throw new TypeError("Feature flag 'enabled' must be a boolean."); |
| 20 | + } |
| 21 | + if (featureFlag.conditions !== undefined) { |
| 22 | + validateFeatureEnablementConditions(featureFlag.conditions); |
| 23 | + } |
| 24 | + if (featureFlag.variants !== undefined) { |
| 25 | + validateVariants(featureFlag.variants); |
| 26 | + } |
| 27 | + if (featureFlag.allocation !== undefined) { |
| 28 | + validateVariantAllocation(featureFlag.allocation); |
| 29 | + } |
| 30 | + if (featureFlag.telemetry !== undefined) { |
| 31 | + validateTelemetryOptions(featureFlag.telemetry); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function validateFeatureEnablementConditions(conditions: any) { |
| 36 | + if (typeof conditions !== "object") { |
| 37 | + throw new TypeError("Feature flag 'conditions' must be an object."); |
| 38 | + } |
| 39 | + if (conditions.requirement_type !== undefined && conditions.requirement_type !== "Any" && conditions.requirement_type !== "All") { |
| 40 | + throw new TypeError("'requirement_type' must be 'Any' or 'All'."); |
| 41 | + } |
| 42 | + if (conditions.client_filters !== undefined) { |
| 43 | + validateClientFilters(conditions.client_filters); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function validateClientFilters(client_filters: any) { |
| 48 | + if (!Array.isArray(client_filters)) { |
| 49 | + throw new TypeError("Feature flag conditions 'client_filters' must be an array."); |
| 50 | + } |
| 51 | + |
| 52 | + for (const filter of client_filters) { |
| 53 | + if (typeof filter.name !== "string") { |
| 54 | + throw new TypeError("Client filter 'name' must be a string."); |
| 55 | + } |
| 56 | + if (filter.parameters !== undefined && typeof filter.parameters !== "object") { |
| 57 | + throw new TypeError("Client filter 'parameters' must be an object."); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function validateVariants(variants: any) { |
| 63 | + if (!Array.isArray(variants)) { |
| 64 | + throw new TypeError("Feature flag 'variants' must be an array."); |
| 65 | + } |
| 66 | + |
| 67 | + for (const variant of variants) { |
| 68 | + if (typeof variant.name !== "string") { |
| 69 | + throw new TypeError("Variant 'name' must be a string."); |
| 70 | + } |
| 71 | + // skip configuration_value validation as it accepts any type |
| 72 | + if (variant.status_override !== undefined && typeof variant.status_override !== "string") { |
| 73 | + throw new TypeError("Variant 'status_override' must be a string."); |
| 74 | + } |
| 75 | + if (variant.status_override !== undefined && variant.status_override !== "None" && variant.status_override !== "Enabled" && variant.status_override !== "Disabled") { |
| 76 | + throw new TypeError("Variant 'status_override' must be 'None', 'Enabled', or 'Disabled'."); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function validateVariantAllocation(allocation: any) { |
| 82 | + if (typeof allocation !== "object") { |
| 83 | + throw new TypeError("Variant 'allocation' must be an object."); |
| 84 | + } |
| 85 | + |
| 86 | + if (allocation.default_when_disabled !== undefined && typeof allocation.default_when_disabled !== "string") { |
| 87 | + throw new TypeError("Variant allocation 'default_when_disabled' must be a string."); |
| 88 | + } |
| 89 | + if (allocation.default_when_enabled !== undefined && typeof allocation.default_when_enabled !== "string") { |
| 90 | + throw new TypeError("Variant allocation 'default_when_enabled' must be a string."); |
| 91 | + } |
| 92 | + if (allocation.user !== undefined) { |
| 93 | + validateUserVariantAllocation(allocation.user); |
| 94 | + } |
| 95 | + if (allocation.group !== undefined) { |
| 96 | + validateGroupVariantAllocation(allocation.group); |
| 97 | + } |
| 98 | + if (allocation.percentile !== undefined) { |
| 99 | + validatePercentileVariantAllocation(allocation.percentile); |
| 100 | + } |
| 101 | + if (allocation.seed !== undefined && typeof allocation.seed !== "string") { |
| 102 | + throw new TypeError("Variant allocation 'seed' must be a string."); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function validateUserVariantAllocation(UserAllocations: any) { |
| 107 | + if (!Array.isArray(UserAllocations)) { |
| 108 | + throw new TypeError("Variant 'user' allocation must be an array."); |
| 109 | + } |
| 110 | + |
| 111 | + for (const allocation of UserAllocations) { |
| 112 | + if (typeof allocation !== "object") { |
| 113 | + throw new TypeError("Elements in variant 'user' allocation must be an object."); |
| 114 | + } |
| 115 | + if (typeof allocation.variant !== "string") { |
| 116 | + throw new TypeError("User allocation 'variant' must be a string."); |
| 117 | + } |
| 118 | + if (!Array.isArray(allocation.users)) { |
| 119 | + throw new TypeError("User allocation 'users' must be an array."); |
| 120 | + } |
| 121 | + for (const user of allocation.users) { |
| 122 | + if (typeof user !== "string") { |
| 123 | + throw new TypeError("Elements in user allocation 'users' must be strings."); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function validateGroupVariantAllocation(groupAllocations: any) { |
| 130 | + if (!Array.isArray(groupAllocations)) { |
| 131 | + throw new TypeError("Variant 'group' allocation must be an array."); |
| 132 | + } |
| 133 | + |
| 134 | + for (const allocation of groupAllocations) { |
| 135 | + if (typeof allocation !== "object") { |
| 136 | + throw new TypeError("Elements in variant 'group' allocation must be an object."); |
| 137 | + } |
| 138 | + if (typeof allocation.variant !== "string") { |
| 139 | + throw new TypeError("Group allocation 'variant' must be a string."); |
| 140 | + } |
| 141 | + if (!Array.isArray(allocation.groups)) { |
| 142 | + throw new TypeError("Group allocation 'groups' must be an array."); |
| 143 | + } |
| 144 | + for (const group of allocation.groups) { |
| 145 | + if (typeof group !== "string") { |
| 146 | + throw new TypeError("Elements in group allocation 'groups' must be strings."); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +function validatePercentileVariantAllocation(percentileAllocations: any) { |
| 153 | + if (!Array.isArray(percentileAllocations)) { |
| 154 | + throw new TypeError("Variant 'percentile' allocation must be an array."); |
| 155 | + } |
| 156 | + |
| 157 | + for (const allocation of percentileAllocations) { |
| 158 | + if (typeof allocation !== "object") { |
| 159 | + throw new TypeError("Elements in variant 'percentile' allocation must be an object."); |
| 160 | + } |
| 161 | + if (typeof allocation.variant !== "string") { |
| 162 | + throw new TypeError("Percentile allocation 'variant' must be a string."); |
| 163 | + } |
| 164 | + if (typeof allocation.from !== "number" || allocation.from < 0 || allocation.from > 100) { |
| 165 | + throw new TypeError("Percentile allocation 'from' must be a number between 0 and 100."); |
| 166 | + } |
| 167 | + if (typeof allocation.to !== "number" || allocation.to < 0 || allocation.to > 100) { |
| 168 | + throw new TypeError("Percentile allocation 'to' must be a number between 0 and 100."); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | +// #endregion |
| 173 | + |
| 174 | +// #region Telemetry |
| 175 | +function validateTelemetryOptions(telemetry: any) { |
| 176 | + if (typeof telemetry !== "object") { |
| 177 | + throw new TypeError("Feature flag 'telemetry' must be an object."); |
| 178 | + } |
| 179 | + if (telemetry.enabled !== undefined && typeof telemetry.enabled !== "boolean") { |
| 180 | + throw new TypeError("Telemetry 'enabled' must be a boolean."); |
| 181 | + } |
| 182 | + if (telemetry.metadata !== undefined && typeof telemetry.metadata !== "object") { |
| 183 | + throw new TypeError("Telemetry 'metadata' must be an object."); |
| 184 | + } |
| 185 | +} |
| 186 | +// #endregion |
0 commit comments