Skip to content

Commit a55072d

Browse files
authored
docs: update readme (#84)
1 parent faa202f commit a55072d

File tree

1 file changed

+54
-61
lines changed

1 file changed

+54
-61
lines changed

README.md

+54-61
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
1-
jsonschema: JSON schema validator
2-
===========================================
1+
# jsonschema
32

4-
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.
84

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).
136

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.
168

17-
Installation
18-
------------
9+
We are currently improving the library to be as JIT-friendly as possible.
1910

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.
2112

22-
The preferred way to install this library is to use Luarocks:
13+
## Supported versions
2314

24-
luarocks install jsonschema
15+
| Specification version | Supported? |
16+
| ------------------------------------------------------------------- | ---------- |
17+
| < Draft 4 ||
18+
| [Draft 4](https://json-schema.org/specification-links.html#draft-4) ||
19+
| Draft 5 ||
20+
| [Draft 6](https://json-schema.org/specification-links.html#draft-6) ||
21+
| [Draft 7](https://json-schema.org/specification-links.html#draft-7) ||
2522

26-
Running the tests:
23+
## Installation
2724

28-
git submodule update --init --recursive
29-
make dev
30-
make test
25+
This library supports Lua versions 5.2 and 5.3, and LuaJIT version 2.1 beta.
3126

32-
The project references the pcre regular library.
27+
To install the library via LuaRocks:
3328

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
3641

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:
3843

39-
Usage
40-
-----
44+
```shell
45+
git submodule update --init --recursive
46+
make dev
47+
make test
48+
```
4149

42-
### Getting started
50+
## Usage
4351

4452
```lua
4553
local jsonschema = require 'jsonschema'
4654

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
4956
local myvalidator = jsonschema.generate_validator {
5057
type = 'object',
5158
properties = {
@@ -59,57 +66,43 @@ print(myvalidator{ foo='hello', bar=42 })
5966

6067
### Advanced usage
6168

62-
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.
6470

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:
6972

7073
```lua
7174
local v = 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`
7477
null = null_token,
7578

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
7982
match_pattern = function(string, patt)
8083
return ... -- boolean value
8184
end,
8285

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
8889
external_resolver = function(url)
8990
return ... -- Lua table
9091
end,
9192

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
9495
name = "myschema",
9596
})
9697
```
9798

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
105100

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:
107102

103+
- Empty tables and empty arrays are the same for Lua.
108104

109105
On the other hand, some extra features are supported:
110106

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

Comments
 (0)