You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides a JSON schema draft 4, draft 6, draft 7 validator for Lua/LuaJIT.
5
-
Note that even though it uses the JSON Schema semantics, it is neither bound or limited
6
-
to JSON. It can be used to validate saner key/value data formats as well (Lua
7
-
tables, msgpack, bencode, ...).
3
+
A pure Lua JSON Schema validator library for Lua/LuaJIT.
8
4
9
-
It has been designed to validate incoming data for HTTP APIs so it is decently
10
-
fast: it works by transforming the given schema into a pure Lua function
11
-
on-the-fly. Work is currently in progress to make it as JIT-friendly as
12
-
possible.
5
+
Validation is neither bound nor limited to JSON and can be used to validate other key-value data formats like [Lua tables](https://www.lua.org/pil/2.5.html), [msgpack](https://msgpack.org/index.html), and [bencode](https://en.wikipedia.org/wiki/Bencode).
13
6
14
-
This project base on [ljsonschema](https://github.com/jdesgats/ljsonschema). Many thanks to the author `jdesgats` for the perfect work. The re-implementation is we need to support OpenResty env,
15
-
and we can use more optimization methods only available in OpenResty, which can make it run faster in OpenResty land.
7
+
The library is designed to validate incoming HTTP requests and is quite performant. Underneath, the library transforms the given schema to a pure Lua function on-the-fly.
16
8
17
-
Installation
18
-
------------
9
+
We are currently improving the library to be as JIT-friendly as possible.
19
10
20
-
This module is pure Lua/LuaJIT project, it support Lua 5.2, Lua 5.3, LuaJIT 2.1 beta.
11
+
Thanks to [Julien Desgats](https://github.com/jdesgats) for his work on [ljsonschema](https://github.com/jdesgats/ljsonschema) on top which this project is built upon. This project is a reimplementation that makes it much faster in OpenResty environments by using specific optimization methods.
21
12
22
-
The preferred way to install this library is to use Luarocks:
This library supports Lua versions 5.2 and 5.3, and LuaJIT version 2.1 beta.
31
26
32
-
The project references the pcre regular library.
27
+
To install the library via LuaRocks:
33
28
34
-
If you were using the LuaJIT of OpenResty, it will use the built-in `ngx.re.find` automatically.
35
-
But if you are using Lua 5.2, 5.3 or LuaJIT 2.1 beta, it will use `lrexlib-pcre` instead.
29
+
```shell
30
+
luarocks install jsonschema
31
+
```
32
+
33
+
> [!NOTE]
34
+
> This library references the PCRE regex library.
35
+
>
36
+
> If you use the LuaJIT of OpenResty, it will automatically use the built in `ngx.re.find` function. But if you use Lua versions 5.2, 5.3 or LuaJIT version 2.1 beta, it will use the function `lrexlib-pcre` instead.
37
+
>
38
+
> This library also relies on [net-url](https://luarocks.org/modules/golgote/net-url) library and it should be installed.
39
+
40
+
## Development
36
41
37
-
In addition, the project also relies on the `net_url` library, which you need to install anyway.
42
+
To build this library locally and run the tests:
38
43
39
-
Usage
40
-
-----
44
+
```shell
45
+
git submodule update --init --recursive
46
+
make dev
47
+
make test
48
+
```
41
49
42
-
### Getting started
50
+
##Usage
43
51
44
52
```lua
45
53
localjsonschema=require'jsonschema'
46
54
47
-
-- Note: do cache the result of schema compilation as this is a quite
48
-
-- expensive process
55
+
-- Note: Cache the result of the schema compilation as this is quite expensive
Some advanced features of JSON Schema are not possible to implement using the
63
-
standard library and require third party libraries to be work.
69
+
Some advanced features of JSON Schema cannot be implemented using the standard library and requires third-party libraries to work.
64
70
65
-
In order to not force one particular library, and not bloat this library for
66
-
the simple schemas, extension points are provided: the `generate_validator`
67
-
takes a second table argument that can be used to customise the generated
68
-
parser.
71
+
In order to not bloat this library for simple usage, extension points are provided. The `generate_validator` takes in a table argument that can be used to customize the generated parser:
69
72
70
73
```lua
71
74
localv=jsonschema.generate_validator(schema, {
72
-
--a value used to check null elements in the validated documents
73
-
--defaults to `cjson.null` (if available) or `nil`
75
+
--A value used to check null elements in the validated documents
76
+
--Defaults to `cjson.null` (if available) or `nil`
74
77
null=null_token,
75
78
76
-
--function called to match patterns, defaults to `ngx.re.find` in OpenResty
77
-
-- or `rex.find` from lrexlib-pcre on other occassions.
78
-
-- The pattern given here will obey the ECMA-262 specification.
79
+
--Function called to match patterns, defaults to `ngx.re.find` in OpenResty
80
+
-- or `rex.find` from lrexlib-pcre on other occassions
81
+
-- The pattern given here will follow the ECMA-262 specification
79
82
match_pattern=function(string, patt)
80
83
return...-- boolean value
81
84
end,
82
85
83
-
-- function called to resolve external schemas. It is called with the full
84
-
-- url to fetch (without the fragment part) and must return the
85
-
-- corresponding schema as a Lua table.
86
-
-- There is no default implementation: this function must be provided if
87
-
-- resolving external schemas is required.
86
+
-- Function called to resolve external schemas
87
+
-- Called with the full URL to fetch (without the fragment part) and must return the corresponding schema as a Lua table
88
+
-- No default implementation: this function must be provided if resolving external schemas is required
88
89
external_resolver=function(url)
89
90
return...-- Lua table
90
91
end,
91
92
92
-
--name when generating the validator function, it might ease debugging as
93
-
-- as it will appear in stack traces.
93
+
--Name when generating the validator function
94
+
--Might ease debugging as it will appear in stack traces
94
95
name="myschema",
95
96
})
96
97
```
97
98
98
-
Differences with JSONSchema
99
-
---------------------------
100
-
101
-
Due to the nature of the Lua language, the full JSON schema support is
102
-
difficult to reach. Some of the limitations can be solved using the advanced
103
-
options detailed previously, but some features are not supported (correctly)
104
-
at this time:
99
+
## Differences with JSONSchema
105
100
106
-
* Empty tables and empty arrays are the same from Lua point of view
101
+
Due to the limitations of Lua, supporting the JSON Schema specification completely is difficult. Some of these limitations can be overcome using the extension points mentioned above while some limitations still exist:
107
102
103
+
- Empty tables and empty arrays are the same for Lua.
108
104
109
105
On the other hand, some extra features are supported:
110
106
111
-
* The type `table` can be used to match arrays or objects, it is also much
112
-
faster than `array` or `object` as it does not involve walking the table to
113
-
find out if it's a sequence or a hash
114
-
* The type `function` can be used to check for functions
115
-
107
+
- The type `table` can be used to match arrays or objects. It is much faster than `array` or `object` as it does not involve walking the table to find out if it's a sequence or a hash.
108
+
- The type `function` can be used to check for functions.
0 commit comments