From dae9a662c23724749d78cda42f9f14bd4b450b66 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 26 Mar 2014 16:59:47 +1000 Subject: Write, use, and test isValid(). --- ClientHamr/GuiLua/skang.lua | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'ClientHamr/GuiLua/skang.lua') diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index f88ff1c..cb97a76 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua @@ -217,10 +217,17 @@ Thing.append = function (self,data) -- Append to the value of this Thing. end Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors. - self.errors = {} - -- TODO - Should check for required, matching mask, matching type, etc. - return true - end + -- Anything that overrides this method, should call this super method first. + local value = self.module[self.names[1] ] + self.errors = {} + if 'nil' == type(value) then + if self.required then table.insert(self.errors, self.names[1] .. ' is required!') end + else + if self.types[1] ~= type(value) then table.insert(self.errors, self.names[1] .. ' should be a ' .. self.types[1] .. ', but it is a ' .. type(value) .. '!') end + end + -- TODO - Should check for matching mask, and anything else. + return #(self.errors) == 0 +end Thing.remove = function (self) -- Delete this Thing. end @@ -264,8 +271,13 @@ Thing.__newindex = function (table, key, value) end print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help) else - thing:isValid() - print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) + -- NOTE - invalid values are still stored, this is by design. + if not thing:isValid() then + for i, v in ipairs(thing.errors) do + print('ERROR - ' .. v) + end + end +-- print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) -- TODO - Go through it's linked things and set them to. end else -- cgit v1.1