 BadEntityTypeError.prototype.__proto__ = Error.prototype
 
 
+BadDataError = (message) ->
+	error = new Error(message)
+	error.name = 'BadData'
+	error.__proto__ = BadDataError.prototype
+	return error
+BadDataError.prototype.__proto__ = Error.prototype
+
+
 module.exports = ProjectFileAgent =
 
 	sanitizeData: (data) ->
-		# TODO:
-		#   - Nothing?
 		return data
+
+	_validate: (data) ->
+		return (
+			!!data.source_project_id &&
+			!!data.source_entity_path &&
+			!!data.source_project_display_name
+		)
 
 	writeIncomingFileToDisk:
 		(project_id, data, current_user_id, callback = (error, fsPath) ->) ->
 			callback = _.once(callback)
+			if !ProjectFileAgent._validate(data)
+				return callback(new BadDataError())
 			{source_project_id, source_entity_path} = data
 			AuthorizationManager.canUserReadProject current_user_id, source_project_id,
 				null, (err, canRead) ->

	handleError: (error, req, res, next) ->
		if error instanceof AccessDeniedError
			res.status(403).send("You do not have access to this project")
		else if error instanceof FileNotFoundError
			res.status(404).send("The file does not exist")
		else if error instanceof BadEntityTypeError
			res.status(404).send("The file is the wrong type")  # TODO: better error message
		else
			next(error)
		next()

	return error
AccessDeniedError.prototype.__proto__ = Error.prototype


BadEntityTypeError = (message) ->
	error = new Error(message)
	error.name = 'BadEntityType'
