Debian Codegraph @

lualib/lua_shape/jsonschema.lua

raw
1 --[[
2 Copyright (c) 2025, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 -- Lua shape validation library - JSON Schema exporter
18 -- Converts lua_shape schemas to JSON Schema format
19
20 local exports = {}
21
22 -- Convert a schema node to JSON Schema
23 local function to_jsonschema_impl(schema, opts)
24 opts = opts or {}
25
26 if not schema or not schema.tag then
27 return {}
28 end
29
30 local result = {}
31 local schema_opts = schema.opts or {}
32
33 -- Add description from doc
34 if schema_opts.doc and schema_opts.doc.summary then
35 result.description = schema_opts.doc.summary
36 end
37
38 local tag = schema.tag
39
40 -- Scalar types
41 if tag == "scalar" then
42 local kind = schema.kind
43
44 if kind == "string" then
45 result.type = "string"
46
47 if schema_opts.min_len then
48 result.minLength = schema_opts.min_len
49 end
50 if schema_opts.max_len then
51 result.maxLength = schema_opts.max_len
52 end
53 if schema_opts.pattern then
54 result.pattern = schema_opts.pattern
55 end
56
57 elseif kind == "number" or kind == "integer" then
58 result.type = kind == "integer" and "integer" or "number"
59
60 if schema_opts.min then
61 result.minimum = schema_opts.min
62 end
63 if schema_opts.max then
64 result.maximum = schema_opts.max
65 end
66
67 elseif kind == "boolean" then
68 result.type = "boolean"
69
70 elseif kind == "enum" then
71 if schema_opts.enum then
72 result.enum = schema_opts.enum
73 end
74
75 elseif kind == "literal" then
76 result.const = schema_opts.literal
77 end
78
79 -- Array type
80 elseif tag == "array" then
81 result.type = "array"
82
83 if schema.item_schema then
84 result.items = to_jsonschema_impl(schema.item_schema, opts)
85 end
86
87 if schema_opts.min_items then
88 result.minItems = schema_opts.min_items
89 end
90 if schema_opts.max_items then
91 result.maxItems = schema_opts.max_items
92 end
93
94 -- Table type
95 elseif tag == "table" then
96 result.type = "object"
97 result.properties = {}
98 result.required = {}
99
100 -- Process fields
101 for field_name, field_spec in pairs(schema.fields or {}) do
102 result.properties[field_name] = to_jsonschema_impl(field_spec.schema, opts)
103
104 -- Add to required if not optional
105 if not field_spec.optional then
106 table.insert(result.required, field_name)
107 end
108
109 -- Add default if present
110 if field_spec.default ~= nil then
111 result.properties[field_name].default = field_spec.default
112 end
113
114 -- Add origin metadata if present (for mixin tracking)
115 if field_spec.origin and opts.include_origin then
116 result.properties[field_name]["x-rspamd-origin"] = field_spec.origin
117 end
118 end
119
120 -- Handle open/closed table
121 if schema_opts.open == false then
122 if schema_opts.extra then
123 -- Allow additional properties matching extra schema
124 result.additionalProperties = to_jsonschema_impl(schema_opts.extra, opts)
125 else
126 result.additionalProperties = false
127 end
128 else
129 result.additionalProperties = true
130 end
131
132 -- Remove empty required array
133 if #result.required == 0 then
134 result.required = nil
135 end
136
137 -- one_of type
138 elseif tag == "one_of" then
139 result.oneOf = {}
140
141 for _, variant in ipairs(schema.variants or {}) do
142 local variant_schema = to_jsonschema_impl(variant.schema, opts)
143
144 -- Add title if variant has a name
145 if variant.name and opts.include_variant_names then
146 variant_schema.title = variant.name
147 end
148
149 table.insert(result.oneOf, variant_schema)
150 end
151
152 -- Optional wrapper
153 elseif tag == "optional" then
154 result = to_jsonschema_impl(schema.inner, opts)
155
156 -- Add null as allowed type
157 if result.type then
158 if type(result.type) == "string" then
159 result.type = { result.type, "null" }
160 else
161 table.insert(result.type, "null")
162 end
163 end
164
165 if schema.default ~= nil then
166 result.default = schema.default
167 end
168
169 -- Transform wrapper
170 elseif tag == "transform" then
171 -- For JSON Schema, just export the inner schema
172 -- Transform semantics don't apply to JSON Schema validation
173 result = to_jsonschema_impl(schema.inner, opts)
174
175 -- Reference
176 elseif tag == "ref" then
177 local ref_id = schema.ref_id
178 result["$ref"] = "#/definitions/" .. ref_id
179 end
180
181 return result
182 end
183
184 -- Convert a schema to JSON Schema
185 function exports.from_schema(schema, opts)
186 opts = opts or {}
187
188 local result = {
189 ["$schema"] = "http://json-schema.org/draft-07/schema#"
190 }
191
192 -- Add schema ID if provided
193 if opts.id then
194 result["$id"] = opts.id
195 end
196
197 -- Add title if provided
198 if opts.title then
199 result.title = opts.title
200 end
201
202 -- Convert schema
203 local schema_json = to_jsonschema_impl(schema, opts)
204 for k, v in pairs(schema_json) do
205 result[k] = v
206 end
207
208 return result
209 end
210
211 -- Export all schemas from a registry
212 function exports.export_registry(registry, opts)
213 opts = opts or {}
214 -- local base_id = opts.base_id or "https://rspamd.com/schema/"
215
216 local result = {
217 ["$schema"] = "http://json-schema.org/draft-07/schema#",
218 definitions = {}
219 }
220
221 local schemas = registry:export_all()
222
223 for id, schema in pairs(schemas) do
224 result.definitions[id] = to_jsonschema_impl(schema, opts)
225 end
226
227 return result
228 end
229
230 return exports