@@ -349,8 +349,12 @@ class FeatureToggles {
349349 return scopeKey === undefined || typeof scopeKey === "string" ;
350350 }
351351
352+ static _isValidScopeMapValue ( value ) {
353+ return typeof value === "string" ;
354+ }
355+
352356 // NOTE: this function is used during initialization, so we cannot check this.__isInitialized
353- async _validateFeatureValue ( featureKey , value , scopeMap , scopeKey ) {
357+ async _validateFeatureValue ( featureKey , value , { scopeMap, scopeKey, isChange = false } = { } ) {
354358 if ( ! this . __isConfigProcessed ) {
355359 return [ { errorMessage : "not initialized" } ] ;
356360 }
@@ -395,9 +399,14 @@ class FeatureToggles {
395399 return [ { featureKey, scopeKey, errorMessage : "scopeKey is not valid" } ] ;
396400 }
397401
398- // NOTE: value === null is our way of encoding featureKey resetting changes, so it is always allowed
402+ // NOTE: value === null is our way of encoding featureKey resetting changes, so it is allowed for changes but not
403+ // for actual values
399404 if ( value === null ) {
400- return [ ] ;
405+ if ( isChange ) {
406+ return [ ] ;
407+ } else {
408+ return [ { featureKey, ...( scopeKey && { scopeKey } ) , errorMessage : "value null is not allowed" } ] ;
409+ }
401410 }
402411
403412 // NOTE: skip validating active properties during initialization
@@ -533,7 +542,10 @@ class FeatureToggles {
533542 async validateFeatureValue ( featureKey , value , scopeMap = undefined ) {
534543 return scopeMap === undefined
535544 ? await this . _validateFeatureValue ( featureKey , value )
536- : await this . _validateFeatureValue ( featureKey , value , scopeMap , FeatureToggles . getScopeKey ( scopeMap ) ) ;
545+ : await this . _validateFeatureValue ( featureKey , value , {
546+ scopeMap,
547+ scopeKey : FeatureToggles . getScopeKey ( scopeMap ) ,
548+ } ) ;
537549 }
538550
539551 /**
@@ -560,12 +572,10 @@ class FeatureToggles {
560572 let validatedStateScopedValues = { } ;
561573
562574 for ( const [ scopeKey , value ] of Object . entries ( scopedValues ) ) {
563- const entryValidationErrors = await this . _validateFeatureValue (
564- featureKey ,
565- value ,
566- FeatureToggles . getScopeMap ( scopeKey ) ,
567- scopeKey
568- ) ;
575+ const entryValidationErrors = await this . _validateFeatureValue ( featureKey , value , {
576+ scopeMap : FeatureToggles . getScopeMap ( scopeKey ) ,
577+ scopeKey,
578+ } ) ;
569579 let updateValue = value ;
570580 if ( Array . isArray ( entryValidationErrors ) && entryValidationErrors . length > 0 ) {
571581 validationErrors = validationErrors . concat ( entryValidationErrors ) ;
@@ -902,10 +912,6 @@ class FeatureToggles {
902912 // START OF GET_FEATURE_VALUE SECTION
903913 // ========================================
904914
905- static _isValidScopeMapValue ( value ) {
906- return typeof value === "string" ;
907- }
908-
909915 /**
910916 * This is used to make sure scopeMap is either undefined or a shallow map with string entries. This happens for all
911917 * public interfaces with a scopeMap parameter, except {@link validateFeatureValue} and {@link changeFeatureValue}.
@@ -1242,7 +1248,11 @@ class FeatureToggles {
12421248 scopeMap
12431249 ) ;
12441250
1245- const validationErrors = await this . _validateFeatureValue ( featureKey , newValue , scopeMap , scopeKey ) ;
1251+ const validationErrors = await this . _validateFeatureValue ( featureKey , newValue , {
1252+ scopeMap,
1253+ scopeKey,
1254+ isChange : true ,
1255+ } ) ;
12461256 if ( Array . isArray ( validationErrors ) && validationErrors . length > 0 ) {
12471257 logger . warning (
12481258 new VError (
@@ -1289,7 +1299,11 @@ class FeatureToggles {
12891299
12901300 async _changeRemoteFeatureValue ( featureKey , newValue , scopeMap , options ) {
12911301 const scopeKey = FeatureToggles . getScopeKey ( scopeMap ) ;
1292- const validationErrors = await this . _validateFeatureValue ( featureKey , newValue , scopeMap , scopeKey ) ;
1302+ const validationErrors = await this . _validateFeatureValue ( featureKey , newValue , {
1303+ scopeMap,
1304+ scopeKey,
1305+ isChange : true ,
1306+ } ) ;
12931307 if ( Array . isArray ( validationErrors ) && validationErrors . length > 0 ) {
12941308 return validationErrors ;
12951309 }
0 commit comments