-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.lua
More file actions
359 lines (310 loc) · 11.3 KB
/
schema.lua
File metadata and controls
359 lines (310 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
-- schema.lua — basic type validators
local schema = {}
local function path_str(path)
if #path == 0 then return "." end
local parts = {}
for _, p in ipairs(path) do
parts[#parts + 1] = (type(p) == "number") and ("[" .. p .. "]") or ("." .. p)
end
return table.concat(parts)
end
local Validator = {}
Validator.__index = Validator
function Validator:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o._checks = o._checks or {}
o._optional = false
o._nullable = false
o._custom_message = nil
return o
end
function Validator:optional()
local v = self:clone()
v._optional = true
return v
end
function Validator:nullable()
local v = self:clone()
v._nullable = true
return v
end
function Validator:message(msg)
local v = self:clone()
v._custom_message = msg
return v
end
function Validator:_add_check(fn)
local v = self:clone()
v._checks[#v._checks + 1] = fn
return v
end
function Validator:_add_transform(fn)
local v = self:clone()
v._transforms = v._transforms or {}
v._transforms[#v._transforms + 1] = fn
return v
end
function Validator:clone()
local copy = {}
for k, val in pairs(self) do copy[k] = val end
copy._checks = {}
for _, c in ipairs(self._checks) do copy._checks[#copy._checks + 1] = c end
setmetatable(copy, getmetatable(self))
return copy
end
function Validator:parse(value, path)
path = path or {}
if value == nil then
if self._optional then return nil, nil end
return nil, {{ path = path_str(path), message = self._custom_message or ("required value missing at path " .. path_str(path)) }}
end
local ok, err = self:_check_type(value, path)
if not ok then return nil, {{ path = path_str(path), message = err }} end
local errors = {}
for _, check in ipairs(self._checks) do
local valid, e = check(value, path)
if not valid then errors[#errors + 1] = { path = path_str(path), message = e } end
end
if #errors > 0 then return nil, errors end
-- Apply transforms
local result = value
if self._transforms then
for _, transform in ipairs(self._transforms) do
result = transform(result)
end
end
return result, nil
end
function Validator:_check_type(value, path) _ = value; _ = path; return true, nil end
local StringValidator = Validator:new({ _type = "string" })
function StringValidator:_check_type(value, path)
if type(value) ~= "string" then
return false, "expected string, got " .. type(value) .. " at path " .. path_str(path)
end
return true, nil
end
local NumberValidator = Validator:new({ _type = "number" })
function NumberValidator:_check_type(value, path)
if type(value) ~= "number" then
return false, "expected number, got " .. type(value) .. " at path " .. path_str(path)
end
return true, nil
end
local BooleanValidator = Validator:new({ _type = "boolean" })
function BooleanValidator:_check_type(value, path)
if type(value) ~= "boolean" then
return false, "expected boolean, got " .. type(value) .. " at path " .. path_str(path)
end
return true, nil
end
function schema.string()
local v = StringValidator:new(); setmetatable(v, StringValidator); StringValidator.__index = StringValidator; return v
end
function StringValidator:coerce()
return self:_add_transform(function(v)
return tostring(v)
end)
end
function schema.number()
local v = NumberValidator:new(); setmetatable(v, NumberValidator); NumberValidator.__index = NumberValidator; return v
end
function NumberValidator:coerce()
return self:_add_transform(function(v)
if type(v) == "string" then
return tonumber(v) or v
end
return v
end)
end
function schema.boolean()
local v = BooleanValidator:new(); setmetatable(v, BooleanValidator); BooleanValidator.__index = BooleanValidator; return v
end
local function push(path, key)
local p = {}
for _, v in ipairs(path) do p[#p + 1] = v end
p[#p + 1] = key
return p
end
local TableValidator = Validator:new({ _type = "table" })
function TableValidator:new(shape)
local o = Validator.new(self, { _shape = shape or {} })
return o
end
function TableValidator:_check_type(value, path)
if type(value) ~= "table" then
return false, "expected table, got " .. type(value) .. " at path " .. path_str(path)
end
return true, nil
end
function TableValidator:parse(value, path)
path = path or {}
if value == nil then
if self._optional then return nil, nil end
return nil, {{ path = path_str(path), message = "required value missing at path " .. path_str(path) }}
end
if type(value) ~= "table" then
return nil, {{ path = path_str(path), message = "expected table, got " .. type(value) .. " at path " .. path_str(path) }}
end
local errors = {}
local result = {}
for key, field_schema in pairs(self._shape) do
local parsed, errs = field_schema:parse(value[key], push(path, key))
if errs then
for _, e in ipairs(errs) do errors[#errors + 1] = e end
else
result[key] = parsed
end
end
for key, val in pairs(value) do
if self._shape[key] == nil then result[key] = val end
end
if #errors > 0 then return nil, errors end
return result, nil
end
function schema.table(shape)
local v = TableValidator:new(shape); setmetatable(v, TableValidator); TableValidator.__index = TableValidator; return v
end
schema.object = schema.table
local ArrayValidator = Validator:new({ _type = "array" })
function ArrayValidator:new(item_schema)
local o = Validator.new(self, { _item_schema = item_schema })
return o
end
function ArrayValidator:parse(value, path)
path = path or {}
if value == nil then
if self._optional then return nil, nil end
return nil, {{ path = path_str(path), message = "required array missing at path " .. path_str(path) }}
end
if type(value) ~= "table" then
return nil, {{ path = path_str(path), message = "expected array, got " .. type(value) .. " at path " .. path_str(path) }}
end
local errors = {}
local result = {}
for i, item in ipairs(value) do
if self._item_schema then
local parsed, errs = self._item_schema:parse(item, push(path, i))
if errs then for _, e in ipairs(errs) do errors[#errors + 1] = e end
else result[i] = parsed end
else
result[i] = item
end
end
if #errors > 0 then return nil, errors end
for _, check in ipairs(self._checks) do
local ok, err = check(result, path)
if not ok then errors[#errors + 1] = { path = path_str(path), message = err } end
end
if #errors > 0 then return nil, errors end
return result, nil
end
function ArrayValidator:min(n)
return self:_add_check(function(v, p)
if #v < n then return false, "array too short: expected at least " .. n .. " items, got " .. #v .. " at path " .. path_str(p) end
return true, nil
end)
end
function ArrayValidator:max(n)
return self:_add_check(function(v, p)
if #v > n then return false, "array too long: expected at most " .. n .. " items, got " .. #v .. " at path " .. path_str(p) end
return true, nil
end)
end
function ArrayValidator:nonempty()
return self:min(1)
end
function TableValidator:strict()
local v = self:clone()
v._strict = true
return v
end
function TableValidator:extend(extra_shape)
local v = self:clone()
v._shape = {}
for k, s in pairs(self._shape) do v._shape[k] = s end
for k, s in pairs(extra_shape) do v._shape[k] = s end
return v
end
function schema.array(item_schema)
local v = ArrayValidator:new(item_schema); setmetatable(v, ArrayValidator); ArrayValidator.__index = ArrayValidator; return v
end
local EnumValidator = Validator:new({ _type = "enum" })
function EnumValidator:new(values)
local o = Validator.new(self, { _values = values or {} })
o._values_set = {}
for _, v in ipairs(values) do o._values_set[v] = true end
return o
end
function EnumValidator:_check_type(value, path)
if not self._values_set[value] then
local allowed = {}
for _, v in ipairs(self._values) do allowed[#allowed + 1] = tostring(v) end
return false, "expected one of [" .. table.concat(allowed, ", ") .. "], got " .. tostring(value) .. " at path " .. path_str(path)
end
return true, nil
end
local UnionValidator = Validator:new({ _type = "union" })
function UnionValidator:new(schemas)
local o = Validator.new(self, { _schemas = schemas or {} })
return o
end
function UnionValidator:parse(value, path)
path = path or {}
if value == nil and self._optional then return nil, nil end
for _, s in ipairs(self._schemas) do
local parsed, errs = s:parse(value, path)
if not errs then return parsed, nil end
end
return nil, {{ path = path_str(path), message = self._custom_message or ("value did not match any variant at path " .. path_str(path)) }}
end
local LiteralValidator = Validator:new({ _type = "literal" })
function LiteralValidator:new(expected)
local o = Validator.new(self, { _expected = expected })
return o
end
function LiteralValidator:_check_type(value, path)
if value ~= self._expected then
return false, "expected literal " .. tostring(self._expected) .. ", got " .. tostring(value) .. " at path " .. path_str(path)
end
return true, nil
end
function schema.enum(values)
local v = EnumValidator:new(values); setmetatable(v, EnumValidator); EnumValidator.__index = EnumValidator; return v
end
function schema.union(schemas)
local v = UnionValidator:new(schemas); setmetatable(v, UnionValidator); UnionValidator.__index = UnionValidator; return v
end
function schema.literal(value)
local v = LiteralValidator:new(value); setmetatable(v, LiteralValidator); LiteralValidator.__index = LiteralValidator; return v
end
function schema.any()
local v = Validator:new(); setmetatable(v, Validator); return v
end
return schema
-- path helper: returns dot-notation path string
local function fmt_path(path)
if not path or #path == 0 then return "root" end
local parts = {}
for _, p in ipairs(path) do
if type(p) == "number" then parts[#parts+1] = "[" .. p .. "]"
else parts[#parts+1] = "." .. p end
end
return table.concat(parts)
end
-- date validator
-- url validator
-- strict mode
-- refine
-- optional(): value may be absent (nil); validation is skipped entirely when nil
-- clone(): _checks list is deep-copied; _shape table reference is shared (shallow)
-- string validators: min, max, pattern and email are defined as _add_check extensions
-- _values_set: O(1) membership check mirror of _values list; both updated together
-- strict(): returns a clone with _strict=true; TableValidator:parse rejects undeclared keys
-- refine(fn): _add_check wrapper for custom predicates; fn(value, path) -> bool, errmsg
-- push(path, key): creates a new path table each call; path is not mutated in-place
-- schema.object is an alias for schema.table; both validate Lua tables with a shape
-- clone(): _transforms table is not deep-copied; use _add_transform to create a new copy
-- union(): schemas are tried in the order passed; first success wins (no backtracking)
-- path_str({}): empty path returns "." to indicate the root of the document