Repository navigation
Expand file tree
/
Copy pathshape.lua
More file actions
557 lines (491 loc) · 14.2 KB
/
Copy pathshape.lua
File metadata and controls
557 lines (491 loc) · 14.2 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
-- Small runtime validator. Shorthand rules handle simple type guards;
-- explicit schemas handle composition and transformations.
local M = {}
---@alias OpencodeShapeRule string|(fun(value: any): boolean)|table|OpencodeShapeSchema<any>
---@alias OpencodeShapeParser fun(value: any, path: string): boolean, any
---@alias OpencodeShapeRuleParser fun(value: any, rule: OpencodeShapeRule, path: string): boolean, any
---@class OpencodeShapeObjectOptions
---@field strict? boolean
---@class OpencodeShapeSchema<T>
---@field __index OpencodeShapeSchema<any>
---@field _parse OpencodeShapeParser
---@field parse fun(self: OpencodeShapeSchema<T>, value: any, message?: string): T
---@field safe_parse fun(self: OpencodeShapeSchema<T>, value: any): OpencodeShapeResult<T>
---@field check fun(self: OpencodeShapeSchema<T>, value: any): boolean
---@field is fun(self: OpencodeShapeSchema<T>, value: any): boolean
---@field optional fun(self: OpencodeShapeSchema<T>): OpencodeShapeSchema<T|nil>
---@field nullable fun(self: OpencodeShapeSchema<T>): OpencodeShapeSchema<T|nil>
---@field array fun(self: OpencodeShapeSchema<T>): OpencodeShapeSchema<T[]>
---@field constraint fun(self: OpencodeShapeSchema<T>, fn: (fun(value: T): boolean), message?: string): OpencodeShapeSchema<T>
---@field min fun(self: OpencodeShapeSchema<T>, value: number): OpencodeShapeSchema<T>
---@field max fun(self: OpencodeShapeSchema<T>, value: number): OpencodeShapeSchema<T>
---@field transform fun(self: OpencodeShapeSchema<T>, fn: fun(value: T): any): OpencodeShapeSchema<any>
---@field convert fun(self: OpencodeShapeSchema<T>, fn: fun(value: T): any): OpencodeShapeSchema<any>
---@field or_ fun(self: OpencodeShapeSchema<T>, other: OpencodeShapeRule): OpencodeShapeSchema<T>
---@class OpencodeShapeResult<T>
---@field success boolean
---@field data? T
---@field error? string
local Schema = {}
---@cast Schema OpencodeShapeSchema<any>
Schema.__index = Schema
local function is_schema(value)
if type(value) ~= 'table' then
return false
end
return getmetatable(value) == Schema
end
local function issue(path, expected, value)
return string.format('%s: expected %s, got %s', path, expected, type(value))
end
local function path_field(path, field)
if type(field) == 'string' and field:match('^[%a_][%w_]*$') then
return path .. '.' .. field
end
return path .. '[' .. tostring(field) .. ']'
end
local function sequence_length(value)
---@type number
local length = 0
for key in pairs(value) do
if type(key) ~= 'number' or key % 1 ~= 0 or key < 1 then
return nil
end
length = math.max(length, key)
end
for index = 1, length do
if rawget(value, index) == nil then
return nil
end
end
return length
end
local function is_sequence(value)
return sequence_length(value) ~= nil
end
---@param parser OpencodeShapeParser
---@return OpencodeShapeSchema<any>
local function new_schema(parser)
return setmetatable({ _parse = parser }, Schema)
end
local function copy_table(value)
local result = {}
for key, entry in pairs(value) do
result[key] = entry
end
return result
end
---@param value any
---@param spec table<any, OpencodeShapeRule>
---@param path string
---@param strict boolean
---@param parse OpencodeShapeRuleParser
---@return boolean, any
local function parse_object(value, spec, path, strict, parse)
if type(value) ~= 'table' then
return false, issue(path, 'table', value)
end
local fields = {}
local changed = false
for field, rule in pairs(spec) do
local ok, parsed = parse(value[field], rule, path_field(path, field))
if not ok then
return false, parsed
end
fields[#fields + 1] = { field, parsed }
changed = changed or parsed ~= value[field]
end
if strict then
for field in pairs(value) do
if spec[field] == nil then
return false, path_field(path, field) .. ': unexpected field'
end
end
end
if not changed then
return true, value
end
local result = copy_table(value)
for _, field in ipairs(fields) do
result[field[1]] = field[2]
end
return true, result
end
---@param value any
---@param item OpencodeShapeRule
---@param path string
---@param parse OpencodeShapeRuleParser
---@return boolean, any
local function parse_array(value, item, path, parse)
local length = type(value) == 'table' and sequence_length(value) or nil
if length == nil then
return false, issue(path, 'array', value)
end
local result = {}
local changed = false
for index = 1, length do
local ok, parsed = parse(value[index], item, path .. '[' .. index .. ']')
if not ok then
return false, parsed
end
result[index] = parsed
changed = changed or parsed ~= value[index]
end
return true, changed and result or value
end
---@param value any
---@param rule OpencodeShapeRule
---@param path string
---@return boolean, any
local function parse_rule(value, rule, path)
if is_schema(rule) then
---@cast rule OpencodeShapeSchema<any>
return rule._parse(value, path)
end
if type(rule) == 'string' then
if type(value) == rule then
return true, value
end
return false, issue(path, rule, value)
end
if type(rule) == 'function' then
local ok, valid = pcall(rule, value)
if ok and valid then
return true, value
end
return false, issue(path, 'predicate', value)
end
if type(rule) == 'table' then
if is_sequence(rule) and #rule > 0 then
for _, expected in ipairs(rule) do
if value == expected then
return true, value
end
end
return false, issue(path, 'one of listed values', value)
end
---@cast rule table<any, OpencodeShapeRule>
return parse_object(value, rule, path, false, parse_rule)
end
return false, path .. ': invalid shape rule'
end
local function primitive(expected)
return new_schema(function(value, path)
if type(value) == expected then
return true, value
end
return false, issue(path, expected, value)
end)
end
---@param value any
---@param message? string
---@return any
function Schema:parse(value, message)
local ok, result = self._parse(value, '$')
if not ok then
error(message ~= nil and tostring(message) or result, 0)
end
return result
end
---@param value any
---@return OpencodeShapeResult<any>
function Schema:safe_parse(value)
local ok, result = pcall(self.parse, self, value)
if ok then
return { success = true, data = result }
end
return { success = false, error = tostring(result) }
end
---@param value any
---@return boolean
function Schema:check(value)
local ok, valid = pcall(self._parse, value, '$')
return ok and valid == true
end
Schema.is = Schema.check
function Schema:optional()
return M.optional(self)
end
function Schema:nullable()
return M.nullable(self)
end
function Schema:array()
return M.array(self)
end
---@param predicate fun(value: any): boolean
---@param description? string
---@return OpencodeShapeSchema<any>
function Schema:constraint(predicate, description)
local source = self
return new_schema(function(value, path)
local ok, parsed = source._parse(value, path)
if not ok then
return false, parsed
end
local valid, result = pcall(predicate, parsed)
if valid and result then
return true, parsed
end
return false, issue(path, description or 'constrained value', parsed)
end)
end
---@param minimum number
---@return OpencodeShapeSchema<number>
function Schema:min(minimum)
return self:constraint(function(value)
return value >= minimum
end, 'at least ' .. tostring(minimum))
end
---@param maximum number
---@return OpencodeShapeSchema<number>
function Schema:max(maximum)
return self:constraint(function(value)
return value <= maximum
end, 'at most ' .. tostring(maximum))
end
---@param transformer fun(value: any): any
---@return OpencodeShapeSchema<any>
function Schema:transform(transformer)
local source = self
return new_schema(function(value, path)
local ok, parsed = source._parse(value, path)
if not ok then
return false, parsed
end
local transformed, result = pcall(transformer, parsed)
if not transformed then
return false, path .. ': transform failed: ' .. tostring(result)
end
return true, result
end)
end
---@param converter fun(value: any): any
---@return OpencodeShapeSchema<any>
function Schema:convert(converter)
return self:transform(function(value)
local result = converter(value)
if result == nil then
error('conversion returned nil', 0)
end
return result
end)
end
function Schema:or_(other)
return M.union(self, other)
end
---@param value any
---@param spec OpencodeShapeRule
---@param message? string
---@return any
function M.validate(value, spec, message)
local ok, result = parse_rule(value, spec, '$')
if not ok then
error(message ~= nil and tostring(message) or result, 0)
end
return result
end
M.parse = M.validate
---@param condition boolean
---@param message string
---@return boolean
function M.expect(condition, message)
if not condition then
error(tostring(message), 0)
end
return condition
end
M.assert = M.expect
---@param value any
---@param spec OpencodeShapeRule
---@return boolean
function M.check(value, spec)
local ok, valid = pcall(parse_rule, value, spec, '$')
return ok and valid == true
end
M.is = M.check
---@param value any
---@param spec OpencodeShapeRule
---@param message? string
---@return OpencodeShapeResult<any>
function M.safe_parse(value, spec, message)
local ok, result = pcall(M.validate, value, spec, message)
if ok then
return { success = true, data = result }
end
return { success = false, error = tostring(result) }
end
---@param spec table<any, OpencodeShapeRule>
---@param options? OpencodeShapeObjectOptions
---@return OpencodeShapeSchema<table>
function M.object(spec, options)
options = options or {}
return new_schema(function(value, path)
return parse_object(value, spec, path, options.strict == true, parse_rule)
end)
end
---@param spec table<any, OpencodeShapeRule>
---@return OpencodeShapeSchema<table>
function M.strict_object(spec)
return M.object(spec, { strict = true })
end
---@generic T
---@param item? OpencodeShapeRule
---@return OpencodeShapeSchema<T[]>
function M.array(item)
item = item or M.any()
return new_schema(function(value, path)
return parse_array(value, item, path, parse_rule)
end)
end
---@generic T
---@param values T[]
---@return OpencodeShapeSchema<T>
function M.enum(values)
return new_schema(function(value, path)
for _, expected in ipairs(values) do
if value == expected then
return true, value
end
end
return false, issue(path, 'one of listed values', value)
end)
end
M.one_of = M.enum
---@generic T
---@param expected T
---@return OpencodeShapeSchema<T>
function M.literal(expected)
return new_schema(function(value, path)
if value == expected then
return true, value
end
return false, issue(path, 'literal ' .. tostring(expected), value)
end)
end
---@param first OpencodeShapeRule|OpencodeShapeRule[]
---@param ... OpencodeShapeRule
---@return OpencodeShapeSchema<any>
function M.union(first, ...)
---@type OpencodeShapeRule[]
local options
if type(first) == 'table' and not is_schema(first) and is_sequence(first) then
---@cast first OpencodeShapeRule[]
options = first
else
options = { first, ... }
end
return new_schema(function(value, path)
for _, rule in ipairs(options) do
local ok, parsed = parse_rule(value, rule, path)
if ok then
return true, parsed
end
end
return false, issue(path, 'one of union schemas', value)
end)
end
---@generic T
---@param rule OpencodeShapeRule
---@return OpencodeShapeSchema<T|nil>
function M.optional(rule)
return new_schema(function(value, path)
if value == nil then
return true, nil
end
return parse_rule(value, rule, path)
end)
end
M.nullable = M.optional
---@generic T
---@param predicate fun(value: any): boolean
---@param description? string
---@return OpencodeShapeSchema<T>
function M.custom(predicate, description)
return new_schema(function(value, path)
local ok, valid = pcall(predicate, value)
if ok and valid then
return true, value
end
return false, issue(path, description or 'predicate', value)
end)
end
---@generic T
---@param rule OpencodeShapeRule
---@param predicate fun(value: T): boolean
---@param description? string
---@return OpencodeShapeSchema<T>
function M.constraint(rule, predicate, description)
local schema = new_schema(function(value, path)
return parse_rule(value, rule, path)
end)
return schema:constraint(predicate, description)
end
---@return OpencodeShapeSchema<any>
function M.any()
return new_schema(function(value)
return true, value
end)
end
M.unknown = M.any
---@return OpencodeShapeSchema<string>
function M.string()
return primitive('string')
end
---@return OpencodeShapeSchema<number>
function M.number()
return primitive('number')
end
---@return OpencodeShapeSchema<boolean>
function M.boolean()
return primitive('boolean')
end
---@return OpencodeShapeSchema<function>
M['function'] = function()
return primitive('function')
end
---@return OpencodeShapeSchema<thread>
function M.thread()
return primitive('thread')
end
---@return OpencodeShapeSchema<userdata>
function M.userdata()
return primitive('userdata')
end
---@return OpencodeShapeSchema<table>
function M.table()
return primitive('table')
end
---@return OpencodeShapeSchema<integer>
function M.integer()
local schema = M.constraint(M.number(), function(value)
return value % 1 == 0
end, 'integer')
---@cast schema OpencodeShapeSchema<integer>
return schema
end
---@generic T, U
---@param rule OpencodeShapeRule
---@param transformer fun(value: T): U
---@return OpencodeShapeSchema<U>
function M.transform(rule, transformer)
local schema = new_schema(function(value, path)
return parse_rule(value, rule, path)
end)
return schema:transform(transformer)
end
---@generic T, U
---@param rule OpencodeShapeRule
---@param converter fun(value: T): U
---@return OpencodeShapeSchema<U>
function M.convert(rule, converter)
local schema = new_schema(function(value, path)
return parse_rule(value, rule, path)
end)
return schema:convert(converter)
end
setmetatable(M, {
__call = function(_, value, spec, message)
return M.validate(value, spec, message)
end,
})
return M