增加json数据验证文件,将json校验单独提取出文件

This commit is contained in:
wanglei 2025-10-30 09:03:44 +08:00
parent 4022a395c8
commit 899496bd0d
14 changed files with 232 additions and 116 deletions

View File

@ -3,24 +3,14 @@
--- Created by admin.
--- DateTime: 2025/10/28 11:09
---
local jsonschema = require("jsonschema")
local resp = require("util.response")
local authDao = require("dao.auth")
local jwt = require("resty.jwt")
local conf = require("config")
local validatorJson = require("validator.system.auth")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "password", type = "string"},
{name = "captcha", type = "string"},
{name = "checkKey", type = "string"},
}, required = {"username", "password"}}
}
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
@ -40,8 +30,8 @@ function _M.login()
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -71,8 +61,10 @@ function _M.logout()
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--判断请求体数据是否为空
if body_data == nil then
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return

View File

@ -3,25 +3,12 @@
--- Created by .
--- DateTime: 2025/9/25 08:25
--- 业务逻辑 对账户数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local accountDao = require("dao.account")
local validatorJson = require("validator.system.account")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "password", type = "string"},
{name = "real_name", type = "string"},
{name = "department", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"username"}}
}
--获取所有账户信息
function _M.getSystemAccounts()
local pageNum = ngx.var.pagenum or 1
@ -46,7 +33,7 @@ function _M.addSystemAccount()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -72,8 +59,10 @@ function _M.updateSystemAccount(m)
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--判断请求体数据是否为空
if body_data == nil then
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return

View File

@ -3,27 +3,12 @@
--- Created by .
--- DateTime: 2025/9/27 16:02
--- 业务逻辑 对应用数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local applicationDao = require("dao.application")
local validatorJson = require("validator.system.application")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "display_name", type = "string"},
{name = "logo", type = "string"},
{name = "title", type = "string"},
{name = "name", type = "string"},
{name = "favicon", type = "string"},
{name = "description", type = "string"},
{name = "homepage_url", type = "string"},
{name = "redirect_uris", type = "string"},
}, required = {"name", "redirect_uris"}}
}
--获取所有应用程序信息
function _M.getSystemApplications()
--获取页码和请求的数据量
@ -62,7 +47,7 @@ function _M.addSystemApplication()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -89,7 +74,7 @@ function _M.updateSystemApplication(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -3,26 +3,12 @@
--- Created by .
--- DateTime: 2025/9/28 10:22
--- 业务逻辑 对组织架构数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local departmentDao = require("dao.department")
local validatorJson = require("validator.system.department")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "seq", type = "string"},
{name = "description", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
{name = "parent_id", type = "string"},
{name = "leader", type = "string"},
{name = "status", type = "string"},
}, required = {"name"}}
}
--获取所有组织架构信息
function _M.getSystemDepartments()
--获取页码和请求的数据量
@ -47,7 +33,7 @@ function _M.addSystemDepartment()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -74,7 +60,7 @@ function _M.updateSystemDepartment(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -3,26 +3,12 @@
--- Created by .
--- DateTime: 2025/9/27 17:06
--- 业务逻辑 对权限数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local permissionDao = require("dao.permission")
local validatorJson = require("validator.system.permission")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "phone", type = "string"},
{name = "email", type = "string"},
{name = "idcard", type = "string"},
{name = "name", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"username", "phone", "email", "idcard"}}
}
--获取所有权限信息
function _M.getSystemPermissions()
--获取页码和请求的数据量
@ -54,7 +40,7 @@ function _M.addSystemPermission()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -81,7 +67,7 @@ function _M.updateSystemPermission(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -3,22 +3,12 @@
--- Created by .
--- DateTime: 2025/9/27 15:19
--- 业务逻辑 对用户角色数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local roleDao = require("dao.role")
local validatorJson = require("validator.system.role")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "role_name", type = "string"},
{name = "description", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
}, required = {"role_name"}}
}
--获取所有角色信息
function _M.getSystemRoles()
--获取页码和请求的数据量
@ -44,7 +34,7 @@ function _M.addSystemRole()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -71,7 +61,7 @@ function _M.updateSystemRole(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -3,26 +3,12 @@
--- Created by .
--- DateTime: 2025/9/25 08:19
--- 业务逻辑 对用户数据表进行数据表业务处理
local jsonschema = require("jsonschema")
local resp = require("util.response")
local userDao = require("dao.user")
local validatorJson = require("validator.system.user")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "phone", type = "string"},
{name = "email", type = "string"},
{name = "idcard", type = "string"},
{name = "name", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"username", "phone", "email", "idcard"}}
}
--获取所有用户信息
function _M.getSystemUsers()
--获取页码和请求的数据量
@ -47,8 +33,8 @@ function _M.addSystemUser()
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -74,8 +60,8 @@ function _M.updateSystemUser(m)
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -0,0 +1,26 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/30 08:09
---业务逻辑 对账户登录的参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "password", type = "string"},
{name = "captcha", type = "string"},
{name = "checkKey", type = "string"},
}, required = {"username", "password"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,29 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:18
--- 业务逻辑 对账户数据参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "password", type = "string"},
{name = "real_name", type = "string"},
{name = "department", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"name"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,31 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:14
--- 业务逻辑 对应用数据参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "display_name", type = "string"},
{name = "logo", type = "string"},
{name = "title", type = "string"},
{name = "name", type = "string"},
{name = "favicon", type = "string"},
{name = "description", type = "string"},
{name = "homepage_url", type = "string"},
{name = "redirect_uris", type = "string"},
}, required = {"name", "redirect_uris"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,30 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:12
--- 业务逻辑 对组织架构数据进行参数数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "name", type = "string"},
{name = "seq", type = "string"},
{name = "description", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
{name = "parent_id", type = "string"},
{name = "leader", type = "string"},
{name = "status", type = "string"},
}, required = {"name"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,30 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:11
--- 业务逻辑 对权限参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "phone", type = "string"},
{name = "email", type = "string"},
{name = "idcard", type = "string"},
{name = "name", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"username", "phone", "email", "idcard"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,26 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:13
--- 业务逻辑 对用户角色参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "role_name", type = "string"},
{name = "description", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
}, required = {"role_name"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M

View File

@ -0,0 +1,30 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/10/30 08:19
--- 业务逻辑 对用户参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "phone", type = "string"},
{name = "email", type = "string"},
{name = "idcard", type = "string"},
{name = "name", type = "string"},
{name = "office_phone", type = "string"},
{name = "telephone", type = "string"},
{name = "display_name", type = "string"},
}, required = {"username", "phone", "email", "idcard"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(jsonData, schema)
return ok
end
return _M