Skip to content

Commit

Permalink
feature: added rapidjson for schema validation. (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
moonming authored Jun 10, 2019
1 parent 10e63c6 commit 1737fb7
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 157 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ addons:
- perl
- etcd
- luarocks
- cmake

cache:
directories:
Expand Down
91 changes: 91 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,94 @@ https://github.com/knyar/nginx-lua-prometheus/blob/master/README.md
Licensed under MIT license.

%%%%%%%%%
rapidjson

https://github.com/Tencent/rapidjson

Tencent is pleased to support the open source community by making RapidJSON available.

Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.

If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License.
If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license.
A copy of the MIT License is included in this file.

Other dependencies and licenses:

Open Source Software Licensed Under the BSD License:
--------------------------------------------------------------------

The msinttypes r29
Copyright (c) 2006-2013 Alexander Chemeris
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Open Source Software Licensed Under the JSON License:
--------------------------------------------------------------------

json.org
Copyright (c) 2002 JSON.org
All Rights Reserved.

JSON_checker
Copyright (c) 2002 JSON.org
All Rights Reserved.


Terms of the JSON License:
---------------------------------------------------

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Terms of the MIT License:
--------------------------------------------------------------------

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

%%%%%%%%%

lua-rapidjson

https://github.com/xpol/lua-rapidjson

MIT License

Copyright (C) 2015 Xpol Wan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

%%%%%%%%%
3 changes: 2 additions & 1 deletion apisix-0.4-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ dependencies = {
"lua-resty-etcd = 0.5",
"lua-resty-balancer = 0.02rc5",
"lua-resty-ngxvar = 0.2",
"lua-resty-jit-uuid = 0.0.7"
"lua-resty-jit-uuid = 0.0.7",
"rapidjson = 0.5.2-1",
}

build = {
Expand Down
Empty file removed logs/placehold.txt
Empty file.
1 change: 0 additions & 1 deletion lua/apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ return {
table = require("apisix.core.table"),
request = require("apisix.core.request"),
response = require("apisix.core.response"),
typeof = require("apisix.core.typeof"),
lrucache = require("apisix.core.lrucache"),
schema = require("apisix.core.schema"),
ctx = require("apisix.core.ctx"),
Expand Down
28 changes: 15 additions & 13 deletions lua/apisix/core/schema.lua
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
122 changes: 0 additions & 122 deletions lua/apisix/core/typeof.lua

This file was deleted.

19 changes: 13 additions & 6 deletions lua/apisix/plugins/example-plugin.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
local core = require("apisix.core")


-- TODO: need a more powerful way to define the schema
local args_schema = {
i = "int",
s = "string",
t = "table",
-- 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
local schema = {
type = "object",
properties = {
i = {type = "number", minimum = 0},
s = {type = "string"},
t = {type = "array", minItems = 1},
},
required = {"i"}
}


Expand All @@ -19,7 +25,8 @@ local _M = {


function _M.check_args(conf)
local ok, err = core.schema.check_args(conf, args_schema)
local ok, err = core.schema.check_args(schema, conf)

if not ok then
return false, err
end
Expand Down
Loading

0 comments on commit 1737fb7

Please sign in to comment.