From 69067579e5785deabcaef68c84e3c36df3710526 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 31 Mar 2021 22:15:38 +0200 Subject: [PATCH] schema: key & value checks before entry checks --- debug.lua | 34 ++++++++++++++++++++++++++++++++++ schema.lua | 20 ++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 debug.lua diff --git a/debug.lua b/debug.lua new file mode 100644 index 0000000..e357c47 --- /dev/null +++ b/debug.lua @@ -0,0 +1,34 @@ +local function gather_info() + local locals = {} + local index = 1 + while true do + local name, value = debug.getlocal(2, index) + if not name then break end + table.insert(locals, {name, value}) + index = index + 1 + end + local upvalues = {} + local func = debug.getinfo(2).func + local envs = getfenv(func) + index = 1 + while true do + local name, value = debug.getupvalue(func, index) + if not name then break end + table.insert(upvalues, {name, value}) + index = index + 1 + end + return { + locals = locals, + upvalues = upvalues, + [envs == _G and "globals" or "envs"] = envs + } +end + +local c = 3 +function test() + local a = 1 + b = 2 + error(gather_info().upvalues[1][1]) +end + +test() \ No newline at end of file diff --git a/schema.lua b/schema.lua index b378498..fdb610e 100644 --- a/schema.lua +++ b/schema.lua @@ -258,6 +258,16 @@ function load(self, override, params) assert(self.nan or override == override, "nan") end elseif self.type == "table" then + if self.keys then + for key, value in pairs(override) do + override[load(self.keys, key, params)], override[key] = value, nil + end + end + if self.values then + for key, value in pairs(override) do + override[key] = load(self.values, value, params) + end + end if self.entries then for key, schema in pairs(self.entries) do if schema.required and override[key] == nil then @@ -273,16 +283,6 @@ function load(self, override, params) end end end - if self.keys then - for key, value in pairs(override) do - override[load(self.keys, key, params)], override[key] = value, nil - end - end - if self.values then - for key, value in pairs(override) do - override[key] = load(self.values, value, params) - end - end assert((not self.list) or modlib.table.count(override) == #override, "list") else assert((not self.values) or self.values[override], "values")