-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added rapidjson for schema validation. (#67)
- Loading branch information
Showing
9 changed files
with
203 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ addons: | |
- perl | ||
- etcd | ||
- luarocks | ||
- cmake | ||
|
||
cache: | ||
directories: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
-- todo: change to https://github.com/iresty/ljsonschema | ||
|
||
local typeof = require("apisix.core.typeof") | ||
local tostring = tostring | ||
local pairs = pairs | ||
local rapidjson_schema_validator = require('rapidjson').SchemaValidator | ||
local rapidjson_schema_doc = require('rapidjson').SchemaDocument | ||
local rapidjson_doc = require('rapidjson').Document | ||
|
||
|
||
local _M = {version = 0.1} | ||
|
||
local schema_sd = {} | ||
|
||
function _M.check_args(args, schema) | ||
for k, v in pairs(schema) do | ||
if not typeof[v](args[k]) then | ||
return nil, "args." .. k .. " expect " .. v .. " value but got: [" | ||
.. tostring(args[k]) .. "]" | ||
end | ||
-- You can follow this document to write schema: | ||
-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema | ||
-- rapidjson not supported `format` in draft-04 yet | ||
function _M.check_args(schema, json) | ||
local sd = schema_sd[schema] | ||
if not sd then | ||
sd = rapidjson_schema_doc(schema) | ||
schema_sd[schema] = sd | ||
end | ||
|
||
return true | ||
local validator = rapidjson_schema_validator(sd) | ||
local d = rapidjson_doc(json) | ||
return validator:validate(d) | ||
end | ||
|
||
return _M |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.