diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/datamapper.lua | 37 | ||||
-rw-r--r-- | util/jsonschema.lua | 276 |
2 files changed, 168 insertions, 145 deletions
diff --git a/util/datamapper.lua b/util/datamapper.lua index 25c4c986..2378314c 100644 --- a/util/datamapper.lua +++ b/util/datamapper.lua @@ -1,3 +1,5 @@ +-- This file is generated from teal-src/util/datamapper.lua + local st = require("util.stanza"); local pointer = require("util.jsonpointer"); @@ -29,15 +31,28 @@ end local value_goes = {} local function resolve_schema(schema, root) - if type(schema) == "table" and schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then - local referenced = pointer.resolve(root, schema["$ref"]:sub(2)); - if referenced ~= nil then - return referenced + if type(schema) == "table" then + if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then + return pointer.resolve(root, schema["$ref"]:sub(2)) end end return schema end +local function guess_schema_type(schema) + local schema_types = schema.type + if type(schema_types) == "string" then + return schema_types + elseif schema_types ~= nil then + error("schema has unsupported 'type' property") + elseif schema.properties then + return "object" + elseif schema.items then + return "array" + end + return "string" +end + local function unpack_propschema(propschema, propname, current_ns) local proptype = "string" @@ -49,9 +64,9 @@ local function unpack_propschema(propschema, propname, current_ns) local enums if type(propschema) == "table" then - proptype = propschema.type + proptype = guess_schema_type(propschema); elseif type(propschema) == "string" then - proptype = propschema + error("schema as string is not supported: " .. propschema .. " {" .. current_ns .. "}" .. propname) end if proptype == "object" or proptype == "array" then @@ -209,9 +224,10 @@ function parse_array(schema, s, root) end local function parse(schema, s) - if schema.type == "object" then + local s_type = guess_schema_type(schema) + if s_type == "object" then return parse_object(schema, s, schema) - elseif schema.type == "array" then + elseif s_type == "array" then return parse_array(schema, s, schema) else error("top-level scalars unsupported") @@ -306,7 +322,8 @@ function unparse(schema, t, current_name, current_ns, ctx, root) local out = ctx or st.stanza(current_name, {xmlns = current_ns}) - if schema.type == "object" then + local s_type = guess_schema_type(schema) + if s_type == "object" then for prop, propschema in pairs(schema.properties) do propschema = resolve_schema(propschema, root) @@ -319,7 +336,7 @@ function unparse(schema, t, current_name, current_ns, ctx, root) end return out - elseif schema.type == "array" then + elseif s_type == "array" then local itemschema = resolve_schema(schema.items, root) local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(itemschema, current_name, current_ns) for _, item in ipairs(t) do diff --git a/util/jsonschema.lua b/util/jsonschema.lua index 8286fa19..48a3351f 100644 --- a/util/jsonschema.lua +++ b/util/jsonschema.lua @@ -1,3 +1,5 @@ +-- This file is generated from teal-src/util/jsonschema.lua + local m_type = math.type or function (n) return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; end; @@ -10,12 +12,12 @@ local json_type_name = json.json_type_name local schema_t = {} -local json_schema_object = {xml_t = {}} - -local type_validators = {} +local json_schema_object = { xml_t = {} } local function simple_validate(schema, data) - if schema == "object" and type(data) == "table" then + if schema == nil then + return true + elseif schema == "object" and type(data) == "table" then return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "string") elseif schema == "array" and type(data) == "table" then return type(data) == "table" and (next(data) == nil or type((next(data, nil))) == "number") @@ -23,143 +25,169 @@ local function simple_validate(schema, data) return m_type(data) == schema elseif schema == "null" then return data == null + elseif type(schema) == "table" then + for _, one in ipairs(schema) do + if simple_validate(one, data) then + return true + end + end + return false else return type(data) == schema end end -type_validators.string = function(schema, data) +local complex_validate - if type(data) == "string" then - if schema.maxLength and #data > schema.maxLength then - return false - end - if schema.minLength and #data < schema.minLength then - return false - end - return true +local function validate(schema, data, root) + if type(schema) == "boolean" then + return schema + else + return complex_validate(schema, data, root) end - return false end -type_validators.number = function(schema, data) - if schema.multipleOf and data % schema.multipleOf ~= 0 then - return false - end +function complex_validate(schema, data, root) - if schema.maximum and not (data <= schema.maximum) then - return false + if root == nil then + root = schema end - if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then - return false + if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then + local referenced = pointer.resolve(root, schema["$ref"]:sub(2)) + if referenced ~= nil and referenced ~= root and referenced ~= schema then + if not validate(referenced, data, root) then + return false + end + end end - if schema.minimum and not (data >= schema.minimum) then + if not simple_validate(schema.type, data) then return false end - if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then - return false + if schema.type == "object" then + if type(data) == "table" then + + for k in pairs(data) do + if not (type(k) == "string") then + return false + end + end + end end - return true -end + if schema.type == "array" then + if type(data) == "table" then -type_validators.integer = type_validators.number + for i in pairs(data) do + if not (math.type(i) == "integer") then + return false + end + end + end + end -local function validate(schema, data, root) - if type(schema) == "boolean" then - return schema + if schema["enum"] ~= nil then + local match = false + for _, v in ipairs(schema["enum"]) do + if v == data then + + match = true + break + end + end + if not match then + return false + end end - if type(schema) == "string" then - return simple_validate(schema, data) + + if type(data) == "string" then + if schema.maxLength and #data > schema.maxLength then + return false + end + if schema.minLength and #data < schema.minLength then + return false + end end - if type(schema) == "table" then - if root == nil then - root = schema + + if type(data) == "number" then + if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then + return false end - if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then - local referenced = pointer.resolve(root, schema["$ref"]:sub(2)) - if referenced ~= nil then - return validate(referenced, data, root) - end + + if schema.maximum and not (data <= schema.maximum) then + return false end - if schema.allOf then - for _, sub in ipairs(schema.allOf) do - if not validate(sub, data, root) then - return false - end - end - return true + if schema.exclusiveMaximum and not (data < schema.exclusiveMaximum) then + return false end - if schema.oneOf then - local valid = 0 - for _, sub in ipairs(schema.oneOf) do - if validate(sub, data, root) then - valid = valid + 1 - end - end - return valid == 1 + if schema.minimum and not (data >= schema.minimum) then + return false end - if schema.anyOf then - for _, sub in ipairs(schema.anyOf) do - if validate(sub, data, root) then - return true - end - end + if schema.exclusiveMinimum and not (data > schema.exclusiveMinimum) then return false end + end - if schema["not"] then - if validate(schema["not"], data, root) then + if schema.allOf then + for _, sub in ipairs(schema.allOf) do + if not validate(sub, data, root) then return false end end + end - if schema["if"] then - if validate(schema["if"], data, root) then - if schema["then"] then - return validate(schema["then"], data, root) - end - else - if schema["else"] then - return validate(schema["else"], data, root) - end + if schema.oneOf then + local valid = 0 + for _, sub in ipairs(schema.oneOf) do + if validate(sub, data, root) then + valid = valid + 1 end end - - if schema.const ~= nil and schema.const ~= data then + if valid ~= 1 then return false end + end - if schema["enum"] ~= nil then - for _, v in ipairs(schema["enum"]) do - if v == data then - return true - end + if schema.anyOf then + local match = false + for _, sub in ipairs(schema.anyOf) do + if validate(sub, data, root) then + match = true + break end + end + if not match then return false end + end - if schema.type then - if not simple_validate(schema.type, data) then - return false - end + if schema["not"] then + if validate(schema["not"], data, root) then + return false + end + end - local validator = type_validators[schema.type] - if validator then - return validator(schema, data, root) + if schema["if"] ~= nil then + if validate(schema["if"], data, root) then + if schema["then"] then + return validate(schema["then"], data, root) + end + else + if schema["else"] then + return validate(schema["else"], data, root) end end - return true end -end -type_validators.table = function(schema, data, root) + if schema.const ~= nil and schema.const ~= data then + return false + end + if type(data) == "table" then if schema.maxItems and #data > schema.maxItems then @@ -178,24 +206,28 @@ type_validators.table = function(schema, data, root) end end - if schema.properties then - local additional = schema.additionalProperties or true - for k, v in pairs(data) do - if schema.propertyNames and not validate(schema.propertyNames, k, root) then + if schema.propertyNames ~= nil then + for k in pairs(data) do + if not validate(schema.propertyNames, k, root) then return false end - local s = schema.properties[k] or additional - if not validate(s, v, root) then + end + end + + if schema.properties then + for k, sub in pairs(schema.properties) do + if data[k] ~= nil and not validate(sub, data[k], root) then return false end end - elseif schema.additionalProperties then + end + + if schema.additionalProperties ~= nil then for k, v in pairs(data) do - if schema.propertyNames and not validate(schema.propertyNames, k, root) then - return false - end - if not validate(schema.additionalProperties, v, root) then - return false + if schema.properties == nil or schema.properties[k] == nil then + if not validate(schema.additionalProperties, v, root) then + return false + end end end end @@ -212,9 +244,11 @@ type_validators.table = function(schema, data, root) end local p = 0 - if schema.prefixItems then + if schema.prefixItems ~= nil then for i, s in ipairs(schema.prefixItems) do - if validate(s, data[i], root) then + if data[i] == nil then + break + elseif validate(s, data[i], root) then p = i else return false @@ -222,7 +256,7 @@ type_validators.table = function(schema, data, root) end end - if schema.items then + if schema.items ~= nil then for i = p + 1, #data do if not validate(schema.items, data[i], root) then return false @@ -230,7 +264,7 @@ type_validators.table = function(schema, data, root) end end - if schema.contains then + if schema.contains ~= nil then local found = false for i = 1, #data do if validate(schema.contains, data[i], root) then @@ -242,37 +276,9 @@ type_validators.table = function(schema, data, root) return false end end - - return true end - return false -end -type_validators.object = function(schema, data, root) - if type(data) == "table" then - for k in pairs(data) do - if not (type(k) == "string") then - return false - end - end - - return type_validators.table(schema, data, root) - end - return false -end - -type_validators.array = function(schema, data, root) - if type(data) == "table" then - - for i in pairs(data) do - if not (type(i) == "number") then - return false - end - end - - return type_validators.table(schema, data, root) - end - return false + return true end json_schema_object.validate = validate; |