【发布时间】:2019-10-17 12:06:46
【问题描述】:
我一直在寻找超过 4 天,但我还没有找到对基于 lua 的 json 模式编译器的代码的很多支持。主要是我一直在处理
- ljsonschema (https://github.com/jdesgats/ljsonschema)
- rjson (https://luarocks.org/modules/romaboy/rjson)
但是以上任何一个都没有直接使用。
在处理了luarocks 上的问题后,我终于让ljsonschema 工作了,但是JSON 语法看起来与普通的JSON 结构不同——例如:用等号代替分号,键名没有双引号等。
ljsonschema 支持
{ type = 'object', properties = {
foo = { type = 'string' },
bar = { type = 'number' },},}
我需要:
{ "type" : "object",
"properties" : {
"foo" : { "type" : "string" },
"bar" : { "type" : "number" }}}
对于rjson,安装位置本身存在问题。虽然安装顺利,但在运行 lua 代码时永远无法找到 .so 文件。另外,我找不到太多的开发支持。
请帮助指出正确的方向,以防我遗漏了什么。 我有 json 架构和一个示例 json,我只需要一个 lua 代码来帮助围绕它编写一个程序。
这是为 Kong CE 编写自定义 JSON 验证插件。
更新: 我希望下面的代码与 ljsonschema 一起使用:
local jsonschema = require 'jsonschema'
-- Note: do cache the result of schema compilation as this is a quite
-- expensive process
local myvalidator = jsonschema.generate_validator{
"type" : "object",
"properties" : {
"foo" : { "type" : "string" },
"bar" : { "type" : "number" }
}
}
print(myvalidator { "foo":"hello", "bar":42 })
但我得到了错误:'}' expected (to close '{' at line 5) near ':'
【问题讨论】:
-
你有尝试使用
ljsonschema的例子吗? -
@Nifim:请参阅更新部分。我已经添加了我正在尝试工作的示例代码。感谢您检查..
标签: json validation lua schema kong-plugin