From b0550f324e114daef37dea7f03df7ef76c5bb869 Mon Sep 17 00:00:00 2001 From: colby-matthiasson Date: Fri, 24 Jul 2020 13:33:53 -0700 Subject: [PATCH 1/2] Fixed config key validation and unset error handling --- messages/config.json | 2 ++ src/config/config.ts | 22 +++++++++++++++++++--- src/config/configStore.ts | 10 ++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/messages/config.json b/messages/config.json index 91d37ff559..7cb98dac69 100644 --- a/messages/config.json +++ b/messages/config.json @@ -3,6 +3,8 @@ "InvalidConfigValue": "Invalid config value. %s", "InvalidInstanceUrl": "Specify a valid Salesforce instance URL", "InvalidApiVersion": "Specify a valid Salesforce API version, for example, 42.0", + "InvalidIsvDebuggerSid": "Specify a valid Debugger SID", + "InvalidIsvDebuggerUrl": "Specify a valid Debugger URL", "InvalidBooleanConfigValue": "The config value can only be set to true or false.", "InvalidProjectWorkspace": "This directory does not contain a valid Salesforce DX project", "SchemaValidationWarning": "The config file: %s is not schema valid\nDue to: %s", diff --git a/src/config/config.ts b/src/config/config.ts index d97acf206a..91e2c710e4 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -255,14 +255,30 @@ export class Config extends ConfigFile { hidden: true, input: { // If a value is provided validate it otherwise no value is unset. - validator: value => isString(value) && sfdc.validateApiVersion(value), + validator: value => value == null || (isString(value) && sfdc.validateApiVersion(value)), failedMessage: Config.messages.getMessage('InvalidApiVersion') } }, { key: Config.DEFAULT_DEV_HUB_USERNAME }, { key: Config.DEFAULT_USERNAME }, - { key: Config.ISV_DEBUGGER_SID, encrypted: true }, - { key: Config.ISV_DEBUGGER_URL }, + { + key: Config.ISV_DEBUGGER_SID, + encrypted: true, + input: { + // If a value is provided validate it otherwise no value is unset. + validator: value => value == null || isString(value), + failedMessage: Config.messages.getMessage('InvalidIsvDebuggerSid') + } + }, + { + key: Config.ISV_DEBUGGER_URL, + encrypted: true, + input: { + // If a value is provided validate it otherwise no value is unset. + validator: value => value == null || isString(value), + failedMessage: Config.messages.getMessage('InvalidIsvDebuggerUrl') + } + }, { key: Config.DISABLE_TELEMETRY, input: { diff --git a/src/config/configStore.ts b/src/config/configStore.ts index f90fdf8d51..97ace61fd6 100644 --- a/src/config/configStore.ts +++ b/src/config/configStore.ts @@ -6,8 +6,9 @@ */ import { AsyncCreatable, set } from '@salesforce/kit'; -import { AnyJson, definiteEntriesOf, definiteValuesOf, get, getAnyJson, JsonMap, Optional } from '@salesforce/ts-types'; -import { Dictionary } from '@salesforce/ts-types'; +import { AnyJson, definiteEntriesOf, definiteValuesOf, Dictionary, get, getAnyJson, JsonMap, Optional } from '@salesforce/ts-types'; +import { SfdxError } from '../sfdxError'; +import { Config } from './config'; /** * The allowed types stored in a config store. @@ -128,6 +129,11 @@ export abstract class BaseConfigStore extends * @param key The key. */ public unset(key: string): boolean { + const property = Config.getAllowedProperties().find(allowedProp => allowedProp.key === key); + + if (!property) { + throw SfdxError.create('@salesforce/core', 'config', 'UnknownConfigKey', [key]); + } return delete this.contents[key]; } From 6095dd69ab4ed5e4324e5a46d8ec5bed74bb538a Mon Sep 17 00:00:00 2001 From: colby-matthiasson Date: Wed, 29 Jul 2020 12:43:39 -0700 Subject: [PATCH 2/2] feat: overwriting configStore unset --- src/config/config.ts | 15 +++++++++++++++ src/config/configStore.ts | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/config/config.ts b/src/config/config.ts index 91e2c710e4..94356b5ae4 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -232,6 +232,21 @@ export class Config extends ConfigFile { return this.getContents(); } + /** + * Unsets a value for a property. + * + * **Throws** *{@link SfdxError}{ name: 'UnknownConfigKey' }* If the input validator fails. + * @param key The property to unset. + */ + public unset(key: string): boolean { + const property = Config.allowedProperties.find(allowedProp => allowedProp.key === key); + + if (!property) { + throw SfdxError.create('@salesforce/core', 'config', 'UnknownConfigKey', [key]); + } + return super.unset(property.key); + } + /** * Initializer for supported config types. */ diff --git a/src/config/configStore.ts b/src/config/configStore.ts index 97ace61fd6..851648790a 100644 --- a/src/config/configStore.ts +++ b/src/config/configStore.ts @@ -6,9 +6,16 @@ */ import { AsyncCreatable, set } from '@salesforce/kit'; -import { AnyJson, definiteEntriesOf, definiteValuesOf, Dictionary, get, getAnyJson, JsonMap, Optional } from '@salesforce/ts-types'; -import { SfdxError } from '../sfdxError'; -import { Config } from './config'; +import { + AnyJson, + definiteEntriesOf, + definiteValuesOf, + Dictionary, + get, + getAnyJson, + JsonMap, + Optional +} from '@salesforce/ts-types'; /** * The allowed types stored in a config store. @@ -129,11 +136,6 @@ export abstract class BaseConfigStore extends * @param key The key. */ public unset(key: string): boolean { - const property = Config.getAllowedProperties().find(allowedProp => allowedProp.key === key); - - if (!property) { - throw SfdxError.create('@salesforce/core', 'config', 'UnknownConfigKey', [key]); - } return delete this.contents[key]; }