diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-11-30 22:35:33 +0200 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-12-03 23:30:18 +0200 |
commit | 4fd4280e0fdef0903a4ce00debb3f94c67c52d41 (patch) | |
tree | 46bb4dd54ee959e9c64cfae56a631b3d26f64166 | |
parent | f8feba97a9ea3b1718f8c2a4a218f4a1a54711dd (diff) | |
download | aconf-4fd4280e0fdef0903a4ce00debb3f94c67c52d41.tar.bz2 aconf-4fd4280e0fdef0903a4ce00debb3f94c67c52d41.tar.xz |
model: field nomalization method
-rw-r--r-- | acf2/model/aaa.lua | 8 | ||||
-rw-r--r-- | acf2/model/field.lua | 20 | ||||
-rw-r--r-- | acf2/model/init.lua | 6 | ||||
-rw-r--r-- | acf2/model/set.lua | 2 |
4 files changed, 16 insertions, 20 deletions
diff --git a/acf2/model/aaa.lua b/acf2/model/aaa.lua index c8a8328..7de4cfb 100644 --- a/acf2/model/aaa.lua +++ b/acf2/model/aaa.lua @@ -4,7 +4,6 @@ See LICENSE file for license details --]] local M = require('acf2.model') -local object = require('acf2.object') local digest = require('crypto').digest @@ -20,11 +19,10 @@ end local hash_pattern = '^(%w+)%$(%w+)%$%x+$' -local Password = object.class(M.String) +local Password = require('acf2.object').class(M.String) -function Password:_validate(context, value) - value = object.super(self, M.String):_validate(context, value) - if not value or value:find(hash_pattern) then return value end +function Password:normalize(context, value) + if value:find(hash_pattern) then return value end local salt = '' for i = 1,12 do 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 diff --git a/acf2/model/init.lua b/acf2/model/init.lua index 6b89384..17f9ff4 100644 --- a/acf2/model/init.lua +++ b/acf2/model/init.lua @@ -111,11 +111,7 @@ function M.Reference:load(context, options) ) and self:follow(context, ref) or ref end -function M.Reference:_validate(context, value) - super(self, M.Reference):_validate(context, value) - - if value == nil then return end - +function M.Reference:normalize(context, value) if isinstance(value, node.TreeNode) then value = node.path(value) end local path = context.path diff --git a/acf2/model/set.lua b/acf2/model/set.lua index 0e73b38..808dc58 100644 --- a/acf2/model/set.lua +++ b/acf2/model/set.lua @@ -42,7 +42,7 @@ function M.Set:init(context, params) function mt.contains(v) return find( - node.BoundMember(self, pth.wildcard, params.field):_validate(v) + node.BoundMember(self, pth.wildcard, params.field):normalize(v) ) and true or false end |