Pass a message to the errors thrown by validators

This commit is contained in:
Ben Ogle 2014-09-24 12:21:07 -07:00
parent 2c1190b552
commit ba4df1b002

View File

@ -402,13 +402,13 @@ Config.addSchemaValidators
'integer': 'integer':
coercion: (value, schema) -> coercion: (value, schema) ->
value = parseInt(value) value = parseInt(value)
throw new Error() if isNaN(value) throw new Error('Value cannot be coerced into an int') if isNaN(value)
value value
'number': 'number':
coercion: (value, schema) -> coercion: (value, schema) ->
value = parseFloat(value) value = parseFloat(value)
throw new Error() if isNaN(value) throw new Error('Value cannot be coerced into a number') if isNaN(value)
value value
'boolean': 'boolean':
@ -421,18 +421,18 @@ Config.addSchemaValidators
'string': 'string':
coercion: (value, schema) -> coercion: (value, schema) ->
throw new Error() if typeof value isnt 'string' throw new Error('Value must be a string') if typeof value isnt 'string'
value value
'null': 'null':
# null sort of isnt supported. It will just unset in this case # null sort of isnt supported. It will just unset in this case
coercion: (value, schema) -> coercion: (value, schema) ->
throw new Error() unless value == null throw new Error('Value must be an object') unless value == null
value value
'object': 'object':
coercion: (value, schema) -> coercion: (value, schema) ->
throw new Error() if typeof value isnt 'object' throw new Error('Value must be an object') if typeof value isnt 'object'
return value unless schema.properties? return value unless schema.properties?
for prop, childSchema of schema.properties for prop, childSchema of schema.properties
value[prop] = @executeSchemaValidators(value[prop], childSchema) if prop of value value[prop] = @executeSchemaValidators(value[prop], childSchema) if prop of value
@ -440,7 +440,7 @@ Config.addSchemaValidators
'array': 'array':
coercion: (value, schema) -> coercion: (value, schema) ->
throw new Error() unless Array.isArray(value) throw new Error('Value must be an array') unless Array.isArray(value)
itemSchema = schema.items itemSchema = schema.items
if itemSchema? if itemSchema?
@executeSchemaValidators(item, itemSchema) for item in value @executeSchemaValidators(item, itemSchema) for item in value