Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vm-001/lua-jsonschema-mocker
JSON Schema mocker for Lua/LuaJIT
https://github.com/vm-001/lua-jsonschema-mocker
Last synced: 12 days ago
JSON representation
JSON Schema mocker for Lua/LuaJIT
- Host: GitHub
- URL: https://github.com/vm-001/lua-jsonschema-mocker
- Owner: vm-001
- Created: 2023-12-05T06:12:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-08T09:51:48.000Z (about 1 year ago)
- Last Synced: 2025-01-19T20:24:42.207Z (16 days ago)
- Language: Lua
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-resty - lua-jsonschema-mocker - JSON Schema mocker. (Libraries)
README
# lua-jsonschema-mocker
JSON Schema Mocker is a Lua library to generate mock data from a [JSON Schema](https://json-schema.org/specification).
## Supported features
| Category | Supported |
|----------|--------------------------------------------------------------|
| boolean | supported |
| integer | `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` |
| number | `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` |
| string | `minLength`, `maxLength`, `format`(`date` and `date-time`) |
| array | `minItems`, `maxItems` |
| object | supported |
| null | supported |
| others | `enum`, `allOf`, `oneOf` |## Installation
Install via LuaRocks:
```
luarocks install jsonschema-mocker
```## Usage
```lua
local jsonschema_mocker = require "jsonschema-mocker"math.randomseed(os.time())
local mocker = jsonschema_mocker.new()
local res = mocker:mock({
type = "object",
properties = {
age = { type = "integer", minimum = 0 },
name = { type = "string" },
activated = { type = "boolean" },
hobbies = { type = "array", items = { type = "string" }, minItems = 0, maxItems = 5 },
rank = { enum = { "A", "B", "C", "D" } },
}
})--[[ res
{
activated = false,
age = 27,
hobbies = { "v7t8fCape", "OEt7ExyKuUzBentHd", "S6MtSh", "IrowzGG7POFoDBio" },
name = "DP6CjCN7mlMnAo",
rank = "C"
}
-- ]]
```### Advanced usage
```lua
local jsonschema_mocker = require "jsonschema-mocker"
local mocker = jsonschema_mocker.new({
-- integer mocker options
integer = {
minimum = 0,
maximum = 999,
},
-- number mocker options
number = {
minimum = 0,
maximum = 999,
},
-- string mocker options
string = {
min_length = 1,
max_length = 20,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
formatters = nil,
},
-- array mocker options
array = {
min_items = 0,
max_items = 0,
},
-- object mocker optiosn
object = {
only_required_properties = false,
},
-- customize mock result function
customize = function(res, schema)
return res
end
})
```For more usage samples, refer to the `/samples` directory.
## License