Make boolean schema validator a little tighter

This commit is contained in:
Ben Ogle
2014-09-25 12:52:44 -07:00
parent 6b4ce902ba
commit 800dee09ba
3 changed files with 22 additions and 12 deletions

View File

@@ -645,14 +645,18 @@ describe "Config", ->
atom.config.set('foo.bar.aBool', 'FALSE')
expect(atom.config.get('foo.bar.aBool')).toBe false
atom.config.set('foo.bar.aBool', 1)
expect(atom.config.get('foo.bar.aBool')).toBe true
expect(atom.config.get('foo.bar.aBool')).toBe false
atom.config.set('foo.bar.aBool', 0)
expect(atom.config.get('foo.bar.aBool')).toBe false
atom.config.set('foo.bar.aBool', {})
expect(atom.config.get('foo.bar.aBool')).toBe true
expect(atom.config.get('foo.bar.aBool')).toBe false
atom.config.set('foo.bar.aBool', null)
expect(atom.config.get('foo.bar.aBool')).toBe false
# unset
atom.config.set('foo.bar.aBool', undefined)
expect(atom.config.get('foo.bar.aBool')).toBe true
describe 'when the value has an "string" type', ->
beforeEach ->
schema =
@@ -691,7 +695,7 @@ describe "Config", ->
atom.config.set 'foo.bar',
anInt: '23'
nestedObject:
nestedBool: 't'
nestedBool: 'true'
expect(atom.config.get('foo.bar')).toEqual
anInt: 23
nestedObject:

View File

@@ -100,14 +100,14 @@ module.exports =
type: 'object'
properties:
eol:
type: ['string', 'boolean']
type: ['boolean', 'string']
default: '\u00ac'
space:
type: ['string', 'boolean']
type: ['boolean', 'string']
default: '\u00b7'
tab:
type: ['string', 'boolean']
type: ['boolean', 'string']
default: '\u00bb'
cr:
type: ['string', 'boolean']
type: ['boolean', 'string']
default: '\u00a4'

View File

@@ -154,9 +154,8 @@ pathWatcher = require 'pathwatcher'
#
# #### boolean
#
# Values will be coerced into a Boolean. `'true'` and `'t'` will be coerced into
# `true`. Numbers, arrays, objects, and anything else will be coerced via
# `!!value`.
# Values will be coerced into a Boolean. `'true'` and `'false'` will be coerced into
# a boolean. Numbers, arrays, objects, and anything else will not be coerced.
#
# ```coffee
# config:
@@ -647,9 +646,16 @@ Config.addSchemaValidators
coercion: (keyPath, value, schema) ->
switch typeof value
when 'string'
value.toLowerCase() in ['true', 't']
if value.toLowerCase() in ['true']
true
else if value.toLowerCase() in ['false']
false
else
throw new Error("Cannot coerce #{keyPath}, #{JSON.stringify(value)} must be a boolean or the string 'true' or 'false'")
when 'boolean'
value
else
!!value
throw new Error("Cannot coerce #{keyPath}, #{JSON.stringify(value)} must be a boolean or the string 'true' or 'false'")
'string':
coercion: (keyPath, value, schema) ->