summaryrefslogtreecommitdiffstats
path: root/acf2/model/field.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-11-30 22:35:33 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-12-03 23:30:18 +0200
commit4fd4280e0fdef0903a4ce00debb3f94c67c52d41 (patch)
tree46bb4dd54ee959e9c64cfae56a631b3d26f64166 /acf2/model/field.lua
parentf8feba97a9ea3b1718f8c2a4a218f4a1a54711dd (diff)
downloadaconf-4fd4280e0fdef0903a4ce00debb3f94c67c52d41.tar.bz2
aconf-4fd4280e0fdef0903a4ce00debb3f94c67c52d41.tar.xz
model: field nomalization method
Diffstat (limited to 'acf2/model/field.lua')
-rw-r--r--acf2/model/field.lua20
1 files changed, 11 insertions, 9 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua
index c31e6be..d2470ca 100644
--- a/acf2/model/field.lua
+++ b/acf2/model/field.lua
@@ -113,18 +113,23 @@ end
function M.Field:_load(context) return context.txn:get(context.addr) end
function M.Field:_validate(context, value)
- if self.required and value == nil then
- raise(context.path, 'Required value not set')
+ if value == nil then
+ if self.required then raise(context.path, 'Required value not set') end
+ return
end
- if self.choice and value ~= nil and not util.contains(
+
+ value = self:normalize(context, value)
+ if self.choice and not util.contains(
map(function(ch) return ch.value end, self.choice), value
) then
raise(context.path, 'Invalid value')
end
- if value ~= nil then self:validate(context, value) end
+ self:validate(context, value)
return value
end
+function M.Field:normalize(context, value) return value end
+
function M.Field:validate(context, value) end
function M.Field:save(context, value)
@@ -178,11 +183,8 @@ function M.Number:init(params)
self.dtype = 'number'
end
-function M.Number:_validate(context, value)
- return super(self, M.Number):_validate(
- context,
- value and tonumber(value) or value
- )
+function M.Number:normalize(context, value)
+ return value and tonumber(value) or value
end