       defaults
 
   scrubValue: (keyPath, value) ->
-    value = @constructor.executeSchemaValidators(value, schema) if schema = @getSchema(keyPath)
+    value = @constructor.executeSchemaValidators(keyPath, value, schema) if schema = @getSchema(keyPath)
     value
 
 # Base schema validators. These will coerce raw input into the specified type,

          !!value

  'string':
    coercion: (value, schema) ->
      throw new Error('Value must be a string') if typeof value isnt 'string'
      value

  'null':
    # null sort of isnt supported. It will just unset in this case
    coercion: (value, schema) ->
      throw new Error('Value must be an object') unless value == null
      value

  'object':
    coercion: (value, schema) ->
      throw new Error('Value must be an object') if typeof value isnt 'object'
      return value unless schema.properties?
      newValue = {}
      for prop, childSchema of schema.properties
        continue unless prop of value
        try
          newValue[prop] = @executeSchemaValidators(value[prop], childSchema)
        catch error
          console.warn "Error setting value #{error.message}"
      newValue

  'array':
    coercion: (value, schema) ->
      throw new Error('Value must be an array') unless Array.isArray(value)
      itemSchema = schema.items
      if itemSchema?
        newValue = []
        for item in value
          try
            newValue.push @executeSchemaValidators(item, itemSchema)
          catch error
            console.warn "Error setting value: #{error.message}"
        newValue
      else
        value

  '*':
    minimumAndMaximumCoercion: (value, schema) ->
      return value unless typeof value is 'number'
      if schema.minimum? and typeof schema.minimum is 'number'
        value = Math.max(value, schema.minimum)

# specification.
Config.addSchemaValidators
  'integer':
    coercion: (keyPath, value, schema) ->
      value = parseInt(value)
      throw new Error("Cannot set #{keyPath}, #{JSON.stringify(value)} cannot be coerced into an int") if isNaN(value)
      value

  'number':
    coercion: (keyPath, value, schema) ->
      value = parseFloat(value)
      throw new Error("Cannot set #{keyPath}, #{JSON.stringify(value)} cannot be coerced into a number") if isNaN(value)
      value

  'boolean':
    coercion: (keyPath, value, schema) ->
      switch typeof value
        when 'string'
          value.toLowerCase() in ['true', 't']
