1
1
--- @class render.md.debug.ValidatorSpec
2
2
--- @field private validator render.md.debug.Validator
3
- --- @field private nilable ? boolean
4
- --- @field private suffix string
5
- --- @field private input ? table< string, any>
3
+ --- @field private config ? table<string , any>
4
+ --- @field private nilable boolean
5
+ --- @field private path string
6
6
--- @field private opts table<string , vim.validate.Spec>
7
7
local Spec = {}
8
8
Spec .__index = Spec
9
9
10
10
--- @param validator render.md.debug.Validator
11
- --- @param path string
12
- --- @param input table<string , any>
11
+ --- @param config table< string, any>
12
+ --- @param nilable boolean
13
13
--- @param key ? string | string[]
14
- --- @param nilable ? boolean
14
+ --- @param path ? string
15
15
--- @return render.md.debug.ValidatorSpec
16
- function Spec .new (validator , path , input , key , nilable )
16
+ function Spec .new (validator , config , nilable , key , path )
17
17
local self = setmetatable ({}, Spec )
18
18
self .validator = validator
19
+ self .config = config
19
20
self .nilable = nilable
20
- self .suffix = path
21
- self .input = input
21
+ self .path = path or ' '
22
22
self .opts = {}
23
23
if key ~= nil then
24
24
key = type (key ) == ' table' and key or { key }
25
- self .suffix = self .suffix .. ' .' .. table.concat (key , ' .' )
26
- self .input = vim .tbl_get (self .input , unpack (key ))
27
- assert (self .input ~= nil or self .nilable == true )
25
+ self .path = self .path .. ' .' .. table.concat (key , ' .' )
26
+ self .config = vim .tbl_get (self .config , unpack (key ))
27
+ assert (self .config ~= nil or self .nilable )
28
28
end
29
29
return self
30
30
end
31
31
32
32
--- @return string
33
- function Spec :get_suffix ()
34
- return self .suffix
33
+ function Spec :get_path ()
34
+ return self .path
35
35
end
36
36
37
37
--- @return table<string , any>
38
- function Spec :get_input ()
39
- return self .input
38
+ function Spec :get_config ()
39
+ return self .config
40
40
end
41
41
42
+ --- @param nilable boolean
42
43
--- @param f fun ( spec : render.md.debug.ValidatorSpec )
43
- --- @param nilable ? boolean
44
- function Spec :for_each (f , nilable )
45
- for name in pairs (self .input or {}) do
46
- f (Spec .new (self .validator , self .suffix , self .input , name , nilable ))
44
+ function Spec :for_each (nilable , f )
45
+ for name in pairs (self .config or {}) do
46
+ local spec = Spec .new (self .validator , self .config , nilable , name , self .path )
47
+ f (spec )
48
+ spec :check ()
47
49
end
48
50
end
49
51
@@ -123,7 +125,7 @@ function Spec:handle_types(input_types)
123
125
else
124
126
types = input_types
125
127
end
126
- if self .nilable then
128
+ if self .nilable and not vim . tbl_contains ( types , ' nil ' ) then
127
129
table.insert (types , ' nil' )
128
130
end
129
131
return types , # types == 0 and ' ' or (' or type ' .. vim .inspect (types ))
@@ -135,20 +137,24 @@ end
135
137
--- @param message string ?
136
138
--- @return render.md.debug.ValidatorSpec
137
139
function Spec :add (keys , logic , message )
138
- if self .input ~= nil then
140
+ if self .config ~= nil then
139
141
keys = type (keys ) == ' table' and keys or { keys }
140
142
for _ , key in ipairs (keys ) do
141
143
--- @diagnostic disable-next-line : assign-type-mismatch
142
- self .opts [key ] = { self .input [key ], logic , message or self .nilable }
144
+ self .opts [key ] = { self .config [key ], logic , message or self .nilable }
143
145
end
144
146
end
145
147
return self
146
148
end
147
149
148
150
function Spec :check ()
149
- if self .input ~ = nil then
150
- self . validator : check ( self . suffix , self . input , self . opts )
151
+ if self .config = = nil then
152
+ return
151
153
end
154
+ if vim .tbl_count (self .opts ) == 0 then
155
+ return
156
+ end
157
+ self .validator :check (self .path , self .config , self .opts )
152
158
end
153
159
154
160
--- @class render.md.debug.Validator
@@ -165,25 +171,25 @@ function Validator.new()
165
171
return self
166
172
end
167
173
168
- --- @param path string
169
- --- @param input table<string , any>
174
+ --- @param config table< string, any>
175
+ --- @param nilable boolean
170
176
--- @param key ? string | string[]
171
- --- @param nilable ? boolean
177
+ --- @param path ? string
172
178
--- @return render.md.debug.ValidatorSpec
173
- function Validator :spec (path , input , key , nilable )
174
- return Spec .new (self , path , input , key , nilable )
179
+ function Validator :spec (config , nilable , key , path )
180
+ return Spec .new (self , config , nilable , key , path )
175
181
end
176
182
177
183
--- @param suffix string
178
- --- @param input table<string , any>
184
+ --- @param config table<string , any>
179
185
--- @param opts table<string , vim.validate.Spec>
180
- function Validator :check (suffix , input , opts )
186
+ function Validator :check (suffix , config , opts )
181
187
local path = self .prefix .. suffix
182
188
local ok , err = pcall (vim .validate , opts )
183
189
if not ok then
184
190
table.insert (self .errors , path .. ' .' .. err )
185
191
end
186
- for key , _ in pairs (input ) do
192
+ for key , _ in pairs (config ) do
187
193
if opts [key ] == nil then
188
194
table.insert (self .errors , string.format (' %s.%s: is not a valid key' , path , key ))
189
195
end
0 commit comments