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

@@ -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) ->