Handle minimum and maximum keywords on number types

This commit is contained in:
Ben Ogle
2014-09-24 09:47:54 -07:00
parent ac67430926
commit f7f28e7995
2 changed files with 47 additions and 1 deletions

View File

@@ -404,12 +404,26 @@ Config.addTypeFilters
throw new Error() if isNaN(value)
value
minMaxCoercion: (value, schema) ->
if schema.minimum? and typeof schema.minimum is 'number'
value = Math.max(value, schema.minimum)
if schema.maximum? and typeof schema.maximum is 'number'
value = Math.min(value, schema.maximum)
value
'number':
coercion: (value, schema) ->
value = parseFloat(value)
throw new Error() if isNaN(value)
value
minMaxCoercion: (value, schema) ->
if schema.minimum? and typeof schema.minimum is 'number'
value = Math.max(value, schema.minimum)
if schema.maximum? and typeof schema.maximum is 'number'
value = Math.min(value, schema.maximum)
value
'boolean':
coercion: (value, schema) ->
switch typeof value
@@ -419,7 +433,7 @@ Config.addTypeFilters
!!value
'string':
typeCheck: (value, schema) ->
coercion: (value, schema) ->
throw new Error() if typeof value isnt 'string'
value