diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 2e1b29b6d..1209a51dc 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -671,12 +671,21 @@ describe "Config", -> atom.config.set('foo.bar.aString', 'yep') expect(atom.config.get('foo.bar.aString')).toBe 'yep' - it 'will not set non-strings', -> - expect(atom.config.set('foo.bar.aString', null)).toBe false - expect(atom.config.get('foo.bar.aString')).toBe 'ok' + it 'will only set strings, numbers and booleans', -> + expect(atom.config.set('foo.bar.aString', 123)).toBe true + expect(atom.config.get('foo.bar.aString')).toBe '123' - expect(atom.config.set('foo.bar.aString', 123)).toBe false - expect(atom.config.get('foo.bar.aString')).toBe 'ok' + expect(atom.config.set('foo.bar.aString', true)).toBe false + expect(atom.config.get('foo.bar.aString')).toBe '123' + + expect(atom.config.set('foo.bar.aString', null)).toBe false + expect(atom.config.get('foo.bar.aString')).toBe '123' + + expect(atom.config.set('foo.bar.aString', [])).toBe false + expect(atom.config.get('foo.bar.aString')).toBe '123' + + expect(atom.config.set('foo.bar.aString', nope: 'nope')).toBe false + expect(atom.config.get('foo.bar.aString')).toBe '123' describe 'when the value has an "object" type', -> beforeEach -> diff --git a/src/config.coffee b/src/config.coffee index e9e048602..1f23d7a5d 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -390,7 +390,8 @@ class Config # This value is stored in Atom's internal configuration file. # # * `keyPath` The {String} name of the key. - # * `value` The value of the setting. + # * `value` The value of the setting. Passing `undefined` will revert the + # setting to the default value. # # Returns a {Boolean} # * `true` if the value was set. @@ -672,8 +673,11 @@ Config.addSchemaValidators 'string': coercion: (keyPath, value, schema) -> - throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string") if typeof value isnt 'string' - value + switch typeof value + when 'number', 'string' + value.toString() + else + throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string or number") 'null': # null sort of isnt supported. It will just unset in this case