summaryrefslogtreecommitdiffstats
path: root/acf/model/field.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/model/field.lua')
-rw-r--r--acf/model/field.lua26
1 files changed, 20 insertions, 6 deletions
diff --git a/acf/model/field.lua b/acf/model/field.lua
index d2e62a5..e1bdeaf 100644
--- a/acf/model/field.lua
+++ b/acf/model/field.lua
@@ -1,10 +1,13 @@
--[[
-Copyright (c) 2012 Kaarle Ritvanen
+Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
module(..., package.seeall)
+local err = require('acf.error')
+local raise = err.raise
+
local node = require('acf.model.node')
local object = require('acf.object')
@@ -63,10 +66,10 @@ end
function Field:_validate(txn, path, value)
if self.required and value == nil then
- error('Required value not set: '..path)
+ raise(path, 'Required value not set')
end
if self.choice and value ~= nil and not contains(self.choice, value) then
- error('Invalid value for '..path..': '..value)
+ raise(path, 'Invalid value')
end
if value ~= nil then self:validate(txn, path, value) end
return value
@@ -92,16 +95,27 @@ function TreeNode:save(txn, path, addr, value)
if object.isinstance(value, node.TreeNode) then
-- TODO clone if TreeNode has wrong path
- assert(node.path(value) == path)
+ if node.path(value) ~= path then
+ raise(path, 'Attempted to assign foreign object as value')
+ end
return
end
txn:set(addr)
+
if value then
- assert(type(value) == 'table')
+ if type(value) ~= 'table' then
+ raise(path, 'Cannot assign primitive value')
+ end
+
txn:set(addr, 'table')
local new = self:load(txn, path, addr, true)
- for k, v in pairs(value) do new[k] = v end
+
+ local errors = err.ErrorDict()
+ for k, v in pairs(value) do
+ errors:collect(function() new[k] = v end)
+ end
+ errors:raise()
end
end